Skip to content

Commit

Permalink
Merge pull request #510 from RedCattleWealth/master
Browse files Browse the repository at this point in the history
query accept a function to determine if the query is matched
  • Loading branch information
pgte committed Mar 25, 2016
2 parents 11b4b45 + cd7cd1a commit 4445d5d
Show file tree
Hide file tree
Showing 4 changed files with 23,849 additions and 23,636 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -230,6 +230,20 @@ nock('http://example.com')
.reply(200, {results: [{id: 'pgte'}]});
```

Nock supports passing a function to query. The function determines if the actual query matches or not.

```js
nock('http://example.com')
.get('/users')
.query(function(actualQueryObject){
// do some compare with the actual Query Object
// return true for matched
// return false for not matched
return true;
})
.reply(200, {results: [{id: 'pgte'}]});
```

To mock the entire url regardless of the passed query string:

```js
Expand Down
54 changes: 32 additions & 22 deletions lib/interceptor.js
Expand Up @@ -263,35 +263,40 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {

// Only check for query string matches if this.queries is an object
if (_.isObject(this.queries)) {
// Make sure that you have an equal number of keys. We are
// looping through the passed query params and not the expected values
// if the user passes fewer query params than expected but all values
// match this will throw a false positive. Testing that the length of the
// passed query params is equal to the length of expected keys will prevent
// us from doing any value checking BEFORE we know if they have all the proper
// params
debug('this.queries: %j', this.queries);
debug('queries: %j', queries);
if (_.size(this.queries) !== _.size(queries)) {
matchQueries = false;
} else {
var self = this;
_.forOwn(queries, function matchOneKeyVal(val, key) {
var expVal = self.queries[key];
var isMatch = true;
if (val === undefined || expVal === undefined) {

if(_.isFunction(this.queries)){
matchQueries = this.queries(queries);
}else {
// Make sure that you have an equal number of keys. We are
// looping through the passed query params and not the expected values
// if the user passes fewer query params than expected but all values
// match this will throw a false positive. Testing that the length of the
// passed query params is equal to the length of expected keys will prevent
// us from doing any value checking BEFORE we know if they have all the proper
// params
debug('this.queries: %j', this.queries);
debug('queries: %j', queries);
if (_.size(this.queries) !== _.size(queries)) {
matchQueries = false;
} else {
var self = this;
_.forOwn(queries, function matchOneKeyVal(val, key) {
var expVal = self.queries[key];
var isMatch = true;
if (val === undefined || expVal === undefined) {
isMatch = false;
} else if (expVal instanceof RegExp) {
isMatch = common.matchStringOrRegexp(val, expVal);
isMatch = common.matchStringOrRegexp(val, expVal);
} else if (_.isArray(expVal) || _.isObject(expVal)) {
isMatch = _.isEqual(val, expVal);
isMatch = _.isEqual(val, expVal);
} else {
isMatch = common.matchStringOrRegexp(val, expVal);
isMatch = common.matchStringOrRegexp(val, expVal);
}
matchQueries = matchQueries && !!isMatch;
matchQueries = matchQueries && !!isMatch;
});
}
debug('matchQueries: %j', matchQueries);
}
debug('matchQueries: %j', matchQueries);
}

// Remove the query string from the path
Expand Down Expand Up @@ -397,6 +402,11 @@ Interceptor.prototype.query = function query(queries) {
this.queries = queries;
}

if(_.isFunction(queries)){
this.queries = queries;
return this;
}

for (var q in queries) {
if (_.isUndefined(this.queries[q])) {
var value = queries[q];
Expand Down

0 comments on commit 4445d5d

Please sign in to comment.