diff --git a/.changeset/dirty-days-fix.md b/.changeset/dirty-days-fix.md new file mode 100644 index 000000000000..75c205093f43 --- /dev/null +++ b/.changeset/dirty-days-fix.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: correctly include exported http methods in allow header diff --git a/packages/kit/src/runtime/server/utils.js b/packages/kit/src/runtime/server/utils.js index ed8c4803d4c0..68dad34d3348 100644 --- a/packages/kit/src/runtime/server/utils.js +++ b/packages/kit/src/runtime/server/utils.js @@ -41,7 +41,7 @@ export function method_not_allowed(mod, method) { export function allowed_methods(mod) { const allowed = []; - for (const method in ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']) { + for (const method of ['GET', 'POST', 'PUT', 'PATCH', 'DELETE']) { if (method in mod) allowed.push(method); } diff --git a/packages/kit/test/apps/basics/test/server.test.js b/packages/kit/test/apps/basics/test/server.test.js index cad3f9f88fd6..f2e09a28e683 100644 --- a/packages/kit/test/apps/basics/test/server.test.js +++ b/packages/kit/test/apps/basics/test/server.test.js @@ -108,6 +108,13 @@ test.describe('Endpoints', () => { }); }); + test('invalid request method returns allow header', async ({ request }) => { + const response = await request.post('/endpoint-output/body'); + + expect(response.status()).toBe(405); + expect(response.headers()['allow'].includes('GET')); + }); + // TODO all the remaining tests in this section are really only testing // setResponse, since we're not otherwise changing anything on the response. // might be worth making these unit tests instead