Browse Source

only initialize req.clean/errors/warnings if not initialized yet

also added tests for sanitizeAll
pull/666/head
Stephen Hess 8 years ago
parent
commit
06310bd3b7
  1. 12
      sanitiser/sanitizeAll.js
  2. 1
      test/unit/run.js
  3. 161
      test/unit/sanitiser/sanitizeAll.js

12
sanitiser/sanitizeAll.js

@ -1,12 +1,10 @@
function sanitize( req, sanitizers, cb ){
// init an object to store clean (sanitized) input parameters if not initialized
req.clean = req.clean || {};
// init an object to store clean
// (sanitized) input parameters
req.clean = {};
// init errors and warnings arrays
req.errors = [];
req.warnings = [];
// init errors and warnings arrays if not initialized
req.errors = req.errors || [];
req.warnings = req.warnings || [];
// source of input parameters
// (in this case from the GET querystring params)

1
test/unit/run.js

@ -63,6 +63,7 @@ var tests = [
require('./sanitiser/autocomplete'),
require('./sanitiser/place'),
require('./sanitiser/reverse'),
require('./sanitiser/sanitizeAll'),
require('./sanitiser/search'),
require('./sanitiser/search_fallback'),
require('./sanitiser/wrap'),

161
test/unit/sanitiser/sanitizeAll.js

@ -0,0 +1,161 @@
var sanitizeAll = require('../../../sanitiser/sanitizeAll');
module.exports.tests = {};
module.exports.tests.all = function(test, common) {
test('req.clean/errors/warnings should be initialized when they are not', function(t) {
var req = {};
var sanitizers = [
function() {
req.clean.a = 'first sanitizer';
return {
errors: ['error 1', 'error 2'],
warnings: ['warning 1', 'warning 2']
};
},
function() {
req.clean.b = 'second sanitizer';
return {
errors: ['error 3'],
warnings: ['warning 3']
};
}
];
var expected_req = {
clean: {
a: 'first sanitizer',
b: 'second sanitizer'
},
errors: ['error 1', 'error 2', 'error 3'],
warnings: ['warning 1', 'warning 2', 'warning 3']
};
sanitizeAll(req, sanitizers, function(){
t.deepEquals(req, expected_req);
t.end();
});
});
test('req.clean/errors/warnings should not be initialized when they already have been', function(t) {
var req = {
clean: {
alreadyInitialized: true
},
errors: ['pre-existing error'],
warnings: ['pre-existing warning']
};
var sanitizers = [
function() {
req.clean.a = 'first sanitizer';
return {
errors: ['error 1', 'error 2'],
warnings: ['warning 1', 'warning 2']
};
},
function() {
req.clean.b = 'second sanitizer';
return {
errors: ['error 3'],
warnings: ['warning 3']
};
}
];
var expected_req = {
clean: {
alreadyInitialized: true,
a: 'first sanitizer',
b: 'second sanitizer'
},
errors: ['pre-existing error', 'error 1', 'error 2', 'error 3'],
warnings: ['pre-existing warning', 'warning 1', 'warning 2', 'warning 3']
};
sanitizeAll(req, sanitizers, function(){
t.deepEquals(req, expected_req);
t.end();
});
});
test('req.query should be passed to individual sanitizers when available', function(t) {
var req = {
query: {
value: 'query value'
}
};
var sanitizers = [
function(params) {
req.clean.query = params;
return {
errors: [],
warnings: []
};
}
];
var expected_req = {
query: {
value: 'query value'
},
clean: {
query: {
value: 'query value'
}
},
errors: [],
warnings: []
};
sanitizeAll(req, sanitizers, function(){
t.deepEquals(req, expected_req);
t.end();
});
});
test('an empty object should be passed to individual sanitizers when req.query is unavailable', function(t) {
var req = {};
var sanitizers = [
function(params) {
if (Object.keys(params).length === 0) {
req.clean.empty_object_was_passed = true;
}
return {
errors: [],
warnings: []
};
}
];
var expected_req = {
clean: {
empty_object_was_passed: true
},
errors: [],
warnings: []
};
sanitizeAll(req, sanitizers, function(){
t.deepEquals(req, expected_req);
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANITIZE sanitizeAll ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};
Loading…
Cancel
Save