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

request params with a key but no value should be considered unset #1676

Merged
merged 1 commit into from
Jul 16, 2024
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
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