Lesson 25 of 60
GoTesting HTTP Handlers
20 minutes
Course progress42%
HTTP Handler Tests
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
net/http/httptest makes it easy to test servers.
func TestHelloHandler(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/hello", nil)
w := httptest.NewRecorder()
helloHandler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
resp.StatusCode != http.StatusOK || (body) != {
t.Fatalf(, resp.StatusCode, body)
}
}