It all depends what you're testing. If you're testing your handlers themselves, I'd do something like this.
func TestHandlers(t *testing.T) {
tests := []struct {
Desc string
Handler func(Example, http.ResponseWriter, *http.Request)
Path string
Params url.Values
Status int
Match map[string]bool
}{{
Desc: "basic",
Handler: Example.HandleOne,
Path: "/world",
Status: http.StatusOK,
Match: map[string]bool{
"Hello": true,
"world": true,
},
}, {
Desc: "basic",
Handler: Example.HandleTwo,
Path: "/world",
Params: url.Values{
"name": {"gopher"},
},
Status: http.StatusOK,
Match: map[string]bool{
"Hello": true,
"world": false,
"gopher": true,
},
}}
for _, test := range tests {
record := httptest.NewRecorder()
req := &http.Request{
Method: "GET",
URL: &url.URL{Path: test.Path},
Form: test.Params,
}
test.Handler(Example{}, record, req)
if got, want := record.Code, test.Status; got != want {
t.Errorf("%s: response code = %d, want %d", test.Desc, got, want)
}
for re, match := range test.Match {
if got := regexp.MustCompile(re).Match(record.Body.Bytes()); got != match {
t.Errorf("%s: %q ~ /%s/ = %v, want %v", test.Desc, record.Body, re, got, match)
}
}
}
}