Skip to content

Instantly share code, notes, and snippets.

View Kiran0007Patil's full-sized avatar

Kiran0007Patil

View GitHub Profile
//Inside user_test.go
func DisplayTestCaseResults(functionalityName string, tests []TestStruct, t *testing.T) {
for _, test := range tests {
if test.observedStatusCode == test.expectedStatusCode {
t.Logf("Passed Case:\n request body : %s \n expectedStatus : %d \n responseBody : %s \n observedStatusCode : %d \n", test.requestBody, test.expectedStatusCode, test.responseBody, test.observedStatusCode)
} else {
t.Errorf("Failed Case:\n request body : %s \n expectedStatus : %d \n responseBody : %s \n observedStatusCode : %d \n", test.requestBody, test.expectedStatusCode, test.responseBody, test.observedStatusCode)
}
//Inside user_test.go
func TestUserSignup(t *testing.T) {
url := "http://localhost:9090/signup"
tests := []TestStruct{
{`{}`, BadRequestCode, "", 0},
{`{"username":""}`, BadRequestCode, "", 0},
{`{"username":"kiran","email":"[email protected]"}`, BadRequestCode, "", 0},
{`{"username":"kiran","email":"[email protected]","password" : "123456"}`, SuccessRequestCode, "", 0},
//Inside user_test.go
const (
BadRequestCode = 400
SuccessRequestCode = 200
)
type TestStruct struct {
requestBody string
expectedStatusCode int
responseBody string
//Inside user.go
func userListHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
users, err := json.Marshal(users)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(users)
//Inside user.go
func signUpHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
failureResponse(w, err.Error())
return
}
type SignupRequestBody struct {
//Inside user.go
type User struct {
Id int `json:"id"`
Email string `json:"email"`
Username string `json:"username"`
Password string `json:"password"`
}
var users = []User{}
//Inside main.go
func main() {
router := httprouter.New()
router.POST("/signup", signUpHandler)
router.GET("/users_list", userListHandler)
port := ":9090"
fmt.Println("Starting server on ", port)
log.Fatal(http.ListenAndServe(port, router))
}