celeristest

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package celeristest provides test utilities for celeris handlers.

Use NewContext to create a celeris.Context and ResponseRecorder pair, then pass the context to a handler and inspect the recorder.

ctx, rec := celeristest.NewContext("GET", "/hello")
defer celeristest.ReleaseContext(ctx)
err := handler(ctx)
if rec.StatusCode != 200 { ... }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReleaseContext

func ReleaseContext(ctx *celeris.Context)

ReleaseContext returns a celeris.Context to the pool. The context must not be used after this call.

Types

type Option

type Option func(*config)

Option configures a test context.

func WithBasicAuth added in v1.1.0

func WithBasicAuth(user, pass string) Option

WithBasicAuth sets the Authorization header for HTTP Basic auth.

func WithBody

func WithBody(body []byte) Option

WithBody sets the request body.

func WithContentType

func WithContentType(ct string) Option

WithContentType is a shorthand for WithHeader("content-type", ct).

func WithCookie added in v1.1.0

func WithCookie(name, value string) Option

WithCookie adds a cookie to the request.

func WithHandlers added in v1.2.1

func WithHandlers(handlers ...celeris.HandlerFunc) Option

WithHandlers sets the handler chain on the test context. This enables middleware chain testing where mw1 calls c.Next() → mw2 runs → ... → final handler. Pass celeris.HandlerFunc values; they are stored as []any to avoid import cycles.

func WithHeader

func WithHeader(key, value string) Option

WithHeader adds a request header.

func WithParam

func WithParam(key, value string) Option

WithParam adds a URL parameter (e.g. from :id in the route pattern).

func WithQuery

func WithQuery(key, value string) Option

WithQuery adds a query parameter.

func WithRemoteAddr added in v1.1.0

func WithRemoteAddr(addr string) Option

WithRemoteAddr sets the remote address on the test stream.

type ResponseRecorder

type ResponseRecorder struct {
	// StatusCode is the HTTP status code written by the handler.
	StatusCode int
	// Headers are the response headers as key-value pairs.
	Headers [][2]string
	// Body is the raw response body bytes.
	Body []byte
}

ResponseRecorder captures the response written by a handler.

func NewContext

func NewContext(method, path string, opts ...Option) (*celeris.Context, *ResponseRecorder)

NewContext creates a celeris.Context and ResponseRecorder for testing. The returned context has the given method and path, plus any options applied. Call ReleaseContext when done to clean up pooled resources.

func NewContextT

func NewContextT(t *testing.T, method, path string, opts ...Option) (*celeris.Context, *ResponseRecorder)

NewContextT is like NewContext but registers an automatic cleanup with t.Cleanup so callers do not need to defer ReleaseContext manually.

func (*ResponseRecorder) BodyString

func (r *ResponseRecorder) BodyString() string

BodyString returns the response body as a string.

func (*ResponseRecorder) Header

func (r *ResponseRecorder) Header(key string) string

Header returns the value of the first response header matching the given key. Returns empty string if the header is not present.

Jump to

Keyboard shortcuts

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