diff --git a/__tests__/application/respond.js b/__tests__/application/respond.js index 46e6c6012..ccc57e269 100644 --- a/__tests__/application/respond.js +++ b/__tests__/application/respond.js @@ -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() diff --git a/lib/application.js b/lib/application.js index 6f42d83d2..3a3852b0d 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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')