Help us shape the future of Go! Take the 2026 Go Developer Survey

httpfixture

package
v1.138.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

HTTP fixture replay

NewHandler() creates an empty handler for stateless request/response exchanges. SetReusableFixtures(calls) replaces the test-wide reusable fallbacks; nil clears them. Invalid replacements leave the old fixtures intact. Replacing reusable fixtures does not reset the current step or its failures. StartStep installs a step's consumable fixtures and returns any failures from the previous step. Verify checks the current step for unexpected calls and unused fixtures.

Matching includes the method, escaped path, query parameters, JSON body, and listed request headers. JSON object key order and whitespace are ignored; array order and values matter. An absent body differs from JSON null.

Independent request signatures may interleave. Identical signatures consume responses FIFO. Requests and fixture consumption are protected by a mutex. Test-wide reusable exchanges are intended for invariant client initialization, such as host metadata; they are not required to be consumed.

Documentation

Overview

Package httpfixture replays HTTP exchanges without simulating remote state.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Body

type Body struct {
	JSON any `yaml:"json"`
}

Body distinguishes an absent HTTP body from a JSON value, including JSON null.

type Call

type Call struct {
	Request  Request  `yaml:"request"`
	Response Response `yaml:"response"`
}

Call pairs a request signature with its response.

type Handler

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

Handler permits independent requests to interleave and consumes identical request signatures FIFO. A mutex keeps matching and consumption atomic.

func NewHandler

func NewHandler() *Handler

NewHandler returns an empty handler; fixture sets are configured separately.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP prefers consumable step fixtures over reusable fallbacks. Matching and consumption share a lock so concurrent requests cannot consume a call twice.

func (*Handler) SetReusableFixtures

func (h *Handler) SetReusableFixtures(calls []Call) error

SetReusableFixtures replaces test-wide fallback exchanges, such as metadata. They survive step transitions, may be matched repeatedly, and need not be consumed. Invalid fixtures leave the previous set intact.

func (*Handler) StartStep

func (h *Handler) StartStep(label string, calls []Call) error

StartStep reports the previous step's failures and installs the next exchanges. Installing them even after failure allows the runner to attempt cleanup.

func (*Handler) Verify

func (h *Handler) Verify() error

Verify reports unexpected requests and unconsumed step exchanges without clearing failures. Reusable fixtures are excluded from consumption checks.

type Request

type Request struct {
	Method  string              `yaml:"method"`
	Path    string              `yaml:"path"`
	Query   map[string][]string `yaml:"query,omitempty"`
	Headers map[string]string   `yaml:"headers,omitempty"`
	Body    *Body               `yaml:"body"`
}

Request defines a complete signature; only listed headers are compared.

type Response

type Response struct {
	Status  int               `yaml:"status"`
	Headers map[string]string `yaml:"headers,omitempty"`
	Body    *Body             `yaml:"body"`
}

Response defines the HTTP reply, with an optional JSON body.

Jump to

Keyboard shortcuts

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