testutil

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RedactSimulation

func RedactSimulation(sim *Simulation, opts RedactOptions)

RedactSimulation sanitizes all sensitive data in a simulation.

func ResetCounters

func ResetCounters()

ResetCounters resets the redaction counters (call between capture runs).

func SaveSimulation

func SaveSimulation(path string, sim *Simulation) error

SaveSimulation writes a simulation to a JSON file.

Types

type Pair

type Pair struct {
	Description string   `json:"description,omitempty"`
	Request     Request  `json:"request"`
	Response    Response `json:"response"`
}

Pair is a single recorded request/response exchange.

type RecordingTransport

type RecordingTransport struct {
	Base  http.RoundTripper
	Pairs []Pair
}

RecordingTransport wraps an http.RoundTripper and records all request/response pairs.

func (*RecordingTransport) BuildSimulation

func (rt *RecordingTransport) BuildSimulation() *Simulation

BuildSimulation creates a Simulation from recorded pairs.

func (*RecordingTransport) RoundTrip

func (rt *RecordingTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes the request and records the exchange.

type RedactOptions

type RedactOptions struct {
	// Domain to replace real TP domain with.
	ReplacementDomain string
	// RealDomain is the actual domain to find and replace.
	RealDomain string
}

RedactOptions controls what gets redacted in simulations.

func DefaultRedactOptions

func DefaultRedactOptions(realDomain string) RedactOptions

DefaultRedactOptions returns standard redaction settings.

type Request

type Request struct {
	Method string            `json:"method"`
	Path   string            `json:"path"`
	Query  map[string]string `json:"query,omitempty"`
}

Request describes the expected HTTP request to match.

type Response

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

Response describes the canned HTTP response to return.

func (Response) BodyBytes

func (r Response) BodyBytes() []byte

BodyBytes returns the response body as raw bytes. If body is a JSON string (e.g. XML content), it unquotes it. If body is a JSON object/array, it returns the raw JSON.

type Simulation

type Simulation struct {
	Pairs []Pair `json:"pairs"`
}

Simulation holds a set of request/response pairs for replay.

func LoadSimulation

func LoadSimulation(path string) (*Simulation, error)

LoadSimulation reads a simulation file from disk.

func LoadSimulationsFromDir

func LoadSimulationsFromDir(dir string) (*Simulation, error)

LoadSimulationsFromDir loads all .json files from a directory.

type SimulationServer

type SimulationServer struct {
	Server *httptest.Server
	// contains filtered or unexported fields
}

SimulationServer wraps an httptest.Server that replays recorded simulations.

func NewSimulationServer

func NewSimulationServer(sim *Simulation) *SimulationServer

NewSimulationServer creates and starts a test server from simulation data.

func (*SimulationServer) Close

func (ss *SimulationServer) Close()

Close shuts down the test server.

func (*SimulationServer) Requests

func (ss *SimulationServer) Requests() []recordedRequest

Requests returns all recorded requests.

func (*SimulationServer) URL

func (ss *SimulationServer) URL() string

URL returns the test server's URL.

Jump to

Keyboard shortcuts

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