Skip to content

Commit

Permalink
Merge pull request #2172 from czardoz/master
Browse files Browse the repository at this point in the history
Add support for deflate content encoding
  • Loading branch information
simov committed Apr 16, 2016
2 parents 5a8ddc8 + 750fe85 commit 00eaf55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion request.js
Expand Up @@ -399,7 +399,7 @@ Request.prototype.init = function (options) {
}

if (self.gzip && !self.hasHeader('accept-encoding')) {
self.setHeader('accept-encoding', 'gzip')
self.setHeader('accept-encoding', 'gzip, deflate')
}

if (self.uri.auth && !self.hasHeader('authorization')) {
Expand Down Expand Up @@ -928,6 +928,9 @@ Request.prototype.onRequestResponse = function (response) {
if (contentEncoding === 'gzip') {
responseContent = zlib.createGunzip()
response.pipe(responseContent)
} else if (contentEncoding === 'deflate') {
responseContent = zlib.createInflate()
response.pipe(responseContent)
} else {
// Since previous versions didn't check for Content-Encoding header,
// ignore any invalid values to preserve backwards-compatibility
Expand Down
18 changes: 18 additions & 0 deletions tests/test-gzip.js
Expand Up @@ -31,6 +31,12 @@ var server = http.createServer(function(req, res) {
res.end(data)
})
}
} else if (/\bdeflate\b/i.test(req.headers['accept-encoding'])) {
res.setHeader('Content-Encoding', 'deflate')
zlib.deflate(testContent, function (err, data) {
assert.equal(err, null)
res.end(data)
})
} else {
res.end(testContent)
}
Expand Down Expand Up @@ -209,6 +215,18 @@ tape('pause before streaming from a gzip request object', function(t) {
}, 100)
})

tape('transparently supports deflate decoding to callbacks', function(t) {
var options = { url: 'http://localhost:6767/foo', gzip: true, headers: { 'Accept-Encoding': 'deflate' } }

request.get(options, function(err, res, body) {
t.equal(err, null)
t.equal(res.headers['content-encoding'], 'deflate')
t.equal(body, testContent)
t.end()
})
})


tape('cleanup', function(t) {
server.close(function() {
t.end()
Expand Down

0 comments on commit 00eaf55

Please sign in to comment.