mockhttp

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 11 Imported by: 0

README

mockhttp

HTTP mock server for end-to-end tests. Declare the endpoints the code under test calls, start the server, point the app at ms.URL() and assert every endpoint was hit.

func TestCreateBook(t *testing.T) {
	ms := mockhttp.NewMockServer() // or mockhttp.WithPort(12000)
	ms.Get("/isbn", mockhttp.MatchQueryParams(url.Values{"title": {"Foundation"}})).
		Return(mockhttp.StatusCode(http.StatusOK), mockhttp.JSONBody(`{"isbn":"9780345317988"}`))

	ms.Start(t) // define every endpoint before Start
	defer ms.Teardown()

	// call the app, configured with ms.URL() as the upstream address

	ms.AssertExpectations()
}
API What it does
Get, Post, Put, Patch, Delete, Head register an endpoint (chi pattern) with optional matchers
MatchQueryParams, MatchHeader, MatchJSONBody fail the test when the request does not match
StatusCode, Headers, JSONBody, JSONFileBody build the response
AssertExpectations, AssertNotCalled check which endpoints were called
Router, Server the underlying chi.Router and httptest.Server

Unmapped routes fail the test with 404/405.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher func(t *testing.T, r *http.Request)

func MatchHeader

func MatchHeader(headers http.Header) Matcher

func MatchJSONBody

func MatchJSONBody(jsonBody string) Matcher

func MatchQueryParams

func MatchQueryParams(qp url.Values) Matcher

type MockServer

type MockServer struct {
	T *testing.T
	// contains filtered or unexported fields
}

MockServer is an HTTP testing server designed for easy mocking of REST APIs.

func NewMockServer

func NewMockServer(opts ...Option) *MockServer

NewMockServer creates a MockServer with the provided options.

func (*MockServer) AssertExpectations

func (ms *MockServer) AssertExpectations()

AssertExpectations verifies that every registered endpoint was called at least once.

func (*MockServer) AssertNotCalled

func (ms *MockServer) AssertNotCalled(endpoint string)

AssertNotCalled verifies that the given endpoint was never called.

func (*MockServer) Delete

func (ms *MockServer) Delete(pattern string, matchers ...Matcher) *Returner

Delete creates a mock endpoint for a delete request.

func (*MockServer) Get

func (ms *MockServer) Get(pattern string, matchers ...Matcher) *Returner

Get creates a mock endpoint for a get request.

func (*MockServer) Head

func (ms *MockServer) Head(pattern string, matchers ...Matcher) *Returner

Head creates a mock endpoint for a head request.

func (*MockServer) Patch

func (ms *MockServer) Patch(pattern string, matchers ...Matcher) *Returner

Patch creates a mock endpoint for a patch request.

func (*MockServer) Port

func (ms *MockServer) Port() int

Port returns the TCP port where the MockServer is listening. It can be a statically configured port or a dynamic allocated one.

func (*MockServer) Post

func (ms *MockServer) Post(pattern string, matchers ...Matcher) *Returner

Post creates a mock endpoint for a post request.

func (*MockServer) Put

func (ms *MockServer) Put(pattern string, matchers ...Matcher) *Returner

Put creates a mock endpoint for a put request.

func (*MockServer) Router

func (ms *MockServer) Router() chi.Router

Router exposes the internal chi.Router to allow configurations not supported by the helper methods.

func (*MockServer) Server

func (ms *MockServer) Server() *httptest.Server

Server exposes the internal testing HTTP server.

func (*MockServer) Start

func (ms *MockServer) Start(t *testing.T)

Start initializes the MockServer on a background goroutine.

Important: All endpoint mocks MUST be defined before calling this method.

func (*MockServer) Teardown

func (ms *MockServer) Teardown()

Teardown stops the HTTP server.

Call this with a defer after starting the server.

func (*MockServer) URL

func (ms *MockServer) URL() string

URL returns the MockServer URL. Value is http://127.0.0.1:<server port>

type Option

type Option func(ms *MockServer)

Option represents a MockServer configuration.

func WithPort

func WithPort(port int) Option

WithPort defines a static TCP port for the MockServer to listen.

type Responder

type Responder func(w http.ResponseWriter)

Responder configures a http.ResponseWriter to send data back.

func Headers

func Headers(headers http.Header) Responder

Headers is a Responder that defines the response headers.

func JSONBody

func JSONBody(jsonStr string) Responder

JSONBody is a Responder that defines the response body as a JSON string.

func JSONFileBody

func JSONFileBody(t *testing.T, filePath string) Responder

JSONFileBody is a Responder that defines the response body as a JSON file.

func StatusCode

func StatusCode(code int) Responder

StatusCode is a Responder that defines the response status code.

type Returner

type Returner struct {
	// contains filtered or unexported fields
}

Returner constructs the endpoint's response with a collection of Responders.

func (*Returner) Endpoint

func (r *Returner) Endpoint() string

Endpoint returns the endpoint name (method + path) that this Returner represents.

func (*Returner) Return

func (r *Returner) Return(builders ...Responder) *Returner

Return set up a collection of Responders.

Jump to

Keyboard shortcuts

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