apptest

package
v3.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package apptest provides the request/response plumbing that nearly every Fiber test repeats: sending a request through an app, checking the transport error, the status and the body, borrowing a request context from an app, and waiting on a channel with a deadline.

The helpers are generic over the app type instead of naming *fiber.App, so this package does not import fiber. That lets the in-package tests of the fiber package itself use them without an import cycle, alongside the tests of every middleware, extractor and client package. Type arguments are always inferred from the app's methods, so call sites never spell them out:

apptest.ExpectBody(t, app, "/hello", fiber.StatusOK, "world")

Every helper marks itself with testing.TB.Helper, so a failure is reported at the line of the call in the test. The helpers stop the test with FailNow and must therefore be called from the goroutine running the test.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Body

func Body(tb testing.TB, resp *http.Response) string

Body is BodyBytes as a string.

func BodyBytes

func BodyBytes(tb testing.TB, resp *http.Response) []byte

BodyBytes reads and closes the response body, failing the test on a read error; the body is closed even then. The body is replaced by an in-memory copy, so calling BodyBytes or Body again, or reading resp.Body afterwards, yields the same bytes.

func Ctx

func Ctx[C any](tb testing.TB, app CtxPool[C]) C

Ctx acquires a context for a blank request from app and returns it to the pool when the test ends, after any parallel subtests have finished. That ordering is what a deferred ReleaseCtx cannot give a test whose subtests call t.Parallel, because the deferred call runs before they start.

Call Ctx once per context: every call registers its own cleanup.

func CtxAs

func CtxAs[T, C any](tb testing.TB, app CtxPool[C]) T

CtxAs is Ctx for tests that need the concrete context type, such as *fiber.DefaultCtx, instead of the interface. The test fails if the app hands out a context of any other type. T is the only type argument to spell out:

c := apptest.CtxAs[*fiber.DefaultCtx](t, app)

func CtxFor

func CtxFor[C any](tb testing.TB, app CtxPool[C], fctx *fasthttp.RequestCtx) C

CtxFor is Ctx for a context built around fctx, for tests that prepare the request on the fasthttp context first.

func Dial

Dial opens a connection to ln with a five-second deadline, closed when tb ends, for a test that writes raw requests and reads what comes back.

func Do

func Do[C any, T Target](tb testing.TB, app Tester[C], target T, cfg ...C) *http.Response

Do sends target through app and returns the response, failing the test if the app returns an error. It asserts nothing about the response itself. Pass the app's test config (fiber.TestConfig) as cfg to change its timeout.

func Expect

func Expect[C any, T Target](tb testing.TB, app Tester[C], target T, status int) *http.Response

Expect sends target through app and fails the test unless the response has the given status. A mismatch reports the response body, which usually says why. The response is returned for further assertions on its headers.

func ExpectBody

func ExpectBody[C any, T Target](tb testing.TB, app Tester[C], target T, status int, body string) *http.Response

ExpectBody is Expect that also fails the test unless the response body is exactly body. The body stays readable from the returned response.

func FieldLines

func FieldLines(prefix, value string, n int) string

FieldLines returns n header lines named prefix followed by the line's index, each holding value, for a raw request or response with more lines than a limit allows.

func Pairs

func Pairs(kv []string) iter.Seq2[string, string]

Pairs yields the name/value pairs of kv, the way the test helpers that take headers, cookies or other pairs as alternating variadic strings read them. It panics when kv has an odd length, because the last name would silently be dropped.

func Recv

func Recv[T any](tb testing.TB, ch <-chan T, timeout time.Duration, msgAndArgs ...any) T

Recv returns the next value from ch and fails the test when none arrives within timeout. msgAndArgs say what the test was waiting for, as the optional message of a testify assertion does. Like the other helpers it stops the test with FailNow, so it must be called from the goroutine running the test.

func Req

func Req(method, target string, opts ...Option) *http.Request

Req returns a body-less request for method and target with opts applied. Requests with a body or anything the options do not cover are built with httptest.NewRequest and passed to the helpers directly.

func Serve

func Serve[C any](tb testing.TB, app Server[C], config ...C) *fasthttputil.InmemoryListener

Serve serves app on an in-memory listener until tb ends, when the app is shut down and the error Listener returned is checked, and returns the listener, ready to dial: a server that stops before it serves, as one whose BeforeServeFunc fails does, fails the test with its error instead of leaving a dial waiting for an accept that never comes.

Types

type CtxPool

type CtxPool[C any] interface {
	AcquireCtx(fctx *fasthttp.RequestCtx) C
	ReleaseCtx(c C)
}

CtxPool is implemented by *fiber.App. C is the app's context type (fiber.CustomCtx) and is inferred from the app, never written out.

type Option

type Option func(*http.Request)

Option modifies a request built by Req.

func WithHeader

func WithHeader(key, value string) Option

WithHeader sets a request header, replacing any value already present.

func WithHeaderIf

func WithHeaderIf(key, value string) Option

WithHeaderIf is WithHeader that leaves the request alone when value is empty.

func WithHost

func WithHost(host string) Option

WithHost sets the request's Host, which is how Fiber matches domain routes.

type Server

type Server[C any] interface {
	Listener(ln net.Listener, config ...C) error
	Shutdown() error
}

Server is the part of an app that serves a listener and shuts down, as *fiber.App does.

type Target

type Target interface {
	string | *http.Request
}

Target is what a helper sends: a string is the target of a body-less GET request, a *http.Request is sent as it is.

type Tester

type Tester[C any] interface {
	Test(req *http.Request, config ...C) (*http.Response, error)
}

Tester is implemented by *fiber.App. C is the app's config type (fiber.TestConfig) and is inferred from the app, never written out.

Jump to

Keyboard shortcuts

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