- type args struct {
- responseRecorder *httptest.ResponseRecorder
- r *http.Request
- }
- tests := []struct {
- name string
- args args
- wantedStatus int
- wantedBody string
- }{
- {
- name: "StatusHandler with correct path and method, should return OK",
- args: args{
- responseRecorder: httptest.NewRecorder(),
- r: newRequest("GET", "/", nil, t),
- },
- wantedStatus: http.StatusOK,
- wantedBody: "All is well!",
- },
- {
- name: "StatusHandler with incorrect path, should return NotFound",
- args: args{
- responseRecorder: httptest.NewRecorder(),
- r: newRequest("GET", "/wrong", nil, t),
- },
- wantedStatus: http.StatusNotFound,
- wantedBody: "404 not found.\n",
- },
- {
- name: "StatusHandler with incorrect method, should return MethodNotAllowed",
- args: args{
- responseRecorder: httptest.NewRecorder(),
- r: newRequest("PUT", "/", nil, t),
- },
- wantedStatus: http.StatusMethodNotAllowed,
- wantedBody: "Method is not supported.\n",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- handler := http.HandlerFunc(StatusHandler)
- handler.ServeHTTP(tt.args.responseRecorder, tt.args.r)
- assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code)