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] handle undefined body on endpoint output #1808

Merged
merged 12 commits into from
Jul 10, 2021
5 changes: 5 additions & 0 deletions .changeset/fluffy-cheetahs-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

handle undefined body on endpoint output
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default async function render_route(request, route) {
);
}

let { status = 200, body, headers = {} } = response;
let { status = 200, body = {}, headers = {} } = response;
ignatiusmb marked this conversation as resolved.
Show resolved Hide resolved

headers = lowercase_keys(headers);
const type = headers['content-type'];
Expand Down
39 changes: 39 additions & 0 deletions packages/kit/test/apps/basics/src/routes/endpoint-output/_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as assert from 'uvu/assert';

/** @type {import('test').TestMaker} */
export default function (test) {
test('not ok on void endpoint', null, async ({ fetch }) => {
const res = await fetch('/endpoint-output/empty', { method: 'DELETE' });
assert.equal(res.ok, false);
});
test('200 status on empty endpoint', null, async ({ fetch }) => {
const res = await fetch('/endpoint-output/empty');
assert.equal(res.status, 200);
assert.equal(await res.json(), {});
ignatiusmb marked this conversation as resolved.
Show resolved Hide resolved
});

test('set-cookie without body', null, async ({ fetch }) => {
const res = await fetch('/endpoint-output/headers');
assert.equal(res.status, 200);
assert.equal(res.headers.has('set-cookie'), true);
});

test('200 status by default', null, async ({ fetch }) => {
const res = await fetch('/endpoint-output/body');
assert.equal(res.status, 200);
assert.equal(await res.text(), '{}');
});

test('does not throw on blob method', null, async ({ fetch }) => {
const res = await fetch('/endpoint-output/empty');
assert.type(await res.blob(), 'object');
});
test('does not throw on arrayBuffer method', null, async ({ fetch }) => {
const res = await fetch('/endpoint-output/empty');
assert.type(await res.arrayBuffer(), 'object');
});
test('does not throw on buffer method', null, async ({ fetch }) => {
const res = await fetch('/endpoint-output/empty');
assert.type(await res.buffer(), 'object');
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
return { body: {} };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
return {};
}

/** @type {import('@sveltejs/kit').RequestHandler} */
export function del() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
return {
headers: {
'Set-Cookie': 'foo=bar'
}
};
}