runMiddleware function

Future<void> runMiddleware(
  1. Middleware middleware,
  2. Context ctx,
  3. Future<void> next()
)

Runs a single middleware and handles Response returns.

Use this when testing a middleware directly without a full chain. This properly writes Response objects to the context like Chase does.

Example:

final ctx = TestContext.get('/');
await runMiddleware(
  RateLimit(options),
  ctx,
  () async {}, // next handler
);
expect(ctx.response.statusCode, 429);

Implementation

Future<void> runMiddleware(
  Middleware middleware,
  Context ctx,
  Future<void> Function() next,
) async {
  final result = await middleware.handle(ctx, next);
  if (result is Response) {
    await result.writeTo(ctx.res.$raw);
  }
}