Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions __tests__/response/body.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,34 @@ describe('res.body=', () => {
assert.strictEqual(stream.destroyed, true)
})

it('should cleanup a JSON stream when replaced by null', () => {
const res = response()
const stream = new Stream.PassThrough()

res.type = 'json'
res.body = stream
res.body = null

assert.strictEqual(res.body, 'null')
assert.strictEqual(res.status, 200)
assert.strictEqual(res.type, 'application/json')
assert.strictEqual(stream.destroyed, true)
})

it('should cleanup a JSON stream when replaced by undefined', () => {
const res = response()
const stream = new Stream.PassThrough()

res.type = 'json'
res.body = stream
res.body = undefined

assert.strictEqual(res.body, 'null')
assert.strictEqual(res.status, 200)
assert.strictEqual(res.type, 'application/json')
assert.strictEqual(stream.destroyed, true)
})

it('should not throw unhandled errors when replacing failing stream', async () => {
const res = response()

Expand Down
1 change: 1 addition & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ module.exports = {
if (!statuses.empty[this.status]) {
if (this.type === 'application/json') {
this._body = 'null'
cleanupPreviousStream()
return
}
this.status = 204
Expand Down
Loading