httpmock

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package httpmock provides a minimal mock http.RoundTripper for tests, modeled on gh's pkg/httpmock (simplified). A Registry holds route stubs that are consumed in registration order, which allows simulating retry sequences (e.g. 502 then 200) by registering the same route twice.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher func(req *http.Request) bool

Matcher decides whether a stub applies to a request.

func REST

func REST(method, path string) Matcher

REST matches a request by HTTP method and URL path (e.g. "/v1/me").

type Registry

type Registry struct {
	Requests []*http.Request
	// contains filtered or unexported fields
}

Registry is an http.RoundTripper that serves registered stubs and records every matched request in Requests.

func (*Registry) Register

func (r *Registry) Register(m Matcher, resp Responder)

Register adds a stub. Stubs are matched in registration order and each stub responds at most once.

func (*Registry) RoundTrip

func (r *Registry) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

func (*Registry) Verify

func (r *Registry) Verify(t testing.TB)

Verify fails the test when registered stubs were never matched.

type Responder

type Responder func(req *http.Request) (*http.Response, error)

Responder produces the stubbed response for a matched request.

func ErrorResponse

func ErrorResponse(err error) Responder

ErrorResponse simulates a transport-level failure (e.g. connection refused).

func JSONResponse

func JSONResponse(status int, v any) Responder

JSONResponse marshals v as the response body with Content-Type application/json.

func StatusStringResponse

func StatusStringResponse(status int, body string) Responder

StatusStringResponse responds with the given status code and body.

func WithHeader

func WithHeader(responder Responder, key, value string) Responder

WithHeader decorates a responder with an extra response header.

Jump to

Keyboard shortcuts

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