basttest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package basttest provides test helpers for Bast handlers and apps. NewCtx builds a *bast.Ctx outside the pool — safe for unit tests, never pooled. NewApp wraps httptest for full integration tests through the module system.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCtx

func NewCtx(opts ...CtxOption) *bast.Ctx

NewCtx builds a *bast.Ctx outside the pool for unit testing. All fields are zero-valued unless overridden with options. Never call the pool release path on a test ctx — it is not pooled.

func NewStreamCtx added in v0.2.0

func NewStreamCtx(opts ...StreamCtxOption) *bast.StreamCtx

NewStreamCtx builds a *bast.StreamCtx outside the pool for unit testing stream handlers. All fields are zero-valued unless overridden with options. Never pool or reuse a test StreamCtx.

Types

type Assertions

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

Assertions provides fluent assertions over a TestResponse.

func (*Assertions) BodyContains

func (a *Assertions) BodyContains(substr string) *Assertions

BodyContains asserts the response body contains substr.

func (*Assertions) HeaderIs

func (a *Assertions) HeaderIs(key, value string) *Assertions

HeaderIs asserts the response header key equals value.

func (*Assertions) StatusIs

func (a *Assertions) StatusIs(code int) *Assertions

StatusIs asserts the response status code equals code.

type CtxOption

type CtxOption func(*bast.Ctx)

CtxOption configures a *bast.Ctx built by NewCtx.

func WithBody

func WithBody(v any) CtxOption

WithBody marshals v to JSON, sets Content-Type, and attaches it as the request body.

func WithHeader

func WithHeader(key, value string) CtxOption

WithHeader sets a request header on the Ctx.

func WithIP

func WithIP(ip string) CtxOption

WithIP sets a spoofed RemoteAddr for IP testing.

func WithMethod

func WithMethod(method string) CtxOption

WithMethod sets the HTTP method on the underlying request.

func WithParam

func WithParam(key, value string) CtxOption

WithParam sets a route path parameter on the Ctx.

func WithPath

func WithPath(path string) CtxOption

WithPath sets the URL path on the underlying request.

func WithQuery

func WithQuery(key, value string) CtxOption

WithQuery sets a URL query parameter on the underlying request.

func WithRawBody

func WithRawBody(b []byte) CtxOption

WithRawBody sets raw bytes as the request body.

func WithStore

func WithStore(key string, val any) CtxOption

WithStore pre-populates the Ctx store — simulates a guard having already run.

type RequestOption

type RequestOption func(*http.Request)

RequestOption configures a request sent via Do.

func WithJSONBody

func WithJSONBody(v any) RequestOption

WithJSONBody marshals v to JSON and attaches it as the request body.

func WithRequestHeader

func WithRequestHeader(key, value string) RequestOption

WithRequestHeader sets a header on the outgoing request.

type StreamCtxOption added in v0.2.0

type StreamCtxOption func(*bast.StreamCtx)

StreamCtxOption configures a *bast.StreamCtx built by NewStreamCtx.

func WithStreamHeader added in v0.2.0

func WithStreamHeader(key, value string) StreamCtxOption

WithStreamHeader sets a request header on the StreamCtx.

func WithStreamMethod added in v0.2.0

func WithStreamMethod(method string) StreamCtxOption

WithStreamMethod sets the HTTP method on the underlying request.

func WithStreamParam added in v0.2.0

func WithStreamParam(key, value string) StreamCtxOption

WithStreamParam sets a route path parameter on the StreamCtx.

func WithStreamPath added in v0.2.0

func WithStreamPath(path string) StreamCtxOption

WithStreamPath sets the URL path on the underlying request.

func WithStreamStore added in v0.2.0

func WithStreamStore(key string, val any) StreamCtxOption

WithStreamStore pre-populates the StreamCtx store — simulates a guard having already run.

type TestApp

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

TestApp wraps a Bast app backed by httptest for integration testing. Tests the full request lifecycle: routing, guards, middleware, error boundary.

func NewApp

func NewApp(modules ...bast.Module) *TestApp

NewApp builds a TestApp from one or more modules.

func (*TestApp) Close

func (a *TestApp) Close()

Close shuts down the test server. Call in TestMain or t.Cleanup.

func (*TestApp) Do

func (a *TestApp) Do(method, path string, opts ...RequestOption) *TestResponse

Do sends a request through the full app stack and returns a TestResponse.

type TestResponse

type TestResponse struct {
	Code    int
	Body    []byte
	Headers http.Header
}

TestResponse holds the result of a TestApp.Do call.

func (*TestResponse) Assert

func (r *TestResponse) Assert(t *testing.T) *Assertions

Assert returns a fluent assertion helper bound to t.

func (*TestResponse) JSON

func (r *TestResponse) JSON(v any) error

JSON unmarshals the response body into v.

Jump to

Keyboard shortcuts

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