testutil

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package testutil provides test helpers for code that uses the relay HTTP client. It is inspired by OkHttp's MockWebServer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockResponse

type MockResponse struct {
	// Status is the HTTP status code (default 200).
	Status int

	// Headers are response headers merged into the reply.
	Headers map[string]string

	// Body is the response body as a string.
	Body string

	// Delay introduces artificial latency before the response is written.
	Delay time.Duration
	// contains filtered or unexported fields
}

MockResponse defines the response the MockServer should return for the next queued request.

type MockServer

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

MockServer is a test HTTP server that serves queued responses in FIFO order and records incoming requests.

func NewMockServer

func NewMockServer() *MockServer

NewMockServer starts a new local HTTP test server. Call MockServer.Close when the test is done.

func (*MockServer) Close

func (s *MockServer) Close()

Close shuts down the test server.

func (*MockServer) Enqueue

func (s *MockServer) Enqueue(responses ...MockResponse)

Enqueue adds one or more responses to the response queue. Responses are served in FIFO order.

func (*MockServer) EnqueueError

func (s *MockServer) EnqueueError()

EnqueueError causes the next request to fail with a connection error.

func (*MockServer) RequestCount

func (s *MockServer) RequestCount() int

RequestCount returns the total number of requests received since the server was created.

func (*MockServer) TakeRequest

func (s *MockServer) TakeRequest(timeout time.Duration) (*RecordedRequest, error)

TakeRequest returns the next recorded request in FIFO order. It blocks until a request arrives or the timeout elapses, whichever comes first. Returns an error if the timeout expires before a request is available.

func (*MockServer) URL

func (s *MockServer) URL() string

URL returns the base URL of the test server (e.g. "http://127.0.0.1:PORT").

type RecordedHTTPRequest

type RecordedHTTPRequest struct {
	Method  string
	URL     string
	Headers http.Header
	// Body contains the raw request body bytes. May be nil for requests with
	// no body (GET, HEAD) or if the body could not be read.
	Body []byte
}

RecordedHTTPRequest holds the details of a request that was intercepted by a RequestRecorder.

type RecordedRequest

type RecordedRequest struct {
	Method  string
	Path    string
	Headers http.Header
	Body    []byte
	Query   url.Values
}

RecordedRequest holds details of an HTTP request captured by the MockServer.

type RequestRecorder

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

RequestRecorder intercepts outgoing requests from a relay client without actually sending them. It is useful when you only need to assert on what was sent, not on the response.

func NewRequestRecorder

func NewRequestRecorder() *RequestRecorder

NewRequestRecorder creates a new, empty RequestRecorder.

func (*RequestRecorder) Middleware

func (r *RequestRecorder) Middleware() func(http.RoundTripper) http.RoundTripper

Middleware returns an http.RoundTripper wrapping function that records every outgoing request. Pass the result to relay.WithTransportMiddleware.

func (*RequestRecorder) Requests

func (r *RequestRecorder) Requests() []*RecordedHTTPRequest

Requests returns a snapshot of all intercepted requests.

func (*RequestRecorder) Reset

func (r *RequestRecorder) Reset()

Reset clears all recorded requests.

Jump to

Keyboard shortcuts

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