Browse Source

added test for early bail when errors are present

simplified conditionals using lodash `.get`
pull/782/head
Stephen Hess 8 years ago
parent
commit
e83c12cd0e
  1. 4
      controller/search.js
  2. 23
      test/unit/controller/search.js

4
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();
}

23
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() {

Loading…
Cancel
Save