Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions celeristest/celeristest.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type config struct {
params [][2]string
cookies [][2]string
remoteAddr string
handlers []any
}

// WithBody sets the request body.
Expand Down Expand Up @@ -128,6 +129,18 @@ func WithRemoteAddr(addr string) Option {
return func(c *config) { c.remoteAddr = addr }
}

// 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 WithHandlers(handlers ...celeris.HandlerFunc) Option {
return func(c *config) {
c.handlers = make([]any, len(handlers))
for i, h := range handlers {
c.handlers[i] = h
}
}
}

// ReleaseContext returns a [celeris.Context] to the pool. The context must not
// be used after this call.
func ReleaseContext(ctx *celeris.Context) {
Expand Down Expand Up @@ -191,5 +204,8 @@ func NewContext(method, path string, opts ...Option) (*celeris.Context, *Respons
for _, p := range cfg.params {
ctxkit.AddParam(ctx, p[0], p[1])
}
if len(cfg.handlers) > 0 {
ctxkit.SetHandlers(ctx, cfg.handlers)
}
return ctx, rec
}
9 changes: 9 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ func init() {
ctx := c.(*Context)
ctx.params = append(ctx.params, Param{Key: key, Value: value})
}
ctxkit.SetHandlers = func(c any, handlers []any) {
ctx := c.(*Context)
chain := make([]HandlerFunc, len(handlers))
for i, h := range handlers {
chain[i] = h.(HandlerFunc)
}
ctx.handlers = chain
ctx.index = -1
}
}

// Context is the request context passed to handlers. It is pooled via sync.Pool.
Expand Down
1 change: 1 addition & 0 deletions internal/ctxkit/ctxkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ var (
NewContext func(s *stream.Stream) any
ReleaseContext func(c any)
AddParam func(c any, key, value string)
SetHandlers func(c any, handlers []any)
)