httpmock

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package httpmock provides a mock HTTP RoundTripper for testing purposes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RoundTrip

type RoundTrip struct {
	RequestCheck RoundTripRequestCheck
	Response     RoundTripResponse
}

RoundTrip defines the behavior for a single HTTP response in the sequence.

type RoundTripRequestCheck

type RoundTripRequestCheck func(t *testing.T, req *http.Request)

RoundTripRequestCheck defines a function type for checking HTTP requests.

func ChainRoundTripRequestChecks

func ChainRoundTripRequestChecks(checks ...RoundTripRequestCheck) RoundTripRequestCheck

ChainRoundTripRequestChecks chains multiple RoundTripRequestCheck functions into a single RoundTripRequestCheck.

func RoundTripRequestCheckJSON

func RoundTripRequestCheckJSON[T any](checkFn func(t *testing.T, data T)) RoundTripRequestCheck

RoundTripRequestCheckJSON creates a RoundTripRequestCheck that parses the request body as JSON into the specified type T and applies the provided check function.

type RoundTripResponse

type RoundTripResponse struct {
	Status  int
	Headers map[string]string
	Body    []byte
	// Error allows simulating a network error (RoundTrip returns error)
	Error error
}

RoundTripResponse defines the components of an HTTP response.

type RoundTripper

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

RoundTripper is a simplistic http.RoundTripper that serves a pre-defined sequence of responses.

func NewRoundTripper

func NewRoundTripper(t *testing.T, resps []RoundTrip) *RoundTripper

NewRoundTripper creates a new RoundTripper.

func (*RoundTripper) RoundTrip

func (m *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface.