Documentation
¶
Overview ¶
Package testhttpclient provides a tool for testing code that uses HTTP client(s).
Index ¶
- Constants
- func MockJSONResponse(code int, body interface{}) *http.Response
- func MockResponse(code int, body []byte, contentType string) *http.Response
- func MockXMLResponse(code int, body interface{}) *http.Response
- type MockHttpClient
- func (m *MockHttpClient) AddError(method, url string, err error) *MockHttpClient
- func (m *MockHttpClient) AddLiteralByteResponse(method, url string, wholeResponse []byte) *MockHttpClient
- func (m *MockHttpClient) AddLiteralResponse(method, url string, wholeResponse string) *MockHttpClient
- func (m *MockHttpClient) AddOutcome(method, url string, outcome Outcome) *MockHttpClient
- func (m *MockHttpClient) AddResponse(method, url string, response *http.Response) *MockHttpClient
- func (m *MockHttpClient) CapturedBody(i int) *bodypkg.Body
- func (m *MockHttpClient) Do(req *http.Request) (*http.Response, error)
- func (m *MockHttpClient) RemainingOutcomes() []string
- func (m *MockHttpClient) Reset()
- func (m *MockHttpClient) RoundTrip(req *http.Request) (*http.Response, error)
- type Outcome
- type TestingT
Constants ¶
const ( ContentTypeApplicationJSON = "application/json; charset=UTF-8" ContentTypeApplicationXML = "application/xml; charset=UTF-8" )
Variables ¶
This section is empty.
Functions ¶
func MockJSONResponse ¶ added in v0.14.0
MockJSONResponse builds a JSON http.Response using a literal JSON string or a data struct.
func MockResponse ¶
MockResponse builds a http.Response. If contentType blank it is ignored.
func MockXMLResponse ¶ added in v0.14.0
MockXMLResponse builds an XML http.Response using a literal XML string or a data struct.
Types ¶
type MockHttpClient ¶
type MockHttpClient struct { CapturedRequests []*http.Request // contains filtered or unexported fields }
MockHttpClient is a HttpClient that holds some stubbed outcomes.
func New ¶
func New(t TestingT) *MockHttpClient
func (*MockHttpClient) AddError ¶
func (m *MockHttpClient) AddError(method, url string, err error) *MockHttpClient
AddError adds an expected outcome that returns an error instead of a response.
func (*MockHttpClient) AddLiteralByteResponse ¶
func (m *MockHttpClient) AddLiteralByteResponse(method, url string, wholeResponse []byte) *MockHttpClient
func (*MockHttpClient) AddLiteralResponse ¶
func (m *MockHttpClient) AddLiteralResponse(method, url string, wholeResponse string) *MockHttpClient
AddLiteralResponse adds an expected outcome that has a literal HTTP response as provided. An example might be
HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 18 {"A":"foo","B":7}
func (*MockHttpClient) AddOutcome ¶
func (m *MockHttpClient) AddOutcome(method, url string, outcome Outcome) *MockHttpClient
AddOutcome adds an outcome directly.
func (*MockHttpClient) AddResponse ¶
func (m *MockHttpClient) AddResponse(method, url string, response *http.Response) *MockHttpClient
AddResponse adds an expected outcome that returns a response.
func (*MockHttpClient) CapturedBody ¶
func (m *MockHttpClient) CapturedBody(i int) *bodypkg.Body
CapturedBody gets the request body from the i'th request.
func (*MockHttpClient) Do ¶
Do is a pluggable method that implements standard library behaviour using stubbed behaviours. See httpclient.HttpClient. This uses RoundTrip.
func (*MockHttpClient) RemainingOutcomes ¶
func (m *MockHttpClient) RemainingOutcomes() []string
RemainingOutcomes describes the remaining outcomes. Typically, this should be empty at the end of a test (otherwise there might be a setup error).
func (*MockHttpClient) Reset ¶
func (m *MockHttpClient) Reset()
Reset deletes all outcomes and captured responses.