Browse Source

Merge branch 'mget' into mget-for-real

pull/35/head
Harish Krishna 10 years ago
parent
commit
d74a08247b
  1. 21
      sanitiser/_id.js
  2. 47
      test/unit/sanitiser/get.js

21
sanitiser/_id.js

@ -24,18 +24,33 @@ function sanitize( req ){
if('string' !== typeof params.id || !params.id.length){
return errormessage('id');
}
// id format
if(params.id.indexOf('/') === -1) {
return errormessage('id', 'invalid: must be of the format type/id for ex: \'geoname/4163334\'');
}
req.clean.id = params.id;
var param = params.id.split('/');
var param_type = param[0];
var param_id = param[1];
// id text
if('string' !== typeof param_id || !param_id.length){
return errormessage('id');
}
// type text
if('string' !== typeof params.type || !params.type.length){
if('string' !== typeof param_type || !param_type.length){
return errormessage('type');
}
// type text must be one of the indeces
if(indeces.indexOf(params.type) == -1){
if(indeces.indexOf(param_type) == -1){
return errormessage('type', 'type must be one of these values - [' + indeces.join(", ") + ']');
}
req.clean.type = params.type;
req.clean.id = param_id;
req.clean.type = param_type;
return { 'error': false };

47
test/unit/sanitiser/get.js

@ -5,6 +5,7 @@ var get = require('../../../sanitiser/get'),
indeces = require('../../../query/indeces'),
defaultIdError = 'invalid param \'id\': text length, must be >0',
defaultTypeError = 'invalid param \'type\': text length, must be >0',
defaultFormatError = 'invalid: must be of the format type/id for ex: \'geoname/4163334\'',
defaultError = defaultIdError,
defaultMissingTypeError = 'type must be one of these values - [' + indeces.join(", ") + ']',
defaultClean = { id: '123', type: 'geoname' },
@ -28,31 +29,34 @@ module.exports.tests.interface = function(test, common) {
module.exports.tests.sanitize_id_and_type = function(test, common) {
var inputs = {
valid: [
{id:'1', type:'geoname'},
{id:'2', type:'osmnode'},
{id:'3', type:'geoname'},
{id:'4', type:'osmway'},
{id:'5', type:'admin0'}
'geoname/1',
'osmnode/2',
'admin0/53',
'osmway/44',
'geoname/5'
],
invalid: [
{id:undefined, type:undefined},
{id:null, type:null},
{id:'', type:''},
{id:'4', type:''},
{id:'5', type:'gibberish'}
'/',
'',
'//',
'geoname/',
'/234',
'gibberish/23'
]
};
test('invalid id and/or type', function(t) {
test('invalid input', function(t) {
inputs.invalid.forEach( function( input ){
sanitize({ id: input.id, type: input.type }, function( err, clean ){
sanitize({ id: input }, function( err, clean ){
switch (err) {
case defaultIdError:
t.equal(err, defaultIdError, input.id + ' is invalid (missing id)'); break;
t.equal(err, defaultIdError, input + ' is invalid (missing id)'); break;
case defaultTypeError:
t.equal(err, defaultTypeError, input.type + ' is invalid (missing type)'); break;
t.equal(err, defaultTypeError, input + ' is invalid (missing type)'); break;
case defaultFormatError:
t.equal(err, defaultFormatError, input + ' is invalid (invalid format)'); break;
case defaultMissingTypeError:
t.equal(err, defaultMissingTypeError, input.type + ' is an unknown type'); break;
t.equal(err, defaultMissingTypeError, input + ' is an unknown type'); break;
default: break;
}
t.equal(clean, undefined, 'clean not set');
@ -61,12 +65,13 @@ module.exports.tests.sanitize_id_and_type = function(test, common) {
t.end();
});
test('valid id and/or type', function(t) {
test('valid input', function(t) {
inputs.valid.forEach( function( input ){
var expected = { id: input.id, type: input.type };
sanitize({ id: input.id, type: input.type, }, function( err, clean ){
t.equal(err, undefined, 'no error (' + input.id + ', ' + input.type + ')' );
t.deepEqual(clean, expected, 'clean set correctly (' + input.id + ', ' + input.type + ')');
var input_parts = input.split('/');
var expected = { id: input_parts[1], type: input_parts[0] };
sanitize({ id: input }, function( err, clean ){
t.equal(err, undefined, 'no error (' + input + ')' );
t.deepEqual(clean, expected, 'clean set correctly (' + input + ')');
});
});
t.end();
@ -97,7 +102,7 @@ module.exports.tests.middleware_failure = function(test, common) {
module.exports.tests.middleware_success = function(test, common) {
test('middleware success', function(t) {
var req = { query: { id: '123', type: 'geoname' }};
var req = { query: { id: 'geoname/123' }};
var next = function( message ){
t.equal(message, undefined, 'no error message set');
t.deepEqual(req.clean, defaultClean);

Loading…
Cancel
Save