Skip to content

Commit

Permalink
Fix allowUnmocked when last interceptor was removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
djanowski authored and gr2m committed Sep 23, 2017
1 parent 06db20a commit 9268b44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/intercept.js
Expand Up @@ -106,6 +106,9 @@ function add(key, interceptor, scope, scopeOptions, host) {
interceptor.__nock_scopeHost = host;
interceptor.interceptionCounter = 0;

if (scopeOptions.allowUnmocked)
allInterceptors[key].allowUnmocked = true;

allInterceptors[key].scopes.push(interceptor);
}

Expand Down Expand Up @@ -163,7 +166,16 @@ function interceptorsFor(options) {
});

if (!matchingInterceptor && common.matchStringOrRegexp(basePath, interceptor.key)) {
matchingInterceptor = interceptor.scopes;
if (interceptor.scopes.length === 0 && interceptor.allowUnmocked) {
matchingInterceptor = [
{
options: { allowUnmocked: true },
matchIndependentOfBody: function() { return false }
}
];
} else {
matchingInterceptor = interceptor.scopes;
}
// false to short circuit the .each
return false;
}
Expand Down
20 changes: 19 additions & 1 deletion tests/test_intercept.js
Expand Up @@ -41,7 +41,7 @@ test("double activation throws exception", function(t) {
t.end();
});

test("allow override works (2)", function(t) {
test("allow unmocked works (2)", function(t) {
var scope =
nock("https://httpbin.org",{allowUnmocked: true}).
post("/post").
Expand All @@ -61,6 +61,24 @@ test("allow override works (2)", function(t) {
});
});

test("allow unmocked works after one interceptor is removed", function(t) {
var scope =
nock("https://example.org",{allowUnmocked: true}).
get("/").
reply(200, "Mocked");

mikealRequest("https://example.org", function(err, resp, body) {
t.error(err);
t.equal(body, 'Mocked');

mikealRequest("https://example.org/foo", function(err, resp, body) {
t.error(err);
t.assert(~body.indexOf('Example Domain'));
t.end();
});
});
});

test("reply callback's requestBody should automatically parse to JSON", function(t) {
var requestBodyFixture = {
id: 1,
Expand Down

0 comments on commit 9268b44

Please sign in to comment.