This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Inside user_test.go | |
| const ( | |
| BadRequestCode = 400 | |
| SuccessRequestCode = 200 | |
| ) | |
| type TestStruct struct { | |
| requestBody string | |
| expectedStatusCode int | |
| responseBody string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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)) | |
| } |