Skip to content

Commit

Permalink
fix: allow header methods list for 405s (#9655)
Browse files Browse the repository at this point in the history
see #8967 for explanation

regressed in #8731, probably because that PR started from an older branch
  • Loading branch information
gtm-nayan authored Apr 13, 2023
1 parent 5a1ac46 commit 208877d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-avocados-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correct allow header methods list for 405s
10 changes: 4 additions & 6 deletions packages/kit/src/runtime/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ export function method_not_allowed(mod, method) {

/** @param {Partial<Record<import('types').HttpMethod, any>>} mod */
export function allowed_methods(mod) {
const allowed = [];
const allowed = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'].filter(
(method) => method in mod
);

for (const method in ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']) {
if (method in mod) allowed.push(method);
}

if (mod.GET || mod.HEAD) allowed.push('HEAD');
if ('GET' in mod || 'HEAD' in mod) allowed.push('HEAD');

return allowed;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/kit/test/apps/basics/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ test.describe('Endpoints', () => {
const response = await request.post('/endpoint-output/body');

expect(response.status()).toBe(405);
expect(response.headers()['allow'].includes('GET'));

const allow_header = response.headers()['allow'];
expect(allow_header).toMatch(/\bGET\b/);
expect(allow_header).toMatch(/\bHEAD\b/);
});

// TODO all the remaining tests in this section are really only testing
Expand Down

0 comments on commit 208877d

Please sign in to comment.