httpmock

package
v0.12.9 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package httpmock is an http.RoundTripper test double for stubbing outbound HTTP in unit tests, keeping them off the live network with NO test-only seam in production code. It mirrors cli/cli's pkg/httpmock pattern: a Registry implements http.RoundTripper and matches each request against registered stubs. Callers inject Registry.Client() wherever production code accepts an *http.Client (e.g. the Factory's HttpClient noun), so the request URL is the real one — the transport, not a swapped URL, is the seam.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchAny

func MatchAny(*http.Request) bool

MatchAny matches every request.

Types

type Matcher

type Matcher func(*http.Request) bool

Matcher reports whether a request should be served by a stub.

func REST

func REST(method, pathContains string) Matcher

REST matches a request by HTTP method and a substring of its URL path. clawker's outbound calls are few and distinct by path, so a substring match is sufficient and reads clearly at the call site.

type Registry

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

Registry is an http.RoundTripper test double. Register stubs in the order you expect requests; each request is matched against stubs in registration order and served by the first match. Every request is recorded (Requests) for assertions, and an unmatched request returns an error — so a test that makes an unexpected call fails loudly instead of reaching the network.

func (*Registry) Client

func (r *Registry) Client() *http.Client

Client returns an *http.Client whose Transport is this registry — pass it wherever production code wants an *http.Client.

func (*Registry) Register

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

Register adds a stub. The first registered stub whose matcher accepts a request serves it.

func (*Registry) RoundTrip

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

RoundTrip implements http.RoundTripper.

type Responder

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

Responder produces the response (or error) for a matched request.

func JSONResponse

func JSONResponse(v any) Responder

JSONResponse is a 200 whose body is the JSON encoding of v.

func StatusJSONResponse

func StatusJSONResponse(status int, v any) Responder

StatusJSONResponse responds with status and the JSON encoding of v.

func StatusStringResponse

func StatusStringResponse(status int, body string) Responder

StatusStringResponse responds with status and a raw string body.

func StringResponse

func StringResponse(body string) Responder

StringResponse is a 200 with a raw string body.

Jump to

Keyboard shortcuts

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