Skip to content

Commit cc2c530

Browse files
committed
fix: preserve headers when reason is undefined in writeHead
When res.writeHead is called with 3 arguments where statusText/reason is undefined (or not a string), ensure headers passed as the 3rd argument are preserved and merged into the response headers. Fixes #254
1 parent 783ed70 commit cc2c530

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,15 @@ function compression (options) {
240240
})
241241
})
242242

243+
var _writeHead = res.writeHead
244+
res.writeHead = function writeHead (statusCode, reason, headers) {
245+
if (arguments.length > 2 && typeof reason !== 'string') {
246+
return _writeHead.call(this, statusCode, headers)
247+
}
248+
249+
return _writeHead.apply(this, arguments)
250+
}
251+
243252
next()
244253
}
245254
}

test/compression.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,93 @@ describe('compression()', function () {
973973
.expect(200, done)
974974
})
975975
})
976+
977+
describe('res.writeHead', function () {
978+
it('should support headers with undefined statusMessage', function (done) {
979+
var server = createServer({ threshold: 0 }, function (req, res) {
980+
res.writeHead(200, undefined, {
981+
'Content-Type': 'text/plain',
982+
'X-Custom': 'header'
983+
})
984+
res.end('hello, world')
985+
})
986+
987+
request(server)
988+
.get('/')
989+
.set('Accept-Encoding', 'gzip')
990+
.expect('X-Custom', 'header')
991+
.expect('Content-Encoding', 'gzip')
992+
.expect(200, 'hello, world', done)
993+
})
994+
995+
it('should support headers with string statusMessage', function (done) {
996+
var server = createServer({ threshold: 0 }, function (req, res) {
997+
res.writeHead(200, 'OK', {
998+
'Content-Type': 'text/plain',
999+
'X-Custom': 'header'
1000+
})
1001+
res.end('hello, world')
1002+
})
1003+
1004+
request(server)
1005+
.get('/')
1006+
.set('Accept-Encoding', 'gzip')
1007+
.expect('X-Custom', 'header')
1008+
.expect('Content-Encoding', 'gzip')
1009+
.expect(200, 'hello, world', done)
1010+
})
1011+
1012+
it('should support array headers with undefined statusMessage', function (done) {
1013+
var server = createServer({ threshold: 0 }, function (req, res) {
1014+
res.writeHead(200, undefined, [
1015+
['Content-Type', 'text/plain'],
1016+
['X-Custom', 'array']
1017+
])
1018+
res.end('hello, world')
1019+
})
1020+
1021+
request(server)
1022+
.get('/')
1023+
.set('Accept-Encoding', 'gzip')
1024+
.expect('X-Custom', 'array')
1025+
.expect('Content-Encoding', 'gzip')
1026+
.expect(200, 'hello, world', done)
1027+
})
1028+
1029+
it('should support array headers with string statusMessage', function (done) {
1030+
var server = createServer({ threshold: 0 }, function (req, res) {
1031+
res.writeHead(200, 'OK', [
1032+
['Content-Type', 'text/plain'],
1033+
['X-Custom', 'array']
1034+
])
1035+
res.end('hello, world')
1036+
})
1037+
1038+
request(server)
1039+
.get('/')
1040+
.set('Accept-Encoding', 'gzip')
1041+
.expect('X-Custom', 'array')
1042+
.expect('Content-Encoding', 'gzip')
1043+
.expect(200, 'hello, world', done)
1044+
})
1045+
1046+
it('should support uncompressed response with undefined statusMessage', function (done) {
1047+
var server = createServer({ filter: function () { return false } }, function (req, res) {
1048+
res.writeHead(200, undefined, {
1049+
'Content-Type': 'text/plain',
1050+
'X-Custom': 'header'
1051+
})
1052+
res.end('hello, world')
1053+
})
1054+
1055+
request(server)
1056+
.get('/')
1057+
.set('Accept-Encoding', 'gzip')
1058+
.expect('X-Custom', 'header')
1059+
.expect(shouldNotHaveHeader('Content-Encoding'))
1060+
.expect(200, 'hello, world', done)
1061+
})
1062+
})
9761063
})
9771064

9781065
function createServer (opts, fn) {

0 commit comments

Comments
 (0)