fix(router): reject invalid and ambiguous paths

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-02 20:39:09 -05:00
1 parent 790db661e5
commit 8e38fffd77
6 files changed
+150 -9

No files matched your search

+14 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { compilePattern, matchPattern, matchRoute, allowedMethods } from '../src/http/router';
import { compilePattern, matchPattern, matchRoute, allowedMethods, normalizeRequestPath } from '../src/http/router';
import type { RouteDef } from '../src/http/types';
const route = (method: string, pattern: string): RouteDef => ({
@@ -53,6 +53,19 @@ describe('compilePattern', () => {
});
});
describe('normalizeRequestPath', () => {
it('treats one trailing slash as equivalent and preserves root', () => {
expect(normalizeRequestPath('/users')).toBe('/users');
expect(normalizeRequestPath('/users/')).toBe('/users');
expect(normalizeRequestPath('/')).toBe('/');
});
it('rejects repeated slashes and malformed percent encoding', () => {
expect(() => normalizeRequestPath('/users//42')).toThrow(/repeated slash/);
expect(() => normalizeRequestPath('/users/%E0%A4%A')).toThrow(/malformed path encoding/);
});
});
describe('matchRoute precedence', () => {
it('static beats :param beats catch-all regardless of registration order', () => {
const routes = [route('GET', '/a/:rest*'), route('GET', '/a/:id'), route('GET', '/a/b')];