Skip to content

Commit

Permalink
Remove set content headers that break response
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Mar 23, 2022
1 parent 24cf06e commit 313b0dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
50 changes: 50 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 313b0dd

Please sign in to comment.