Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: undefined HTTP method handlers not responding with 405 #1041

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/start/api/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function methodNotAllowed() {
return new Response(null, { status: 405 });
}
13 changes: 8 additions & 5 deletions packages/start/fs-router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,17 +386,20 @@ export function stringifyAPIRoutes(
* @return {string}
*/
function _stringifyRoutes(/** @type {(RouteConfig)[]} */ routes) {
const methodNotAllowed = jsFile.addNamedImport("methodNotAllowed", "solid-start/api/utils");
edivados marked this conversation as resolved.
Show resolved Hide resolved
return (
`[\n` +
routes
.map(
i =>
`{\n${[
...API_METHODS.filter(j => i.apiPath?.[j]).map(
v =>
i.apiPath != null &&
`${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`
),
...API_METHODS.map(v => {
if (i.componentPath && v === "GET")
return undefined;
else if (i.apiPath?.[v])
return `${v}: ${jsFile.addNamedImport(v, path.posix.resolve(i.apiPath[v]))}`;
return `${v}: ${methodNotAllowed}`;
}),
i.componentPath ? `GET: "skip"` : undefined,
`path: ${JSON.stringify(i.path)}`
]
Expand Down
24 changes: 24 additions & 0 deletions test/api-routes-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ test.describe("api routes", () => {
"src/routes/api/[param]/index.js": js`
import { json } from "solid-start/server";
export let GET = ({ params }) => json(params);
`,
"src/routes/method-not-allowed.jsx": js`
export default function Page() { return <div>page</div>; }
`,
"src/routes/api/method-not-allowed.js": js`
export function POST () { return new Response(); }
`
}
});
Expand Down Expand Up @@ -280,5 +286,23 @@ test.describe("api routes", () => {
expect(res.headers.get("content-type")).toEqual("application/json; charset=utf-8");
expect(await res.json()).toEqual({ static: true });
});

test("should return 405 for undefined handlers on route with only a default export", async () => {
let res = await fixture.requestDocument("/method-not-allowed", { method: "GET" });
expect(res.status).toEqual(200);
["POST", "PUT", "PATCH", "DELETE"].forEach(async method => {
res = await fixture.requestDocument("/method-not-allowed", { method });
expect(res.status).toEqual(405);
})
});

test("should return 405 for undefined handlers on route with only a POST export", async () => {
let res = await fixture.requestDocument("/api/method-not-allowed", { method: "POST" });
expect(res.status).toEqual(200);
["GET", "PUT", "PATCH", "DELETE"].forEach(async method => {
res = await fixture.requestDocument("/api/method-not-allowed", { method });
expect(res.status).toEqual(405);
});
});
}
});