Skip to content

Commit

Permalink
fix: Set body to if and @likegun closes koajs#1059 closes koajs#998
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Aug 18, 2021
1 parent 94c2ac4 commit 6cf6a95
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
## Fixes

- fix: Do not response Content-Length if Transfer-Encoding is defined #1562 @charlyzeng
- fix: Set body to `null` if `ctx.type = json` and `ctx.body = null` #1059 @likegun

2.13.1 / 2021-01-04
==================
Expand Down
15 changes: 15 additions & 0 deletions __tests__/response/length.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ describe('res.length', () => {
it('should return a number', () => {
const res = response()

res.body = null
assert.strictEqual(res.length, undefined)

res.body = 'foo'
res.remove('Content-Length')
assert.strictEqual(res.length, 3)
Expand Down Expand Up @@ -63,4 +66,16 @@ describe('res.length', () => {
})
})
})

describe('and a .type is set to json', () => {
describe('and a .body is set to null', () => {
it('should return a number', () => {
const res = response()

res.type = 'json'
res.body = null
assert.strictEqual(res.length, 4)
})
})
})
})
8 changes: 7 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ module.exports = {

// no content
if (val == null) {
if (!statuses.empty[this.status]) this.status = 204
if (!statuses.empty[this.status]) {
if (this.type === 'application/json') {
this._body = 'null'
return
}
this.status = 204
}
if (val === null) this._explicitNullBody = true
this.remove('Content-Type')
this.remove('Content-Length')
Expand Down

0 comments on commit 6cf6a95

Please sign in to comment.