diff --git a/lib/context.js b/lib/context.js index deb142c..faece14 100644 --- a/lib/context.js +++ b/lib/context.js @@ -71,6 +71,12 @@ module.exports = { val = n; } + if (204 == val || 304 == val) { + this.res.removeHeader('Content-Type'); + this.res.removeHeader('Content-Length'); + this.res.removeHeader('Transfer-Encoding'); + } + this.res.statusCode = val; }, diff --git a/test/context.js b/test/context.js index 6788e87..cf3cc54 100644 --- a/test/context.js +++ b/test/context.js @@ -1,5 +1,6 @@ var _context = require('../lib/context'); +var request = require('supertest'); var assert = require('assert'); var koa = require('..'); var fs = require('fs'); @@ -172,6 +173,29 @@ describe('ctx.status=', function(){ }) }) }) + + describe('when 204', function(){ + it('should strip content related header fields', function(done){ + var app = koa(); + + app.use(function(next){ + return function *(){ + this.set('Content-Type', 'application/json'); + this.set('Content-Length', '15'); + this.set('Transfer-Encoding', 'chunked'); + this.status = 204; + assert(null == this.responseHeader['content-type']); + assert(null == this.responseHeader['content-length']); + assert(null == this.responseHeader['transfer-encoding']); + } + }); + + request(app.listen()) + .get('/') + .expect(204) + .end(done); + }) + }) }) describe('ctx.stale', function(){