Skip to content

Commit

Permalink
Allow undefined body sent to sendData() (#20981)
Browse files Browse the repository at this point in the history
Co-authored-by: rokobekavac0 <roko.beakavac2003@gmail.com>
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
  • Loading branch information
4 people committed Jan 26, 2021
1 parent 07d4af9 commit 004ad62
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/next-server/server/api-utils.ts
Expand Up @@ -231,7 +231,7 @@ export function sendData(
res: NextApiResponse,
body: any
): void {
if (body === null) {
if (body === null || body === undefined) {
res.end()
return
}
Expand Down
3 changes: 3 additions & 0 deletions test/integration/api-support/pages/api/undefined.js
@@ -0,0 +1,3 @@
export default (req, res) => {
res.json(undefined)
}
6 changes: 6 additions & 0 deletions test/integration/api-support/test/index.test.js
Expand Up @@ -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',
Expand Down

0 comments on commit 004ad62

Please sign in to comment.