fix: HEAD when only GET is registered in .route() (#3587)
This commit is contained in:
@@ -788,6 +788,27 @@ Deno.test("App support HEAD if only GET is registered", async () => {
|
||||
expect(res.status).toEqual(200);
|
||||
});
|
||||
|
||||
Deno.test(
|
||||
"App support HEAD in .route() when only GET is registered",
|
||||
async () => {
|
||||
const app = new App()
|
||||
.route("/", {
|
||||
handler: {
|
||||
GET() {
|
||||
return { data: {} };
|
||||
},
|
||||
},
|
||||
component: () => <h1>foo</h1>,
|
||||
});
|
||||
|
||||
const server = new FakeServer(app.handler());
|
||||
|
||||
const res = await server.head("/");
|
||||
expect(res.body).toEqual(null);
|
||||
expect(res.status).toEqual(200);
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test("App - .appWrapper()", async () => {
|
||||
const app = new App()
|
||||
.appWrapper(({ Component }) => (
|
||||
|
||||
@@ -93,8 +93,8 @@ Deno.test("fsRoutes - registers HTTP methods on router", async () => {
|
||||
|
||||
await res.body?.cancel();
|
||||
if (method === "GET" && other === "HEAD") {
|
||||
// When only a HEAD route is registered it should return 404
|
||||
expect(res.status).toEqual(404);
|
||||
// GET route + HEAD request should return 200
|
||||
expect(res.status).toEqual(200);
|
||||
} else {
|
||||
expect(res.status).toEqual(405);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { type Method, patternToSegments } from "./router.ts";
|
||||
import type { LayoutConfig, Route } from "./types.ts";
|
||||
import { type Context, getInternals } from "./context.ts";
|
||||
import { recordSpanError, tracer } from "./otel.ts";
|
||||
import { isHandlerByMethod } from "./handlers.ts";
|
||||
import { type HandlerFn, isHandlerByMethod } from "./handlers.ts";
|
||||
import {
|
||||
type AsyncAnyComponent,
|
||||
type PageProps,
|
||||
@@ -167,9 +167,16 @@ export async function renderRoute<State>(
|
||||
attributes: { "fresh.span_type": "fs_routes/handler" },
|
||||
}, async (span) => {
|
||||
try {
|
||||
const fn = isHandlerByMethod(handlers)
|
||||
? handlers[method] ?? null
|
||||
: handlers;
|
||||
let fn: HandlerFn<unknown, State> | null = null;
|
||||
if (isHandlerByMethod(handlers)) {
|
||||
if (handlers[method] !== undefined) {
|
||||
fn = handlers[method];
|
||||
} else if (method === "HEAD" && handlers.GET !== undefined) {
|
||||
fn = handlers.GET;
|
||||
}
|
||||
} else {
|
||||
fn = handlers;
|
||||
}
|
||||
|
||||
if (fn === null) return await ctx.next();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user