testutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 17 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 the test plumbing go-batchdata's packages share: in-memory and recording HTTP fakes over HTTP/1.1 and HTTP/2, fmt leak assertions, and checks of error types and JSON round trips.

It is imported only by tests. It imports testify and the standard library, and never a package of this module, so every test package can use it.

Index

Constants

View Source
const RaceEnabled = false

RaceEnabled reports whether the race detector is on. It changes allocation counts and sizes, so allocation guards skip under it.

Variables

This section is empty.

Functions

func AllocStats

func AllocStats(runs int, f func()) (allocs, allocated float64)

AllocStats reports the average number of allocations and of bytes allocated by one run of f, measured like testing.AllocsPerRun: one warm-up run, then runs runs with GOMAXPROCS set to 1. Unlike AllocsPerRun, it keeps fractions and also reports bytes.

func AssertDiffRedacted

func AssertDiffRedacted[T any](tb testing.TB, a, b *T, secrets ...string) bool

AssertDiffRedacted compares a and b, each held in unexported struct fields, with assert.Equal, and fails tb unless that comparison fails without printing a secret. testify prints both values and their diff by reflection, so this covers what a failing assertion on such values would log.

func AssertNeverPrints

func AssertNeverPrints(tb testing.TB, v any, secrets ...string) bool

AssertNeverPrints formats v with every verb of FormatVerbs and fails tb when any output holds a secret, as AssertRedacted checks.

func AssertRedacted

func AssertRedacted(tb testing.TB, out string, secrets []string, msgAndArgs ...any) bool

AssertRedacted fails tb when out contains any secret, in plain text or as hex in either case. msgAndArgs annotate a failure.

func AssertShapesNeverPrint

func AssertShapesNeverPrint[T any](tb testing.TB, p *T, secrets ...string) bool

AssertShapesNeverPrint formats every Shape of p with every verb of FormatVerbs and fails tb when any output holds a secret, as AssertRedacted checks.

func BlockUntilDone

func BlockUntilDone(r *http.Request) (*http.Response, error)

BlockUntilDone is an in-memory round trip that never answers: it drains the request, waits for the request's context to end and returns its error.

func Drain

func Drain(r *http.Request) int64

Drain reads and closes the request body, as a real transport would, and returns its length.

func FormatVerbs

func FormatVerbs() []string

FormatVerbs returns the fmt verbs a caller might apply to a value. Most of them bypass String and GoString and print fields by reflection, which dereferences pointers and reaches unexported fields.

func RequireErrorAs

func RequireErrorAs[E error](tb testing.TB, err error) E

RequireErrorAs returns the first error in err's chain of type E, failing tb unless err is non-nil and has one.

func RequireJSONRoundTrip

func RequireJSONRoundTrip[T any](tb testing.TB, v T, msgAndArgs ...any)

RequireJSONRoundTrip marshals v, unmarshals the result into a new T and fails tb unless it equals v. msgAndArgs annotate a failure.

func SkipUnderRace

func SkipUnderRace(tb testing.TB)

SkipUnderRace skips the test when the race detector is on, because it changes allocation counts and sizes.

Types

type CountingTransport

type CountingTransport struct {
	Next http.RoundTripper
	// contains filtered or unexported fields
}

CountingTransport counts the round trips it passes on to Next.

func (*CountingTransport) Attempts

func (c *CountingTransport) Attempts() int

Attempts returns the number of round trips so far.

func (*CountingTransport) RoundTrip

func (c *CountingTransport) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip counts the attempt and delegates to Next.

type Protocol

type Protocol string

Protocol selects the HTTP version a Server and its Client speak.

const (
	HTTP1 Protocol = "HTTP/1.1"
	HTTP2 Protocol = "HTTP/2.0"
)

Protocols.

func Protocols

func Protocols() []Protocol

Protocols returns both protocols, for suites that run over each.

func (Protocol) Name

func (p Protocol) Name() string

Name names p for subtests: "http1" or "http2". Unlike p itself, it has no slash, which -run would read as a subtest separator.

type Request

type Request struct {
	Method string
	Proto  string
	// URL is the request's URL as received: the path and query on a server,
	// the full URL in a transport.
	URL    string
	Header http.Header
	Body   []byte
}

Request is one request as a test saw it.

func Record

func Record(r *http.Request) Request

Record reads and closes r's body and returns a copy of the request.

type RoundTripFunc

type RoundTripFunc func(*http.Request) (*http.Response, error)

RoundTripFunc adapts a function to http.RoundTripper.

func (RoundTripFunc) RoundTrip

func (f RoundTripFunc) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip returns f(r).

type Server

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

Server is a local TLS server that records every request before handing it to its handler. Handlers run on server goroutines, so they should only answer; tests assert on Requests afterward.

func NewServer

func NewServer(tb testing.TB, h http.HandlerFunc) *Server

NewServer starts an HTTP/1.1 Server; see NewServerProto.

func NewServerProto

func NewServerProto(tb testing.TB, proto Protocol, h http.HandlerFunc) *Server

NewServerProto starts a Server speaking proto that records each request and then calls h, which can still read the request body. It is closed when the test ends. Its error log is discarded: when a test ends while the client is still finishing a spare TLS handshake, the closing server would log a harmless "use of closed network connection".

func (*Server) Client

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

Client returns an HTTP client that trusts the server's certificate and speaks the server's protocol.

func (*Server) Hits

func (s *Server) Hits() int

Hits returns the number of requests received.

func (*Server) Requests

func (s *Server) Requests() []Request

Requests returns a copy of the recorded requests, in arrival order.

func (*Server) URL

func (s *Server) URL() string

URL returns the server's base URL, such as https://127.0.0.1:port.

type Shape

type Shape struct {
	Name  string
	Value any
}

Shape is one way a caller might hold a value, named for test output.

func Shapes

func Shapes[T any](p *T) []Shape

Shapes returns the ways a caller might hold p: the pointer, the value, a pointer to the pointer, exported and unexported struct fields, and slices and maps of pointers and values.

Jump to

Keyboard shortcuts

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