From 65d0e74a61df2efb6c860b0ceba3f8ffc9e64362 Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Fri, 7 Feb 2020 14:28:39 -0700 Subject: [PATCH 1/2] fix: allowMocked when using a callback for the path When an Interceptor was created with a comparator for the path, the `matchOrigin` function was comparing the string equivalent of the function instead of evaluating it. I'm not a fan of the fact that the function `matchOrigin` is comparing the pathname, but that's a refactor for another day. Fixes: #1867 --- lib/interceptor.js | 7 ++++++- tests/test_allow_unmocked.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/interceptor.js b/lib/interceptor.js index e1a71b5ed..3ace0509d 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -355,6 +355,7 @@ module.exports = class Interceptor { * match the provided options. */ matchOrigin(options) { + const isPathFn = typeof this.path === 'function'; const isRegex = this.path instanceof RegExp const isRegexBasePath = this.scope.basePath instanceof RegExp @@ -370,9 +371,13 @@ module.exports = class Interceptor { if (this.scope.transformPathFunction) { path = this.scope.transformPathFunction(path) } - const comparisonKey = isRegex ? this.__nock_scopeKey : this._key + const comparisonKey = isPathFn || isRegex ? this.__nock_scopeKey : this._key const matchKey = `${method} ${proto}://${options.host}${path}` + if(isPathFn) { + return !!(matchKey.match(comparisonKey) && this.path(path)) + } + if (isRegex && !isRegexBasePath) { return !!matchKey.match(comparisonKey) && this.path.test(path) } diff --git a/tests/test_allow_unmocked.js b/tests/test_allow_unmocked.js index 496d7f3f9..74fe4b5d5 100644 --- a/tests/test_allow_unmocked.js +++ b/tests/test_allow_unmocked.js @@ -1,6 +1,7 @@ 'use strict' const http = require('http') +const { expect } = require('chai') const url = require('url') const mikealRequest = require('request') const { test } = require('tap') @@ -233,6 +234,18 @@ test('match hostname using regexp with allowUnmocked (issue-1076)', t => { }) }) +// https://github.com/nock/nock/issues/1867 +test('match path using callback with allowUnmocked', async t => { + const scope = nock('http://example.test', { allowUnmocked: true }) + .get((uri => uri.endsWith('bar'))) + .reply() + + const { statusCode } = await got('http://example.test/foo/bar') + expect(statusCode).to.equal(200) + + scope.done() +}) + // https://github.com/nock/nock/issues/835 test('match multiple paths to domain using regexp with allowUnmocked', async t => { const server = http.createServer((request, response) => { From cd24533738362173d47d080d95e0cc1d80a764b8 Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Sun, 9 Feb 2020 21:38:00 -0700 Subject: [PATCH 2/2] format --- lib/interceptor.js | 4 ++-- tests/test_allow_unmocked.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/interceptor.js b/lib/interceptor.js index 3ace0509d..0bcc17125 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -355,7 +355,7 @@ module.exports = class Interceptor { * match the provided options. */ matchOrigin(options) { - const isPathFn = typeof this.path === 'function'; + const isPathFn = typeof this.path === 'function' const isRegex = this.path instanceof RegExp const isRegexBasePath = this.scope.basePath instanceof RegExp @@ -374,7 +374,7 @@ module.exports = class Interceptor { const comparisonKey = isPathFn || isRegex ? this.__nock_scopeKey : this._key const matchKey = `${method} ${proto}://${options.host}${path}` - if(isPathFn) { + if (isPathFn) { return !!(matchKey.match(comparisonKey) && this.path(path)) } diff --git a/tests/test_allow_unmocked.js b/tests/test_allow_unmocked.js index 74fe4b5d5..967eeda9e 100644 --- a/tests/test_allow_unmocked.js +++ b/tests/test_allow_unmocked.js @@ -237,7 +237,7 @@ test('match hostname using regexp with allowUnmocked (issue-1076)', t => { // https://github.com/nock/nock/issues/1867 test('match path using callback with allowUnmocked', async t => { const scope = nock('http://example.test', { allowUnmocked: true }) - .get((uri => uri.endsWith('bar'))) + .get(uri => uri.endsWith('bar')) .reply() const { statusCode } = await got('http://example.test/foo/bar')