From a8277e916545c966cd8cc097c6ae9b1cba44610e Mon Sep 17 00:00:00 2001 From: rokobekavac0 Date: Mon, 11 Jan 2021 16:01:04 +0100 Subject: [PATCH 1/2] Add a check if body is undefined --- packages/next/next-server/server/api-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index f04176eb33d96..07f34d68b66be 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -229,7 +229,7 @@ export function sendData( res: NextApiResponse, body: any ): void { - if (body === null) { + if (body === null || body === undefined) { res.end() return } From b0d329182c669edea64a9b838a38cf78b79de2d2 Mon Sep 17 00:00:00 2001 From: rokobekavac0 Date: Fri, 15 Jan 2021 14:37:18 +0100 Subject: [PATCH 2/2] Add test for undefined response body --- test/integration/api-support/pages/api/undefined.js | 3 +++ test/integration/api-support/test/index.test.js | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 test/integration/api-support/pages/api/undefined.js diff --git a/test/integration/api-support/pages/api/undefined.js b/test/integration/api-support/pages/api/undefined.js new file mode 100644 index 0000000000000..0ccd4fcc75e94 --- /dev/null +++ b/test/integration/api-support/pages/api/undefined.js @@ -0,0 +1,3 @@ +export default (req, res) => { + res.json(undefined) +} diff --git a/test/integration/api-support/test/index.test.js b/test/integration/api-support/test/index.test.js index a7fec5424ccf0..de878765f842a 100644 --- a/test/integration/api-support/test/index.test.js +++ b/test/integration/api-support/test/index.test.js @@ -122,6 +122,12 @@ function runTests(dev = false) { expect(body).toBe(true) }) + it('should support undefined response body', async () => { + const res = await fetchViaHTTP(appPort, '/api/undefined', null, {}) + const body = res.ok ? await res.text() : null + expect(body).toBe('') + }) + it('should return error with invalid JSON', async () => { const data = await fetchViaHTTP(appPort, '/api/parse', null, { method: 'POST',