diff --git a/HISTORY.md b/HISTORY.md index c2ad6df..fa5535b 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,7 @@ unreleased ========== + * Remove set content headers that break response * deps: on-finished@2.4.1 * deps: statuses@2.0.1 - Rename `425 Unordered Collection` to standard `425 Too Early` diff --git a/index.js b/index.js index 9ebee63..2a2d31d 100644 --- a/index.js +++ b/index.js @@ -278,6 +278,11 @@ function send (req, res, status, headers, message) { res.statusCode = status res.statusMessage = statuses.message[status] + // remove any content headers + res.removeHeader('Content-Encoding') + res.removeHeader('Content-Language') + res.removeHeader('Content-Range') + // response headers setHeaders(res, headers) diff --git a/test/test.js b/test/test.js index f610770..a815654 100644 --- a/test/test.js +++ b/test/test.js @@ -458,6 +458,56 @@ describe('finalhandler(req, res)', function () { }) }) + describe('headers set', function () { + it('should persist set headers', function (done) { + var server = http.createServer(function (req, res) { + var done = finalhandler(req, res) + res.setHeader('Server', 'foobar') + done() + }) + + request(server) + .get('/foo') + .expect(404) + .expect('Server', 'foobar') + .end(done) + }) + + it('should override content-type and length', function (done) { + var server = http.createServer(function (req, res) { + var done = finalhandler(req, res) + res.setHeader('Content-Type', 'image/png') + res.setHeader('Content-Length', '50') + done() + }) + + request(server) + .get('/foo') + .expect(404) + .expect('Content-Type', 'text/html; charset=utf-8') + .expect('Content-Length', '142') + .end(done) + }) + + it('should remove other content headers', function (done) { + var server = http.createServer(function (req, res) { + var done = finalhandler(req, res) + res.setHeader('Content-Encoding', 'gzip') + res.setHeader('Content-Language', 'jp') + res.setHeader('Content-Range', 'bytes 0-2/10') + done() + }) + + request(server) + .get('/foo') + .expect(404) + .expect(shouldNotHaveHeader('Content-Encoding')) + .expect(shouldNotHaveHeader('Content-Language')) + .expect(shouldNotHaveHeader('Content-Range')) + .end(done) + }) + }) + describe('request started', function () { it('should not respond', function (done) { var server = http.createServer(function (req, res) {