Fix res.json support for string / null (#36186)
Continuation of #33592 with updates tests / changes. Co-Authored-By: Balázs Orbán <info@balazsorban.com> Co-authored-by: Balázs Orbán <info@balazsorban.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
parent
e69820accc
commit
1582e11185
5 changed files with 24 additions and 2 deletions
|
@ -442,7 +442,7 @@ function sendJson(res: NextApiResponse, jsonBody: any): void {
|
|||
res.setHeader('Content-Type', 'application/json; charset=utf-8')
|
||||
|
||||
// Use send to handle request
|
||||
res.send(jsonBody)
|
||||
res.send(JSON.stringify(jsonBody))
|
||||
}
|
||||
|
||||
function isNotValidData(str: string): boolean {
|
||||
|
|
3
test/integration/api-support/pages/api/json-null.js
Normal file
3
test/integration/api-support/pages/api/json-null.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
export default (req, res) => {
|
||||
res.json(null)
|
||||
}
|
3
test/integration/api-support/pages/api/json-string.js
Normal file
3
test/integration/api-support/pages/api/json-string.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
export default (req, res) => {
|
||||
res.json('Hello world!')
|
||||
}
|
|
@ -163,11 +163,27 @@ function runTests(dev = false) {
|
|||
})
|
||||
|
||||
it('should support undefined response body', async () => {
|
||||
const res = await fetchViaHTTP(appPort, '/api/undefined', null, {})
|
||||
const res = await fetchViaHTTP(appPort, '/api/json-undefined', null, {})
|
||||
const body = res.ok ? await res.text() : null
|
||||
expect(body).toBe('')
|
||||
})
|
||||
|
||||
it('should support string in JSON response body', async () => {
|
||||
const res = await fetchViaHTTP(appPort, '/api/json-string', null, {})
|
||||
const body = res.ok ? await res.text() : null
|
||||
expect(body).toBe('"Hello world!"')
|
||||
})
|
||||
|
||||
it('should support null in JSON response body', async () => {
|
||||
const res = await fetchViaHTTP(appPort, '/api/json-null')
|
||||
const body = res.ok ? await res.json() : 'Not null'
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('content-type')).toBe(
|
||||
'application/json; charset=utf-8'
|
||||
)
|
||||
expect(body).toBe(null)
|
||||
})
|
||||
|
||||
it('should return error with invalid JSON', async () => {
|
||||
const data = await fetchViaHTTP(appPort, '/api/parse', null, {
|
||||
method: 'POST',
|
||||
|
|
Loading…
Reference in a new issue