xhttptest

package
v1.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2019 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchBody

func MatchBody(expected []byte) func(*http.Request) bool

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 MatchBodyString

func MatchBodyString(expected string) func(*http.Request) bool

func MatchHeader

func MatchHeader(name, expected string) func(*http.Request) bool

MatchHeader returns a request matcher that matches against a request header

func MatchMethod

func MatchMethod(expected string) func(*http.Request) bool

MatchMethod returns a request matcher that verifies each request has a specific method

func MatchURL

func MatchURL(expected *url.URL) func(*http.Request) bool

MatchURL returns a request matcher that verifies each request has an exact URL.

func MatchURLString

func MatchURLString(expected string) func(*http.Request) bool

MatchURLString returns a request matcher that verifies the request's URL translates to the given string.

func NewResponse

func NewResponse(statusCode int, body []byte) *http.Response

NewResponse provides a convenient way of synthesizing a client response, similar to httptest.NewRequest. This function initializes most members to useful values for testing with.

Types

type ExpectedResponse

type ExpectedResponse struct {
	StatusCode int
	Body       []byte
	Header     http.Header
	Err        error
}

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

type MockBody struct {
	mock.Mock
}

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) Close

func (mb *MockBody) Close() error

func (*MockBody) OnCloseError

func (mb *MockBody) OnCloseError(err error) *mock.Call

OnCloseError sets an expectation for a call to Close that simply returns the given error. The given error can be nil.

func (*MockBody) OnReadError

func (mb *MockBody) OnReadError(err error) *mock.Call

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).

func (*MockBody) Read

func (mb *MockBody) Read(p []byte) (int, error)

type MockTransactor

type MockTransactor struct {
	mock.Mock
}

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

func (mt *MockTransactor) Do(request *http.Request) (*http.Response, error)

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.

func (*MockTransactor) RoundTrip

func (mt *MockTransactor) RoundTrip(request *http.Request) (*http.Response, error)

RoundTrip is a mocked HTTP transaction call. Use On or OnRoundTrip to setup behaviors for this method.

type TransactCall

type TransactCall struct {
	*mock.Call
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL