Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,8 @@ res.send = function send(body) {
var etagFn = app.get('etag fn')
var generateETag = !this.get('ETag') && typeof etagFn === 'function'

// Because Content-Length and Transfer-Encoding can't be present in the response headers together,
// Content-Length should be added only if there is no Transfer-Encoding header
var len;
if (chunk !== undefined && !this.get('Transfer-Encoding')) {
if (chunk !== undefined) {
if (Buffer.isBuffer(chunk)) {
// get length of Buffer
len = chunk.length
Expand All @@ -179,7 +177,11 @@ res.send = function send(body) {
len = chunk.length
}

this.set('Content-Length', len);
// Because Content-Length and Transfer-Encoding can't be present in the response headers together,
// Content-Length should be added only if there is no Transfer-Encoding header
if (!this.get('Transfer-Encoding')) {
this.set('Content-Length', len);
}
}

// populate ETag
Expand Down
15 changes: 15 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,5 +616,20 @@ describe('res', function(){
.expect(200, '', done);
})
});

it('should still generate an ETag', function(done){
var app = express();

app.use(function(_, res){
res.set('Transfer-Encoding', 'chunked').send('hello');
});

request(app)
.get('/')
.expect(utils.shouldNotHaveHeader('Content-Length'))
.expect(utils.shouldHaveHeader('Transfer-Encoding'))
.expect('ETag', 'W/"5-qvTGHdzF6KLavt4PO0gs2a6pQ00"')
.expect(200, 'hello', done);
})
})
})