Skip to content

Commit

Permalink
Fail if interceptor has query parameters but not the query
Browse files Browse the repository at this point in the history
  • Loading branch information
yodeyer committed Feb 12, 2017
1 parent 0fb8c3d commit b9d2823
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/interceptor.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
var queryString;
var queries;

if (this.queries && (queryIndex = path.indexOf('?')) !== -1) {
queryString = path.slice(queryIndex + 1);
if (this.queries) {
queryIndex = path.indexOf('?');
queryString = (queryIndex !== -1) ? path.slice(queryIndex + 1) : '';
queries = qs.parse(queryString);

// Only check for query string matches if this.queries is an object
Expand Down Expand Up @@ -299,7 +300,9 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
}

// Remove the query string from the path
path = path.substr(0, queryIndex);
if(queryIndex !== -1) {
path = path.substr(0, queryIndex);
}
}

if (typeof this.uri === 'function') {
Expand Down
18 changes: 18 additions & 0 deletions tests/test_intercept.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4772,6 +4772,24 @@ test('query() matches query values regardless of their type of declaration', fun
})
});

test('query() doesn\'t match query values of requests without query string', function (t) {
var scope1 = nock('http://google.com')
.get('/')
.query({num:1,bool:true,empty:null,str:'fou'})
.reply(200, 'scope1');

var scope2 = nock('http://google.com')
.get('/')
.reply(200, 'scope2');

mikealRequest('http://google.com/', function(err, res) {
if (err) throw err;
t.equal(res.statusCode, 200);
t.equal(res.body, 'scope2');
t.end();
})
});

test('query() matches a query string using regexp', function (t) {
var scope = nock('http://google.com')
.get('/')
Expand Down

0 comments on commit b9d2823

Please sign in to comment.