httpspec allow you to create HTTP API specifications with ease.
The documentation maintained in GoDoc, including the examples.
package mypkg
func TestMyHandlerCreate(t *testing.T) {
s := testcase.NewSpec(t)
// subject
httpspec.SubjectLet(s, func(t *testcase.T) http.Handler {
return MyHandler{}
})
// Arrange
httpspec.ContentTypeIsJSON(s)
httpspec.Method.LetValue(s, http.MethodPost)
httpspec.Path.LetValue(s, `/`)
httpspec.Body.Let(s, func(t *testcase.T) interface{} {
// this will end up as {"foo":"bar"} in the request body
return map[string]string{"foo": "bar"}
})
s.Then(`it will...`, func(t *testcase.T) {
// Act
rr := httpspec.SubjectGet(t)
// Assert
assert.Must(t).Equal( http.StatusOK, rr.Code)
var resp CreateResponse
assert.Must(t).Nil( json.Unmarshal(rr.Body.Bytes(), &resp))
// ...
})
}