test(routebuilder): add validation tests for handler and middleware
- Add tests to validate middleware and handler signatures - Ensure TypeError is thrown for invalid signatures
This commit is contained in:
@@ -10,11 +10,24 @@ import type { Handler, Middleware } from '../Types/mod.ts';
|
|||||||
|
|
||||||
// Dummy objects
|
// Dummy objects
|
||||||
// deno-lint-ignore require-await
|
// deno-lint-ignore require-await
|
||||||
const dummyHandler: Handler = async () => new Response('ok');
|
const dummyHandler: Handler = async (_) => new Response('ok');
|
||||||
|
// deno-lint-ignore require-await
|
||||||
|
const wrongHandler: Handler = async () => new Response('ok'); // Wrong signature, no ctx
|
||||||
const dummyMiddleware: Middleware = async (_, next) => await next();
|
const dummyMiddleware: Middleware = async (_, next) => await next();
|
||||||
|
// deno-lint-ignore require-await
|
||||||
|
const wrongMiddleware: Middleware = async () => new Response('ok'); // Wrong signature, no ctx, next
|
||||||
const dummyDef: IRouteDefinition = { method: 'GET', path: '/hello' };
|
const dummyDef: IRouteDefinition = { method: 'GET', path: '/hello' };
|
||||||
const dummyMatcher = () => ({ params: {} });
|
const dummyMatcher = () => ({ params: {} });
|
||||||
|
|
||||||
|
Deno.test('middleware: throws if middleware signature is wrong', () => {
|
||||||
|
const builder = new RouteBuilder(() => {}, dummyDef);
|
||||||
|
assertThrows(
|
||||||
|
() => builder.middleware(wrongMiddleware).handle(dummyHandler),
|
||||||
|
TypeError,
|
||||||
|
'Middleware at index 0 is not a valid function.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test('middleware: single middleware is registered correctly', () => {
|
Deno.test('middleware: single middleware is registered correctly', () => {
|
||||||
let registered: IInternalRoute | null = null as IInternalRoute | null;
|
let registered: IInternalRoute | null = null as IInternalRoute | null;
|
||||||
|
|
||||||
@@ -51,6 +64,15 @@ Deno.test('middleware: preserves order of middleware', () => {
|
|||||||
assertEquals(result!.middlewares, [mw1, mw2]);
|
assertEquals(result!.middlewares, [mw1, mw2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test('handle: throws if handler signature is wrong', () => {
|
||||||
|
const builder = new RouteBuilder(() => {}, dummyDef);
|
||||||
|
assertThrows(
|
||||||
|
() => builder.handle(wrongHandler),
|
||||||
|
TypeError,
|
||||||
|
'Route handler must be a function returning a Promise<Response>.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test('handle: uppercases method', () => {
|
Deno.test('handle: uppercases method', () => {
|
||||||
let result: IInternalRoute | null = null as IInternalRoute | null;
|
let result: IInternalRoute | null = null as IInternalRoute | null;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user