Skip to content

Commit

Permalink
Merge pull request #240 from timoxley/bug/pipe-unzip
Browse files Browse the repository at this point in the history
unzip does not work when using pipe
  • Loading branch information
tj committed Jul 5, 2013
2 parents 2566d9c + 671aec7 commit 6306c12
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ node_modules
test.js
components
test/node/fixtures/tmp.json
test/node/fixtures/temp.txt
6 changes: 5 additions & 1 deletion lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,11 @@ Request.prototype.write = function(data, encoding){
Request.prototype.pipe = function(stream, options){
this.buffer(false);
this.end().req.on('response', function(res){
res.pipe(stream, options);

if (/^(deflate|gzip)$/.test(res.headers['content-encoding'])) {
res.pipe(require('zlib').createUnzip())
.pipe(stream, options)
} else res.pipe(stream, options);
});
return stream;
};
Expand Down
17 changes: 17 additions & 0 deletions test/node/inflate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

var request = require('../../')
, express = require('express')
, fs = require('fs')
, zlib

/**
Expand Down Expand Up @@ -43,6 +44,22 @@ if (zlib) {
done();
});
});
describe('with pipe', function() {
beforeEach(function(done) {
fs.unlink(__dirname + '/fixtures/temp.txt', function() {done()})
})
it('should deflate during pipe', function(done) {
var writeStream = fs.createWriteStream(__dirname + '/fixtures/temp.txt')
var req = request
.get('http://localhost:3080/binary')

req.on('end', function() {
fs.readFileSync(__dirname + '/fixtures/temp.txt', 'utf8').should.be.equal(subject)
done()
})
req.pipe(writeStream)
})
})

describe('without encoding set', function(){
it('should emit buffers', function(done){
Expand Down

0 comments on commit 6306c12

Please sign in to comment.