Skip to content

Commit

Permalink
Merge pull request #35 from kmudrick/support-query
Browse files Browse the repository at this point in the history
Support capturing query params
  • Loading branch information
tbranyen authored Sep 21, 2016
2 parents 244d890 + e1b0c9e commit 487a488
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
57 changes: 55 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ function mock(superagent) {
return this;
};

// Patch Request.query()
var oldQuery = originalMethods.query = reqProto.query;
reqProto.query = function(objectOrString) {
var state = this._superagentMockerState;
if (!state || !state.current) {
return oldQuery.call(this, objectOrString);
}
var obj = {};
if (isString(objectOrString)) {
obj = parseQueryString(objectOrString);
}
else if (isObject(objectOrString)) {
obj = stringifyValues(objectOrString);
}
state.request.query = mergeObjects(state.request.query, obj);
return this;
}

return mock; // chaining

}
Expand Down Expand Up @@ -193,7 +211,8 @@ function patch(superagent, prop, method) {
current: current,
request: {
headers: {},
body: {}
body: {},
query: {}
},
};
return orig;
Expand Down Expand Up @@ -231,7 +250,8 @@ Route.prototype.match = function(method, url, body) {
url: url,
params: params || {},
body: mergeObjects(body, req.body),
headers: req.headers
headers: req.headers,
query: req.query
});
return mergeObjects({
status: 200
Expand All @@ -253,6 +273,15 @@ function isObject(obj) {
return null != obj && 'object' == typeof obj;
}

/**
* Simple string test
* @param any val Variable to test
* @return bool True if variable is a string
*/
function isString(val) {
return 'string' === typeof val;
}

/**
* Exec function and return value, or just return arg
* @param {fn|any} val Value or fn to exec
Expand All @@ -263,6 +292,30 @@ function value(val) {
: val;
}

/**
* Parses a query string like "foo=bar&baz=bat" into objects like
* { foo: 'bar', baz: 'bat' }
* @param s string
*/
function parseQueryString(s) {
return s.split('&').reduce(function (obj, param) {
var parts = param.split('=');
var key = parts.shift();
var val = parts.shift();
if (key && val) {
obj[key] = val;
}
return obj;
}, {});
}

function stringifyValues(oldObj) {
return Object.keys(oldObj).reduce(function(obj, key) {
obj[key] = String(oldObj[key]);
return obj;
}, {});
}

/**
* Object.assign replacement
* This will always create a new object which has all of the own
Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('superagent mock', function() {
it('should work correct with unmocked requests', function(done) {
request
.get('http://example.com')
.query({ foo: 'bar' })
.end(function(err, res) {
done(err);
});
Expand Down Expand Up @@ -288,6 +289,31 @@ describe('superagent mock', function() {
;
});

it('should parse parameters from query()', function(done) {
mock.get('/topics/:id', function(req) {
return req;
});
request
.get('/topics/5')
.query('hello=world')
.query('xx=yy&zz=0')
.query({ test: 'yay' })
.query({ foo: 'bar', baz: 'bat' })
.end(function(_, data) {
data.should.have.property('query');
should.deepEqual(data.query, {
hello: 'world',
xx: 'yy',
zz: '0',
test: 'yay',
foo: 'bar',
baz: 'bat'
});
done();
})
;
});

it('should remove patches by unmock()', function() {
mock.unmock(request);
(request._patchedBySuperagentMocker === void 0).should.be.true;
Expand Down

0 comments on commit 487a488

Please sign in to comment.