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: action not found should return 404 #11278

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-sloths-cry.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly return 404 when a form action is not found
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/page/actions.js
Expand Up @@ -216,7 +216,7 @@ async function call_action(event, actions) {

const action = actions[name];
if (!action) {
throw new Error(`No action with name '${name}' found`);
throw error(404, `No action with name '${name}' found`);
}

if (!is_form_content_type(event.request)) {
Expand Down
21 changes: 21 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Expand Up @@ -1238,6 +1238,27 @@ test.describe('Actions', () => {
expect(error.message).toBe('Actions expect form-encoded data (received application/json)');
expect(response.status()).toBe(415);
});
test('submitting to a form action that does not exists, should return http status code 404', async ({
baseURL,
page
}) => {
const randomActionName = 'some-random-action';

const body = new FormData();
body.append('foo', 'bar');

const response = await page.request.fetch(`${baseURL}/actions/enhance?/${randomActionName}`, {
method: 'POST',
body,
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
headers: {
Origin: `${baseURL}`
}
});
const { type, error } = await response.json();
expect(type).toBe('error');
expect(error.message).toBe(`No action with name '${randomActionName}' found`);
expect(response.status()).toBe(404);
});
});

// Run in serial to not pollute the log with (correct) cookie warnings
Expand Down