replay

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package replay provides strict, local-only transports for deterministic tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadCases

func LoadCases[T any](path string) ([]T, error)

LoadCases reads portable synthetic case arrays used by both language suites.

func SemanticEqual

func SemanticEqual(a, b []byte) bool

SemanticEqual compares JSON structure while retaining exact decimal number values.

Types

type Exchange

type Exchange struct {
	Request  Request  `json:"request"`
	Response Response `json:"response"`
}

Exchange is one recorded request and its response. Bodies are byte-exact unless JSON is true.

func LoadExchange

func LoadExchange(path string) (Exchange, error)

LoadExchange decodes a JSON cassette file. Response bodies are represented as JSON strings.

type HeadersMode

type HeadersMode string

HeadersMode controls cassette header matching. Empty mode is exact.

const (
	HeadersExact    HeadersMode = "exact"
	HeadersRequired HeadersMode = "required"
)

type Pair

type Pair struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

type RecordedMessage

type RecordedMessage struct {
	Direction string          `json:"direction"`
	Frame     string          `json:"frame"`
	Payload   json.RawMessage `json:"payload"`
}

RecordedMessage preserves a sanitized wire frame and its direction. Tests select the exchange needed for a behavior instead of replaying a whole stream.

type Request

type Request struct {
	Method       string          `json:"method"`
	Origin       string          `json:"origin"`
	Path         string          `json:"path"`
	Query        []Pair          `json:"query"`
	Headers      http.Header     `json:"headers"`
	HeadersMode  HeadersMode     `json:"headers_mode,omitempty"`
	Body         json.RawMessage `json:"body"`
	BodyEncoding string          `json:"body_encoding,omitempty"`
	JSON         bool            `json:"json,omitempty"`
}

type Response

type Response struct {
	Status       int             `json:"status"`
	Headers      http.Header     `json:"headers"`
	Body         json.RawMessage `json:"body"`
	BodyEncoding string          `json:"body_encoding,omitempty"`
	JSON         bool            `json:"json,omitempty"`
}

type SessionRecording

type SessionRecording struct {
	Messages []RecordedMessage `json:"messages"`
}

func LoadSessionRecording

func LoadSessionRecording(path string) (SessionRecording, error)

type Transport

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

Transport replays each cassette at most once and never dials a network destination.

func NewTransport

func NewTransport(xs ...Exchange) *Transport

func NewUnorderedTransport added in v0.5.0

func NewUnorderedTransport(xs ...Exchange) *Transport

NewUnorderedTransport permits explicitly independent requests to consume cassettes in any order while still requiring a one-time exact match.

func (*Transport) AssertConsumed

func (t *Transport) AssertConsumed() error

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(request *http.Request) (*http.Response, error)

type WSHandshake added in v0.5.0

type WSHandshake struct {
	Origin  string      `json:"origin,omitempty"`
	Host    string      `json:"host,omitempty"`
	Path    string      `json:"path,omitempty"`
	Query   url.Values  `json:"query,omitempty"`
	Headers http.Header `json:"headers,omitempty"`
}

WSHandshake is the expected upgrade origin, host, path, query, and application headers. Empty Origin means the request must have no Origin header. Empty Host means the server's loopback host, which is assigned after the test server starts.

type WSStep

type WSStep struct {
	// Channel names an AsyncAPI channel for the production inventory gate.
	Channel string          `json:"channel,omitempty"`
	Kind    string          `json:"kind"`
	Frame   string          `json:"frame"`
	Body    json.RawMessage `json:"body"`
	// Template enables explicit $uuid:name and $ref:name bindings in JSON text frames.
	Template bool `json:"template,omitempty"`
}

WSStep either expects a client frame or sends a server frame. Kind is "expect" or "send".

type WebSocketServer

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

WebSocketServer serves one scripted connection on a loopback httptest server.

func NewWebSocketServer

func NewWebSocketServer(steps []WSStep, timeout time.Duration, handshakes ...WSHandshake) *WebSocketServer

NewWebSocketServer starts a strict scripted WebSocket peer. Each read has a bounded deadline.

func (*WebSocketServer) AssertComplete

func (w *WebSocketServer) AssertComplete(timeout time.Duration) error

AssertComplete waits for the script and reports mismatch, disconnect, or timeout.

func (*WebSocketServer) Close

func (w *WebSocketServer) Close()

Close stops the server and closes an active client connection so shutdown remains bounded.

func (*WebSocketServer) URL

func (w *WebSocketServer) URL() string

URL returns the local websocket URL for a client dialer.

func (*WebSocketServer) WaitStep added in v0.5.0

func (w *WebSocketServer) WaitStep(index int, timeout time.Duration) error

WaitStep waits until the strict peer has consumed step index, or reports its first mismatch. It lets tests trigger later client actions without sleeping.

Jump to

Keyboard shortcuts

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