Skip to content

Commit

Permalink
failsafe on removing a non-existent interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Mar 2, 2012
1 parent c2d67fc commit 27f72c4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
15 changes: 9 additions & 6 deletions lib/intercept.js
Expand Up @@ -23,12 +23,15 @@ function remove(interceptor) {
interceptor,
thisInterceptor;

for(var i = 0; i < interceptors.length; i++) {
thisInterceptor = interceptors[i];
if (thisInterceptor._key === interceptor._key) {
interceptors.splice(i, 1);
break;
if (interceptors) {
for(var i = 0; i < interceptors.length; i++) {
thisInterceptor = interceptors[i];
if (thisInterceptor._key === interceptor._key) {
interceptors.splice(i, 1);
break;
}
}

}
//if (! interceptors) { delete allInterceptors[hostKey]; }
}
Expand Down Expand Up @@ -102,7 +105,7 @@ http.ClientRequest = OverridenClientRequest;
if (! matches && allowUnmocked) {
return oldRequest.apply(module, arguments);
}

req = new EventEmitter();
res = RequestOverrider(req, options, interceptors, remove);
if (callback) {
Expand Down
2 changes: 1 addition & 1 deletion lib/request_overrider.js
Expand Up @@ -173,7 +173,7 @@ function RequestOverrider(req, options, interceptors, remove) {
callnext();
});
};

return req;
}

Expand Down
5 changes: 3 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{ "name" : "nock"
, "description" : "HTTP Server mocking for Node.js"
, "tags" : ["Mock", "HTTP", "testing", "isolation"]
, "version" : "0.10.1"
, "version" : "0.10.2"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>"
, "contributors" :
[ {"name": "Roly Fentanes" }
Expand All @@ -24,7 +24,8 @@
, "engines" : ["node >= 0.4.10"]
, "main" : "./index"
, "devDependencies": {
"tap": "0.2.x"
"tap": "0.2.x",
"request": "*"
}
, "scripts": { "test": "node node_modules/tap/bin/tap.js tests" }
}
34 changes: 34 additions & 0 deletions tests/test_intercept.js
Expand Up @@ -4,6 +4,7 @@ var https = require('https');
var util = require('util');
var events = require('events');
var tap = require('tap');
var mikealRequest = require('request');

tap.test("get gets mocked", function(t) {
var dataCalled = false
Expand Down Expand Up @@ -1171,3 +1172,36 @@ tap.test('clean all works', function(t) {
});
req.end()
});


tap.test('username and password works', function(t) {
var scope = nock('http://passwordyy.com')
.log(console.log)
.get('/')
.reply(200, "Welcome, username");

http.request({
hostname: 'passwordyy.com',
auth: "username:password",
path: '/'
}, function(res) {
scope.done();
t.end();
}).end();
});


tap.test('works with mikeal/request and username and password', function(t) {
var scope = nock('http://passwordyyyyy.com')
.get('/abc')
.reply(200, "Welcome, username");

mikealRequest({uri: 'http://username:password@passwordyyyyy.com/abc', log:true}, function(err, res, body) {
console.log(err);
t.ok(! err, 'error');
t.ok(scope.isDone());
t.equal(body, "Welcome, username");
t.end();
});

});

0 comments on commit 27f72c4

Please sign in to comment.