diff --git a/middleware/sendJSON.js b/middleware/sendJSON.js index 94353c6c..6a33cfe7 100644 --- a/middleware/sendJSON.js +++ b/middleware/sendJSON.js @@ -1,4 +1,5 @@ -var check = require('check-types'); +var check = require('check-types'), + es = require('elasticsearch'); function sendJSONResponse(req, res, next) { @@ -21,17 +22,18 @@ function sendJSONResponse(req, res, next) { geocoding.errors.forEach( function( err ){ // custom status codes for instances of the Error() object. if( err instanceof Error ){ - // we can extract the error type from the constructor name - switch( err.constructor.name ){ - // elasticsearch errors - // see: https://github.com/elastic/elasticsearch-js/blob/master/src/lib/errors.js - case 'RequestTimeout': statusCode = 408; break; // 408 Request Timeout - case 'NoConnections': statusCode = 502; break; // 502 Bad Gateway - case 'ConnectionFault': statusCode = 502; break; // 502 Bad Gateway - case 'Serialization': statusCode = 500; break; // 500 Internal Server Error - case 'Generic': statusCode = 500; break; // 500 Internal Server Error - default: statusCode = 500; // 500 Internal Server Error - } + /* + elasticsearch errors + see: https://github.com/elastic/elasticsearch-js/blob/master/src/lib/errors.js + + 408 Request Timeout + 500 Internal Server Error + 502 Bad Gateway + */ + if( err instanceof es.errors.RequestTimeout ){ statusCode = 408; } + else if( err instanceof es.errors.NoConnections ){ statusCode = 502; } + else if( err instanceof es.errors.ConnectionFault ){ statusCode = 502; } + else { statusCode = 500; } } }); } diff --git a/test/ciao_test_data.js b/test/ciao_test_data.js index 70b329f6..2ccee53c 100644 --- a/test/ciao_test_data.js +++ b/test/ciao_test_data.js @@ -54,5 +54,5 @@ actions.push( function( done ){ // perform all actions in series async.series( actions, function( err, resp ){ - console.log('test data inported'); + console.log('test data imported'); }); diff --git a/test/unit/middleware/sendJSON.js b/test/unit/middleware/sendJSON.js new file mode 100644 index 00000000..d4166bc8 --- /dev/null +++ b/test/unit/middleware/sendJSON.js @@ -0,0 +1,200 @@ +var es = require('elasticsearch'), + middleware = require('../../../middleware/sendJSON'); + +module.exports.tests = {}; + +module.exports.tests.invalid = function(test, common) { + test('invalid $res', function(t) { + var res; + + middleware(null, res, function () { + t.pass('next() called.'); + t.end(); + }); + }); + + test('invalid $res.body', function(t) { + var res = { body: 1 }; + + middleware(null, res, function () { + t.pass('next() called.'); + t.end(); + }); + }); + + test('invalid $res.body.geocoding', function(t) { + var res = { body: { geocoding: 1 } }; + + middleware(null, res, function () { + t.pass('next() called.'); + t.end(); + }); + }); +}; + +module.exports.tests.default_status = function(test, common) { + test('no errors', function(t) { + var res = { body: { geocoding: {} } }; + + res.status = function( code ){ + return { json: function( body ){ + t.equal( code, 200, 'default status' ); + t.deepEqual( body, res.body, 'body set' ); + t.end(); + }}; + }; + + middleware(null, res); + }); + + test('empty errors array', function(t) { + var res = { body: { geocoding: {}, errors: [] } }; + + res.status = function( code ){ + return { json: function( body ){ + t.equal( code, 200, 'default status' ); + t.deepEqual( body, res.body, 'body set' ); + t.end(); + }}; + }; + + middleware(null, res); + }); +}; + +module.exports.tests.default_error_status = function(test, common) { + test('default error code', function(t) { + var res = { body: { geocoding: { + errors: [ 'an error' ] + }}}; + + res.status = function( code ){ + return { json: function( body ){ + t.equal( code, 400, 'default status' ); + t.deepEqual( body, res.body, 'body set' ); + t.end(); + }}; + }; + + middleware(null, res); + }); +}; + +module.exports.tests.generic_server_error = function(test, common) { + test('generic server error', function(t) { + var res = { body: { geocoding: { + errors: [ new Error('an error') ] + }}}; + + res.status = function( code ){ + return { json: function( body ){ + t.equal( code, 500, 'default status' ); + t.deepEqual( body, res.body, 'body set' ); + t.end(); + }}; + }; + + middleware(null, res); + }); +}; + +module.exports.tests.generic_elasticsearch_error = function(test, common) { + test('generic elasticsearch error', function(t) { + var res = { body: { geocoding: { + errors: [ new es.errors.Generic('an error') ] + }}}; + + res.status = function( code ){ + return { json: function( body ){ + t.equal( code, 500, 'default status' ); + t.deepEqual( body, res.body, 'body set' ); + t.end(); + }}; + }; + + middleware(null, res); + }); +}; + +module.exports.tests.request_timeout = function(test, common) { + test('request timeout', function(t) { + var res = { body: { geocoding: { + errors: [ new es.errors.RequestTimeout('an error') ] + }}}; + + res.status = function( code ){ + return { json: function( body ){ + t.equal( code, 408, 'default status' ); + t.deepEqual( body, res.body, 'body set' ); + t.end(); + }}; + }; + + middleware(null, res); + }); +}; + +module.exports.tests.no_connections = function(test, common) { + test('no connections', function(t) { + var res = { body: { geocoding: { + errors: [ new es.errors.NoConnections('an error') ] + }}}; + + res.status = function( code ){ + return { json: function( body ){ + t.equal( code, 502, 'default status' ); + t.deepEqual( body, res.body, 'body set' ); + t.end(); + }}; + }; + + middleware(null, res); + }); +}; + +module.exports.tests.connection_fault = function(test, common) { + test('connection fault', function(t) { + var res = { body: { geocoding: { + errors: [ new es.errors.ConnectionFault('an error') ] + }}}; + + res.status = function( code ){ + return { json: function( body ){ + t.equal( code, 502, 'default status' ); + t.deepEqual( body, res.body, 'body set' ); + t.end(); + }}; + }; + + middleware(null, res); + }); +}; + +module.exports.tests.serialization = function(test, common) { + test('serialization', function(t) { + var res = { body: { geocoding: { + errors: [ new es.errors.Serialization('an error') ] + }}}; + + res.status = function( code ){ + return { json: function( body ){ + t.equal( code, 500, 'default status' ); + t.deepEqual( body, res.body, 'body set' ); + t.end(); + }}; + }; + + middleware(null, res); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('[middleware] sendJSON: ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/run.js b/test/unit/run.js index 1a6f7a90..828d99b5 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -27,6 +27,7 @@ var tests = [ require('./middleware/localNamingConventions'), require('./middleware/dedupe'), require('./middleware/parseBBox'), + require('./middleware/sendJSON'), require('./middleware/normalizeParentIds'), require('./query/autocomplete'), require('./query/autocomplete_defaults'),