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
- func AllocStats(runs int, f func()) (allocs, allocated float64)
- func AssertDiffRedacted[T any](tb testing.TB, a, b *T, secrets ...string) bool
- func AssertNeverPrints(tb testing.TB, v any, secrets ...string) bool
- func AssertRedacted(tb testing.TB, out string, secrets []string, msgAndArgs ...any) bool
- func AssertShapesNeverPrint[T any](tb testing.TB, p *T, secrets ...string) bool
- func BlockUntilDone(r *http.Request) (*http.Response, error)
- func Drain(r *http.Request) int64
- func FormatVerbs() []string
- func RequireErrorAs[E error](tb testing.TB, err error) E
- func RequireJSONRoundTrip[T any](tb testing.TB, v T, msgAndArgs ...any)
- func SkipUnderRace(tb testing.TB)
- type CountingTransport
- type Protocol
- type Request
- type RoundTripFunc
- type Server
- type Shape
Constants ¶
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 ¶
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 ¶
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 ¶
AssertNeverPrints formats v with every verb of FormatVerbs and fails tb when any output holds a secret, as AssertRedacted checks.
func AssertRedacted ¶
AssertRedacted fails tb when out contains any secret, in plain text or as hex in either case. msgAndArgs annotate a failure.
func AssertShapesNeverPrint ¶
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 ¶
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 ¶
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 ¶
RequireErrorAs returns the first error in err's chain of type E, failing tb unless err is non-nil and has one.
func RequireJSONRoundTrip ¶
RequireJSONRoundTrip marshals v, unmarshals the result into a new T and fails tb unless it equals v. msgAndArgs annotate a failure.
func SkipUnderRace ¶
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.
type Protocol ¶
type Protocol string
Protocol selects the HTTP version a Server and its Client speak.
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.
type RoundTripFunc ¶
RoundTripFunc adapts a function to http.RoundTripper.
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 ¶
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 ¶
Client returns an HTTP client that trusts the server's certificate and speaks the server's protocol.
func (*Server) URL ¶
URL returns the server's base URL, such as https://127.0.0.1:port.