Skip to content

Commit

Permalink
fix: middlewares not being used when GET handler is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
pcardune committed Jan 8, 2025
1 parent 70d6cbd commit ff25d7a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/plugins/fs_routes/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ export async function fsRoutes<State>(
isHandlerByMethod(handlers) &&
!Object.keys(handlers).includes("GET");
if (missingGetHandler) {
app.get(routePath, renderMiddleware(components, undefined));
const combined = middlewares.concat(
renderMiddleware(components, undefined),
);
app.get(routePath, ...combined);
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/plugins/fs_routes/mod_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,47 @@ Deno.test("fsRoutes - registers default GET route for component without GET hand
);
});

Deno.test("fsRoutes - default GET route works with nested middleware", async () => {
const server = await createServer<{ text: string }>({
"routes/_middleware.ts": {
handler: (ctx) => {
ctx.state.text = "A";
return ctx.next();
},
},
"routes/foo/_middleware.ts": {
handler: (ctx) => {
ctx.state.text += "B";
return ctx.next();
},
},
"routes/foo/noGetHandler.tsx": {
default: (ctx) => {
return <h1>{ctx.state.text}</h1>;
},
handlers: {
POST: () => new Response("POST"),
},
},
});

const postRes = await server.post("/foo/noGetHandler");
expect(postRes.status).toEqual(200);
expect(postRes.headers.get("Content-Type")).toEqual(
"text/plain;charset=UTF-8",
);
expect(await postRes.text()).toEqual("POST");

const getRes = await server.get("/foo/noGetHandler");
expect(getRes.status).toEqual(200);
expect(getRes.headers.get("Content-Type")).toEqual(
"text/html; charset=utf-8",
);
expect(await getRes.text()).toContain(
"<h1>AB</h1>",
);
});

Deno.test("fsRoutes - default GET route doesn't override existing handler", async () => {
const server = await createServer<{ value: boolean }>({
"routes/withGetHandler.tsx": {
Expand Down

0 comments on commit ff25d7a

Please sign in to comment.