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

httpclient

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package httpclient supplies deterministic outbound HTTP transports for tests. A fake never falls through to the network. Recorded data is explicitly private test data; ordinary formatting omits URLs, headers and bodies.

Index

Constants

View Source
const MaxRecordedBodyBytes = 1 << 20
View Source
const MaxRecordedBytes = 16 << 20
View Source
const MaxRecordedURLBytes = 16 << 10
View Source
const MaxRequests = 1024

Variables

View Source
var Exhausted = fault.New(fault.Missing, "outbound HTTP fake has no remaining outcome")
View Source
var Unmatched = fault.New(fault.Missing, "outbound HTTP fake has no matching route")

Unmatched reports a request that matches no Route of a routed fake.

Functions

This section is empty.

Types

type Fake

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

Fake is a mutex-protected HTTP transport fake with queued outcomes and bounded request recording.

func New

func New(outcomes ...Outcome) (*Fake, error)

New answers requests with outcomes in arrival order.

func NewRoutes

func NewRoutes(routes ...Route) (*Fake, error)

NewRoutes answers each request from the first matching Route. A request matching no route fails with Unmatched; an exhausted route with Exhausted.

func (*Fake) AssertNotSent

func (f *Fake) AssertNotSent(t testing.TB, match func(Request) bool)

AssertNotSent fails t when any recorded request satisfies match.

func (*Fake) AssertSent

func (f *Fake) AssertSent(t testing.TB, match func(Request) bool)

AssertSent fails t unless a recorded request satisfies match.

func (*Fake) AssertSentCount

func (f *Fake) AssertSentCount(t testing.TB, expected int, match func(Request) bool)

AssertSentCount fails t unless exactly expected requests satisfy match.

func (*Fake) Format

func (*Fake) Format(state fmt.State, _ rune)

Format writes a fixed label without exposing fake requests.

func (*Fake) Pending

func (f *Fake) Pending() int

Pending returns the synchronized count of unused global and route outcomes.

func (*Fake) Requests

func (f *Fake) Requests() []Request

Requests returns a synchronized snapshot with independent header maps and body slices.

func (*Fake) RoundTrip

func (f *Fake) RoundTrip(request *http.Request) (response *http.Response, err error)

RoundTrip records a bounded request and returns the next matching fake outcome. It consumes and closes the request body.

func (*Fake) Sent

func (f *Fake) Sent() int

Sent returns the synchronized recorded request count; a nil fake returns zero.

type Outcome

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

Outcome is a fake HTTP response or transport failure.

func Fail

func Fail(err error) Outcome

Fail creates a fake outcome containing the supplied error.

func Respond

func Respond(status int, headers http.Header, body []byte) Outcome

Respond copies headers and body into a response outcome, returning a zero outcome if recording bounds are exceeded.

func (Outcome) Format

func (Outcome) Format(state fmt.State, _ rune)

Format writes a fixed label without exposing outcome data.

type Request

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

Request is a recorded outbound HTTP request.

func (Request) Body

func (r Request) Body() []byte

Body returns a copy of the recorded body.

func (Request) Format

func (Request) Format(state fmt.State, _ rune)

Format writes a fixed label without exposing request data.

func (Request) Headers

func (r Request) Headers() http.Header

Headers returns a copy of the recorded headers.

func (Request) Method

func (r Request) Method() string

Method returns the recorded HTTP method.

func (Request) URL

func (r Request) URL() string

URL returns the recorded URL.

type Route

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

Route answers requests whose method and URL match, in declaration order of its outcomes. The pattern uses path.Match syntax against "host/path" (scheme and query excluded), for example "api.example.com/v1/users/*". An empty method matches every method.

func On

func On(method, pattern string, outcomes ...Outcome) Route

On declares a fake route with a method, pattern, and outcome sequence.

func (Route) Format

func (Route) Format(state fmt.State, _ rune)

Format writes a fixed label without exposing route data.

Jump to

Keyboard shortcuts

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