mirror of https://github.com/pelias/api.git
Harish Krishna
10 years ago
5 changed files with 124 additions and 0 deletions
@ -0,0 +1,30 @@
|
||||
|
||||
var logger = require('../src/logger'), |
||||
_sanitize = require('../sanitiser/_sanitize'), |
||||
sanitizers = { |
||||
input: require('../sanitiser/_input'), |
||||
size: require('../sanitiser/_size'), |
||||
layers: function( req ) { |
||||
req.query.layers = 'admin'; |
||||
var layers = require('../sanitiser/_layers'); |
||||
return layers(req); |
||||
}, |
||||
latlonzoom: require('../sanitiser/_geo') |
||||
}; |
||||
|
||||
var sanitize = function(req, cb) { _sanitize(req, sanitizers, cb); }; |
||||
|
||||
// export sanitize for testing
|
||||
module.exports.sanitize = sanitize; |
||||
|
||||
// middleware
|
||||
module.exports.middleware = function( req, res, next ){ |
||||
sanitize( req, function( err, clean ){ |
||||
if( err ){ |
||||
res.status(400); // 400 Bad Request
|
||||
return next(err); |
||||
} |
||||
req.clean = clean; |
||||
next(); |
||||
}); |
||||
}; |
@ -0,0 +1,76 @@
|
||||
|
||||
var coarse = require('../../../sanitiser/coarse'), |
||||
_sanitize = coarse.sanitize, |
||||
middleware = coarse.middleware, |
||||
valid_layers = [ 'admin0', 'admin1', 'admin2', 'neighborhood' ], |
||||
sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.interface = function(test, common) { |
||||
test('sanitize interface', function(t) { |
||||
t.equal(typeof sanitize, 'function', 'sanitize is a function'); |
||||
t.equal(sanitize.length, 2, 'sanitize interface'); |
||||
t.end(); |
||||
}); |
||||
test('middleware interface', function(t) { |
||||
t.equal(typeof middleware, 'function', 'middleware is a function'); |
||||
t.equal(middleware.length, 3, 'sanitizee has a valid middleware'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.layers = function(test, common) { |
||||
test('valid layers', function(t) { |
||||
sanitize({ input: 'test', lat: 0, lon: 0 }, function( err, clean ){ |
||||
t.equal(err, undefined, 'no error'); |
||||
t.deepEqual(clean.layers, valid_layers, 'layers set correctly'); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.middleware_failure = function(test, common) { |
||||
test('middleware failure', function(t) { |
||||
var res = { status: function( code ){ |
||||
t.equal(code, 400, 'status set'); |
||||
}}; |
||||
var next = function( message ){ |
||||
var defaultError = 'invalid param \'input\': text length, must be >0'; |
||||
t.equal(message, defaultError); |
||||
t.end(); |
||||
}; |
||||
middleware( {}, res, next ); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.middleware_success = function(test, common) { |
||||
test('middleware success', function(t) { |
||||
var req = { query: { input: 'test', lat: 0, lon: 0 }}; |
||||
var next = function( message ){ |
||||
var defaultClean = {
|
||||
input: 'test', |
||||
size: 10, |
||||
layers: [ 'admin0', 'admin1', 'admin2', 'neighborhood' ], |
||||
lat: 0, |
||||
lon: 0, |
||||
zoom: 10
|
||||
}; |
||||
t.equal(message, undefined, 'no error message set'); |
||||
t.deepEqual(req.clean, defaultClean); |
||||
t.end(); |
||||
}; |
||||
middleware( req, undefined, next ); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE /coarse ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue