diff --git a/controller/search.js b/controller/search.js index 3fa3da2f..f3ed67ad 100644 --- a/controller/search.js +++ b/controller/search.js @@ -11,14 +11,14 @@ function setup( apiConfig, esclient, query ){ function controller( req, res, next ){ // do not run controller when a request // validation error has occurred. - if( req.errors && req.errors.length ){ + if (_.get(req, 'errors', []).length > 0) { return next(); } // do not run controller if there are already results // this was added during libpostal integration. if the libpostal parse/query // doesn't return anything then fallback to old search-engine-y behavior - if (res && res.hasOwnProperty('data') && res.data.length > 0) { + if (_.get(res, 'data', []).length > 0) { return next(); } diff --git a/test/unit/controller/search.js b/test/unit/controller/search.js index 2b87990d..41d32bcd 100644 --- a/test/unit/controller/search.js +++ b/test/unit/controller/search.js @@ -173,6 +173,29 @@ module.exports.tests.timeout = function(test, common) { }); }; +module.exports.tests.existing_errors = function(test, common) { + test('req with errors should not call backend', function(t) { + var esclient = function() { + throw new Error('esclient should not have been called'); + }; + var controller = setup( fakeDefaultConfig, esclient, mockQuery() ); + + // the existence of `errors` means that a sanitizer detected an error, + // so don't call the esclient + var req = { + errors: ['error'] + }; + var res = { }; + + t.doesNotThrow(() => { + controller(req, res, () => {}); + }); + t.end(); + + }); + +}; + module.exports.tests.existing_results = function(test, common) { test('res with existing data should not call backend', function(t) { var backend = function() {