Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support capturing query params #35

Merged
merged 3 commits into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ function mock(superagent) {
try {
var response = current(request);
if (response.status !== 200) {
cb && cb(response, null);
// superagent puts status and response on the error it returns,
// which should be an actual instance of Error
// See http://visionmedia.github.io/superagent/#error-handling
var error = new Error(response.status);
error.status = response.status;
error.response = response;
cb && cb(error, null);
} else {
cb && cb(null, response);
}
Expand Down Expand Up @@ -134,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 @@ -187,7 +211,8 @@ function patch(superagent, prop, method) {
current: current,
request: {
headers: {},
body: {}
body: {},
query: {}
},
};
return orig;
Expand Down Expand Up @@ -225,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 @@ -247,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 @@ -257,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
28 changes: 28 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 @@ -208,6 +209,8 @@ describe('superagent mock', function() {
request.get('/topics/1')
.end(function(err, data) {
err.should.have.property('status', 500);
err.should.have.property('response');
should.deepEqual(err.response, {body: {}, status: 500});
done();
});
});
Expand Down Expand Up @@ -286,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