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

fix: improve body null check #1814

Merged
merged 5 commits into from
May 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -289,7 +289,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