Skip to content

Commit

Permalink
feat: retrun diffs for matched endpoints routes in 501 response
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick-bonnefond authored and ybonnefond committed Oct 23, 2024
1 parent 28c970c commit 79594e9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 69 deletions.
24 changes: 23 additions & 1 deletion src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { requestDiff } from './diff/requestDiff';
import { middlewares } from './middlewares';
import { Route } from './Route';
import { findCaller, getServerPort, logDiffOn501 } from './utils';
import { methodDiff } from './diff/methodDiff';
import { pathDiff } from './diff/pathDiff';
/**
* @internal
*/
Expand Down Expand Up @@ -142,7 +144,7 @@ function buildRequestHandler(router: Router, emitter: EventEmitter) {
const route = findRoute(router.getRoutes(), req);

if (null === route) {
replyNotImplemented(res, req, emitter);
replyNotImplemented(router.getRoutes(), res, req, emitter);
} else {
reply({ router, route, res, req, emitter });
}
Expand Down Expand Up @@ -227,10 +229,29 @@ function findRoute(routes: Set<Route>, req: Request) {
return null;
}

function findEndpointRouteDiffs(routes: Set<Route>, req: Request) {
const diffs = [];
for (const [route] of routes.entries()) {
const isEndpoint =
[...methodDiff(route, req), ...pathDiff(route, req)].length === 0;
if (isEndpoint) {
diffs.push({
route: {
path: route.getInitializerPath(),
},
diffs: requestDiff(route, req),
});
}
}

return diffs;
}

/**
* @internal
*/
function replyNotImplemented(
routes: Set<Route>,
res: Response,
req: Request,
emitter: EventEmitter,
Expand All @@ -245,6 +266,7 @@ function replyNotImplemented(
{
message: 'No route matched',
request: dbg.getInfo(),
endpointsDiffs: findEndpointRouteDiffs(routes, req),
},
null,
2,
Expand Down
66 changes: 0 additions & 66 deletions test/specs/__snapshots__/debug.spec.ts.snap

This file was deleted.

48 changes: 46 additions & 2 deletions test/specs/debug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,21 @@ describe('debug', () => {
logDiffOn501(sb, route);

const out = await run(req);
expect(out).toMatchSnapshot();
expoectOutputToBe(out, [
'## Request: GET /test-stuff',
'Method',
'- Received: get',
'+ Expected: post',
'Path',
'- Received: /test-stuff',
'+ Expected: /test$/',
'Headers',
'+ x-api-key: 123',
'Body',
'- Received: undefined',
"+ Expected: { name: (val) => val === 'tonton' }",
expect.stringMatching(/^Route registered at .+:\d+:\d+/),
]);
});

it('should output various stuff', async () => {
Expand Down Expand Up @@ -91,7 +105,37 @@ describe('debug', () => {

const out = await run(req);

expect(out).toMatchSnapshot();
expoectOutputToBe(out, [
'## Request: POST /test',
'Method',
'- Received: post',
'+ Expected: put',
'Path',
'- Received: /test',
'+ Expected: /',
'Headers',
'+ x-header-missing: missing-header-123',
'- x-header-extra: x-extra-header-123',
'- x-header-1: Bearer world',
'+ x-header-1: Bearer hello',
'Query',
'+ missingParam: /^[0-9]+$/',
'- extraParam: 10',
'- param1: ten',
'+ param1: /^[0-9]+$/',
'Body',
'+ missingKey: key missing',
'- extraKey: extra key value',
'- pets.0.type: dog',
'+ pets.0.type: cat',
'- firstname: 123',
'+ firstname: /^[a-z]+$/',
'- lastname: Donald',
"+ lastname: val => val === 'Doe'",
'- roles: writer',
"+ roles: [ 'writer', 'reviewer' ]",
expect.stringMatching(/^Route registered at .+:\d+:\d+/),
]);
});

it('should output method diff using logDiff on route', async () => {
Expand Down

0 comments on commit 79594e9

Please sign in to comment.