diff --git a/app.js b/app.js index a951f06b..842cb385 100644 --- a/app.js +++ b/app.js @@ -14,7 +14,7 @@ app.use( require('./middleware/jsonp') ); app.get( '/', require('./controller/index')() ); // suggest API -app.get( '/suggest', require('./sanitiser/suggest'), require('./controller/suggest')() ); +app.get( '/suggest', require('./sanitiser/suggest').middleware, require('./controller/suggest')() ); /** ----------------------- error middleware ----------------------- **/ diff --git a/sanitiser/suggest.js b/sanitiser/suggest.js index d7dfa60a..a2112965 100644 --- a/sanitiser/suggest.js +++ b/sanitiser/suggest.js @@ -68,8 +68,11 @@ function sanitize( params, cb ){ } +// export function +module.exports = sanitize; + // middleware -module.exports = function( req, res, next ){ +module.exports.middleware = function( req, res, next ){ sanitize( req.query, function( err, clean ){ if( err ){ res.status(400); // 400 Bad Request diff --git a/test/unit/run.js b/test/unit/run.js index 50bfccfd..ffc77c68 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -4,7 +4,8 @@ var common = {}; var tests = [ require('./controller/index'), - require('./controller/suggest') + require('./controller/suggest'), + require('./sanitiser/suggest') ]; tests.map(function(t) { diff --git a/test/unit/sanitiser/suggest.js b/test/unit/sanitiser/suggest.js new file mode 100644 index 00000000..6c76a605 --- /dev/null +++ b/test/unit/sanitiser/suggest.js @@ -0,0 +1,57 @@ + +var sanitize = require('../../../sanitiser/suggest'); + +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 sanitize.middleware, 'function', 'middleware is a function'); + t.equal(sanitize.middleware.length, 3, 'sanitize is valid middleware'); + 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 ){ + t.equal(message,'invalid param \'input\': text length, must be >0'); + t.end(); + }; + sanitize.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 ){ + t.equal(message, undefined, 'no error message set'); + t.deepEqual(req.clean, { + input: 'test', lat: 0, lon: 0, + layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood' ], + size: 10, zoom: 10 + }); + t.end(); + }; + sanitize.middleware( req, undefined, next ); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('SANTIZE /suggest ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file