Skip to content

Commit

Permalink
Don't encode responses with Cache-Control: no-transform
Browse files Browse the repository at this point in the history
  • Loading branch information
glenjamin committed Sep 9, 2015
1 parent c2af8bd commit 1659fd0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ function compression(options) {
return
}

// Don't compress for Cache-Control: no-transform
// https://tools.ietf.org/html/rfc7234#section-5.2.1.6
var cacheControls = (res.getHeader('Cache-Control') || '').split(',')
var noTransform = cacheControls.some(function(cacheControl) {
return cacheControl.trim() == 'no-transform'
})
if (noTransform) {
nocompress('no-transform')
return
}

// vary
vary(res, 'Accept-Encoding')

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

describe("Cache-Control", function() {

[
'no-transform',
'public, no-transform',
'no-transform, private',
'no-transform , max-age=1000',
'max-age=1000 , no-transform'
].forEach(function(headerValue) {
it('should skip Cache-Control: ' + headerValue, function(done){
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.setHeader('Cache-Control', headerValue)
res.end('hello, world')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect(shouldNotHaveHeader('Content-Encoding'))
.expect(200, done)
})
});

[
'not-no-transform',
'public',
'no-transform-thingy'
].forEach(function(headerValue) {
it('should not skip Cache-Control: ' + headerValue, function(done){
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.setHeader('Cache-Control', headerValue)
res.end('hello, world')
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect('Content-Encoding', 'gzip')
.expect(200, done)
})
});

})

it('should skip unknown accept-encoding', function(done){
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
Expand Down

0 comments on commit 1659fd0

Please sign in to comment.