Permalink
Browse files

initial support for streams 2

  • Loading branch information...
1 parent fd0d449 commit 3228760f2e7641de2bd15d2bea29425987c4d26a @pgte pgte committed Dec 3, 2013
Showing with 43 additions and 1 deletion.
  1. +11 −0 lib/request_overrider.js
  2. +32 −1 tests/test_intercept.js
@@ -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() {
@@ -230,6 +240,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
}
if (! isStream(responseBody)) {
response.emit('data', responseBody);
+ response.emit('readable');
}
});
}
@@ -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('http://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.