diff --git a/app.js b/app.js index d4cf0257..51eada83 100644 --- a/app.js +++ b/app.js @@ -22,6 +22,7 @@ var controllers = {}; controllers.index = require('./controller/index'); controllers.doc = require('./controller/doc'); controllers.suggest = require('./controller/suggest'); +controllers.suggest_near_me = require('./controller/suggest_near_me'); controllers.search = require('./controller/search'); /** ----------------------- routes ----------------------- **/ @@ -34,6 +35,7 @@ app.get( '/doc', sanitisers.doc.middleware, controllers.doc() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); +app.get( '/suggest_near_me', sanitisers.suggest.middleware, controllers.suggest_near_me() ); // search API app.get( '/search', sanitisers.search.middleware, controllers.search() ); diff --git a/controller/suggest_near_me.js b/controller/suggest_near_me.js new file mode 100644 index 00000000..956363b8 --- /dev/null +++ b/controller/suggest_near_me.js @@ -0,0 +1,40 @@ + +var service = { suggest: require('../service/suggest') }; +var geojsonify = require('../helper/geojsonify').suggest; + +function setup( backend, query ){ + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + query = query || require('../query/suggest'); + + function controller( req, res, next ){ + + // backend command + var cmd = { + index: 'pelias', + body: query( req.clean ) + }; + + // query backend + service.suggest( backend, cmd, function( err, docs ){ + + // error handler + if( err ){ return next( err ); } + + // 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; \ No newline at end of file diff --git a/test/unit/controller/suggest_near_me.js b/test/unit/controller/suggest_near_me.js new file mode 100644 index 00000000..bfb96195 --- /dev/null +++ b/test/unit/controller/suggest_near_me.js @@ -0,0 +1,91 @@ + +var setup = require('../../../controller/suggest'), + mockBackend = require('../mock/backend'), + mockQuery = require('../mock/query'); + +module.exports.tests = {}; + +module.exports.tests.interface = function(test, common) { + test('valid interface', function(t) { + t.equal(typeof setup, 'function', 'setup is a function'); + t.equal(typeof setup(), 'function', 'setup returns a controller'); + t.end(); + }); +}; + +// functionally test controller (backend success) +module.exports.tests.functional_success = function(test, common) { + + // expected geojson features for 'client/suggest/ok/1' fixture + var expected = [{ + type: 'Feature', + geometry: { + type: 'Point', + coordinates: [ 101, -10.1 ] + }, + properties: { + id: 'mockid1', + type: 'mocktype', + value: 1 + } + }, { + type: 'Feature', + geometry: { + type: 'Point', + coordinates: [ 101, -10.1 ] + }, + properties: { + id: 'mockid2', + type: 'mocktype', + value: 2 + } + }]; + + test('functional success', function(t) { + var backend = mockBackend( 'client/suggest/ok/1', function( cmd ){ + t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); + }); + var controller = setup( backend, mockQuery() ); + var res = { + status: function( code ){ + t.equal(code, 200, 'status set'); + return res; + }, + json: function( json ){ + t.equal(typeof json, 'object', 'returns json'); + t.equal(typeof json.date, 'number', 'date set'); + t.equal(json.type, 'FeatureCollection', 'valid geojson'); + t.true(Array.isArray(json.features), 'features is array'); + t.deepEqual(json.features, expected, 'values correctly mapped'); + t.end(); + } + }; + controller( { clean: { a: 'b' } }, res ); + }); +}; + +// functionally test controller (backend failure) +module.exports.tests.functional_failure = function(test, common) { + test('functional failure', function(t) { + var backend = mockBackend( 'client/suggest/fail/1', function( cmd ){ + t.deepEqual(cmd, { body: { a: 'b' }, index: 'pelias' }, 'correct backend command'); + }); + var controller = setup( backend, mockQuery() ); + var next = function( message ){ + t.equal(message,'a backend error occurred','error passed to errorHandler'); + t.end(); + }; + controller( { clean: { a: 'b' } }, undefined, next ); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('GET /suggest ' + 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/run.js b/test/unit/run.js index ec1309be..f828c6c5 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -6,6 +6,7 @@ var tests = [ require('./controller/index'), require('./controller/doc'), require('./controller/suggest'), + require('./controller/suggest_near_me'), require('./controller/search'), require('./service/mget'), require('./service/search'),