internal/apptest
import "github.com/gofiber/fiber/v3/internal/apptest"
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.
Functions
func Body(tb testing.TB, resp *http.Response) string
Source: internal/apptest/apptest.go:139
Body is BodyBytes as a string.
func BodyBytes(tb testing.TB, resp *http.Response) []byte
Source: internal/apptest/apptest.go:127
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[C any](tb testing.TB, app CtxPool[C]) C
Source: internal/apptest/ctx.go:23
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[T, C any](tb testing.TB, app CtxPool[C]) T
Source: internal/apptest/ctx.go:44
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[C any](tb testing.TB, app CtxPool[C], fctx *fasthttp.RequestCtx) C
Source: internal/apptest/ctx.go:31
CtxFor is Ctx for a context built around fctx, for tests that prepare the request on the fasthttp context first.
func Dial(tb testing.TB, ln *fasthttputil.InmemoryListener) net.Conn
Source: internal/apptest/serve.go:68
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[C any, T Target](tb testing.TB, app Tester[C], target T, cfg ...C) *http.Response
Source: internal/apptest/apptest.go:89
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[C any, T Target](tb testing.TB, app Tester[C], target T, status int) *http.Response
Source: internal/apptest/apptest.go:101
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[C any, T Target](tb testing.TB, app Tester[C], target T, status int, body string) *http.Response
Source: internal/apptest/apptest.go:114
ExpectBody is Expect that also fails the test unless the response body is exactly body. The body stays readable from the returned response.
func FewestAllocs(samples, runs int, fn func()) float64
Source: internal/apptest/allocs.go:10
FewestAllocs returns the lowest allocation count per run across samples. Sampling limits noise from other goroutines and sync.Pool under the race detector. Both counts must be positive. Call it only from sequential tests.
func FieldLines(prefix, value string, n int) string
Source: internal/apptest/serve.go:80
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(kv []string) iter.Seq2[string, string]
Source: internal/apptest/pairs.go:11
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[T any](tb testing.TB, ch <-chan T, timeout time.Duration, msgAndArgs ...any) T
Source: internal/apptest/wait.go:14
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(method, target string, opts ...Option) *http.Request
Source: internal/apptest/apptest.go:66
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[C any](tb testing.TB, app Server[C], config ...C) *fasthttputil.InmemoryListener
Source: internal/apptest/serve.go:25
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.
func WithHeader(key, value string) Option
Source: internal/apptest/apptest.go:45
WithHeader sets a request header, replacing any value already present.
func WithHeaderIf(key, value string) Option
Source: internal/apptest/apptest.go:50
WithHeaderIf is WithHeader that leaves the request alone when value is empty.
func WithHost(host string) Option
Source: internal/apptest/apptest.go:59
WithHost sets the request's Host, which is how Fiber matches domain routes.
Types
type CtxPool
Source: internal/apptest/ctx.go:12
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 CtxPool[C any] interface {
AcquireCtx(fctx *fasthttp.RequestCtx) C
ReleaseCtx(c C)
}Methods
AcquireCtx func(fctx *fasthttp.RequestCtx) CReleaseCtx func(c C)
type Option
Source: internal/apptest/apptest.go:42
Option modifies a request built by Req.
type Option func(*http.Request)type Server
Source: internal/apptest/serve.go:16
Server is the part of an app that serves a listener and shuts down, as *fiber.App does.
type Server[C any] interface {
Listener(ln net.Listener, config ...C) error
Shutdown() error
}Methods
Listener func(ln net.Listener, config ...C) errorShutdown func() error
type Target
Source: internal/apptest/apptest.go:37
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 Target interface {
string | *http.Request
}Methods
string | *http.Request
type Tester
Source: internal/apptest/apptest.go:31
Tester is implemented by *fiber.App. C is the app's config type (fiber.TestConfig) and is inferred from the app, never written out.
type Tester[C any] interface {
Test(req *http.Request, config ...C) (*http.Response, error)
}Methods
Test func(req *http.Request, config ...C) (*http.Response, error)