Permalink
Browse files

superagent works

  • Loading branch information...
pgte committed Dec 3, 2013
1 parent 38e456f commit 22604eba91dd7da34df3f32125832bddeedac9af
Showing with 192 additions and 114 deletions.
  1. +12 −9 lib/request_overrider.js
  2. +102 −44 package.json
  3. +78 −61 tests/test_intercept.js
View
@@ -97,7 +97,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
requestBodyBuffers.push(buffer);
}
};
-
+
var oldEnd = req.end;
req.end = function(buffer, encoding) {
@@ -107,7 +107,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
req.emit('end');
}
};
-
+
req.abort = function() {
aborted = true;
if (!ended) {
@@ -117,7 +117,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
err.code = 'aborted'
response.emit('close', err);
};
-
+
end = function(cb) {
ended = true;
var encoding,
@@ -137,13 +137,13 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
b.copy(requestBody, copyTo);
copyTo += b.length;
});
-
+
requestBody = requestBody.toString();
-
+
interceptors = interceptors.filter(function(interceptor) {
return interceptor.match(options, requestBody);
});
-
+
if (interceptors.length < 1) {
// Try to find a hostname match
interceptors = originalInterceptors.filter(function(interceptor) {
@@ -166,9 +166,9 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
response.headers = interceptor.headers || {};
if (typeof interceptor.body === 'function') {
- responseBody = interceptor.body(options.path, requestBody) || '';
+ responseBody = interceptor.body(options.path, requestBody) || '';
} else {
- responseBody = interceptor.body;
+ responseBody = interceptor.body;
}
if (isStream(responseBody)) {
@@ -188,12 +188,14 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
responseBody = JSON.stringify(responseBody);
}
}
+
response.setEncoding = function(newEncoding) {
encoding = newEncoding;
};
remove(interceptor);
interceptor.discard();
+
if (aborted) { return; }
response.pause = function() {
@@ -211,6 +213,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
callnext();
};
+
if (typeof responseBody !== "undefined") {
next.push(function() {
if (encoding) {
@@ -239,7 +242,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
callnext();
});
};
-
+
process.nextTick(function() {
if (typeof callback === 'function') {
callback(response);
View
@@ -1,47 +1,105 @@
-{ "name" : "nock"
-, "description" : "HTTP Server mocking for Node.js"
-, "tags" : ["Mock", "HTTP", "testing", "isolation"]
-, "version" : "0.24.1"
-, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>"
-, "contributors" :
- [ {"name": "Roly Fentanes" }
- , {"name": "Alexander Simmerl"}
- , {"name": "Pedro Teixeira"}
- , {"name": "Nuno Job"}
- , {"name": "Ian Young"}
- , {"name": "nilsbunger"}
- , {"name": "bacchusrx", "email": "bacchusrx@eightstar.ca"}
- , {"name": "Fabiano França"}
- , {"name": "Sascha Drews"}
- , {"name": "Mike Swift"}
- , {"name": "James Herdman"}
- , {"name": "David Björklund"}
- , {"name": "Andrew Kramolisch"}
- , {"name": "Balazs Nagy"}
- , {"name": "Brian J Brennan"}
- , {"name": "Attila Incze"}
- , {"name": "Mac Angell"}
- , {"name": "Tom Hosford"}
- , {"name": "Aurélien Thieriot"}
- , {"name": "Alex Zylman"}
- , {"name": "Celestino Gomes", "email": "contact@tinogomes.com"}
- , {"name": "David Rousselie"}
- ]
-, "repository" :
- { "type" : "git"
- , "url" : "http://github.com/pgte/nock.git"
- }
-, "bugs" :
- { "url" : "http://github.com/pgte/nock/issues" }
-, "engines" : ["node >= 0.4.10"]
-, "main" : "./index"
-, "dependencies": {
+{
+ "name": "nock",
+ "description": "HTTP Server mocking for Node.js",
+ "tags": [
+ "Mock",
+ "HTTP",
+ "testing",
+ "isolation"
+ ],
+ "version": "0.24.1",
+ "author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
+ "contributors": [
+ {
+ "name": "Roly Fentanes"
+ },
+ {
+ "name": "Alexander Simmerl"
+ },
+ {
+ "name": "Pedro Teixeira"
+ },
+ {
+ "name": "Nuno Job"
+ },
+ {
+ "name": "Ian Young"
+ },
+ {
+ "name": "nilsbunger"
+ },
+ {
+ "name": "bacchusrx",
+ "email": "bacchusrx@eightstar.ca"
+ },
+ {
+ "name": "Fabiano França"
+ },
+ {
+ "name": "Sascha Drews"
+ },
+ {
+ "name": "Mike Swift"
+ },
+ {
+ "name": "James Herdman"
+ },
+ {
+ "name": "David Björklund"
+ },
+ {
+ "name": "Andrew Kramolisch"
+ },
+ {
+ "name": "Balazs Nagy"
+ },
+ {
+ "name": "Brian J Brennan"
+ },
+ {
+ "name": "Attila Incze"
+ },
+ {
+ "name": "Mac Angell"
+ },
+ {
+ "name": "Tom Hosford"
+ },
+ {
+ "name": "Aurélien Thieriot"
+ },
+ {
+ "name": "Alex Zylman"
+ },
+ {
+ "name": "Celestino Gomes",
+ "email": "contact@tinogomes.com"
+ },
+ {
+ "name": "David Rousselie"
+ }
+ ],
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/pgte/nock.git"
+ },
+ "bugs": {
+ "url": "http://github.com/pgte/nock/issues"
+ },
+ "engines": [
+ "node >= 0.4.10"
+ ],
+ "main": "./index",
+ "dependencies": {
"propagate": "0.2.x"
-}
-, "devDependencies": {
+ },
+ "devDependencies": {
"tap": "*",
- "request": "*"
-}
-, "scripts": { "test": "node node_modules/tap/bin/tap.js tests" }
-, "license": "MIT"
+ "request": "*",
+ "superagent": "~0.15.7"
+ },
+ "scripts": {
+ "test": "node node_modules/tap/bin/tap.js tests"
+ },
+ "license": "MIT"
}
Oops, something went wrong.

0 comments on commit 22604eb

Please sign in to comment.