Documentation ¶
Index ¶
- type BodyFormat
- type CleanerT
- type Endpoint
- type MockAPI
- func (m *MockAPI) AssertExpectations(t TestingT)
- func (m *MockAPI) Close()
- func (m *MockAPI) DefaultHandler(response func(http.ResponseWriter, *http.Request)) *MockAPICall
- func (m *MockAPI) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (m *MockAPI) SetFilteredHeaders(headers []string)
- func (m *MockAPI) SetFilteredQueryParams(params []string)
- func (m *MockAPI) URL() string
- func (m *MockAPI) WithJSONReply(req *MockRequest, status int, reply interface{}) *MockAPICall
- func (m *MockAPI) WithJSONReplyAlter(req *MockRequest, status int, reply interface{}) (ret *MockAPICall)
- func (m *MockAPI) WithNoResponseBody(req *MockRequest, status int) *MockAPICall
- func (m *MockAPI) WithRequest(req *MockRequest, resp MockResponse) *MockAPICall
- func (m *MockAPI) WithStreamingReply(req *MockRequest, status int, reply io.Reader) *MockAPICall
- func (m *MockAPI) WithTextReply(req *MockRequest, status int, reply string) *MockAPICall
- type MockAPICall
- type MockRequest
- type MockResponse
- type ResponseFormat
- type TestingT
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BodyFormat ¶
type BodyFormat string
const ( BodyFormatNone BodyFormat = "none" BodyFormatJSON BodyFormat = "json" BodyFormatString BodyFormat = "string" BodyFormatStream BodyFormat = "stream" )
type CleanerT ¶
type CleanerT interface { TestingT Cleanup(func()) }
CleanerT is the interface that may optionally be implemented for Go 1.14 compatibility in addition to generic testing.T compatibility with older versions.
type Endpoint ¶
type Endpoint struct { // Path is the HTTP path this endpoint is served under Path string // Method is the HTTP Method used to invoke this API Method string // BodyFormat is what format of body to take as input BodyFormat BodyFormat // BodyType is the golang type of the Body BodyType string // PathParameters are the parameters required to be in the path PathParameters []string // ResponseFormat is the format of Response that helpers should ResponseFormat ResponseFormat // ResponseType is the golang type of the Response ResponseType string // Headers indicates that this endpoints operation is influenced by // headers which may be present and so the headers should be a part // of the expectation Headers bool // QueryParams indicates that this endpoints operation is influenced by // query params which may be present and so the params should be part // of the expectation QueryParams bool }
Endpoint represents an HTTP endpoint to be mocked. This is mostly used by github.com/mkeeler/mock-http-api/cmd/mock-expect-gen in order to generate expectation helpers for an HTTP API.
type MockAPI ¶
type MockAPI struct {
// contains filtered or unexported fields
}
MockAPI is the container holding all the bits necessary to provide a mocked HTTP API.
func NewMockAPI ¶
NewMockAPI creates a MockAPI. If `t` supports the Go 1.14 Cleanup function then a cleanup routine will be setup to close the MockAPI when the test completes. This will teardown the HTTP server and assert that all the required HTTP calls were made. If not using Go 1.14 then the caller should ensure that Close() is called in order to properly shut things down.
func (*MockAPI) AssertExpectations ¶
AssertExpectations will assert that all expected API invocations have happened and fail the test if any required calls did not happen.
func (*MockAPI) Close ¶
func (m *MockAPI) Close()
Close will stop the HTTP server and also assert that all expected HTTP invocations have happened.
func (*MockAPI) DefaultHandler ¶
func (m *MockAPI) DefaultHandler(response func(http.ResponseWriter, *http.Request)) *MockAPICall
func (*MockAPI) ServeHTTP ¶
func (m *MockAPI) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the HTTP.Handler interface
func (*MockAPI) SetFilteredHeaders ¶
SetFilteredHeaders sets a list of headers that shouldn't be taken into account when recording an API call.
func (*MockAPI) SetFilteredQueryParams ¶
SetFilteredQueryParams sets a list of query params that shouldn't be taken into account when recording an API call.
func (*MockAPI) URL ¶
URL returns the URL the HTTP server is listening on. It will have the form described for the httptest.Server's URL field https://pkg.go.dev/net/http/httptest#Server
func (*MockAPI) WithJSONReply ¶
func (m *MockAPI) WithJSONReply(req *MockRequest, status int, reply interface{}) *MockAPICall
WithJSONReply will setup an expectation for an API call to be made. The supplied status code will be use for the responses reply and the reply object will be JSON encoded and written to the response. If there is an error in JSON encoding it will fail the test object passed into the NewMockAPI constructor if that was non-nil and if it was nil, will panic. The method, path and body parameters are the same as for the Request method.
func (*MockAPI) WithJSONReplyAlter ¶
func (m *MockAPI) WithJSONReplyAlter(req *MockRequest, status int, reply interface{}) (ret *MockAPICall)
func (*MockAPI) WithNoResponseBody ¶
func (m *MockAPI) WithNoResponseBody(req *MockRequest, status int) *MockAPICall
WithNoResponseBody will setup an expectation for an API call to be made. The supplied status code will be used for the responses reply but no response body will be written.
func (*MockAPI) WithRequest ¶
func (m *MockAPI) WithRequest(req *MockRequest, resp MockResponse) *MockAPICall
WithRequest will setup an expectation for an API call to be made. Its is the responsibility of the passed in response function to set the HTTP status code and write out any body. The body may of the MockRequest passed in may be either nil, a []byte or a map[string]interface{}. During processing of the HTTP request, the entire body will be read. If the len is not greater than 0, then nil will be recorded as the body. If the len is greater than 0 an attempt to JSON decode the body contents into a map[string]interface{} is made. If successful the map is recorded as the body, if unsuccessful then the raw []byte is recorded as the body.
func (*MockAPI) WithStreamingReply ¶
func (m *MockAPI) WithStreamingReply(req *MockRequest, status int, reply io.Reader) *MockAPICall
WithStreamingReply will setup an expectation for an API call to be made. The supplied status code will be used for the responses reply and the reply readers content will be copied as the response body.
func (*MockAPI) WithTextReply ¶
func (m *MockAPI) WithTextReply(req *MockRequest, status int, reply string) *MockAPICall
WithTextReply will setup an expectation for an API call to be made. The supplied status code will be use for the responses reply and the reply string will be written to the response.
type MockAPICall ¶
type MockAPICall struct {
// contains filtered or unexported fields
}
MockAPICall is a wrapper around the github.com/stretchr/testify/mock.Call type. It provides a smaller interface that is more suitable for use with the MockAPI type and should prevent some accidental issues.
func (*MockAPICall) Maybe ¶
func (m *MockAPICall) Maybe() *MockAPICall
Maybe marks this API call as optional.
func (*MockAPICall) Once ¶
func (m *MockAPICall) Once() *MockAPICall
Once marks this API call as being expected to occur exactly once.
func (*MockAPICall) Times ¶
func (m *MockAPICall) Times(i int) *MockAPICall
Times marks this API call as being expected to occur the specified number of times.
func (*MockAPICall) Twice ¶
func (m *MockAPICall) Twice() *MockAPICall
Twice marks this API call as being expected to occur exactly twice
func (*MockAPICall) WaitUntil ¶
func (m *MockAPICall) WaitUntil(w <-chan time.Time) *MockAPICall
WaitUntil sets the channel that will block the sending back an HTTP response to this Call. This happens prior to setting the status code as well as writing out any of the reply (before the function passed to MockAPI.Request is called)
type MockRequest ¶
type MockRequest struct {
// contains filtered or unexported fields
}
MockRequest is the container for all the elements pertaining to an expected API request.
func NewMockRequest ¶
func NewMockRequest(method, path string) *MockRequest
NewMockRequest will create a new MockRequest. Other With* methods can then be called to build out the other parts of the expected request
func (*MockRequest) WithBody ¶
func (r *MockRequest) WithBody(body interface{}) *MockRequest
func (*MockRequest) WithHeaders ¶
func (r *MockRequest) WithHeaders(headers map[string]string) *MockRequest
WithHeaders will set these headers to be expected in the request
func (*MockRequest) WithQueryParams ¶
func (r *MockRequest) WithQueryParams(params map[string]string) *MockRequest
WithQueryParams will set these query params to be expected in the request
type MockResponse ¶
type MockResponse func(http.ResponseWriter, *http.Request)
MockResponse is the type of function that the mock HTTP server is expecting to be used to handle setting up the response. This function should write a status code and maybe a body
type ResponseFormat ¶
type ResponseFormat string
const ( ResponseFormatJSON ResponseFormat = "json" ResponseFormatString ResponseFormat = "string" ResponseFormatStream ResponseFormat = "stream" ResponseFormatFunc ResponseFormat = "func" )