Skip to content

Commit

Permalink
initial support for streams 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Dec 3, 2013
1 parent fd0d449 commit 3228760
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
callnext();
};

var read = false;
response.read = function() {
if (isStream(responseBody) && responseBody.read) {
return responseBody.read();
} else if (! read) {
read = true;
return responseBody;
}
}


if (typeof responseBody !== "undefined") {
next.push(function() {
Expand All @@ -230,6 +240,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}
if (! isStream(responseBody)) {
response.emit('data', responseBody);
response.emit('readable');
}
});
}
Expand Down
33 changes: 32 additions & 1 deletion tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -2045,4 +2045,35 @@ test('superagent works with query string', function(t) {
t.equal(res.text, responseText);
t.end();
});
});
});

test('response is streams2 compatible', function(t) {
var responseText = 'streams2 streams2 streams2';
nock('https://stream2hostnameftw')
.get('/somepath')
.reply(200, responseText);


http.request({
host: "stream2hostnameftw"
, path: "/somepath"
}, function(res) {
res.setEncoding('utf8');

var body = '';

res.on('readable', function() {
var buf;
while (buf = res.read())
body += buf;
});

res.once('end', function() {
t.equal(body, responseText);
t.end();
});

}).end();

});

0 comments on commit 3228760

Please sign in to comment.