httpmock

package
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package httpmock provides JSONC-driven outbound HTTP mocks for itestkit cases.

Index

Constants

View Source
const (
	// PlanHTTPCallsHandlerName configures planned outbound HTTP calls.
	PlanHTTPCallsHandlerName = "PlanHTTPCalls"
	// AwaitHTTPCallsHandlerName checks planned HTTP calls during an await step.
	AwaitHTTPCallsHandlerName = "AwaitHTTPCalls"
	// VerifyHTTPCallsHandlerName checks planned HTTP calls during a verify step.
	VerifyHTTPCallsHandlerName = "VerifyHTTPCalls"
)
View Source
const (
	// QueryModeExact requires exact parsed query equality.
	QueryModeExact QueryMode = "exact"
	// QueryModeSubset allows extra actual query parameters.
	QueryModeSubset QueryMode = "subset"
	// HeadersModeExact requires exact header equality.
	HeadersModeExact HeadersMode = "exact"
	// HeadersModeSubset allows extra actual headers.
	HeadersModeSubset HeadersMode = "subset"
	// OrderingStrict requires the observed request order to match the plan order.
	OrderingStrict Ordering = "strict"
	// OrderingAny ignores observed request order during verification.
	OrderingAny Ordering = "any"
	// CountModeExact requires exactly expected_count matching requests.
	CountModeExact CountMode = "exact"
	// CountModeAtLeast requires at least expected_count matching requests.
	CountModeAtLeast CountMode = "at_least"
)

Variables

This section is empty.

Functions

func NewRegistry

func NewRegistry[C serverProvider](customHandlers map[string]itestkit.Handler[C]) itestkit.MapRegistry[C]

NewRegistry returns a registry with preset HTTP mock handlers and custom handlers.

The case harness type C must expose HTTPMock() *Server.

Types

type AwaitHTTPCallsHandler

type AwaitHTTPCallsHandler[C serverProvider] struct{}

AwaitHTTPCallsHandler checks the current HTTP call state.

func (AwaitHTTPCallsHandler[C]) DecodeExpectedResponse

func (AwaitHTTPCallsHandler[C]) DecodeExpectedResponse(raw json.RawMessage) (any, error)

DecodeExpectedResponse decodes the expected await response.

func (AwaitHTTPCallsHandler[C]) DecodeRequest

func (AwaitHTTPCallsHandler[C]) DecodeRequest(raw json.RawMessage) (any, error)

DecodeRequest decodes an empty await request.

func (AwaitHTTPCallsHandler[C]) Invoke

func (AwaitHTTPCallsHandler[C]) Invoke(ctx context.Context, client C, request any) (any, error)

Invoke checks the current HTTP call state.

func (AwaitHTTPCallsHandler[C]) NormalizeResponse

func (AwaitHTTPCallsHandler[C]) NormalizeResponse(response any) (any, error)

NormalizeResponse returns the await response unchanged.

type CallExpectation

type CallExpectation struct {
	Name           string              `json:"name,omitempty"`
	ExpectedCount  int                 `json:"expected_count"`
	CountMode      CountMode           `json:"count_mode,omitempty"`
	Method         string              `json:"method"`
	Path           string              `json:"path"`
	Query          map[string][]string `json:"query,omitempty"`
	QueryMode      QueryMode           `json:"query_mode,omitempty"`
	Headers        map[string][]string `json:"headers,omitempty"`
	HeadersMode    HeadersMode         `json:"headers_mode,omitempty"`
	HeadersPresent []string            `json:"headers_present,omitempty"`
	Body           json.RawMessage     `json:"body,omitempty"`
	BodySubset     json.RawMessage     `json:"body_subset,omitempty"`
	RawBody        *string             `json:"raw_body,omitempty"`
	FormBody       map[string][]string `json:"form_body,omitempty"`
	FormBodySubset map[string][]string `json:"form_body_subset,omitempty"`
	Response       Response            `json:"response"`
}

CallExpectation describes one expected HTTP request and its stub response.

type CheckResult

type CheckResult struct {
	MatchedCount       int               `json:"matched_count"`
	ObservedRequests   []ObservedRequest `json:"observed_requests"`
	LastMismatchReason string            `json:"last_mismatch_reason,omitempty"`
}

CheckResult describes observed request matching state.

type CountMode

type CountMode string

CountMode controls how expected_count is checked.

type HeadersMode

type HeadersMode string

HeadersMode controls HTTP header matching.

type ObservedRequest

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

ObservedRequest describes a request recorded by the mock server.

type Option

type Option func(*config)

Option changes server construction settings.

func WithMaxRequestBodyBytes

func WithMaxRequestBodyBytes(size int64) Option

WithMaxRequestBodyBytes sets the maximum recorded request body size.

type Ordering

type Ordering string

Ordering controls request order verification.

type Plan

type Plan struct {
	Calls    []CallExpectation `json:"calls"`
	Ordering Ordering          `json:"ordering,omitempty"`
}

Plan describes planned HTTP calls for one test case.

type PlanHTTPCallsHandler

type PlanHTTPCallsHandler[C serverProvider] struct{}

PlanHTTPCallsHandler stores the HTTP call plan in the case server.

func (PlanHTTPCallsHandler[C]) DecodeExpectedResponse

func (PlanHTTPCallsHandler[C]) DecodeExpectedResponse(raw json.RawMessage) (any, error)

DecodeExpectedResponse decodes the expected plan response.

func (PlanHTTPCallsHandler[C]) DecodeRequest

func (PlanHTTPCallsHandler[C]) DecodeRequest(raw json.RawMessage) (any, error)

DecodeRequest decodes a planned HTTP call set.

func (PlanHTTPCallsHandler[C]) Invoke

func (PlanHTTPCallsHandler[C]) Invoke(ctx context.Context, client C, request any) (any, error)

Invoke stores the plan in the case server.

func (PlanHTTPCallsHandler[C]) NormalizeResponse

func (PlanHTTPCallsHandler[C]) NormalizeResponse(response any) (any, error)

NormalizeResponse returns the plan response unchanged.

type PlanHTTPCallsResponse

type PlanHTTPCallsResponse struct {
	Planned       bool `json:"planned"`
	ExpectedCalls int  `json:"expected_calls"`
}

PlanHTTPCallsResponse describes a successful plan step result.

type QueryMode

type QueryMode string

QueryMode controls query parameter matching.

type Response

type Response struct {
	Status  int                 `json:"status"`
	Headers map[string][]string `json:"headers,omitempty"`
	Body    json.RawMessage     `json:"body,omitempty"`
	RawBody *string             `json:"raw_body,omitempty"`
}

Response describes the HTTP response returned for a planned request.

type Server

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

Server records HTTP requests and returns planned responses.

func NewPassThroughServer

func NewPassThroughServer(t *testing.T, handler http.Handler, opts ...Option) *Server

NewPassThroughServer starts an HTTP mock server that verifies requests and delegates matched calls to handler.

func NewServer

func NewServer(t *testing.T, opts ...Option) *Server

NewServer starts an HTTP mock server for one test case.

func (*Server) Await

func (server *Server) Await(_ context.Context) (CheckResult, error)

Await checks whether planned HTTP calls have happened yet.

func (*Server) Plan

func (server *Server) Plan(ctx context.Context, plan Plan) error

Plan stores planned HTTP calls.

func (*Server) ServeHTTP

func (server *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP records HTTP requests and returns planned responses.

func (*Server) URL

func (server *Server) URL() string

URL returns the mock server base URL.

func (*Server) Verify

func (server *Server) Verify(ctx context.Context) (CheckResult, error)

Verify checks final HTTP call expectations.

type VerifyHTTPCallsHandler

type VerifyHTTPCallsHandler[C serverProvider] struct{}

VerifyHTTPCallsHandler checks the final HTTP call state.

func (VerifyHTTPCallsHandler[C]) DecodeExpectedResponse

func (VerifyHTTPCallsHandler[C]) DecodeExpectedResponse(raw json.RawMessage) (any, error)

DecodeExpectedResponse decodes the expected verify response.

func (VerifyHTTPCallsHandler[C]) DecodeRequest

func (VerifyHTTPCallsHandler[C]) DecodeRequest(raw json.RawMessage) (any, error)

DecodeRequest decodes an empty verify request.

func (VerifyHTTPCallsHandler[C]) Invoke

func (VerifyHTTPCallsHandler[C]) Invoke(ctx context.Context, client C, request any) (any, error)

Invoke checks the final HTTP call state.

func (VerifyHTTPCallsHandler[C]) NormalizeResponse

func (VerifyHTTPCallsHandler[C]) NormalizeResponse(response any) (any, error)

NormalizeResponse returns the verify response unchanged.

Jump to

Keyboard shortcuts

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