Documentation
¶
Overview ¶
Package testutil holds helpers shared by tests across packages.
Index ¶
- Constants
- func CaptureOutput(t *testing.T, fn func()) (stdout string, stderr string)
- func CaptureStdout(t *testing.T, fn func()) string
- func HandlerClient(h http.Handler) *http.Client
- func NewNoRequestServer(t *testing.T) *httptest.Server
- func StubClient(fn func(*http.Request) (status int, body string)) *http.Client
- type PipeServer
- func (s *PipeServer) Client() *http.Client
- func (s *PipeServer) Close()
- func (s *PipeServer) DialContext(ctx context.Context, _, _ string) (net.Conn, error)
- func (s *PipeServer) SetHandler(h http.Handler)
- func (s *PipeServer) Transport() *http.Transport
- func (s *PipeServer) WebsocketURL(path string) string
- type PollRecorder
- type StubTransport
Constants ¶
const PipeServerURL = "http://pipe.test"
PipeServerURL is the base URL of every PipeServer; nothing resolves it, every dial is a pipe.
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 ¶
CaptureOutput returns what fn writes to os.Stdout and os.Stderr.
func CaptureStdout ¶
CaptureStdout returns what fn writes to os.Stdout.
func HandlerClient ¶ added in v1.18.0
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
NewNoRequestServer returns a server that fails the test on any request, for asserting a call was refused before anything was sent.
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
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
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.