Documentation
¶
Overview ¶
Package httpmock provides stretchr/testify/mock integration
Index ¶
- Constants
- Variables
- func Closed(body io.ReadCloser) bool
- type Body
- type BodyJSON
- type BodyReadCloser
- type CloseIdler
- type Closeable
- type NopRequestChecker
- type Path
- type RequestAsserter
- type RequestAsserterFunc
- type RequestChecker
- type RequestMatcher
- type RequestMatcherFunc
- type RoundTripCall
- func (rtc *RoundTripCall) AssertRequest(a ...RequestAsserter) *RoundTripCall
- func (rtc *RoundTripCall) Error(err error) *RoundTripCall
- func (rtc *RoundTripCall) Response(r *http.Response) *RoundTripCall
- func (rtc *RoundTripCall) Return(r *http.Response, err error) *RoundTripCall
- func (rtc *RoundTripCall) Run(f func(mock.Arguments)) *RoundTripCall
- type RoundTripper
- func (m *RoundTripper) AssertExpectations()
- func (m *RoundTripper) AssertRequest(a ...RequestAsserter) *RoundTripper
- func (m *RoundTripper) Next(next http.RoundTripper) *RoundTripper
- func (m *RoundTripper) OnAny() *RoundTripCall
- func (m *RoundTripper) OnMatchAll(rms ...RequestMatcher) *RoundTripCall
- func (m *RoundTripper) OnMatchAny(rms ...RequestMatcher) *RoundTripCall
- func (m *RoundTripper) OnRequest(request *http.Request) *RoundTripCall
- func (m *RoundTripper) RoundTrip(request *http.Request) (*http.Response, error)
- func (m *RoundTripper) Test(t mock.TestingT)
Constants ¶
const RoundTripMethodName = "RoundTrip"
RoundTripMethodName is the name of the http.RoundTripper.RoundTrip method. Used to start fluent expectation chains.
Variables ¶
var ( // ErrBodyClosed indicates that an attempt to read or close a Body, either request or response, // was made when the Body had already been closed. ErrBodyClosed = errors.New("The body has been closed") )
Functions ¶
func Closed ¶ added in v0.1.2
func Closed(body io.ReadCloser) bool
Closed returns true if the given body has been closed, false otherwise. This method panics if body does not implement Closed.
Types ¶
type Body ¶ added in v0.1.2
type Body string
Body is a RequestAsserter asserts that the request body matches an expected value exactly. Passing an empty expected value is the same as asserting that there is no body.
IMPORTANT: This asserter has to fully read the request body. That means that any subsequent asserters won't have access to the request body.
type BodyJSON ¶ added in v0.1.2
type BodyJSON string
BodyJSON is a RequestAsserter that checks the request body against an expected JSON document. Passing an empty expected value is the same as asserting that there is no body.
IMPORTANT: This asserter has to fully read the request body. That means that any subsequent asserters won't have access to the request body.
func (BodyJSON) Assert ¶ added in v0.1.2
func (b BodyJSON) Assert(assert *assert.Assertions, r *http.Request)
Assert verifies that either:
(1) The request body is nil AND this expected JSON is an empty string (2) The request body is not nil AND it matches this expected JSON. The
match is done via stretchr/testify/assert.JSONEq.
type BodyReadCloser ¶ added in v0.1.2
type BodyReadCloser struct {
// contains filtered or unexported fields
}
BodyReadCloser is a mock type that contains actual content but tracks whether the body has been closed. NopCloser doesn't record whether Close was called, making it unusable for verifying proper http.Response handling.
func BodyBytes ¶ added in v0.1.2
func BodyBytes(b []byte) *BodyReadCloser
BodyBytes returns a *BodyReadCloser backed by the given byte slice.
func BodyString ¶ added in v0.1.2
func BodyString(b string) *BodyReadCloser
BodyString returns a *BodyReadCloser backed by the given string.
func Bodyf ¶ added in v0.1.2
func Bodyf(format string, args ...interface{}) *BodyReadCloser
Bodyf produces a body with fmt.Sprintf. This function is equivalent to:
BodyString(fmt.Sprintf(format, args...))
func (*BodyReadCloser) Close ¶ added in v0.1.2
func (brc *BodyReadCloser) Close() error
Close marks this body as closed. Subsequent calls to this method will return ErrBodyClosed.
func (*BodyReadCloser) Closed ¶ added in v0.1.2
func (brc *BodyReadCloser) Closed() bool
Closed tests if this instance has had Close invoked on it.
type CloseIdler ¶
type CloseIdler struct {
*RoundTripper
}
CloseIdler is a mocked httpaux.CloseIdler that also mocks http.RoundTripper. Typical construction is:
func(t *testing.T) {
ci := &CloseIdler{
RoundTripper: NewRoundTripper(t),
}
}
func (*CloseIdler) CloseIdleConnections ¶
func (m *CloseIdler) CloseIdleConnections()
CloseIdleConnections implements httpaux.CloseIdler and is driven by this mock's expectations.
func (*CloseIdler) OnCloseIdleConnections ¶
func (m *CloseIdler) OnCloseIdleConnections() *mock.Call
OnCloseIdleConnections starts a fluent chain for defining a CloseIdleConnections expectation.
type Closeable ¶ added in v0.1.2
type Closeable interface {
// Closed reports whether Close() has been called on this instance.
Closed() bool
}
Closeable is implemented by anything that can report its closed state. *BodyReadCloser implements this interface.
type NopRequestChecker ¶ added in v0.1.2
type NopRequestChecker struct{}
NopRequestChecker is a RequestChecker that does nothing. This function is useful instead of nil.
func (NopRequestChecker) Assert ¶ added in v0.1.2
func (nrc NopRequestChecker) Assert(*assert.Assertions, *http.Request)
Assert does nothing.
type Path ¶ added in v0.1.2
type Path string
Path verifies a request's URL.Path property. This checker will also fail if the request's URL field is nil.
type RequestAsserter ¶ added in v0.1.2
type RequestAsserter interface {
// Assert asserts that a request is in the correct state. This method
// is used in mock.Run functions to verify a request after an expectation
// has been matched.
Assert(*assert.Assertions, *http.Request)
}
RequestAsserter executes assertions against requests. Implementations are used in mock.Run functions to verify that a request is in the correct state. This is sometimes preferable to matching, which is done before an expectation is selected rather than after.
type RequestAsserterFunc ¶ added in v0.1.2
type RequestAsserterFunc func(*assert.Assertions, *http.Request)
RequestAsserterFunc allows closures to be used directly as RequestAsserters
func (RequestAsserterFunc) Assert ¶ added in v0.1.2
func (raf RequestAsserterFunc) Assert(a *assert.Assertions, r *http.Request)
Assert satisfies the RequestAsserter interface
type RequestChecker ¶ added in v0.1.2
type RequestChecker interface {
RequestMatcher
RequestAsserter
}
RequestChecker is a combined strategy for both matching HTTP requests and asserting HTTP request state.
Implementations must guarantee that any request for which Match returns true must also pass all assertions in Assert. The reverse is also true: any request for which Match returns false must also fail Assert.
func Header ¶ added in v0.1.2
func Header(name string, expected ...string) RequestChecker
Header asserts that a given header has the expected values. All header values must match the expected slice exactly. If no expected values are passed, then the header must have no values.
func Methods ¶ added in v0.1.2
func Methods(expected ...string) RequestChecker
Methods verifies that a request has one of several expected methods. An empty expected slice means that no request will match.
type RequestMatcher ¶ added in v0.1.2
RequestMatcher supplies expectation matching for requests. Implementations are used in mock.MatchedBy functions in order to select which expectation applies.
type RequestMatcherFunc ¶ added in v0.1.2
RequestMatcherFunc allows closures to be used directly as RequestMatchers
type RoundTripCall ¶
RoundTripCall is syntactic sugar around a RoundTrip *mock.Call. This type provides some higher-level and typesafe expectation behavior.
First, create a *RoundTripper. Then, use a *RoundTripper's methods to create instances of this type to set expectations.
func (*RoundTripCall) AssertRequest ¶ added in v0.1.2
func (rtc *RoundTripCall) AssertRequest(a ...RequestAsserter) *RoundTripCall
AssertRequest adds request assertions that are specific to this mocked Call. Multiple calls to this method are cumulative.
Note that any global assertions on the mock that created this call will be executed in addition to these assertions.
func (*RoundTripCall) Error ¶ added in v0.4.0
func (rtc *RoundTripCall) Error(err error) *RoundTripCall
Error is a simpler form of Return that correctly sets this call to return a nil *http.Response with the given error.
func (*RoundTripCall) Response ¶ added in v0.4.0
func (rtc *RoundTripCall) Response(r *http.Response) *RoundTripCall
Response is a simpler form of Return that correctly sets this call to return an *http.Response with a nil error.
func (*RoundTripCall) Return ¶
func (rtc *RoundTripCall) Return(r *http.Response, err error) *RoundTripCall
Return establishes the return values for this RoundTrip invocation.
If this method is not used, the Next round tripper set on the container will be used to generate the return. If no Next has been set, this Call will fail the test when invoked.
func (*RoundTripCall) Run ¶ added in v0.1.2
func (rtc *RoundTripCall) Run(f func(mock.Arguments)) *RoundTripCall
Run establishes a run function for this mock. This does not prevent any assertions from running.
type RoundTripper ¶
RoundTripper is a mocked http.RoundTripper. Instances should be created with NewRoundTripper or NewRoundTripperSuite.
This type alters the Mock API slightly, since each instance is tied to a mock.TestingT instance.
func NewRoundTripper ¶ added in v0.1.2
func NewRoundTripper(t mock.TestingT) *RoundTripper
NewRoundTripper returns a mock http.RoundTripper for the given test.
func NewRoundTripperSuite ¶ added in v0.1.2
func NewRoundTripperSuite(s suite.TestingSuite) *RoundTripper
NewRoundTripperSuite returns a mock http.RoundTripper for the given suite.
func (*RoundTripper) AssertExpectations ¶ added in v0.1.2
func (m *RoundTripper) AssertExpectations()
AssertExpectations uses the TestingT instance set at construction or with Test to assert all the calls have been executed.
func (*RoundTripper) AssertRequest ¶ added in v0.1.2
func (m *RoundTripper) AssertRequest(a ...RequestAsserter) *RoundTripper
AssertRequest adds request assertions that apply to all mocked calls created via this instance.
Setting global assertions simplifies cases where a mock is used for many requests that all must have some similarity. For example, a test case may be testing something that always emits GET requests.
func (*RoundTripper) Next ¶ added in v0.1.3
func (m *RoundTripper) Next(next http.RoundTripper) *RoundTripper
Next sets a delegate for this round tripper. For any Calls that do not have an associated Return, this next instance will be used.
If next is nil, http.DefaultTransport is used instead.
func (*RoundTripper) OnAny ¶ added in v0.1.2
func (m *RoundTripper) OnAny() *RoundTripCall
OnAny is a convenience for starting a *mock.Call expectation which matches any HTTP request.
func (*RoundTripper) OnMatchAll ¶ added in v0.1.2
func (m *RoundTripper) OnMatchAll(rms ...RequestMatcher) *RoundTripCall
OnMatchAll starts a *mock.Call fluent chain that expects a RoundTrip call with a request that matches all the given predicates. If no predicates are supplied, the returned expectation will match any request.
func (*RoundTripper) OnMatchAny ¶ added in v0.1.2
func (m *RoundTripper) OnMatchAny(rms ...RequestMatcher) *RoundTripCall
OnMatchAny starts a *mock.Call fluent chain expects a RoundTrip call with a request that matches any of the given predicates. If no predicates are supplied, the returned expectation won't match any requests.
func (*RoundTripper) OnRequest ¶ added in v0.1.2
func (m *RoundTripper) OnRequest(request *http.Request) *RoundTripCall
OnRequest starts a *mock.Call fluent chain that expects a call to RoundTrip with a request. The expectation requires the exact request instance given.
If middleware may submit a different request to this mock, use MatchAll or MatchAny instead. For example, http.Request.WithContext creates a new request instance with the same state.
func (*RoundTripper) RoundTrip ¶
RoundTrip implements http.RoundTripper and is driven by the mock's expectations.
func (*RoundTripper) Test ¶ added in v0.1.2
func (m *RoundTripper) Test(t mock.TestingT)
Test changes the test instance on this mock.