From fb6947e3fe6d1a86e3faa67fd7b30718b4840035 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 15 Oct 2014 13:57:50 -0400 Subject: [PATCH 01/13] /get endpoint - controller and sanitizer --- app.js | 5 +++++ controller/get.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ sanitiser/_id.js | 39 +++++++++++++++++++++++++++++++++++++++ sanitiser/get.js | 23 +++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 controller/get.js create mode 100644 sanitiser/_id.js create mode 100644 sanitiser/get.js diff --git a/app.js b/app.js index 987b5bcc..a80a4f82 100644 --- a/app.js +++ b/app.js @@ -11,6 +11,7 @@ app.use( require('./middleware/jsonp') ); /** ----------------------- sanitisers ----------------------- **/ var sanitisers = {}; +sanitisers.get = require('./sanitiser/get'); sanitisers.suggest = require('./sanitiser/suggest'); sanitisers.search = sanitisers.suggest; sanitisers.reverse = require('./sanitiser/reverse'); @@ -19,6 +20,7 @@ sanitisers.reverse = require('./sanitiser/reverse'); var controllers = {}; controllers.index = require('./controller/index'); +controllers.get = require('./controller/get'); controllers.suggest = require('./controller/suggest'); controllers.search = require('./controller/search'); @@ -27,6 +29,9 @@ controllers.search = require('./controller/search'); // api root app.get( '/', controllers.index() ); +// get doc API +app.get( '/get', sanitisers.get.middleware, controllers.get() ); + // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); diff --git a/controller/get.js b/controller/get.js new file mode 100644 index 00000000..57842b93 --- /dev/null +++ b/controller/get.js @@ -0,0 +1,44 @@ + +var geojsonify = require('../helper/geojsonify').search; + +function setup( backend ){ + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + + function controller( req, res, next ){ + + // backend command + var cmd = { + index: 'pelias', + type: req.clean.type, + id: req.clean.id + }; + + // query backend + backend().client.get( cmd, function( err, data ){ + + var docs = []; + // handle backend errors + if( err ){ return next( err ); } + + if( data && data.found && data._source ){ + docs.push(data._source); + } + + // convert docs to geojson + var geojson = geojsonify( docs ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); + }); + + } + + return controller; +} + +module.exports = setup; diff --git a/sanitiser/_id.js b/sanitiser/_id.js new file mode 100644 index 00000000..d2fee937 --- /dev/null +++ b/sanitiser/_id.js @@ -0,0 +1,39 @@ +// validate inputs, convert types and apply defaults +// id generally looks like 'geoname/4163334' (type/id) +// so, both type and id are required fields. + +function sanitize( req ){ + + req.clean = req.clean || {}; + var params= req.query; + + // ensure params is a valid object + if( Object.prototype.toString.call( params ) !== '[object Object]' ){ + params = {}; + } + + var errormessage = function(fieldname) { + return { + 'error': true, + 'message': 'invalid param \''+ fieldname + '\': text length, must be >0' + } + }; + + // id text + if('string' !== typeof params.id || !params.id.length){ + return errormessage('id'); + } + req.clean.id = params.id; + + // type text + if('string' !== typeof params.type || !params.type.length){ + return errormessage('type'); + } + req.clean.type = params.type; + + return { 'error': false }; + +} + +// export function +module.exports = sanitize; \ No newline at end of file diff --git a/sanitiser/get.js b/sanitiser/get.js new file mode 100644 index 00000000..64db6edc --- /dev/null +++ b/sanitiser/get.js @@ -0,0 +1,23 @@ + +var logger = require('../src/logger'), + _sanitize = require('../sanitiser/_sanitize'), + sanitizers = { + id: require('../sanitiser/_id') + }; + +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(); + }); +}; \ No newline at end of file From 32e9a13d64ce05d3066e6d7b7138c915f5e55e7a Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 15 Oct 2014 14:12:30 -0400 Subject: [PATCH 02/13] type should be one of the indeces --- sanitiser/_id.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/sanitiser/_id.js b/sanitiser/_id.js index d2fee937..6620a280 100644 --- a/sanitiser/_id.js +++ b/sanitiser/_id.js @@ -4,21 +4,22 @@ function sanitize( req ){ - req.clean = req.clean || {}; - var params= req.query; + req.clean = req.clean || {}; + var params = req.query; + var indeces = require('../query/indeces'); // ensure params is a valid object if( Object.prototype.toString.call( params ) !== '[object Object]' ){ params = {}; } - var errormessage = function(fieldname) { + var errormessage = function(fieldname, message) { return { 'error': true, - 'message': 'invalid param \''+ fieldname + '\': text length, must be >0' + 'message': message || ('invalid param \''+ fieldname + '\': text length, must be >0') } }; - + // id text if('string' !== typeof params.id || !params.id.length){ return errormessage('id'); @@ -31,6 +32,12 @@ function sanitize( req ){ } req.clean.type = params.type; + // type text must be one of the indeces + if(indeces.indexOf(params.type) == -1){ + return errormessage('type', 'type must be one of these values - [' + indeces.join(", ") + ']'); + } + req.clean.type = params.type; + return { 'error': false }; } From 018ee3b945bfecb7c399324db1fd1d1a1edb625c Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 15 Oct 2014 14:55:58 -0400 Subject: [PATCH 03/13] tests - adding get sanitizer tests, renaming sanitiser to suggest because that is what it is really. --- test/unit/run.js | 3 +- test/unit/sanitiser/get.js | 118 ++++++++++++++++++ .../sanitiser/{sanitise.js => suggest.js} | 2 +- 3 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 test/unit/sanitiser/get.js rename test/unit/sanitiser/{sanitise.js => suggest.js} (99%) diff --git a/test/unit/run.js b/test/unit/run.js index 96e9c95a..34b5a92e 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -6,7 +6,8 @@ var tests = [ require('./controller/index'), require('./controller/suggest'), require('./controller/search'), - require('./sanitiser/sanitise'), + require('./sanitiser/suggest'), + require('./sanitiser/get'), require('./query/indeces'), require('./query/suggest'), require('./query/search'), diff --git a/test/unit/sanitiser/get.js b/test/unit/sanitiser/get.js new file mode 100644 index 00000000..d00133c3 --- /dev/null +++ b/test/unit/sanitiser/get.js @@ -0,0 +1,118 @@ + +var get = require('../../../sanitiser/get'), + _sanitize = get.sanitize, + middleware = get.middleware, + indeces = require('../../../query/indeces'), + defaultIdError = 'invalid param \'id\': text length, must be >0', + defaultTypeError = 'invalid param \'type\': text length, must be >0', + defaultError = defaultIdError, + defaultMissingTypeError = 'type must be one of these values - [' + indeces.join(", ") + ']', + defaultClean = { id: '123', type: 'geoname' }, + 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.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'} + ], + invalid: [ + {id:undefined, type:undefined}, + {id:null, type:null}, + {id:'', type:''}, + {id:'4', type:''}, + {id:'5', type:'gibberish'} + ] + }; + + inputs.invalid.forEach( function( input ){ + test('invalid id and/or type', function(t) { + sanitize({ id: input.id, type: input.type }, function( err, clean ){ + switch (err) { + case defaultIdError: + t.equal(err, defaultIdError, 'missing id'); break; + case defaultTypeError: + t.equal(err, defaultTypeError, 'missing type'); break; + case defaultMissingTypeError: + t.equal(err, defaultMissingTypeError, 'unknown type'); break; + default: break; + } + t.equal(clean, undefined, 'clean not set'); + t.end(); + }); + }); + }); + inputs.valid.forEach( function( input ){ + test('valid id and/or type', function(t) { + var expected = { id: input.id, type: input.type }; + sanitize({ id: input.id, type: input.type, }, function( err, clean ){ + t.equal(err, undefined, 'no error'); + t.deepEqual(clean, expected, 'clean set correctly'); + t.end(); + }); + }); + }); +}; + +module.exports.tests.invalid_params = function(test, common) { + test('invalid params', function(t) { + sanitize( undefined, function( err, clean ){ + t.equal(err, defaultError, 'handle invalid params gracefully'); + 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, defaultError); + t.end(); + }; + middleware( {}, res, next ); + }); +}; + +module.exports.tests.middleware_success = function(test, common) { + test('middleware success', function(t) { + var req = { query: { id: '123', type: 'geoname' }}; + var next = function( message ){ + 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 /get ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; \ No newline at end of file diff --git a/test/unit/sanitiser/sanitise.js b/test/unit/sanitiser/suggest.js similarity index 99% rename from test/unit/sanitiser/sanitise.js rename to test/unit/sanitiser/suggest.js index cae2d8b1..66055b68 100644 --- a/test/unit/sanitiser/sanitise.js +++ b/test/unit/sanitiser/suggest.js @@ -241,7 +241,7 @@ module.exports.tests.middleware_success = function(test, common) { module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE /sanitise ' + name, testFunction); + return tape('SANTIZE /suggest ' + name, testFunction); } for( var testCase in module.exports.tests ){ From ed5a1923d3adceb982e5bc750a78efc2c47d4d46 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 15 Oct 2014 15:09:49 -0400 Subject: [PATCH 04/13] grouping test cases when appropriate --- test/unit/sanitiser/get.js | 23 ++++++++-------- test/unit/sanitiser/suggest.js | 49 +++++++++++++++++----------------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/test/unit/sanitiser/get.js b/test/unit/sanitiser/get.js index d00133c3..1ed34637 100644 --- a/test/unit/sanitiser/get.js +++ b/test/unit/sanitiser/get.js @@ -43,32 +43,33 @@ module.exports.tests.sanitize_id_and_type = function(test, common) { ] }; - inputs.invalid.forEach( function( input ){ - test('invalid id and/or type', function(t) { + test('invalid id and/or type', function(t) { + inputs.invalid.forEach( function( input ){ sanitize({ id: input.id, type: input.type }, function( err, clean ){ switch (err) { case defaultIdError: - t.equal(err, defaultIdError, 'missing id'); break; + t.equal(err, defaultIdError, input.id + ' is invalid (missing id)'); break; case defaultTypeError: - t.equal(err, defaultTypeError, 'missing type'); break; + t.equal(err, defaultTypeError, input.type + ' is invalid (missing type)'); break; case defaultMissingTypeError: - t.equal(err, defaultMissingTypeError, 'unknown type'); break; + t.equal(err, defaultMissingTypeError, input.type + ' is an unknown type'); break; default: break; } t.equal(clean, undefined, 'clean not set'); - t.end(); }); }); + t.end(); }); - inputs.valid.forEach( function( input ){ - test('valid id and/or type', function(t) { + + test('valid id and/or type', 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'); - t.deepEqual(clean, expected, 'clean set correctly'); - t.end(); + t.equal(err, undefined, 'no error (' + input.id + ', ' + input.type + ')' ); + t.deepEqual(clean, expected, 'clean set correctly (' + input.id + ', ' + input.type + ')'); }); }); + t.end(); }); }; diff --git a/test/unit/sanitiser/suggest.js b/test/unit/sanitiser/suggest.js index 66055b68..52096be2 100644 --- a/test/unit/sanitiser/suggest.js +++ b/test/unit/sanitiser/suggest.js @@ -26,25 +26,25 @@ module.exports.tests.sanitize_input = function(test, common) { invalid: [ '', 100, null, undefined, new Date() ], valid: [ 'a', 'aa', 'aaaaaaaa' ] }; - inputs.invalid.forEach( function( input ){ - test('invalid input', function(t) { + test('invalid input', function(t) { + inputs.invalid.forEach( function( input ){ sanitize({ input: input, lat: 0, lon: 0 }, function( err, clean ){ - t.equal(err, 'invalid param \'input\': text length, must be >0', 'invalid input'); + t.equal(err, 'invalid param \'input\': text length, must be >0', input + ' is an invalid input'); t.equal(clean, undefined, 'clean not set'); - t.end(); }); }); + t.end(); }); - inputs.valid.forEach( function( input ){ - test('valid input', function(t) { + test('valid input', function(t) { + inputs.valid.forEach( function( input ){ sanitize({ input: input, lat: 0, lon: 0 }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); expected.input = input; t.equal(err, undefined, 'no error'); - t.deepEqual(clean, expected, 'clean set correctly'); - t.end(); + t.deepEqual(clean, expected, 'clean set correctly (' + input + ')'); }); }); + t.end(); }); }; @@ -53,25 +53,25 @@ module.exports.tests.sanitize_lat = function(test, common) { invalid: [ -181, -120, -91, 91, 120, 181 ], valid: [ 0, 45, 90, -0, '0', '45', '90' ] }; - lats.invalid.forEach( function( lat ){ - test('invalid lat', function(t) { + test('invalid lat', function(t) { + lats.invalid.forEach( function( lat ){ sanitize({ input: 'test', lat: lat, lon: 0 }, function( err, clean ){ - t.equal(err, 'invalid param \'lat\': must be >-90 and <90', 'invalid latitude'); + t.equal(err, 'invalid param \'lat\': must be >-90 and <90', lat + ' is an invalid latitude'); t.equal(clean, undefined, 'clean not set'); - t.end(); }); }); + t.end(); }); - lats.valid.forEach( function( lat ){ - test('valid lat', function(t) { + test('valid lat', function(t) { + lats.valid.forEach( function( lat ){ sanitize({ input: 'test', lat: lat, lon: 0 }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); expected.lat = parseFloat( lat ); t.equal(err, undefined, 'no error'); - t.deepEqual(clean, expected, 'clean set correctly'); - t.end(); + t.deepEqual(clean, expected, 'clean set correctly (' + lat + ')'); }); }); + t.end(); }); }; @@ -80,25 +80,26 @@ module.exports.tests.sanitize_lon = function(test, common) { invalid: [ -360, -181, 181, 360 ], valid: [ -180, -1, -0, 0, 45, 90, '-180', '0', '180' ] }; - lons.invalid.forEach( function( lon ){ - test('invalid lon', function(t) { + test('invalid lon', function(t) { + lons.invalid.forEach( function( lon ){ sanitize({ input: 'test', lat: 0, lon: lon }, function( err, clean ){ - t.equal(err, 'invalid param \'lon\': must be >-180 and <180', 'invalid longitude'); + t.equal(err, 'invalid param \'lon\': must be >-180 and <180', lon + ' is an invalid longitude'); t.equal(clean, undefined, 'clean not set'); - t.end(); + }); }); + t.end(); }); - lons.valid.forEach( function( lon ){ - test('valid lon', function(t) { + test('valid lon', function(t) { + lons.valid.forEach( function( lon ){ sanitize({ input: 'test', lat: 0, lon: lon }, function( err, clean ){ var expected = JSON.parse(JSON.stringify( defaultClean )); expected.lon = parseFloat( lon ); t.equal(err, undefined, 'no error'); - t.deepEqual(clean, expected, 'clean set correctly'); - t.end(); + t.deepEqual(clean, expected, 'clean set correctly (' + lon + ')'); }); }); + t.end(); }); }; From 516722f8dcf4a7b0d6f4ba6b0f271a91da52f726 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 15 Oct 2014 16:07:14 -0400 Subject: [PATCH 05/13] dont set type till its a valid type --- sanitiser/_id.js | 1 - 1 file changed, 1 deletion(-) diff --git a/sanitiser/_id.js b/sanitiser/_id.js index 6620a280..7c466110 100644 --- a/sanitiser/_id.js +++ b/sanitiser/_id.js @@ -30,7 +30,6 @@ function sanitize( req ){ if('string' !== typeof params.type || !params.type.length){ return errormessage('type'); } - req.clean.type = params.type; // type text must be one of the indeces if(indeces.indexOf(params.type) == -1){ From 23e6cfbef782935d52e8990a84c6c2a49d57593e Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 15 Oct 2014 17:14:54 -0400 Subject: [PATCH 06/13] mget for real - first pass (experiencing problems with the backend client) --- app.js | 3 +++ controller/mget.js | 55 +++++++++++++++++++++++++++++++++++++++++++++ sanitiser/_ids.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++ sanitiser/mget.js | 23 +++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 controller/mget.js create mode 100644 sanitiser/_ids.js create mode 100644 sanitiser/mget.js diff --git a/app.js b/app.js index a80a4f82..1c071cb1 100644 --- a/app.js +++ b/app.js @@ -12,6 +12,7 @@ app.use( require('./middleware/jsonp') ); var sanitisers = {}; sanitisers.get = require('./sanitiser/get'); +sanitisers.mget = require('./sanitiser/mget'); sanitisers.suggest = require('./sanitiser/suggest'); sanitisers.search = sanitisers.suggest; sanitisers.reverse = require('./sanitiser/reverse'); @@ -21,6 +22,7 @@ sanitisers.reverse = require('./sanitiser/reverse'); var controllers = {}; controllers.index = require('./controller/index'); controllers.get = require('./controller/get'); +controllers.mget = require('./controller/mget'); controllers.suggest = require('./controller/suggest'); controllers.search = require('./controller/search'); @@ -31,6 +33,7 @@ app.get( '/', controllers.index() ); // get doc API app.get( '/get', sanitisers.get.middleware, controllers.get() ); +app.get( '/mget', sanitisers.mget.middleware, controllers.mget() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); diff --git a/controller/mget.js b/controller/mget.js new file mode 100644 index 00000000..fadc4ede --- /dev/null +++ b/controller/mget.js @@ -0,0 +1,55 @@ + +var geojsonify = require('../helper/geojsonify').search; + +function setup( backend ){ + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + + function controller( req, res, next ){ + + // backend command + var cmd = req.clean.ids.map( function(id) { + return { + index: 'pelias', + type: id.type, + id: id.id + }; + }); + cmd = { + index: 'pelias', + type: 'geoname', + ids: cmd.map(function(c){ return c.id }) + } + console.log('cmd:') + console.log(cmd) + // query backend + backend().client.mget( cmd, function( err, data ){ + console.log('error:') + console.log(err) + var docs = []; + // handle backend errors + if( err ){ return next( err ); } + + if( data && data.docs && Array.isArray(data.docs) && data.docs.length ){ + docs = data.docs.map( function( doc ){ + return doc._source; + }); + } + + // convert docs to geojson + var geojson = geojsonify( docs ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); + }); + + } + + return controller; +} + +module.exports = setup; diff --git a/sanitiser/_ids.js b/sanitiser/_ids.js new file mode 100644 index 00000000..8354de9b --- /dev/null +++ b/sanitiser/_ids.js @@ -0,0 +1,56 @@ +// validate inputs, convert types and apply defaults +// id generally looks like 'geoname/4163334' (type/id) +// so, both type and id are required fields. + +function sanitize( req ){ + + req.clean = req.clean || {}; + var params = req.query; + var indeces = require('../query/indeces'); + + // ensure params is a valid object + if( Object.prototype.toString.call( params ) !== '[object Object]' ){ + params = {}; + } + console.log(params) + + var errormessage = function(fieldname, message) { + return { + 'error': true, + 'message': message || ('invalid param \''+ fieldname + '\': text length, must be >0') + } + }; + + if( params && params.ids && params.ids.length ){ + req.clean.ids = []; + console.log(params.ids) + params.ids.split(',').forEach( function(param) { + param = param.split('/'); + var type= param[0]; + var id = param[1]; + console.log(param) + // id text + if('string' !== typeof id || !id.length){ + return errormessage('id'); + } + // type text + if('string' !== typeof type || !type.length){ + return errormessage('type'); + } + // type text must be one of the indeces + if(indeces.indexOf(type) == -1){ + return errormessage('type', 'type must be one of these values - [' + indeces.join(", ") + ']'); + } + req.clean.ids.push({ + id: id, + type: type + }); + }); + } + console.log(req.clean) + return { 'error': false }; + +} + +// export function +module.exports = sanitize; \ No newline at end of file diff --git a/sanitiser/mget.js b/sanitiser/mget.js new file mode 100644 index 00000000..b1f6d40f --- /dev/null +++ b/sanitiser/mget.js @@ -0,0 +1,23 @@ + +var logger = require('../src/logger'), + _sanitize = require('../sanitiser/_sanitize'), + sanitizers = { + id: require('../sanitiser/_ids') + }; + +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(); + }); +}; \ No newline at end of file From 0dd495a3789b5835a8f623e696f7c1dd394e8d7b Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 15 Oct 2014 17:39:36 -0400 Subject: [PATCH 07/13] going from ?id=123&type=geoname to ?id=geoname/123 tests updated --- sanitiser/_id.js | 21 +++++++++++++--- test/unit/sanitiser/get.js | 49 +++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/sanitiser/_id.js b/sanitiser/_id.js index 7c466110..52397128 100644 --- a/sanitiser/_id.js +++ b/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 }; diff --git a/test/unit/sanitiser/get.js b/test/unit/sanitiser/get.js index 1ed34637..36e777d1 100644 --- a/test/unit/sanitiser/get.js +++ b/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'} + invalid: [ + '/', + '', + '//', + '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); From b828a05b01b80eaa42d79653d439418d444710f6 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Thu, 16 Oct 2014 16:10:25 -0400 Subject: [PATCH 08/13] simplifying things, DRY - one endpoint 'GET' handles single/multiple requests. plus test coverage --- app.js | 3 -- controller/get.js | 29 ++++++++---- controller/mget.js | 55 ---------------------- sanitiser/_id.js | 62 ++++++++++++++----------- sanitiser/_ids.js | 56 ----------------------- sanitiser/mget.js | 23 ---------- test/unit/sanitiser/get.js | 94 ++++++++++++++++++++++++-------------- 7 files changed, 114 insertions(+), 208 deletions(-) delete mode 100644 controller/mget.js delete mode 100644 sanitiser/_ids.js delete mode 100644 sanitiser/mget.js diff --git a/app.js b/app.js index 1c071cb1..a80a4f82 100644 --- a/app.js +++ b/app.js @@ -12,7 +12,6 @@ app.use( require('./middleware/jsonp') ); var sanitisers = {}; sanitisers.get = require('./sanitiser/get'); -sanitisers.mget = require('./sanitiser/mget'); sanitisers.suggest = require('./sanitiser/suggest'); sanitisers.search = sanitisers.suggest; sanitisers.reverse = require('./sanitiser/reverse'); @@ -22,7 +21,6 @@ sanitisers.reverse = require('./sanitiser/reverse'); var controllers = {}; controllers.index = require('./controller/index'); controllers.get = require('./controller/get'); -controllers.mget = require('./controller/mget'); controllers.suggest = require('./controller/suggest'); controllers.search = require('./controller/search'); @@ -33,7 +31,6 @@ app.get( '/', controllers.index() ); // get doc API app.get( '/get', sanitisers.get.middleware, controllers.get() ); -app.get( '/mget', sanitisers.mget.middleware, controllers.mget() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); diff --git a/controller/get.js b/controller/get.js index 57842b93..b48e6f64 100644 --- a/controller/get.js +++ b/controller/get.js @@ -5,25 +5,36 @@ function setup( backend ){ // allow overriding of dependencies backend = backend || require('../src/backend'); - + backend = new backend(); + function controller( req, res, next ){ + var docs = req.clean.ids.map( function(id) { + return { + _index: 'pelias', + _type: id.type, + _id: id.id + }; + }); + // backend command var cmd = { - index: 'pelias', - type: req.clean.type, - id: req.clean.id + body: { + docs: docs + } }; - // query backend - backend().client.get( cmd, function( err, data ){ - + // query new backend + backend.client.mget( cmd, function( err, data ){ + var docs = []; // handle backend errors if( err ){ return next( err ); } - if( data && data.found && data._source ){ - docs.push(data._source); + if( data && data.docs && Array.isArray(data.docs) && data.docs.length ){ + docs = data.docs.map( function( doc ){ + return doc._source; + }); } // convert docs to geojson diff --git a/controller/mget.js b/controller/mget.js deleted file mode 100644 index fadc4ede..00000000 --- a/controller/mget.js +++ /dev/null @@ -1,55 +0,0 @@ - -var geojsonify = require('../helper/geojsonify').search; - -function setup( backend ){ - - // allow overriding of dependencies - backend = backend || require('../src/backend'); - - function controller( req, res, next ){ - - // backend command - var cmd = req.clean.ids.map( function(id) { - return { - index: 'pelias', - type: id.type, - id: id.id - }; - }); - cmd = { - index: 'pelias', - type: 'geoname', - ids: cmd.map(function(c){ return c.id }) - } - console.log('cmd:') - console.log(cmd) - // query backend - backend().client.mget( cmd, function( err, data ){ - console.log('error:') - console.log(err) - var docs = []; - // handle backend errors - if( err ){ return next( err ); } - - if( data && data.docs && Array.isArray(data.docs) && data.docs.length ){ - docs = data.docs.map( function( doc ){ - return doc._source; - }); - } - - // convert docs to geojson - var geojson = geojsonify( docs ); - - // response envelope - geojson.date = new Date().getTime(); - - // respond - return res.status(200).json( geojson ); - }); - - } - - return controller; -} - -module.exports = setup; diff --git a/sanitiser/_id.js b/sanitiser/_id.js index 52397128..62860243 100644 --- a/sanitiser/_id.js +++ b/sanitiser/_id.js @@ -1,5 +1,5 @@ // validate inputs, convert types and apply defaults -// id generally looks like 'geoname/4163334' (type/id) +// id generally looks like 'geoname:4163334' (type:id) // so, both type and id are required fields. function sanitize( req ){ @@ -7,6 +7,7 @@ function sanitize( req ){ req.clean = req.clean || {}; var params = req.query; var indeces = require('../query/indeces'); + var delim = ':'; // ensure params is a valid object if( Object.prototype.toString.call( params ) !== '[object Object]' ){ @@ -20,38 +21,45 @@ function sanitize( req ){ } }; - // id text - if('string' !== typeof params.id || !params.id.length){ + if(('string' === typeof params.id && !params.id.length) || params.id === undefined){ 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; + if( params && params.id && params.id.length ){ + req.clean.ids = []; + params.ids = Array.isArray(params.id) ? params.id : [params.id]; + + for (var i=0; i0') - } - }; - - if( params && params.ids && params.ids.length ){ - req.clean.ids = []; - console.log(params.ids) - params.ids.split(',').forEach( function(param) { - param = param.split('/'); - var type= param[0]; - var id = param[1]; - console.log(param) - // id text - if('string' !== typeof id || !id.length){ - return errormessage('id'); - } - // type text - if('string' !== typeof type || !type.length){ - return errormessage('type'); - } - // type text must be one of the indeces - if(indeces.indexOf(type) == -1){ - return errormessage('type', 'type must be one of these values - [' + indeces.join(", ") + ']'); - } - req.clean.ids.push({ - id: id, - type: type - }); - }); - } - console.log(req.clean) - return { 'error': false }; - -} - -// export function -module.exports = sanitize; \ No newline at end of file diff --git a/sanitiser/mget.js b/sanitiser/mget.js deleted file mode 100644 index b1f6d40f..00000000 --- a/sanitiser/mget.js +++ /dev/null @@ -1,23 +0,0 @@ - -var logger = require('../src/logger'), - _sanitize = require('../sanitiser/_sanitize'), - sanitizers = { - id: require('../sanitiser/_ids') - }; - -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(); - }); -}; \ No newline at end of file diff --git a/test/unit/sanitiser/get.js b/test/unit/sanitiser/get.js index 36e777d1..3de73397 100644 --- a/test/unit/sanitiser/get.js +++ b/test/unit/sanitiser/get.js @@ -3,13 +3,19 @@ var get = require('../../../sanitiser/get'), _sanitize = get.sanitize, middleware = get.middleware, 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' }, - sanitize = function(query, cb) { _sanitize({'query':query}, cb); } + delimiter = ':', + defaultLengthError = function(input) { return 'invalid param \''+ input + '\': text length, must be >0' }, + defaultFormatError = 'invalid: must be of the format type:id for ex: \'geoname:4163334\'', + defaultError = 'invalid param \'id\': text length, must be >0', + defaultMissingTypeError = function(input) { + var type = input.split(delimiter)[0]; + return type + ' is invalid. It must be one of these values - [' + indeces.join(", ") + ']'}, + defaultClean = { ids: [ { id: '123', type: 'geoname' } ] }, + sanitize = function(query, cb) { _sanitize({'query':query}, cb); }, + inputs = { + valid: [ 'geoname:1', 'osmnode:2', 'admin0:53', 'osmway:44', 'geoname:5' ], + invalid: [ ':', '', '::', 'geoname:', ':234', 'gibberish:23' ] + }; module.exports.tests = {}; @@ -26,37 +32,19 @@ module.exports.tests.interface = function(test, common) { }); }; -module.exports.tests.sanitize_id_and_type = function(test, common) { - var inputs = { - valid: [ - 'geoname/1', - 'osmnode/2', - 'admin0/53', - 'osmway/44', - 'geoname/5' - ], - invalid: [ - '/', - '', - '//', - 'geoname/', - '/234', - 'gibberish/23' - ] - }; - +module.exports.tests.sanitize_id = function(test, common) { test('invalid input', function(t) { inputs.invalid.forEach( function( input ){ sanitize({ id: input }, function( err, clean ){ switch (err) { - case defaultIdError: - t.equal(err, defaultIdError, input + ' is invalid (missing id)'); break; - case defaultTypeError: - t.equal(err, defaultTypeError, input + ' is invalid (missing type)'); break; + case defaultError: + t.equal(err, defaultError, input + ' is invalid input'); break; + case defaultLengthError(input): + t.equal(err, defaultLengthError(input), input + ' is invalid (missing id/type)'); break; case defaultFormatError: t.equal(err, defaultFormatError, input + ' is invalid (invalid format)'); break; - case defaultMissingTypeError: - t.equal(err, defaultMissingTypeError, input + ' is an unknown type'); break; + case defaultMissingTypeError(input): + t.equal(err, defaultMissingTypeError(input), input + ' is an unknown type'); break; default: break; } t.equal(clean, undefined, 'clean not set'); @@ -67,8 +55,8 @@ module.exports.tests.sanitize_id_and_type = function(test, common) { test('valid input', function(t) { inputs.valid.forEach( function( input ){ - var input_parts = input.split('/'); - var expected = { id: input_parts[1], type: input_parts[0] }; + var input_parts = input.split(delimiter); + var expected = { ids: [ { 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 + ')'); @@ -78,6 +66,42 @@ module.exports.tests.sanitize_id_and_type = function(test, common) { }); }; + +module.exports.tests.sanitize_ids = function(test, common) { + test('invalid input', function(t) { + sanitize({ id: inputs.invalid }, function( err, clean ){ + var input = inputs.invalid[0]; // since it breaks on the first invalid element + switch (err) { + case defaultError: + t.equal(err, defaultError, input + ' is invalid input'); break; + case defaultLengthError(input): + t.equal(err, defaultLengthError(input), input + ' is invalid (missing id/type)'); break; + case defaultFormatError: + t.equal(err, defaultFormatError, input + ' is invalid (invalid format)'); break; + case defaultMissingTypeError(input): + t.equal(err, defaultMissingTypeError(input), input + ' is an unknown type'); break; + default: break; + } + t.equal(clean, undefined, 'clean not set'); + }); + t.end(); + }); + + test('valid input', function(t) { + var expected={}; + expected.ids = []; + inputs.valid.forEach( function( input ){ + var input_parts = input.split(delimiter); + expected.ids.push({ id: input_parts[1], type: input_parts[0] }); + }); + sanitize({ id: inputs.valid }, function( err, clean ){ + t.equal(err, undefined, 'no error' ); + t.deepEqual(clean, expected, 'clean set correctly'); + }); + t.end(); + }); +}; + module.exports.tests.invalid_params = function(test, common) { test('invalid params', function(t) { sanitize( undefined, function( err, clean ){ @@ -102,7 +126,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: 'geoname/123' }}; + var req = { query: { id: 'geoname' + delimiter + '123' }}; var next = function( message ){ t.equal(message, undefined, 'no error message set'); t.deepEqual(req.clean, defaultClean); From 5e42b4ac0c0ccfbbd54140621b535a263de9886f Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Thu, 16 Oct 2014 16:32:17 -0400 Subject: [PATCH 09/13] de-dupe as part of sanitizing --- sanitiser/_id.js | 5 +++++ test/unit/sanitiser/get.js | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/sanitiser/_id.js b/sanitiser/_id.js index 62860243..8f165f22 100644 --- a/sanitiser/_id.js +++ b/sanitiser/_id.js @@ -29,6 +29,11 @@ function sanitize( req ){ req.clean.ids = []; params.ids = Array.isArray(params.id) ? params.id : [params.id]; + // de-dupe + params.ids = params.ids.filter(function(item, pos) { + return params.ids.indexOf(item) == pos; + }); + for (var i=0; i Date: Thu, 16 Oct 2014 16:55:30 -0400 Subject: [PATCH 10/13] ciao --- test/ciao/get/success.coffee | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/ciao/get/success.coffee diff --git a/test/ciao/get/success.coffee b/test/ciao/get/success.coffee new file mode 100644 index 00000000..bd294d79 --- /dev/null +++ b/test/ciao/get/success.coffee @@ -0,0 +1,16 @@ + +#> valid get query +path: '/get?id=geoname:4221195' + +#? 200 ok +response.statusCode.should.equal 200 + +#? valid response +now = new Date().getTime() +should.exist json +should.not.exist json.error +json.date.should.be.within now-5000, now+5000 + +#? valid geojson +json.type.should.equal 'FeatureCollection' +json.features.should.be.instanceof Array \ No newline at end of file From ca10661457634856e4d915369b0ea4ae7be2b8ca Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Thu, 16 Oct 2014 17:03:36 -0400 Subject: [PATCH 11/13] adding a mget ciao test --- test/ciao/get/mget.coffee | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 test/ciao/get/mget.coffee diff --git a/test/ciao/get/mget.coffee b/test/ciao/get/mget.coffee new file mode 100644 index 00000000..ab5004ec --- /dev/null +++ b/test/ciao/get/mget.coffee @@ -0,0 +1,16 @@ + +#> valid get query +path: '/get?id=geoname:4221195&id=geoname:4193595' + +#? 200 ok +response.statusCode.should.equal 200 + +#? valid response +now = new Date().getTime() +should.exist json +should.not.exist json.error +json.date.should.be.within now-5000, now+5000 + +#? valid geojson +json.type.should.equal 'FeatureCollection' +json.features.should.be.instanceof Array \ No newline at end of file From e6376942376332dbc48e253da9f7afdb24c54dbf Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Thu, 16 Oct 2014 17:04:04 -0400 Subject: [PATCH 12/13] doc - get --- docs/get/mget.md | 97 +++++++++++++++++++++++++++++++++++++++++++++ docs/get/success.md | 80 +++++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 docs/get/mget.md create mode 100644 docs/get/success.md diff --git a/docs/get/mget.md b/docs/get/mget.md new file mode 100644 index 00000000..744c9aa1 --- /dev/null +++ b/docs/get/mget.md @@ -0,0 +1,97 @@ +# valid get query + +*Generated: Thu Oct 16 2014 17:02:52 GMT-0400 (EDT)* +## Request +```javascript +{ + "protocol": "http:", + "host": "localhost", + "method": "GET", + "port": 3100, + "path": "/get?id=geoname:4221195&id=geoname:4193595" +} +``` + +## Response +```javascript +Status: 200 +{ + "x-powered-by": "mapzen", + "charset": "utf8", + "cache-control": "public,max-age=60", + "server": "Pelias/0.0.0", + "access-control-allow-origin": "*", + "access-control-allow-methods": "GET", + "access-control-allow-headers": "X-Requested-With,content-type", + "access-control-allow-credentials": "true", + "content-type": "application/json; charset=utf-8", + "content-length": "567", + "etag": "W/\"237-e0425e36\"", + "date": "Thu, 16 Oct 2014 21:02:52 GMT", + "connection": "close" +} +``` +```javascript +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.9207, + 34.36094 + ] + }, + "properties": { + "name": "Sanders Grove Cemetery", + "admin0": "United States", + "admin1": "Georgia", + "admin2": "Hart County", + "text": "Sanders Grove Cemetery, Hart County, United States" + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -83.94213, + 33.32262 + ] + }, + "properties": { + "name": "Etheredge Cemetery", + "admin0": "United States", + "admin1": "Georgia", + "admin2": "Butts County", + "text": "Etheredge Cemetery, Butts County, United States" + } + } + ], + "date": 1413493372681 +} +``` + +## Tests + +### ✓ valid response +```javascript +now = new Date().getTime() +should.exist json +should.not.exist json.error +json.date.should.be.within now-5000, now+5000 +``` + +### ✓ valid geojson +```javascript +json.type.should.equal 'FeatureCollection' +json.features.should.be.instanceof Array +``` + +### ✓ 200 ok +```javascript +response.statusCode.should.equal 200 +``` + diff --git a/docs/get/success.md b/docs/get/success.md new file mode 100644 index 00000000..5ec7689f --- /dev/null +++ b/docs/get/success.md @@ -0,0 +1,80 @@ +# valid get query + +*Generated: Thu Oct 16 2014 17:02:52 GMT-0400 (EDT)* +## Request +```javascript +{ + "protocol": "http:", + "host": "localhost", + "method": "GET", + "port": 3100, + "path": "/get?id=geoname:4221195" +} +``` + +## Response +```javascript +Status: 200 +{ + "x-powered-by": "mapzen", + "charset": "utf8", + "cache-control": "public,max-age=60", + "server": "Pelias/0.0.0", + "access-control-allow-origin": "*", + "access-control-allow-methods": "GET", + "access-control-allow-headers": "X-Requested-With,content-type", + "access-control-allow-credentials": "true", + "content-type": "application/json; charset=utf-8", + "content-length": "317", + "etag": "W/\"13d-bc388729\"", + "date": "Thu, 16 Oct 2014 21:02:52 GMT", + "connection": "close" +} +``` +```javascript +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -82.9207, + 34.36094 + ] + }, + "properties": { + "name": "Sanders Grove Cemetery", + "admin0": "United States", + "admin1": "Georgia", + "admin2": "Hart County", + "text": "Sanders Grove Cemetery, Hart County, United States" + } + } + ], + "date": 1413493372842 +} +``` + +## Tests + +### ✓ valid geojson +```javascript +json.type.should.equal 'FeatureCollection' +json.features.should.be.instanceof Array +``` + +### ✓ 200 ok +```javascript +response.statusCode.should.equal 200 +``` + +### ✓ valid response +```javascript +now = new Date().getTime() +should.exist json +should.not.exist json.error +json.date.should.be.within now-5000, now+5000 +``` + From 81b3668b304989fe5a2971c0c9b6b29ff3055ae8 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 22 Oct 2014 17:06:12 -0400 Subject: [PATCH 13/13] get to doc --- app.js | 8 ++++---- controller/{get.js => doc.js} | 0 sanitiser/{get.js => doc.js} | 0 test/ciao/{get/mget.coffee => doc/msuccess.coffee} | 4 ++-- test/ciao/{get => doc}/success.coffee | 4 ++-- test/unit/run.js | 2 +- test/unit/sanitiser/{get.js => doc.js} | 8 ++++---- 7 files changed, 13 insertions(+), 13 deletions(-) rename controller/{get.js => doc.js} (100%) rename sanitiser/{get.js => doc.js} (100%) rename test/ciao/{get/mget.coffee => doc/msuccess.coffee} (69%) rename test/ciao/{get => doc}/success.coffee (73%) rename test/unit/sanitiser/{get.js => doc.js} (97%) diff --git a/app.js b/app.js index a80a4f82..d4cf0257 100644 --- a/app.js +++ b/app.js @@ -11,7 +11,7 @@ app.use( require('./middleware/jsonp') ); /** ----------------------- sanitisers ----------------------- **/ var sanitisers = {}; -sanitisers.get = require('./sanitiser/get'); +sanitisers.doc = require('./sanitiser/doc'); sanitisers.suggest = require('./sanitiser/suggest'); sanitisers.search = sanitisers.suggest; sanitisers.reverse = require('./sanitiser/reverse'); @@ -20,7 +20,7 @@ sanitisers.reverse = require('./sanitiser/reverse'); var controllers = {}; controllers.index = require('./controller/index'); -controllers.get = require('./controller/get'); +controllers.doc = require('./controller/doc'); controllers.suggest = require('./controller/suggest'); controllers.search = require('./controller/search'); @@ -29,8 +29,8 @@ controllers.search = require('./controller/search'); // api root app.get( '/', controllers.index() ); -// get doc API -app.get( '/get', sanitisers.get.middleware, controllers.get() ); +// doc API +app.get( '/doc', sanitisers.doc.middleware, controllers.doc() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); diff --git a/controller/get.js b/controller/doc.js similarity index 100% rename from controller/get.js rename to controller/doc.js diff --git a/sanitiser/get.js b/sanitiser/doc.js similarity index 100% rename from sanitiser/get.js rename to sanitiser/doc.js diff --git a/test/ciao/get/mget.coffee b/test/ciao/doc/msuccess.coffee similarity index 69% rename from test/ciao/get/mget.coffee rename to test/ciao/doc/msuccess.coffee index ab5004ec..56808be4 100644 --- a/test/ciao/get/mget.coffee +++ b/test/ciao/doc/msuccess.coffee @@ -1,6 +1,6 @@ -#> valid get query -path: '/get?id=geoname:4221195&id=geoname:4193595' +#> valid doc query +path: '/doc?id=geoname:4221195&id=geoname:4193595' #? 200 ok response.statusCode.should.equal 200 diff --git a/test/ciao/get/success.coffee b/test/ciao/doc/success.coffee similarity index 73% rename from test/ciao/get/success.coffee rename to test/ciao/doc/success.coffee index bd294d79..3818aca6 100644 --- a/test/ciao/get/success.coffee +++ b/test/ciao/doc/success.coffee @@ -1,6 +1,6 @@ -#> valid get query -path: '/get?id=geoname:4221195' +#> valid doc query +path: '/doc?id=geoname:4221195' #? 200 ok response.statusCode.should.equal 200 diff --git a/test/unit/run.js b/test/unit/run.js index 34b5a92e..bad988db 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -7,7 +7,7 @@ var tests = [ require('./controller/suggest'), require('./controller/search'), require('./sanitiser/suggest'), - require('./sanitiser/get'), + require('./sanitiser/doc'), require('./query/indeces'), require('./query/suggest'), require('./query/search'), diff --git a/test/unit/sanitiser/get.js b/test/unit/sanitiser/doc.js similarity index 97% rename from test/unit/sanitiser/get.js rename to test/unit/sanitiser/doc.js index 8c7dd33d..dcd15cbb 100644 --- a/test/unit/sanitiser/get.js +++ b/test/unit/sanitiser/doc.js @@ -1,7 +1,7 @@ -var get = require('../../../sanitiser/get'), - _sanitize = get.sanitize, - middleware = get.middleware, +var doc = require('../../../sanitiser/doc'), + _sanitize = doc.sanitize, + middleware = doc.middleware, indeces = require('../../../query/indeces'), delimiter = ':', defaultLengthError = function(input) { return 'invalid param \''+ input + '\': text length, must be >0' }, @@ -150,7 +150,7 @@ module.exports.tests.middleware_success = function(test, common) { module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE /get ' + name, testFunction); + return tape('SANTIZE /doc ' + name, testFunction); } for( var testCase in module.exports.tests ){