Documentation
¶
Overview ¶
Package mockserver provides an HTTP server that stands in for a third party in tests. It is configured through an Expect() recorder and verified with AssertExpectations.
Index ¶
- type Call
- func (c *Call) AnyTimes() *Call
- func (c *Call) Handle(handler func(request *Request) (any, error)) *Call
- func (c *Call) Return(body any) *Call
- func (c *Call) Times(count int) *Call
- func (c *Call) WithBody(body any) *Call
- func (c *Call) WithContentType(contentType string) *Call
- func (c *Call) WithHeader(key, value string) *Call
- func (c *Call) WithQuery(key, value string) *Call
- func (c *Call) WithStatus(statusCode int) *Call
- type Recorder
- type Request
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Call ¶
type Call struct {
mock.Cardinality
// contains filtered or unexported fields
}
Call is an expectation on a method and path.
func (*Call) AnyTimes ¶
AnyTimes lets the expectation match any number of times, including none. Use it for endpoints a dependency calls on its own schedule, which no test asserts on.
func (*Call) Handle ¶
Handle sets a function to compute the reply from the request, for the cases a fixed body cannot express: a response that depends on what was sent, one that varies between calls, or one that arrives late enough to exercise a caller's timeout.
It returns the body, encoded exactly as Call.Return encodes one, and takes precedence over a body set by Return. An error returned from it is reported to the caller as a 500, the same as an expectation that failed to build.
The function runs outside the server's lock, so one that blocks holds up only its own request.
func (*Call) Return ¶
Return sets the body to reply with, inferring the content type: protos and structs are marshaled to JSON, byte slices are served as-is. Without it, an empty 200 is returned.
func (*Call) WithBody ¶
WithBody restricts the expectation to requests carrying the given body. Protos, structs and strings holding JSON are compared semantically rather than byte for byte; anything else is compared exactly. Without it, any body matches.
func (*Call) WithContentType ¶
WithContentType overrides the content type inferred by Return.
func (*Call) WithHeader ¶
WithHeader restricts the expectation to requests carrying the given header.
func (*Call) WithQuery ¶
WithQuery restricts the expectation to requests carrying the given query parameter.
func (*Call) WithStatus ¶
WithStatus sets the status code to reply with, which defaults to 200.
type Recorder ¶
type Recorder struct {
// contains filtered or unexported fields
}
Recorder declares expectations on a Server.
func (*Recorder) Method ¶
Method registers an expectation for any verb. Paths match exactly, on the path alone: use WithQuery to constrain the query string. A path may contain '*' wildcards, each matching any run of characters including slashes, for clients that version their own URLs: "*/users/search" matches "/rest/api/3/users/search".
Expectations are matched in declaration order, and one declared with Times stops matching once it is exhausted, so several expectations on the same path reply in the order they were declared.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server serves canned responses, matched on method and path.
Requests that match no expectation get the fallback response when one is registered, and otherwise fail with 501 and are reported by AssertExpectations. A fallback turns the mock into a catch-all sink, which is what a dependency that makes unpredictable calls of its own during startup needs.
func (*Server) AssertExpectations ¶
AssertExpectations fails the test if any expectation went unmet, or any request matched none while no fallback was registered.
func (*Server) Requests ¶
Requests returns a copy of every request received so far, including unmatched ones.
func (*Server) Reset ¶
func (s *Server) Reset()
Reset drops every expectation and recorded request, so each test starts from a clean slate. The fallback is left in place, since it belongs to the environment rather than to a test.
func (*Server) SetFallback ¶
SetFallback registers the response for requests matching no expectation. Those requests are still recorded by Requests, but no longer reported by AssertExpectations. Passing a nil body serves an empty 200.