Skip to content
Closed
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
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ function compression (options) {
})
})

var _writeHead = res.writeHead
res.writeHead = function writeHead (statusCode, reason, headers) {
if (arguments.length > 2 && typeof reason !== 'string') {
return _writeHead.call(this, statusCode, headers)
}

return _writeHead.apply(this, arguments)
}

next()
}
}
Expand Down
87 changes: 87 additions & 0 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,93 @@ describe('compression()', function () {
.expect(200, done)
})
})

describe('res.writeHead', function () {
it('should support headers with undefined statusMessage', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.writeHead(200, undefined, {
'Content-Type': 'text/plain',
'X-Custom': 'header'
})
res.end('hello, world')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect('X-Custom', 'header')
.expect('Content-Encoding', 'gzip')
.expect(200, 'hello, world', done)
})

it('should support headers with string statusMessage', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.writeHead(200, 'OK', {
'Content-Type': 'text/plain',
'X-Custom': 'header'
})
res.end('hello, world')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect('X-Custom', 'header')
.expect('Content-Encoding', 'gzip')
.expect(200, 'hello, world', done)
})

it('should support array headers with undefined statusMessage', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.writeHead(200, undefined, [
['Content-Type', 'text/plain'],
['X-Custom', 'array']
])
res.end('hello, world')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect('X-Custom', 'array')
.expect('Content-Encoding', 'gzip')
.expect(200, 'hello, world', done)
})

it('should support array headers with string statusMessage', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.writeHead(200, 'OK', [
['Content-Type', 'text/plain'],
['X-Custom', 'array']
])
res.end('hello, world')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect('X-Custom', 'array')
.expect('Content-Encoding', 'gzip')
.expect(200, 'hello, world', done)
})

it('should support uncompressed response with undefined statusMessage', function (done) {
var server = createServer({ filter: function () { return false } }, function (req, res) {
res.writeHead(200, undefined, {
'Content-Type': 'text/plain',
'X-Custom': 'header'
})
res.end('hello, world')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect('X-Custom', 'header')
.expect(shouldNotHaveHeader('Content-Encoding'))
.expect(200, 'hello, world', done)
})
})
})

function createServer (opts, fn) {
Expand Down