Files
core/test/router.test.ts
T
chrnx 0c9f8a545e fix(router): route dot segments the same way on every runtime
The same bytes on the wire reached different routes depending on where
the app ran. `GET /public/../admin` hit a `/admin` route under Deno, Bun
and workerd, and 404'd under Node — 200 vs 404 for one request.

The fetch-based runtimes resolve dot segments inside the `Request`
constructor, before green-tea is handed anything: `new Request(
'http://x/public/../admin').url` is already `http://x/admin`. Node's raw
target has no such step. Rejecting outright — the stricter policy, and
what this module already does for `//` — is not implementable on three of
the four runtimes, because the raw target is unrecoverable there. So Node
converges on the rule the other three cannot opt out of.

`%2e` is a dot to the URL parser, so the encoded spelling resolves too;
leaving it out would keep it as a way to reach a route the plain spelling
resolves away from.

Cost is gated: paths with no `.` and no `%` pay two indexOf scans
(20.0 -> 21.7 ns for `/hello`), a literal dot pays the regex (35.5 ns),
and only real dot segments pay the split (103.6 ns).

The existing parity suite could not have caught this — its `both()`
helper goes through `fetch()`, which normalizes the URL before it reaches
the wire. The new cases write the request line to a socket by hand.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-08-16 23:35:26 -05:00

153 lines
6.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
compilePattern,
matchPattern,
matchRoute,
resolveRoute,
allowedMethods,
normalizeRequestPath,
} from '../src/http/router';
import type { RouteDef } from '../src/http/types';
const route = (method: string, pattern: string): RouteDef => ({
method,
pattern,
transport: 'buffer',
handler: async () => ({ status: 200, headers: {}, body: '' }),
});
describe('matchPattern', () => {
it('matches the root pattern only at root', () => {
expect(matchPattern('/', '/')).toEqual({});
expect(matchPattern('/', '/users')).toBeUndefined();
});
it('matches static and :param segments, decoding params', () => {
expect(matchPattern('/users/:id', '/users/a%20b')).toEqual({ id: 'a b' });
expect(matchPattern('/users/:id', '/users')).toBeUndefined();
expect(matchPattern('/users', '/users/1')).toBeUndefined();
});
it(':name* captures the rest of the path, slashes included', () => {
expect(matchPattern('/files/:path*', '/files/img/2026/logo.png')).toEqual({ path: 'img/2026/logo.png' });
expect(matchPattern('/files/:path*', '/files/a%2Fb')).toEqual({ path: 'a/b' });
});
it(':name* matches zero remaining segments as an empty string', () => {
expect(matchPattern('/files/:path*', '/files')).toEqual({ path: '' });
});
it('matches safe constraints against the complete decoded segment', () => {
expect(matchPattern('/users/:id(\\d+)', '/users/42')).toEqual({ id: '42' });
expect(matchPattern('/users/:id(\\d+)', '/users/42x')).toBeUndefined();
expect(matchPattern('/posts/:slug([a-z0-9_-]+)', '/posts/green-tea_26')).toEqual({ slug: 'green-tea_26' });
expect(matchPattern('/build/:version(\\d{2}\\.\\d{1,2})', '/build/26.8')).toEqual({ version: '26.8' });
});
});
describe('compilePattern', () => {
it('records a stable shape independent of parameter names', () => {
expect(compilePattern('/users/:id').shape).toBe(compilePattern('/users/:name').shape);
expect(compilePattern('/users/:id(\\d+)').shape).not.toBe(compilePattern('/users/:id').shape);
});
it.each([
'/files/:rest*/tail',
'/users/:',
'/users/:id/:id',
'/users//:id',
'/users/:id((a+)+)',
'/users/:id((?=a)a)',
'/users/:id(\\1)',
'/users/:id(a|b)',
])('rejects invalid or unsafe pattern %s', (pattern) => {
expect(() => compilePattern(pattern)).toThrow(/invalid route pattern|unsafe route constraint/);
});
});
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/);
});
// The reference for every case below is `new URL('http://x' + path).pathname`, because that is
// what Deno, Bun and workerd apply before green-tea is handed the request. Node has no such step,
// so this is where the two adapters are made to agree.
it('resolves dot segments the way the URL parser does', () => {
expect(normalizeRequestPath('/public/../admin')).toBe('/admin');
expect(normalizeRequestPath('/./admin')).toBe('/admin');
expect(normalizeRequestPath('/a/b/../../c')).toBe('/c');
expect(normalizeRequestPath('/a/.')).toBe('/a');
expect(normalizeRequestPath('/..')).toBe('/');
expect(normalizeRequestPath('/a/../..')).toBe('/');
});
it('resolves percent-encoded dot segments, which the URL parser also treats as dots', () => {
expect(normalizeRequestPath('/public/%2e%2e/admin')).toBe('/admin');
expect(normalizeRequestPath('/public/%2E%2E/admin')).toBe('/admin');
expect(normalizeRequestPath('/%2e/admin')).toBe('/admin');
expect(normalizeRequestPath('/public/.%2e/admin')).toBe('/admin');
});
it('leaves dots that are not whole segments alone', () => {
expect(normalizeRequestPath('/assets/logo.png')).toBe('/assets/logo.png');
expect(normalizeRequestPath('/files/.hidden')).toBe('/files/.hidden');
expect(normalizeRequestPath('/v1.2/status')).toBe('/v1.2/status');
// %2f is not a separator to the URL parser, so `..%2fb` is one ordinary segment.
expect(normalizeRequestPath('/a/..%2fb')).toBe('/a/..%2fb');
});
});
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')];
expect(matchRoute(routes, 'GET', '/a/b')?.def.pattern).toBe('/a/b');
expect(matchRoute(routes, 'GET', '/a/x')?.def.pattern).toBe('/a/:id');
expect(matchRoute(routes, 'GET', '/a/x/y')?.def.pattern).toBe('/a/:rest*');
});
it('constrained params beat unconstrained params regardless of registration order', () => {
const routes = [route('GET', '/users/:value'), route('GET', '/users/:id(\\d+)')];
expect(matchRoute(routes, 'GET', '/users/42')?.def.pattern).toBe('/users/:id(\\d+)');
expect(matchRoute(routes, 'GET', '/users/diego')?.def.pattern).toBe('/users/:value');
});
it('returns undefined when no pattern matches the path', () => {
expect(matchRoute([route('GET', '/a')], 'GET', '/b')).toBeUndefined();
});
it('does not match a route registered under a different method', () => {
expect(matchRoute([route('POST', '/a')], 'GET', '/a')).toBeUndefined();
});
});
describe('allowedMethods (405 support)', () => {
it('lists methods in canonical order with implicit HEAD and OPTIONS', () => {
const routes = [route('POST', '/a'), route('GET', '/a'), route('GET', '/b')];
expect(allowedMethods(routes, '/a')).toEqual(['GET', 'HEAD', 'POST', 'OPTIONS']);
expect(allowedMethods(routes, '/nope')).toEqual([]);
});
});
describe('resolveRoute', () => {
it('prefers explicit HEAD and otherwise falls back to buffered GET', () => {
const explicit = [route('GET', '/a'), route('HEAD', '/a')];
expect(resolveRoute(explicit, 'HEAD', '/a')).toMatchObject({ implicitHead: false, def: { method: 'HEAD' } });
const fallback = resolveRoute([route('GET', '/a')], 'HEAD', '/a');
expect(fallback).toMatchObject({ implicitHead: true, def: { method: 'GET' } });
});
it('does not use a streaming GET as implicit HEAD', () => {
const streaming = { ...route('GET', '/a'), transport: 'sse' as const };
expect(resolveRoute([streaming], 'HEAD', '/a')).toBeUndefined();
});
});