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 ¶
- func NewCtx(opts ...CtxOption) *bast.Ctx
- func NewStreamCtx(opts ...StreamCtxOption) *bast.StreamCtx
- type Assertions
- type CtxOption
- func WithBody(v any) CtxOption
- func WithHeader(key, value string) CtxOption
- func WithIP(ip string) CtxOption
- func WithMethod(method string) CtxOption
- func WithParam(key, value string) CtxOption
- func WithPath(path string) CtxOption
- func WithQuery(key, value string) CtxOption
- func WithRawBody(b []byte) CtxOption
- func WithStore(key string, val any) CtxOption
- type RequestOption
- type StreamCtxOption
- type TestApp
- type TestResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewCtx ¶
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 ¶
CtxOption configures a *bast.Ctx built by NewCtx.
func WithBody ¶
WithBody marshals v to JSON, sets Content-Type, and attaches it as the request body.
func WithHeader ¶
WithHeader sets a request header on the Ctx.
func WithMethod ¶
WithMethod sets the HTTP method on the underlying request.
func WithRawBody ¶
WithRawBody sets raw bytes as the request body.
type RequestOption ¶
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
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 (*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 ¶
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.