Skip to content

Commit 26d96b3

Browse files
committed
Prefer gzip over deflate on the server
closes #26
1 parent 857fc7c commit 26d96b3

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
unreleased
2+
==========
3+
4+
* Prefer `gzip` over `deflate` on the server
5+
- Not all clients agree on what "deflate" coding means
6+
17
1.3.1 / 2015-01-31
28
==================
39

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ function compression(options) {
159159
var accept = accepts(req)
160160
var method = accept.encoding(['gzip', 'deflate', 'identity'])
161161

162+
// we really don't prefer deflate
163+
if (method === 'deflate' && accept.encoding(['gzip'])) {
164+
method = accept.encoding(['gzip', 'identity'])
165+
}
166+
162167
// negotiation failed
163168
if (!method || method === 'identity') {
164169
nocompress('not acceptable')

test/compression.js

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ var request = require('supertest');
77
var compression = require('..');
88

99
describe('compression()', function(){
10-
it('should gzip files', function(done){
11-
var server = createServer({ threshold: 0 }, function (req, res) {
12-
res.setHeader('Content-Type', 'text/plain')
13-
res.end('hello, world')
14-
})
15-
16-
request(server)
17-
.get('/')
18-
.set('Accept-Encoding', 'gzip')
19-
.expect('Content-Encoding', 'gzip', done)
20-
})
21-
2210
it('should skip HEAD', function(done){
2311
var server = createServer({ threshold: 0 }, function (req, res) {
2412
res.setHeader('Content-Type', 'text/plain')
@@ -394,6 +382,62 @@ describe('compression()', function(){
394382
})
395383
})
396384

385+
describe('when "Accept-Encoding: gzip"', function () {
386+
it('should respond with gzip', function (done) {
387+
var server = createServer({ threshold: 0 }, function (req, res) {
388+
res.setHeader('Content-Type', 'text/plain')
389+
res.end('hello, world')
390+
})
391+
392+
request(server)
393+
.get('/')
394+
.set('Accept-Encoding', 'gzip')
395+
.expect('Content-Encoding', 'gzip', done)
396+
})
397+
})
398+
399+
describe('when "Accept-Encoding: deflate"', function () {
400+
it('should respond with deflate', function (done) {
401+
var server = createServer({ threshold: 0 }, function (req, res) {
402+
res.setHeader('Content-Type', 'text/plain')
403+
res.end('hello, world')
404+
})
405+
406+
request(server)
407+
.get('/')
408+
.set('Accept-Encoding', 'deflate')
409+
.expect('Content-Encoding', 'deflate', done)
410+
})
411+
})
412+
413+
describe('when "Accept-Encoding: gzip, deflate"', function () {
414+
it('should respond with gzip', function (done) {
415+
var server = createServer({ threshold: 0 }, function (req, res) {
416+
res.setHeader('Content-Type', 'text/plain')
417+
res.end('hello, world')
418+
})
419+
420+
request(server)
421+
.get('/')
422+
.set('Accept-Encoding', 'gzip, deflate')
423+
.expect('Content-Encoding', 'gzip', done)
424+
})
425+
})
426+
427+
describe('when "Accept-Encoding: deflate, gzip"', function () {
428+
it('should respond with gzip', function (done) {
429+
var server = createServer({ threshold: 0 }, function (req, res) {
430+
res.setHeader('Content-Type', 'text/plain')
431+
res.end('hello, world')
432+
})
433+
434+
request(server)
435+
.get('/')
436+
.set('Accept-Encoding', 'deflate, gzip')
437+
.expect('Content-Encoding', 'gzip', done)
438+
})
439+
})
440+
397441
describe('.filter', function () {
398442
it('should be a function', function () {
399443
assert.equal(typeof compression.filter, 'function')

0 commit comments

Comments
 (0)