Documentation
¶
Overview ¶
Package httpmock provides functionalities for mocking http server.
Index ¶
- Variables
- func GetBody(r *http.Request) ([]byte, error)
- type BodyMatcher
- type Header
- type HeaderMatcher
- type Mocker
- type Request
- func (r *Request) After(d time.Duration) *Request
- func (r *Request) Once() *Request
- func (r *Request) RequestHandler(handler RequestHandler) *Request
- func (r *Request) Return(v interface{}) *Request
- func (r *Request) ReturnCode(code int) *Request
- func (r *Request) ReturnFile(filePath string) *Request
- func (r *Request) ReturnHeader(header, value string) *Request
- func (r *Request) ReturnHeaders(headers Header) *Request
- func (r *Request) ReturnJSON(body interface{}) *Request
- func (r *Request) Times(i int) *Request
- func (r *Request) Twice() *Request
- func (r *Request) WaitUntil(w <-chan time.Time) *Request
- func (r *Request) WithBody(body interface{}) *Request
- func (r *Request) WithBodyJSON(v interface{}) *Request
- func (r *Request) WithHeader(header, value string) *Request
- func (r *Request) WithHeaders(headers Header) *Request
- type RequestHandler
- type RequestMatcher
- type RequestMatcherConfig
- type RequestMatcherError
- type RequestMatcherOption
- type Server
- func (s *Server) Close()
- func (s *Server) Expect(method, requestURI string) *Request
- func (s *Server) ExpectationsWereMet() error
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) URL() string
- func (s *Server) WithDefaultResponseHeaders(headers Header) *Server
- func (s *Server) WithRequestMatcher(matcher RequestMatcher) *Server
- type TestingT
- type URIMatcher
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupportedDataType = errors.New("unsupported data type")
ErrUnsupportedDataType represents that the data type is not supported.
Functions ¶
Types ¶
type BodyMatcher ¶
BodyMatcher matches a body with an expectation.
func ExactBodyMatcher ¶
func ExactBodyMatcher() BodyMatcher
ExactBodyMatcher matches a body by checking if it is equal to the expectation.
type HeaderMatcher ¶
HeaderMatcher matches header with an expectation.
func ExactHeaderMatcher ¶
func ExactHeaderMatcher() HeaderMatcher
ExactHeaderMatcher matches a header by checking if it is equal to the expectation.
type Request ¶
type Request struct { // Method is the expected HTTP Method of the given request. Method string // RequestURI is the expected HTTP Request URI of the given request. // The uri does not need to be exactly same but satisfies the URIMatcher. RequestURI string // RequestHeader is a list of expected headers of the given request. RequestHeader Header // RequestBody is the expected body of the given request. RequestBody []byte // StatusCode is the response code when the request is handled. StatusCode int // ResponseHeader is a list of response headers to be sent to client when the request is handled. ResponseHeader Header // Do handles the request and returns a result or an error. Do RequestHandler // The number of times to return the return arguments when setting // expectations. 0 means to always return the value. Repeatability int // contains filtered or unexported fields }
Request is an expectation.
func (*Request) After ¶
After sets how long to block until the call returns
Server.Expect(http.MethodGet, "/path"). After(time.Second). Return("hello world!")
func (*Request) Once ¶
Once indicates that that the mock should only return the value once.
Server.Expect(http.MethodGet, "/path"). Return("hello world!"). Once()
func (*Request) RequestHandler ¶
func (r *Request) RequestHandler(handler RequestHandler) *Request
RequestHandler sets the handler to handle a given request.
Server.Expect(http.MethodGet, "/path"). RequestHandler(func(_ *http.Request) ([]byte, error) { return []byte("hello world!"), nil })
func (*Request) Return ¶
Return sets the result to return to client.
Server.Expect(http.MethodGet, "/path"). Return("hello world!")
func (*Request) ReturnCode ¶
ReturnCode sets the response code.
Server.Expect(http.MethodGet, "/path"). ReturnCode(http.StatusBadRequest)
func (*Request) ReturnFile ¶ added in v0.1.1
ReturnFile reads the file using ioutil.ReadFile and uses it as the result to return to client.
Server.Expect(http.MethodGet, "/path"). ReturnFile("resources/fixtures/response.txt")
nolint:unparam
func (*Request) ReturnHeader ¶
ReturnHeader sets a response header.
Server.Expect(http.MethodGet, "/path"). ReturnHeader("foo", "bar")
func (*Request) ReturnHeaders ¶
ReturnHeaders sets a list of response headers.
Server.Expect(http.MethodGet, "/path"). ReturnHeaders(httpmock.Header{"foo": "bar"})
func (*Request) ReturnJSON ¶
ReturnJSON marshals the object using json.Marshal and uses it as the result to return to client.
Server.Expect(http.MethodGet, "/path"). ReturnJSON(map[string]string{"foo": "bar"})
func (*Request) Times ¶
Times indicates that that the mock should only return the indicated number of times.
Server.Expect(http.MethodGet, "/path"). Return("hello world!"). Times(5)
func (*Request) Twice ¶
Twice indicates that that the mock should only return the value twice.
Server.Expect(http.MethodGet, "/path"). Return("hello world!"). Twice()
func (*Request) WaitUntil ¶
WaitUntil sets the channel that will block the mock's return until its closed or a message is received.
Server.Expect(http.MethodGet, "/path"). WaitUntil(time.After(time.Second)). Return("hello world!")
func (*Request) WithBody ¶
WithBody sets the expected body of the given request.
Server.Expect(http.MethodGet, "/path"). WithBody("hello world!")
func (*Request) WithBodyJSON ¶ added in v0.1.2
WithBodyJSON marshals the object and use it as the expected body of the given request.
Server.Expect(http.MethodGet, "/path"). WithBodyJSON(map[string]string{"foo": "bar"})
nolint:unparam
func (*Request) WithHeader ¶
WithHeader sets an expected header of the given request.
Server.Expect(http.MethodGet, "/path"). WithHeader("foo": "bar")
func (*Request) WithHeaders ¶
WithHeaders sets a list of expected headers of the given request.
Server.Expect(http.MethodGet, "/path"). WithHeaders(httpmock.Header{"foo": "bar"})
type RequestHandler ¶
RequestHandler handles the request and returns a result or an error.
type RequestMatcher ¶
RequestMatcher matches a request with one of the expectations.
func DefaultRequestMatcher ¶
func DefaultRequestMatcher() RequestMatcher
DefaultRequestMatcher instantiates a SequentialRequestMatcher with ExactURIMatcher, ExactHeaderMatcher and ExactBodyMatcher.
func SequentialRequestMatcher ¶
func SequentialRequestMatcher(options ...RequestMatcherOption) RequestMatcher
SequentialRequestMatcher matches a request in sequence.
type RequestMatcherConfig ¶
type RequestMatcherConfig struct {
// contains filtered or unexported fields
}
RequestMatcherConfig is config of RequestMatcher.
func ConfigureRequestMatcher ¶
func ConfigureRequestMatcher(options ...RequestMatcherOption) *RequestMatcherConfig
ConfigureRequestMatcher configures ConfigureRequestMatcher with RequestMatcherOption.
type RequestMatcherError ¶
type RequestMatcherError struct {
// contains filtered or unexported fields
}
RequestMatcherError represents an error that occurs while matching a request.
func MatcherError ¶
func MatcherError(expected *Request, request *http.Request, message string, args ...interface{}) *RequestMatcherError
MatcherError instantiates a new RequestMatcherError.
func (RequestMatcherError) Error ¶
func (e RequestMatcherError) Error() string
Error satisfies the error interface.
type RequestMatcherOption ¶
type RequestMatcherOption func(c *RequestMatcherConfig)
RequestMatcherOption configures RequestMatcherConfig.
func WithExactBodyMatcher ¶
func WithExactBodyMatcher() RequestMatcherOption
WithExactBodyMatcher sets BodyMatcher to ExactBodyMatcher.
func WithExactHeaderMatcher ¶
func WithExactHeaderMatcher() RequestMatcherOption
WithExactHeaderMatcher sets HeaderMatcher to ExactHeaderMatcher.
func WithExactURIMatcher ¶
func WithExactURIMatcher() RequestMatcherOption
WithExactURIMatcher sets URIMatcher to ExactURIMatcher.
type Server ¶
type Server struct { // Represents the requests that are expected of a server. ExpectedRequests []*Request // Holds the requested that were made to this server. Requests []Request // contains filtered or unexported fields }
Server is a Mock server.
func MockServer ¶
MockServer creates a mocked server.
func (*Server) ExpectationsWereMet ¶
ExpectationsWereMet checks whether all queued expectations were met in order. If any of them was not met - an error is returned.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves the request.
func (*Server) WithDefaultResponseHeaders ¶
WithDefaultResponseHeaders sets the default response headers of the server.
func (*Server) WithRequestMatcher ¶
func (s *Server) WithRequestMatcher(matcher RequestMatcher) *Server
WithRequestMatcher sets the RequestMatcher of the server.
type TestingT ¶
type TestingT interface { Errorf(format string, args ...interface{}) FailNow() Cleanup(func()) }
TestingT is an interface wrapper around *testing.T.
type URIMatcher ¶
URIMatcher matches an URI with an expectation.
func ExactURIMatcher ¶
func ExactURIMatcher() URIMatcher
ExactURIMatcher matches an url by checking if it is equal to the expectation.