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 ¶
- func Body(tb testing.TB, resp *http.Response) string
- func BodyBytes(tb testing.TB, resp *http.Response) []byte
- func Ctx[C any](tb testing.TB, app CtxPool[C]) C
- func CtxAs[T, C any](tb testing.TB, app CtxPool[C]) T
- func CtxFor[C any](tb testing.TB, app CtxPool[C], fctx *fasthttp.RequestCtx) C
- func Dial(tb testing.TB, ln *fasthttputil.InmemoryListener) net.Conn
- func Do[C any, T Target](tb testing.TB, app Tester[C], target T, cfg ...C) *http.Response
- func Expect[C any, T Target](tb testing.TB, app Tester[C], target T, status int) *http.Response
- func ExpectBody[C any, T Target](tb testing.TB, app Tester[C], target T, status int, body string) *http.Response
- func FieldLines(prefix, value string, n int) string
- func Pairs(kv []string) iter.Seq2[string, string]
- func Recv[T any](tb testing.TB, ch <-chan T, timeout time.Duration, msgAndArgs ...any) T
- func Req(method, target string, opts ...Option) *http.Request
- func Serve[C any](tb testing.TB, app Server[C], config ...C) *fasthttputil.InmemoryListener
- type CtxPool
- type Option
- type Server
- type Target
- type Tester
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BodyBytes ¶
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 ¶
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 ¶
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 ¶
CtxFor is Ctx for a context built around fctx, for tests that prepare the request on the fasthttp context first.
func Dial ¶
func Dial(tb testing.TB, ln *fasthttputil.InmemoryListener) net.Conn
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Option modifies a request built by Req.
func WithHeader ¶
WithHeader sets a request header, replacing any value already present.
func WithHeaderIf ¶
WithHeaderIf is WithHeader that leaves the request alone when value is empty.
type Server ¶
Server is the part of an app that serves a listener and shuts down, as *fiber.App does.