Permalink
Browse files

Fix TypeError in res.send when given Buffer and ETag header set

fixes #3445
  • Loading branch information...
davidwood authored and dougwilson committed Oct 9, 2017
1 parent 48aba21 commit b7817ab1b0bbadabe79fc88819c8338838ead707
Showing with 23 additions and 5 deletions.
  1. +5 −0 History.md
  2. +5 −5 lib/response.js
  3. +13 −0 test/res.send.js
View
@@ -1,3 +1,8 @@
unreleased
==========
* Fix `TypeError` in `res.send` when given `Buffer` and `ETag` header set
4.16.1 / 2017-09-29
===================
View
@@ -178,17 +178,17 @@ res.send = function send(body) {
// populate Content-Length
var len
if (chunk !== undefined) {
if (!generateETag && chunk.length < 1000) {
if (Buffer.isBuffer(chunk)) {
// get length of Buffer
len = chunk.length
} else if (!generateETag && chunk.length < 1000) {
// just calculate length when no ETag + small chunk
len = Buffer.byteLength(chunk, encoding)
} else if (!Buffer.isBuffer(chunk)) {
} else {
// convert chunk to Buffer and calculate
chunk = Buffer.from(chunk, encoding)
encoding = undefined;
len = chunk.length
} else {
// get length of Buffer
len = chunk.length
}
this.set('Content-Length', len);
View
@@ -216,6 +216,19 @@ describe('res', function(){
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, 'hey', done);
})
it('should not override ETag', function (done) {
var app = express()
app.use(function (req, res) {
res.type('text/plain').set('ETag', '"foo"').send(Buffer.from('hey'))
})
request(app)
.get('/')
.expect('ETag', '"foo"')
.expect(200, 'hey', done)
})
})
describe('.send(Object)', function(){

0 comments on commit b7817ab

Please sign in to comment.