Skip to content

Commit

Permalink
fix(#1076): allowUnmocked: true + host regex (#1179)
Browse files Browse the repository at this point in the history
  • Loading branch information
deeptiagrawa authored and gr2m committed Jul 31, 2018
1 parent 8f0912a commit 907be86
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/interceptor.js
Expand Up @@ -336,6 +336,7 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {

Interceptor.prototype.matchIndependentOfBody = function matchIndependentOfBody(options) {
var isRegex = _.isRegExp(this.path);
var isRegexBasePath = _.isRegExp(this.scope.basePath);

var method = (options.method || 'GET').toUpperCase()
, path = options.path
Expand All @@ -362,10 +363,14 @@ Interceptor.prototype.matchIndependentOfBody = function matchIndependentOfBody(o
var comparisonKey = isRegex ? this.__nock_scopeKey : this._key;
var matchKey = method + ' ' + proto + '://' + options.host + path;

if (isRegex) {
if (isRegex && !isRegexBasePath) {
return !!matchKey.match(comparisonKey) && !!path.match(this.path);
}

if(isRegexBasePath) {
return !!matchKey.match(this.scope.basePath) && !!path.match(this.path);
}

return comparisonKey === matchKey;
};

Expand Down
13 changes: 13 additions & 0 deletions tests/test_intercept.js
Expand Up @@ -4716,6 +4716,19 @@ test('match path using regexp with allowUnmocked', function (t) {
});
});

test('match hostname using regexp with allowUnmocked (issue-1076)', function (t) {
nock(/localhost/, {allowUnmocked: true})
.get('/no/regex/here')
.reply(200, 'Match regex');

mikealRequest.get('http://localhost:3000/no/regex/here', function(err, res, body) {
t.type(err, 'null');
t.equal(res.statusCode, 200);
t.equal(body, 'Match regex');
t.end();
});
});

test('match path using function', function (t) {
var path = '/match/uri/function';
var options = {
Expand Down

0 comments on commit 907be86

Please sign in to comment.