Skip to content
Browse files

Merge pull request #981 from visionmedia/pipeevents

Emit response event when piping
  • Loading branch information...
2 parents 8ae7380 + b24ab0b commit b3ef32ca1d50ef050d2efb08443d0607d7b3ae2e @pornel pornel committed May 5, 2016
Showing with 18 additions and 4 deletions.
  1. +12 −4 lib/node/index.js
  2. +6 −0 test/node/pipe.js
View
16 lib/node/index.js
@@ -351,6 +351,13 @@ Request.prototype.pipe = function(stream, options){
return self._redirect(res).pipe(stream, options);
}
+ self.res = res;
+ var response = new Response(self);
+ self.response = response;
+ self.emit('response', response);
+ response.redirects = self._redirectList;
+ if (self._aborted) return;
+
if (self._shouldUnzip(res)) {
res.pipe(zlib.createUnzip()).pipe(stream, options);
} else {
@@ -677,6 +684,11 @@ Request.prototype.end = function(fn){
// response
req.on('response', function(res){
debug('%s %s -> %s', self.method, self.url, res.statusCode);
+
+ if (self.piped) {
+ return;
+ }
+
var max = self._maxRedirects;
var mime = utils.type(res.headers['content-type'] || '') || 'text/plain';
var len = res.headers['content-length'];
@@ -689,10 +701,6 @@ Request.prototype.end = function(fn){
self.res = res;
- if (self.piped) {
- return;
- }
-
// redirect
if (redirect && self._redirects++ != max) {
return self._redirect(res);
View
6 test/node/pipe.js
@@ -47,11 +47,17 @@ describe('request pipe', function(){
it('should act as a readable stream', function(done){
var stream = fs.createWriteStream(destPath);
+ var responseCalled = false;
var req = request.get(base);
req.type('json');
+ req.on('response', function(res){
+ res.should.have.status(200);
+ responseCalled = true;
+ });
stream.on('finish', function(){
JSON.parse(fs.readFileSync(destPath, 'utf8')).should.eql({ name: 'tobi' });
+ responseCalled.should.be.true();
done();
});
req.pipe(stream);

0 comments on commit b3ef32c

Please sign in to comment.
Something went wrong with that request. Please try again.