diff --git a/index.js b/index.js index dcefd81..7025ae5 100644 --- a/index.js +++ b/index.js @@ -256,6 +256,12 @@ function chunkLength (chunk, encoding) { function shouldCompress (req, res) { var type = res.getHeader('Content-Type') + var noCompressionHeader = res.getHeader('x-no-compression') + + if (noCompressionHeader) { + debug('%s not compressed', type) + return false + } if (type === undefined || !compressible(type)) { debug('%s not compressible', type) diff --git a/test/compression.js b/test/compression.js index 61debb4..d8de003 100644 --- a/test/compression.js +++ b/test/compression.js @@ -554,7 +554,7 @@ describe('compression()', function () { .expect(200, 'hello, world', done) }) - it('should not set Vary headerh', function (done) { + it('should not set Vary header', function (done) { var server = createServer({ threshold: 0 }, function (req, res) { res.setHeader('Cache-Control', 'no-transform') res.setHeader('Content-Type', 'text/plain') @@ -570,6 +570,36 @@ describe('compression()', function () { }) }) + describe('when "X-No-Compression" request header', function () { + it('should not compress response', function (done) { + var server = createServer({ threshold: 0 }, function (req, res) { + res.setHeader('X-No-Compression', '0') + res.setHeader('Content-Type', 'text/plain') + res.end('hello, world') + }) + + request(server) + .get('/') + .set('Accept-Encoding', 'gzip, br') + .expect(shouldNotHaveHeader('Content-Encoding')) + .expect(200, 'hello, world', done) + }) + + it('should not set Vary header', function (done) { + var server = createServer({ threshold: 0 }, function (req, res) { + res.setHeader('X-No-Compression', '1') + res.setHeader('Content-Type', 'text/plain') + res.end('hello, world') + }) + + request(server) + .get('/') + .set('Accept-Encoding', 'gzip, br') + .expect(shouldNotHaveHeader('Vary')) + .expect(200, done) + }) + }) + describe('.filter', function () { it('should be a function', function () { assert.strictEqual(typeof compression.filter, 'function')