apitest
Requests

Intercept

Change the built http.Request just before it is sent.

Intercept receives the *http.Request that apitest built, just before it is sent to the handler. Anything the builder cannot express can be set here.

apitest.Handler(handler).
	Intercept(func(req *http.Request) {
		req.URL.RawQuery = "a[]=xxx&a[]=yyy"
		req.Header.Set("X-Signature", sign(req))
	}).
	Get("/hello").
	Expect(t).
	Status(http.StatusOK).
	End()

Typical uses:

  • Query strings that url.Values cannot encode, such as ids[]=1&ids[]=2.
  • Request signatures computed from the method, path, headers and body.
  • Fields of http.Request that the builder does not expose, such as RemoteAddr.

Changes made by the interceptor are visible to the handler, to Debug output and to reports. There is one interceptor per test, and calling Intercept again replaces it.

To inspect the request and response after the test has run, rather than change the request, use Observe.