Documentation ¶
Index ¶
- func MatchBody(expected []byte) func(*http.Request) bool
- func MatchBodyString(expected string) func(*http.Request) bool
- func MatchHeader(name, expected string) func(*http.Request) bool
- func MatchMethod(expected string) func(*http.Request) bool
- func MatchURL(expected *url.URL) func(*http.Request) bool
- func MatchURLString(expected string) func(*http.Request) bool
- func NewResponse(statusCode int, body []byte) *http.Response
- type ExpectedResponse
- type MockBody
- type MockTransactor
- func (mt *MockTransactor) Do(request *http.Request) (*http.Response, error)
- func (mt *MockTransactor) OnDo(matchers ...func(*http.Request) bool) *TransactCall
- func (mt *MockTransactor) OnRoundTrip(matchers ...func(*http.Request) bool) *TransactCall
- func (mt *MockTransactor) RoundTrip(request *http.Request) (*http.Response, error)
- type TransactCall
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MatchBody ¶
MatchBody returns a request matcher that verifies each request has an exact body. The body is consumed, but then replaced so that downstream code can still access the body.
func MatchHeader ¶
MatchHeader returns a request matcher that matches against a request header
func MatchMethod ¶
MatchMethod returns a request matcher that verifies each request has a specific method
func MatchURLString ¶
MatchURLString returns a request matcher that verifies the request's URL translates to the given string.
Types ¶
type ExpectedResponse ¶
ExpectedResponse is a tuple of the expected return values from transactor.Do. This struct provides a simple unit to build table-driven tests from.
type MockBody ¶
MockBody is a stretchr mock for a Request or a Response body, which is really just an io.ReadCloser. This is mainly useful when testing error cases. For testing with actual byte contents, it's generally more convenient to use a *bytes.Buffer or other concrete container of bytes instead of mocking.
func (*MockBody) OnCloseError ¶
OnCloseError sets an expectation for a call to Close that simply returns the given error. The given error can be nil.
func (*MockBody) OnReadError ¶
OnReadError sets an expectation for a call to Read, with any byte slice, that returns the given error (and 0 for bytes read). If the given error is nil, wierd behavior can occur as the mocked Read will return (0, nil).
type MockTransactor ¶
MockTransactor is a stretchr mock for the Do method of an HTTP client or round tripper. This mock extends the behavior of a stretchr mock in a few ways that make clientside HTTP behavior easier to mock.
This type implements the http.RoundTripper interface, and provides a Do method that can implement a subset interface of http.Client.
func (*MockTransactor) Do ¶
Do is a mocked HTTP transaction call. Use On or OnRequest to setup behaviors for this method.
func (*MockTransactor) OnDo ¶
func (mt *MockTransactor) OnDo(matchers ...func(*http.Request) bool) *TransactCall
OnDo sets an On("Do", ...) with the given matchers for a request. The returned Call has some augmented behavior for setting responses.
func (*MockTransactor) OnRoundTrip ¶
func (mt *MockTransactor) OnRoundTrip(matchers ...func(*http.Request) bool) *TransactCall
OnRoundTrip sets an On("Do", ...) with the given matchers for a request. The returned Call has some augmented behavior for setting responses.
type TransactCall ¶
TransactCall is a stretchr mock Call with some extra behavior to make mocking out HTTP client behavior easier
func (*TransactCall) Respond ¶
func (dc *TransactCall) Respond(response *http.Response, err error) *TransactCall
Respond is a convenience for setting a Return(response, err)
func (*TransactCall) RespondWith ¶
func (dc *TransactCall) RespondWith(er ExpectedResponse) *TransactCall
RespondWith creates an (*http.Response, error) tuple from an ExpectedResponse. If the Err field is nil, an *http.Response is created from the other fields. If the Err field is not nil, a nil *http.Response is used.