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

Allow undefined body sent to sendData() #20981

Merged
merged 6 commits into from Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/next/next-server/server/api-utils.ts
Expand Up @@ -229,7 +229,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