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 ¶
- func ReleaseContext(ctx *celeris.Context)
- type Option
- func WithBasicAuth(user, pass string) Option
- func WithBody(body []byte) Option
- func WithContentType(ct string) Option
- func WithCookie(name, value string) Option
- func WithHandlers(handlers ...celeris.HandlerFunc) Option
- func WithHeader(key, value string) Option
- func WithParam(key, value string) Option
- func WithQuery(key, value string) Option
- func WithRemoteAddr(addr string) Option
- type ResponseRecorder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReleaseContext ¶
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
WithBasicAuth sets the Authorization header for HTTP Basic auth.
func WithContentType ¶
WithContentType is a shorthand for WithHeader("content-type", ct).
func WithCookie ¶ added in v1.1.0
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 WithRemoteAddr ¶ added in v1.1.0
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.