From 9268b44dfbd36515af99c5fd67739bac34c46d71 Mon Sep 17 00:00:00 2001 From: Damian Janowski Date: Thu, 24 Nov 2016 15:35:49 -0300 Subject: [PATCH] Fix allowUnmocked when last interceptor was removed. --- lib/intercept.js | 14 +++++++++++++- tests/test_intercept.js | 20 +++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/intercept.js b/lib/intercept.js index 34699b75d..4df1d02ae 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -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); } @@ -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; } diff --git a/tests/test_intercept.js b/tests/test_intercept.js index 716ed51e1..e090b06e4 100755 --- a/tests/test_intercept.js +++ b/tests/test_intercept.js @@ -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"). @@ -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,