Skip to content

Commit 208877d

Browse files
authored
fix: allow header methods list for 405s (#9655)
see #8967 for explanation regressed in #8731, probably because that PR started from an older branch
1 parent 5a1ac46 commit 208877d

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

.changeset/shy-avocados-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: correct allow header methods list for 405s

packages/kit/src/runtime/server/utils.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ export function method_not_allowed(mod, method) {
3434

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

39-
for (const method in ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']) {
40-
if (method in mod) allowed.push(method);
41-
}
42-
43-
if (mod.GET || mod.HEAD) allowed.push('HEAD');
41+
if ('GET' in mod || 'HEAD' in mod) allowed.push('HEAD');
4442

4543
return allowed;
4644
}

packages/kit/test/apps/basics/test/server.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ test.describe('Endpoints', () => {
125125
const response = await request.post('/endpoint-output/body');
126126

127127
expect(response.status()).toBe(405);
128-
expect(response.headers()['allow'].includes('GET'));
128+
129+
const allow_header = response.headers()['allow'];
130+
expect(allow_header).toMatch(/\bGET\b/);
131+
expect(allow_header).toMatch(/\bHEAD\b/);
129132
});
130133

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

0 commit comments

Comments
 (0)