testutil

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testutil holds helpers shared by tests across packages.

Index

Constants

View Source
const PipeServerURL = "http://pipe.test"

PipeServerURL is the base URL of every PipeServer; nothing resolves it, every dial is a pipe.

View Source
const StubBaseURL = "http://stub.invalid"

StubBaseURL is the BaseURL to pin on a client whose transport is stubbed: it must parse, and it must never resolve.

Variables

This section is empty.

Functions

func CaptureOutput

func CaptureOutput(t *testing.T, fn func()) (stdout string, stderr string)

CaptureOutput returns what fn writes to os.Stdout and os.Stderr.

func CaptureStdout

func CaptureStdout(t *testing.T, fn func()) string

CaptureStdout returns what fn writes to os.Stdout.

func HandlerClient added in v1.18.0

func HandlerClient(h http.Handler) *http.Client

HandlerClient returns an http.Client that serves every request from h in memory, so a handler that sets headers runs where StubClient's fn cannot. TODO: replace with httptest.NewTestServer once go.mod moves to Go 1.27.

func NewNoRequestServer added in v1.16.0

func NewNoRequestServer(t *testing.T) *httptest.Server

NewNoRequestServer returns a server that fails the test on any request, for asserting a call was refused before anything was sent.

func StubClient added in v1.12.0

func StubClient(fn func(*http.Request) (status int, body string)) *http.Client

StubClient returns an http.Client that answers from fn.

Types

type PipeServer added in v1.18.0

type PipeServer struct {
	URL string
	// contains filtered or unexported fields
}

PipeServer serves a handler over net.Pipe connections, HTTP and WebSocket alike. A goroutine blocked on a pipe is durably blocked, so unlike httptest.Server it can live in a synctest bubble and the bubble's clock advances. Create it inside the bubble. TODO: replace with httptest.NewTestServer once go.mod moves to Go 1.27.

func NewPipeServer added in v1.18.0

func NewPipeServer(t *testing.T, h http.Handler) *PipeServer

NewPipeServer starts serving h and shuts down at the end of t, closing every connection.

func (*PipeServer) Client added in v1.18.0

func (s *PipeServer) Client() *http.Client

Client returns an http.Client whose every request reaches the server.

func (*PipeServer) Close added in v1.18.0

func (s *PipeServer) Close()

Close stops the server and drops every connection, a hijacked WebSocket's included; it is safe to call twice.

func (*PipeServer) DialContext added in v1.18.0

func (s *PipeServer) DialContext(ctx context.Context, _, _ string) (net.Conn, error)

DialContext opens a new pipe to the server whatever the address.

func (*PipeServer) SetHandler added in v1.18.0

func (s *PipeServer) SetHandler(h http.Handler)

SetHandler replaces the handler, for one that needs the server's URL; call it before any request.

func (*PipeServer) Transport added in v1.18.0

func (s *PipeServer) Transport() *http.Transport

Transport is Client's transport, for code that dials the way a client's transport does.

func (*PipeServer) WebsocketURL added in v1.18.0

func (s *PipeServer) WebsocketURL(path string) string

WebsocketURL is the ws:// form of URL with path appended.

type PollRecorder added in v1.12.0

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

PollRecorder collects the time of each poll a stubbed server sees, so a test can assert the schedule that produced them rather than the poll count alone. A count is a weak witness: a widened schedule and a fixed tick can land on the same number. Under testing/synctest the times come off the bubble's fake clock, which makes the gaps exact.

func (*PollRecorder) Record added in v1.12.0

func (r *PollRecorder) Record()

Record marks one poll as having arrived now. Safe to call from a handler goroutine.

func (*PollRecorder) WidestGap added in v1.12.0

func (r *PollRecorder) WidestGap() time.Duration

WidestGap returns the longest interval between consecutive polls, or 0 when fewer than two arrived. Only a schedule that widens with age can produce a gap above its own base tick.

type StubTransport added in v1.12.0

type StubTransport struct {
	Respond func(*http.Request) (status int, body string)
}

StubTransport answers every request from Respond, with no socket and no server goroutine. A poll loop under testing/synctest needs that: the fake clock only advances while every goroutine in the bubble is durably blocked, and a real network read never counts as durably blocked.

func (StubTransport) RoundTrip added in v1.12.0

func (t StubTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Jump to

Keyboard shortcuts

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