Skip to content

Commit

Permalink
fix: allowMocked when using a callback for the path
Browse files Browse the repository at this point in the history
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: nock#1867
  • Loading branch information
mastermatt committed Feb 7, 2020
1 parent 921eacf commit a9d6dfa
Show file tree
Hide file tree
Showing 3 changed files with 3,336 additions and 2,882 deletions.
7 changes: 6 additions & 1 deletion lib/interceptor.js
Expand Up @@ -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

Expand All @@ -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)
}
Expand Down

0 comments on commit a9d6dfa

Please sign in to comment.