Skip to content

Commit

Permalink
fix: improve body null check (#1814)
Browse files Browse the repository at this point in the history
  • Loading branch information
leslieXin92 committed May 30, 2024
1 parent 00f3fed commit b89e19a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions __tests__/application/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,42 @@ describe('app.respond', () => {
})
})

describe('when .body is undefined', () => {
it('should respond 204 by default', async () => {
const app = new Koa()

app.use(ctx => {
ctx.body = undefined
})

const server = app.listen()

const res = await request(server)
.get('/')
.expect(204)
.expect('')

assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'Content-Type'), false)
})

it('should respond 204 with status=200', async () => {
const app = new Koa()
app.use(ctx => {
ctx.status = 200
ctx.body = undefined
})

const server = app.listen()

const res = await request(server)
.get('/')
.expect(204)
.expect('')

assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'Content-Type'), false)
})
})

describe('when .body is a string', () => {
it('should respond', () => {
const app = new Koa()
Expand Down
2 changes: 1 addition & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function respond (ctx) {
}

// status body
if (body == null) {
if (body === null || body === undefined) {
if (ctx.response._explicitNullBody) {
ctx.response.remove('Content-Type')
ctx.response.remove('Transfer-Encoding')
Expand Down

0 comments on commit b89e19a

Please sign in to comment.