httpmock

package
v1.0.99 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 8 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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(reg *Registry) *http.Client

NewClient returns an http.Client that uses the Registry as its transport.

Types

type Registry

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

Registry records stubs and implements http.RoundTripper.

func (*Registry) Register

func (r *Registry) Register(s *Stub)

Register adds a stub to the registry.

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 asserts all stubs were matched.

type Stub

type Stub struct {
	Method      string      // empty = match any method
	URL         string      // substring match on URL
	Status      int         // default 200
	Body        interface{} // auto JSON-serialized
	RawBody     []byte      // raw bytes (takes precedence over Body when non-nil)
	ContentType string      // override Content-Type header (default: application/json)
	Headers     http.Header // optional full response headers (takes precedence over ContentType)
	Error       error       // optional transport error returned after OnMatch

	// BodyFilter (optional): match only when the captured request body satisfies
	// this predicate. Used to disambiguate multiple stubs that share a URL.
	BodyFilter func([]byte) bool

	// OnMatch (optional): runs synchronously after the stub matches but before
	// the response is composed. Used in tests to inject panics or count
	// in-flight goroutines.
	OnMatch func(req *http.Request)

	// Reusable (optional): when true, the stub stays available for further
	// matches after the first hit.
	Reusable bool

	// Optional (optional): when true, Verify does not require this stub to be
	// matched. Useful for negative assertions via OnMatch.
	Optional bool

	// CapturedHeaders records the request headers of the matched request.
	// Populated after RoundTrip matches this stub.
	CapturedHeaders http.Header
	CapturedBody    []byte
	// CapturedBodies records every captured request body, appended on each
	// match regardless of Reusable — so a one-shot stub's single hit is
	// recorded here too, and len(CapturedBodies) is a sound assertion for "this
	// request was never made". (CapturedBody keeps the most recent capture for
	// back-compat.)
	CapturedBodies [][]byte
	// contains filtered or unexported fields
}

Stub defines a preset HTTP response.

Jump to

Keyboard shortcuts

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