Mocks
Observing mocks
Receive the request and response of every mocked call.
ObserveMocks registers a function that is called after every outbound call that reaches the mock transport:
apitest.New().
ObserveMocks(func(res *http.Response, req *http.Request, a *apitest.APITest) {
if res == nil {
t.Logf("%s %s did not match a mock", req.Method, req.URL)
return
}
t.Logf("%s %s -> %d", req.Method, req.URL, res.StatusCode)
observed.Add(1)
}).
Mocks(getUserMock, getPreferencesMock).
Handler(newUserAPI()).
Get("/user/1234").
Expect(t).
Status(http.StatusOK).
End()type Observe func(*http.Response, *http.Request, *apitest.APITest)The response is nil when the call did not match a mock, or when the mock was set to time out. ObserveMocks can be called several times to register several observers.
Observers suit cross-cutting checks, such as asserting that every outbound call carries a tracing header, or collecting calls for a custom report. Sequence diagrams are built on the same hook.
To observe the final response of the handler instead, use Observe.