fix: HEAD method not handled properly (#3563)

- It should never contain a response body
- It should be equivalent to `GET`
This commit is contained in:
Marvin Hagemeister
2025-10-21 15:43:17 +02:00
committed by GitHub
parent de1810e70d
commit 41b9422dd1
4 changed files with 91 additions and 8 deletions
+4
View File
@@ -439,6 +439,10 @@ export class App<State> {
);
}
if (method === "HEAD") {
return new Response(null, result);
}
return result;
} catch (err) {
ctx.error = err;
+47 -1
View File
@@ -724,22 +724,68 @@ Deno.test("App - .get/post/patch/put/delete/head/all() - lazy", async () => {
const server = new FakeServer(app.handler());
let res = await server.get("/");
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("ok");
res = await server.post("/");
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("ok");
res = await server.put("/");
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("ok");
res = await server.patch("/");
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("ok");
res = await server.delete("/");
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("ok");
res = await server.head("/");
expect(await res.text()).toEqual("ok");
expect(res.status).toEqual(200);
expect(res.body).toEqual(null);
});
Deno.test("App prefer .head over .get", async () => {
const app = new App()
.get(
"/",
() => Promise.resolve(() => new Response("not ok", { status: 500 })),
)
.head("/", () => Promise.resolve(() => new Response("ok")))
.get(
"/:id",
() => Promise.resolve(() => new Response("not ok", { status: 500 })),
)
.head("/:id", () => Promise.resolve(() => new Response("ok")));
const server = new FakeServer(app.handler());
let res = await server.head("/");
expect(res.body).toEqual(null);
expect(res.status).toEqual(200);
res = await server.head("/foo");
expect(res.body).toEqual(null);
expect(res.status).toEqual(200);
});
Deno.test("App support HEAD if only GET is registered", async () => {
const app = new App()
.get("/", () => Promise.resolve(() => new Response("ok")))
.get("/:id", () => Promise.resolve(() => new Response("ok")));
const server = new FakeServer(app.handler());
let res = await server.head("/");
expect(res.body).toEqual(null);
expect(res.status).toEqual(200);
res = await server.head("/foo");
expect(res.body).toEqual(null);
expect(res.status).toEqual(200);
});
Deno.test("App - .appWrapper()", async () => {
+31 -5
View File
@@ -63,15 +63,25 @@ Deno.test("fsRoutes - registers HTTP methods on router", async () => {
const name = method.toLowerCase() as Lowercase<Method>;
const res = await server[name]("/all");
expect(res.status).toEqual(200);
expect(await res.text()).toEqual(method);
if (method === "HEAD") {
expect(res.body).toEqual(null);
} else {
expect(await res.text()).toEqual(method);
}
}
// Check individual routes
for (const method of methods) {
const lower = method.toLowerCase() as Lowercase<Method>;
const res = await server[lower](`/${lower}`);
expect(res.status).toEqual(200);
expect(await res.text()).toEqual(method);
if (method === "HEAD") {
expect(res.body).toEqual(null);
} else {
expect(await res.text()).toEqual(method);
}
// Check that all other methods are forbidden
for (const other of methods) {
@@ -79,8 +89,14 @@ Deno.test("fsRoutes - registers HTTP methods on router", async () => {
const name = other.toLowerCase() as Lowercase<Method>;
const res = await server[name](`/${lower}`);
await res.body?.cancel();
expect(res.status).toEqual(405);
if (method === "GET" && other === "HEAD") {
// When only a HEAD route is registered it should return 404
expect(res.status).toEqual(404);
} else {
expect(res.status).toEqual(405);
}
}
}
});
@@ -96,7 +112,12 @@ Deno.test("fsRoutes - registers fn handler for every method", async () => {
const name = method.toLowerCase() as Lowercase<Method>;
const res = await server[name]("/all");
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("ok");
if (method === "HEAD") {
expect(res.body).toEqual(null);
} else {
expect(await res.text()).toEqual("ok");
}
}
// Check individual routes
@@ -104,7 +125,12 @@ Deno.test("fsRoutes - registers fn handler for every method", async () => {
const lower = method.toLowerCase() as Lowercase<Method>;
const res = await server[lower]("/all");
expect(res.status).toEqual(200);
expect(await res.text()).toEqual("ok");
if (method === "HEAD") {
expect(res.body).toEqual(null);
} else {
expect(await res.text()).toEqual("ok");
}
}
});
+9 -2
View File
@@ -120,7 +120,10 @@ export class UrlPatternRouter<T> implements Router<T> {
if (staticMatch !== undefined) {
result.pattern = url.pathname;
const handlers = staticMatch.byMethod[method];
let handlers = staticMatch.byMethod[method];
if (method === "HEAD" && handlers.length === 0) {
handlers = staticMatch.byMethod.GET;
}
if (handlers.length > 0) {
result.methodMatch = true;
result.handlers.push(...handlers);
@@ -137,7 +140,11 @@ export class UrlPatternRouter<T> implements Router<T> {
result.pattern = route.pattern.pathname;
const handlers = route.byMethod[method];
let handlers = route.byMethod[method];
if (method === "HEAD" && handlers.length === 0) {
handlers = route.byMethod.GET;
}
if (handlers.length > 0) {
result.methodMatch = true;
result.handlers.push(...handlers);