mockserver

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

func (c *Call) AnyTimes() *Call

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

func (c *Call) Handle(handler func(request *Request) (any, error)) *Call

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

func (c *Call) Return(body any) *Call

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

func (c *Call) Times(count int) *Call

Times requires the expectation to be matched exactly count times.

func (*Call) WithBody

func (c *Call) WithBody(body any) *Call

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

func (c *Call) WithContentType(contentType string) *Call

WithContentType overrides the content type inferred by Return.

func (*Call) WithHeader

func (c *Call) WithHeader(key, value string) *Call

WithHeader restricts the expectation to requests carrying the given header.

func (*Call) WithQuery

func (c *Call) WithQuery(key, value string) *Call

WithQuery restricts the expectation to requests carrying the given query parameter.

func (*Call) WithStatus

func (c *Call) WithStatus(statusCode int) *Call

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

func (r *Recorder) Delete(path string) *Call

Delete registers an expectation for a DELETE of the exact path.

func (*Recorder) Get

func (r *Recorder) Get(path string) *Call

Get registers an expectation for a GET of the exact path.

func (*Recorder) Method

func (r *Recorder) Method(method, path string) *Call

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.

func (*Recorder) Patch

func (r *Recorder) Patch(path string) *Call

Patch registers an expectation for a PATCH of the exact path.

func (*Recorder) Post

func (r *Recorder) Post(path string) *Call

Post registers an expectation for a POST of the exact path.

func (*Recorder) Put

func (r *Recorder) Put(path string) *Call

Put registers an expectation for a PUT of the exact path.

type Request

type Request struct {
	Method string
	Path   string
	Query  url.Values
	Header http.Header
	Body   []byte
}

Request is a single request the server received.

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 NewServer

func NewServer() *Server

NewServer starts a mock HTTP server on an ephemeral port.

func (*Server) AssertExpectations

func (s *Server) AssertExpectations(t mock.TestingT) bool

AssertExpectations fails the test if any expectation went unmet, or any request matched none while no fallback was registered.

func (*Server) Close

func (s *Server) Close()

Close shuts the server down.

func (*Server) Expect

func (s *Server) Expect() *Recorder

Expect returns a recorder for declaring expectations.

func (*Server) Requests

func (s *Server) Requests() []*Request

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

func (s *Server) SetFallback(statusCode int, body any) error

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.

func (*Server) URL

func (s *Server) URL() string

URL returns the base URL clients should be pointed at.

Jump to

Keyboard shortcuts

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