w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/foo", nil)
ServeHTTP(w, req)
if got, want := w.Code, http.StatusOK; got != want {
t.Errorf("get /foo: status:\n got: %v\n want: %v", got, want)
}
if got, want := w.Body.String(), "it worked"; got != want {
t.Errorf("get /foo: body:\n got: %v\n want: %v", got, want)
}
It leaves very little to the imagination as to whether or not ServeHTTP works, which is nice.Complexity comes from generating requests and parsing the responses, and that is what leads to the desire to factor things out -- test the functions with their native data types instead of http.Request and http.Response. I think most people choose to factor things out to make that possible, but in the simplest of simple cases, many people just use httptest. It gets the job done.