feat(http): complete HEAD and OPTIONS semantics

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-02 20:45:44 -05:00
1 parent 63be0ef8f7
commit 9a61ba8646
7 files changed
+207 -41

No files matched your search

+48 -1
View File
@@ -1,6 +1,6 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import type { Server } from 'http';
import { createApp, Provider, Route, Get, Post, Module, needs, body, Sse } from '../src/index';
import { createApp, Provider, Route, Get, Head, Options, Post, Module, needs, body, Sse } from '../src/index';
@Provider({ provides: 'db' })
class Db {
@@ -22,6 +22,21 @@ class Ctl {
yield { n: 2 };
})();
}
@Get('/implicit-head') implicitHead() {
return { from: 'get' };
}
@Get('/explicit-head') explicitHeadGet() {
return { from: 'get' };
}
@Head('/explicit-head') explicitHead() {
return { from: 'head' };
}
@Get('/explicit-options') explicitOptionsGet() {
return { from: 'get' };
}
@Options('/explicit-options') explicitOptions() {
return { from: 'options' };
}
}
@Module({ mountpoint: '/', providers: [Db], controllers: [Ctl] })
class M {}
@@ -78,6 +93,38 @@ describe('Node vs app.fetch parity (Node is truth)', () => {
expect(web.status).toBe(node.status);
expect(norm(web.headers)).toEqual(norm(node.headers));
});
it('implicit and explicit HEAD return no body in both adapters', async () => {
for (const path of ['/api/implicit-head', '/api/explicit-head']) {
const { node, web } = await both(path, { method: 'HEAD' });
expect(node.status).toBe(200);
expect(web.status).toBe(node.status);
expect(await node.text()).toBe('');
expect(await web.text()).toBe('');
}
});
it('automatic OPTIONS is 204 with the same canonical Allow header', async () => {
const { node, web } = await both('/api/x', { method: 'OPTIONS' });
expect(node.status).toBe(204);
expect(web.status).toBe(204);
expect(node.headers.get('allow')).toBe('GET, HEAD, OPTIONS');
expect(web.headers.get('allow')).toBe(node.headers.get('allow'));
});
it('explicit OPTIONS route runs normally', async () => {
const { node, web } = await both('/api/explicit-options', { method: 'OPTIONS' });
expect(node.status).toBe(200);
expect(web.status).toBe(200);
expect(await node.json()).toEqual({ from: 'options' });
expect(await web.json()).toEqual({ from: 'options' });
});
it('rejects repeated slashes and malformed encoding with secure 400 responses', async () => {
for (const path of ['/api//x', '/api/%E0%A4%A']) {
const { node, web } = await both(path);
expect(node.status).toBe(400);
expect(web.status).toBe(400);
expect(node.headers.get('x-content-type-options')).toBe('nosniff');
expect(web.headers.get('x-content-type-options')).toBe('nosniff');
}
});
it('SSE first two events', async () => {
const nodeText = await (await fetch(base + '/api/s')).text();
const webText = await (await app.fetch(new Request(base + '/api/s'))).text();
+26 -4
View File
@@ -1,5 +1,12 @@
import { describe, it, expect } from 'vitest';
import { compilePattern, matchPattern, matchRoute, allowedMethods, normalizeRequestPath } from '../src/http/router';
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 => ({
@@ -90,9 +97,24 @@ describe('matchRoute precedence', () => {
});
describe('allowedMethods (405 support)', () => {
it('lists distinct methods whose pattern matches the path', () => {
const routes = [route('GET', '/a'), route('POST', '/a'), route('GET', '/b')];
expect(allowedMethods(routes, '/a').sort()).toEqual(['GET', 'POST']);
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();
});
});
+5 -5
View File
@@ -187,7 +187,7 @@ describe('CORS', () => {
expect(r.headers.get('access-control-allow-credentials')).toBe('true');
});
it('disallowed origin → no allow-origin; bare OPTIONS (no ACRM) → 405 Allow for an existing path', async () => {
it('disallowed origin → no allow-origin; bare OPTIONS (no ACRM) → automatic 204 Allow', async () => {
@Route('/') class C { @Get('/x') x() { return { ok: 1 }; } }
@Module({ mountpoint: '/', controllers: [C] }) class CMod {}
app = createApp({ modules: [CMod], cors: { origins: 'https://a.com' } });
@@ -195,11 +195,11 @@ describe('CORS', () => {
const port = (server.address() as any).port;
const r = await fetch(`http://127.0.0.1:${port}/x`, { headers: { origin: 'https://evil.com' } });
expect(r.headers.get('access-control-allow-origin')).toBeNull();
// A bare OPTIONS (no Access-Control-Request-Method) isn't a CORS preflight; the path exists
// under GET, so the router answers 405 with an Allow header rather than 404.
// A bare OPTIONS (no Access-Control-Request-Method) isn't a CORS preflight; the router
// answers it automatically and advertises the explicit + implicit methods.
const bare = await fetch(`http://127.0.0.1:${port}/x`, { method: 'OPTIONS' });
expect(bare.status).toBe(405);
expect(bare.headers.get('allow')).toContain('GET');
expect(bare.status).toBe(204);
expect(bare.headers.get('allow')).toBe('GET, HEAD, OPTIONS');
});
it("merges a handler's Title-Case Vary with CORS's Origin", async () => {