func TestMyHandler(t *testing.T) { handler := &FakeHandler{ StatusCode: 200, Body: []byte("Hello, world!"), } req, err := http.NewRequest("GET", "/hello", nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() handler.ServeHTTP(w, req) if w.Code != 200 { t.Errorf("expected status code 200 but got %d", w.Code) } if w.Body.String() != "Hello, world!" { t.Errorf("expected response body 'Hello, world!' but got '%s'", w.Body.String()) } }
func TestMyHandler404(t *testing.T) { handler := &FakeHandler{ StatusCode: 404, Body: []byte("Not found"), } req, err := http.NewRequest("GET", "/something", nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() handler.ServeHTTP(w, req) if w.Code != 404 { t.Errorf("expected status code 404 but got %d", w.Code) } if w.Body.String() != "Not found" { t.Errorf("expected response body 'Not found' but got '%s'", w.Body.String()) } }This test creates a fake HTTP request handler that returns a 404 status code and an error message when given a specific HTTP request. It checks that the response code and body are correct.