Skip to content

Commit

Permalink
Update precedence of route param, query and body in req.param and req…
Browse files Browse the repository at this point in the history
….allParams()

Also add tests
  • Loading branch information
sgress454 committed Nov 1, 2016
1 parent 5806f3c commit 820d1eb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/hooks/request/param.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@
module.exports = function _mixinReqParam(req, res) {

req.param = function(param) {
return req.params[param] || req.query[param] || (req.body && req.body[param]);
// If the param exists as a route param, use it.
if (typeof req.params[param] !== 'undefined') {
return req.params[param];
}
// If the param exists as a body param, use it.
if (req.body && typeof req.body[param] !== 'undefined') {
return req.body[param];
}
// Return the query param, if it exists.
return req.query[param];
};

};
};
4 changes: 2 additions & 2 deletions lib/hooks/request/params.all.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ module.exports = function _mixinReqParamsAll(req, res) {
var queryParams = _.cloneDeep(req.query) || {};
var bodyParams = _.cloneDeep(req.body) || {};
var allParams = {};
defaultsDeep(allParams, queryParams);
defaultsDeep(allParams, bodyParams);
defaultsDeep(allParams, queryParams);


// Mixin route params, as long as they have defined values
_.each(Object.keys(req.params), function(paramName) {
if (allParams[paramName] || !_.isUndefined(req.params[paramName])) {
allParams[paramName] = allParams[paramName] || req.params[paramName];
allParams[paramName] = !_.isUndefined(req.params[paramName]) ? req.params[paramName] : allParams[paramName];
}
});
return allParams;
Expand Down
63 changes: 63 additions & 0 deletions test/integration/router.params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,69 @@ describe('router :: ', function() {

});

describe('"touch" param (with no value)', function() {

before(function(){
require('fs').writeFileSync('config/routes.js', 'module.exports.routes = {"/testTouch": function(req,res){res.send(typeof req.param("touch") !== "undefined");}};');
});

it('when sent as a query param, should respond with a truthy value', function(done) {
httpHelper.testRoute('get', 'testTouch?touch', function(err, response) {
if (err) { return done(err); }
assert(response.body==='true', Err.badResponse(response));
done();
});

});

});

describe('req.param() precedence', function() {

before(function(){
require('fs').writeFileSync('config/routes.js', 'module.exports.routes = {"/test/:foo": function(req,res){res.json(req.param("foo"));}, "/test": function(req,res){res.json(req.param("foo"));}};');
});

it('when sent a value is specified in the query, body and route, route param should take precedence', function(done) {
httpHelper.testRoute('post', {url: 'test/abc?foo=123', json: {foo: 666}}, function(err, response) {
if (err) { return done(err); }
assert(response.body==='abc', Err.badResponse(response));
done();
});
});

it('when sent a value is specified in the query and body, body should take precedence', function(done) {
httpHelper.testRoute('post', {url: 'test?foo=123', json: {foo: 666}}, function(err, response) {
if (err) { return done(err); }
assert(response.body===666, Err.badResponse(response));
done();
});
});

});

describe('req.params.allParams', function() {

before(function(){
require('fs').writeFileSync('config/routes.js', 'module.exports.routes = {"/testParams/:foo": function(req,res){res.json(req.params.all());}};');
});

it('should return the correct param values, accounting for precedence', function(done) {
httpHelper.testRoute('post', {url: 'testParams/abc?foo=123&baz=999&bar=555&touch', json: {bar: 666}}, function(err, response) {
if (err) { return done(err); }
assert.equal(response.body.foo, 'abc');
assert.equal(response.body.bar, 666);
assert.equal(response.body.baz, 999);
assert.equal(response.body.touch, '');
done();
});

});

});



});

});

0 comments on commit 820d1eb

Please sign in to comment.