httpmock

package
v0.111.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 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 httpmock provides canonical httptest.Server factories for common fleet upstream patterns. It removes the boilerplate of wiring up httptest.NewServer, setting response codes, and asserting call counts.

Usage:

// a server that always returns 200 JSON:
srv := httpmock.JSON(t, 200, map[string]any{"ok": true})
defer srv.Close()

// a server with per-request routing:
srv := httpmock.New(t).
    Handle("/verify", httpmock.RespondJSON(200, verifyResp)).
    Handle("/health", httpmock.RespondStatus(200)).
    Build()
defer srv.Close()

// assert the number of calls to a path:
srv.AssertCalls(t, "/verify", 1)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder constructs a Server with per-path handlers.

func New

func New(t testing.TB) *Builder

New returns a Builder.

func (*Builder) Build

func (b *Builder) Build() *Server

Build creates the test server and registers t.Cleanup(srv.Close).

func (*Builder) Handle

func (b *Builder) Handle(pattern string, h http.Handler) *Builder

Handle registers a handler for the given pattern (same syntax as http.ServeMux).

func (*Builder) HandleFunc

func (b *Builder) HandleFunc(pattern string, fn http.HandlerFunc) *Builder

HandleFunc registers a handler function for the given pattern.

func (*Builder) WithFallback

func (b *Builder) WithFallback(h http.Handler) *Builder

WithFallback sets the handler for paths not matched by any registered pattern. Default: 404.

type Response

type Response struct {
	Status int
	Body   []byte
	Header http.Header
}

Response describes a canned HTTP response.

func RespondJSON

func RespondJSON(status int, body any) Response

RespondJSON creates a Response with a JSON body.

func RespondStatus

func RespondStatus(status int) Response

RespondStatus creates a Response with no body.

type Server

type Server struct {
	*httptest.Server
	// contains filtered or unexported fields
}

Server wraps httptest.Server and records per-path call counts.

func JSON

func JSON(t testing.TB, status int, body any) *Server

JSON creates a test server that responds to all requests with the given status code and JSON body. t.Cleanup registers server Close automatically.

func Sequence

func Sequence(t testing.TB, responses ...Response) *Server

Sequence creates a test server that responds to successive requests with the provided responses in order. When the sequence is exhausted, the last response is repeated.

func Status

func Status(t testing.TB, status int) *Server

Status creates a test server that responds to all requests with the given HTTP status code and no body.

func (*Server) AssertAtLeast

func (s *Server) AssertAtLeast(t testing.TB, path string, min int)

AssertAtLeast fails t if the number of calls to path is less than min.

func (*Server) AssertCalls

func (s *Server) AssertCalls(t testing.TB, path string, want int)

AssertCalls fails t if the number of calls to path does not equal want.

func (*Server) CallCount

func (s *Server) CallCount(path string) int

CallCount returns the number of requests received for path.

Jump to

Keyboard shortcuts

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