stubserver

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package stubserver provides a configurable HTTP test server for provider testing. It supports SSE fixture replay and fault injection over real HTTP connections, exercising the full client stack (headers, auth, timeouts).

Provider-agnostic: the fixture format is raw SSE text, so it works for Anthropic, OpenAI, and Google streaming formats without modification.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(opts ...Option) http.Handler

NewHandler creates an http.Handler using the same option behavior as New, but without starting an httptest.Server. The caller owns listener lifecycle.

Types

type CapturedRequest

type CapturedRequest struct {
	Method string
	Path   string
	Header http.Header
	Body   []byte
}

CapturedRequest stores an incoming request for later verification.

type Fault

type Fault struct {
	// Mode selects the fault type.
	Mode FaultMode

	// AfterEvents specifies the fault injection point in the event stream.
	// For TCPReset: disconnects after this many events have been sent.
	// For Malformed: replaces the event at this index with corrupt data.
	// For Backpressure: not used (delay applies to all events).
	AfterEvents int

	// StatusCode for non-2xx faults (Throttle, EmptyBody).
	StatusCode int

	// RetryAfter sets the Retry-After header for Throttle faults.
	RetryAfter string

	// MalformedData is the corrupt payload to inject for Malformed faults.
	MalformedData string

	// EventDelay adds per-event delay for Backpressure faults.
	EventDelay time.Duration
}

Fault defines fault injection behavior for a stub server.

type FaultMode

type FaultMode int

FaultMode enumerates the supported fault injection modes.

const (
	// NoFault serves the fixture normally.
	NoFault FaultMode = iota

	// TCPReset (F1) — close the TCP connection after AfterEvents SSE events.
	TCPReset

	// Malformed (F2) — inject MalformedData as a corrupt SSE event after
	// AfterEvents events, then continue or close.
	Malformed

	// Throttle (F3) — return StatusCode (default 429) with optional
	// Retry-After header. Does not serve SSE data.
	Throttle

	// Backpressure (F4) — serve events with EventDelay between each,
	// simulating a slow server to test client timeout behavior.
	Backpressure

	// EmptyBody (F6) — return StatusCode with an empty response body.
	// Tests error classification from status code alone.
	EmptyBody
)

type Option

type Option func(*Server)

Option configures a Server.

func WithFault

func WithFault(fixture string, fault Fault) Option

WithFault configures the server to inject a fault while serving the fixture.

func WithFixture

func WithFixture(fixture string) Option

WithFixture configures the server to replay an SSE fixture string. The fixture should be raw SSE text (e.g., "event: message_start\ndata: {...}\n\n").

func WithFixtureFunc

func WithFixtureFunc(fn func(r *http.Request) string) Option

WithFixtureFunc configures the server to call fn for each request to get the fixture to serve. Useful for per-request fixture selection.

func WithHandler

func WithHandler(h http.HandlerFunc) Option

WithHandler configures a fully custom handler.

func WithJSONFault

func WithJSONFault(fixture string, fault Fault) Option

WithJSONFault configures the server to inject a fault while serving a JSON response. For TCPReset, AfterEvents is reinterpreted as byte count: the connection is closed after writing that many bytes of the response body.

func WithJSONFixture

func WithJSONFixture(fixture string) Option

WithJSONFixture configures the server to serve a canned JSON response. The fixture should be a valid JSON string.

func WithJSONFixtureFunc

func WithJSONFixtureFunc(fn func(r *http.Request) string) Option

WithJSONFixtureFunc configures the server to call fn for each request to get the JSON fixture to serve. Useful for per-request fixture selection.

type Server

type Server struct {
	// URL is the base URL of the running test server.
	URL string
	// contains filtered or unexported fields
}

Server is a configurable HTTP test server for provider testing.

func New

func New(opts ...Option) *Server

New creates a stub server with the given handler options. The server starts immediately and must be closed with Close().

func NewBackpressureServer

func NewBackpressureServer(fixture string, eventDelay time.Duration) *Server

NewBackpressureServer creates a server that sends events with a delay between each to simulate slow server responses.

func NewEmptyBodyServer

func NewEmptyBodyServer(statusCode int) *Server

NewEmptyBodyServer creates a server that returns a non-2xx status with an empty response body.

func NewJSONFaultServer

func NewJSONFaultServer(fixture string, fault Fault) *Server

NewJSONFaultServer creates a server that injects a fault while serving JSON.

func NewJSONServer

func NewJSONServer(fixture string) *Server

NewJSONServer creates a server that serves a canned JSON response.

func NewMalformedServer

func NewMalformedServer(fixture string, afterEvents int, malformedData string) *Server

NewMalformedServer creates a server that injects a malformed SSE event after afterEvents normal events.

func NewSequenceServer

func NewSequenceServer(responses []func(w http.ResponseWriter, r *http.Request)) *Server

NewSequenceServer creates a server that serves different responses for sequential requests. Useful for testing retry behavior.

func NewStatusCodeServer

func NewStatusCodeServer(statusCode int, body string) *Server

NewStatusCodeServer creates a server that returns the given status code with an optional body.

func NewTCPResetServer

func NewTCPResetServer(fixture string, afterEvents int) *Server

NewTCPResetServer creates a server that sends AfterEvents SSE events from fixture then kills the TCP connection.

func NewThrottleServer

func NewThrottleServer(retryAfter string) *Server

NewThrottleServer creates a server that returns HTTP 429 with optional Retry-After header.

func (*Server) ClearRequests

func (s *Server) ClearRequests()

ClearRequests resets the captured request log.

func (*Server) Close

func (s *Server) Close()

Close shuts down the test server.

func (*Server) Listener

func (s *Server) Listener() net.Listener

Listener returns the underlying net.Listener for low-level control.

func (*Server) Requests

func (s *Server) Requests() []CapturedRequest

Requests returns a copy of all captured requests.

Jump to

Keyboard shortcuts

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