Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deflate content encoding #2172

Merged
merged 1 commit into from Apr 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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