Skip to content

Commit 4445d5d

Browse files
committed
Merge pull request #510 from RedCattleWealth/master
query accept a function to determine if the query is matched
2 parents 11b4b45 + cd7cd1a commit 4445d5d

4 files changed

Lines changed: 23849 additions & 23636 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,20 @@ nock('http://example.com')
230230
.reply(200, {results: [{id: 'pgte'}]});
231231
```
232232

233+
Nock supports passing a function to query. The function determines if the actual query matches or not.
234+
235+
```js
236+
nock('http://example.com')
237+
.get('/users')
238+
.query(function(actualQueryObject){
239+
// do some compare with the actual Query Object
240+
// return true for matched
241+
// return false for not matched
242+
return true;
243+
})
244+
.reply(200, {results: [{id: 'pgte'}]});
245+
```
246+
233247
To mock the entire url regardless of the passed query string:
234248

235249
```js

lib/interceptor.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -263,35 +263,40 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
263263

264264
// Only check for query string matches if this.queries is an object
265265
if (_.isObject(this.queries)) {
266-
// Make sure that you have an equal number of keys. We are
267-
// looping through the passed query params and not the expected values
268-
// if the user passes fewer query params than expected but all values
269-
// match this will throw a false positive. Testing that the length of the
270-
// passed query params is equal to the length of expected keys will prevent
271-
// us from doing any value checking BEFORE we know if they have all the proper
272-
// params
273-
debug('this.queries: %j', this.queries);
274-
debug('queries: %j', queries);
275-
if (_.size(this.queries) !== _.size(queries)) {
276-
matchQueries = false;
277-
} else {
278-
var self = this;
279-
_.forOwn(queries, function matchOneKeyVal(val, key) {
280-
var expVal = self.queries[key];
281-
var isMatch = true;
282-
if (val === undefined || expVal === undefined) {
266+
267+
if(_.isFunction(this.queries)){
268+
matchQueries = this.queries(queries);
269+
}else {
270+
// Make sure that you have an equal number of keys. We are
271+
// looping through the passed query params and not the expected values
272+
// if the user passes fewer query params than expected but all values
273+
// match this will throw a false positive. Testing that the length of the
274+
// passed query params is equal to the length of expected keys will prevent
275+
// us from doing any value checking BEFORE we know if they have all the proper
276+
// params
277+
debug('this.queries: %j', this.queries);
278+
debug('queries: %j', queries);
279+
if (_.size(this.queries) !== _.size(queries)) {
280+
matchQueries = false;
281+
} else {
282+
var self = this;
283+
_.forOwn(queries, function matchOneKeyVal(val, key) {
284+
var expVal = self.queries[key];
285+
var isMatch = true;
286+
if (val === undefined || expVal === undefined) {
283287
isMatch = false;
284288
} else if (expVal instanceof RegExp) {
285-
isMatch = common.matchStringOrRegexp(val, expVal);
289+
isMatch = common.matchStringOrRegexp(val, expVal);
286290
} else if (_.isArray(expVal) || _.isObject(expVal)) {
287-
isMatch = _.isEqual(val, expVal);
291+
isMatch = _.isEqual(val, expVal);
288292
} else {
289-
isMatch = common.matchStringOrRegexp(val, expVal);
293+
isMatch = common.matchStringOrRegexp(val, expVal);
290294
}
291-
matchQueries = matchQueries && !!isMatch;
295+
matchQueries = matchQueries && !!isMatch;
292296
});
297+
}
298+
debug('matchQueries: %j', matchQueries);
293299
}
294-
debug('matchQueries: %j', matchQueries);
295300
}
296301

297302
// Remove the query string from the path
@@ -397,6 +402,11 @@ Interceptor.prototype.query = function query(queries) {
397402
this.queries = queries;
398403
}
399404

405+
if(_.isFunction(queries)){
406+
this.queries = queries;
407+
return this;
408+
}
409+
400410
for (var q in queries) {
401411
if (_.isUndefined(this.queries[q])) {
402412
var value = queries[q];

0 commit comments

Comments
 (0)