Documentation ¶
Overview ¶
Package httptest enhances the stdlib net/http/httptest package by integrating it with gopkg.in/gavv/httpexpect.v1, reducing the boilerplate needed for http tests. In addition to the functions that make it easier to stand up a test server, this package also provides a set of tools to make it easier to mock out http client responses.
Test Servers vs. Client mocking
When building a testing fixture for HTTP, you can approach the problem in two ways: Use a mocked server and make real http requests to the server, or use a mocked client and have _no_ server. While this package provides facilities for both, we recommend that you follow the conventions for decided which to use in your tests.
The test server system should be used when the object under test is our own server code; usually that means our own http.Handler implementations. The mocked client system should be used when the object under test is making http requests.
Index ¶
- type Client
- type ClientExpectation
- func (ce *ClientExpectation) Return(r httpmock.Responder) *ClientExpectation
- func (ce *ClientExpectation) ReturnError(msg string) *ClientExpectation
- func (ce *ClientExpectation) ReturnJSON(status int, body interface{}) *ClientExpectation
- func (ce *ClientExpectation) ReturnJSONWithHeader(status int, body interface{}, header http.Header) *ClientExpectation
- func (ce *ClientExpectation) ReturnNotFound() *ClientExpectation
- func (ce *ClientExpectation) ReturnString(status int, body string) *ClientExpectation
- func (ce *ClientExpectation) ReturnStringWithHeader(status int, body string, header http.Header) *ClientExpectation
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct { *http.Client *httpmock.MockTransport }
Client represents an easier way to mock http client behavior. It assumes that your packages use interfaces to store an http client instead of a concrete *http.Client.
type ClientExpectation ¶
ClientExpectation represents a in-process-of-being-built http client mocking operation. The `On` method of `Client` returns an instance of this struct upon which you can call further methods to customize the response.
func (*ClientExpectation) Return ¶
func (ce *ClientExpectation) Return(r httpmock.Responder) *ClientExpectation
Return specifies the response for a ClientExpectation, which is then committed to the connected mock client.
func (*ClientExpectation) ReturnError ¶
func (ce *ClientExpectation) ReturnError(msg string) *ClientExpectation
ReturnError causes this expectation to resolve to an error.
func (*ClientExpectation) ReturnJSON ¶
func (ce *ClientExpectation) ReturnJSON( status int, body interface{}, ) *ClientExpectation
ReturnJSON causes this expectation to resolve to a json-based body with the provided status code. Panics when the provided body cannot be encoded to JSON.
func (*ClientExpectation) ReturnJSONWithHeader ¶
func (ce *ClientExpectation) ReturnJSONWithHeader( status int, body interface{}, header http.Header, ) *ClientExpectation
ReturnJSONWithHeader causes this expectation to resolve to a json-based body with the provided status code and response header. Panics when the provided body cannot be encoded to JSON.
func (*ClientExpectation) ReturnNotFound ¶
func (ce *ClientExpectation) ReturnNotFound() *ClientExpectation
ReturnNotFound is a simple helper that causes this expectation to resolve to a 404 error. If a customized body is needed, use something like `ReturnString“ instead.
func (*ClientExpectation) ReturnString ¶
func (ce *ClientExpectation) ReturnString( status int, body string, ) *ClientExpectation
ReturnString causes this expectation to resolve to a string-based body with the provided status code.
func (*ClientExpectation) ReturnStringWithHeader ¶
func (ce *ClientExpectation) ReturnStringWithHeader( status int, body string, header http.Header, ) *ClientExpectation
ReturnStringWithHeader causes this expectation to resolve to a string-based body with the provided status code and response header.