Skip to content

Commit

Permalink
fix(predicate): request params with a key but no value should be cons…
Browse files Browse the repository at this point in the history
…idered unset
  • Loading branch information
missinglink committed Jul 11, 2024
1 parent 24d3306 commit 1715f8b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions controller/predicates/hasRequestParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const Debug = require('../../helper/debug');
const debugLog = new Debug('controller:predicates:has_request_parameter');
const stackTraceLine = require('../../helper/stackTraceLine');

// returns true IFF req.clean has a key with the supplied name
// returns true IFF req.clean has a key with the supplied name AND a non-empty value
module.exports = (parameter) => (req, res) => {
const has_request_parameter = _.has(req, ['clean', parameter]);
const value = _.get(req, ['clean', parameter]);
const has_request_parameter = _.isNumber(value) || !_.isEmpty(value);

debugLog.push(req, () => ({
reply: {[parameter]: has_request_parameter},
stack_trace: stackTraceLine()
}));

return has_request_parameter;
};
21 changes: 20 additions & 1 deletion test/unit/controller/predicates/hasRequestParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports.tests.interface = (test, common) => {

module.exports.tests.true_conditions = (test, common) => {
test('request with specified parameter should return true', t => {
[[], {}, 'string value', 17].forEach(val => {
[['foo'], {foo: 'bar'}, 'string value', 0, 17].forEach(val => {
const req = {
clean: {
'parameter name': val
Expand All @@ -29,6 +29,25 @@ module.exports.tests.true_conditions = (test, common) => {

};

module.exports.tests.empty_values = (test, common) => {
test('request with specified parameter, but empty value, should true', t => {
[[], {}, '', null, undefined].forEach(val => {
const req = {
clean: {
'parameter name': val
}
};

t.notOk(hasRequestParameter('parameter name')(req));

});

t.end();

});

};

module.exports.tests.false_conditions = (test, common) => {
test('request with undefined clean should return false', t => {
const req = {};
Expand Down

0 comments on commit 1715f8b

Please sign in to comment.