mirror of https://github.com/pelias/api.git
Lily He
8 years ago
5 changed files with 192 additions and 1 deletions
@ -0,0 +1,18 @@
|
||||
/* |
||||
Set a focus.lat and focus.lon if specified in pelias config |
||||
*/ |
||||
var _ = require('lodash'); |
||||
|
||||
function setup(defaultParameters){ |
||||
return function setLocationBias(req, res, next){ |
||||
if (_.isUndefined(req.clean) || _.isUndefined(defaultParameters['focus.point.lat']) || _.isUndefined(defaultParameters['focus.point.lon'])) { |
||||
return next(); |
||||
} |
||||
req.clean['focus.point.lat'] = defaultParameters['focus.point.lat']; |
||||
req.clean['focus.point.lon'] = defaultParameters['focus.point.lon']; |
||||
next(); |
||||
}; |
||||
} |
||||
|
||||
|
||||
module.exports = setup; |
@ -0,0 +1,90 @@
|
||||
var setup = require('../../../sanitizer/_location_bias'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.setLocationBias = function(test, common) { |
||||
test('set focus point', t => { |
||||
var defaultParameters = { // specify focus point latitude and longitude
|
||||
'focus.point.lat': 12.12121212, |
||||
'focus.point.lon': 21.21212121 |
||||
}; |
||||
var locationBias = setup(defaultParameters); |
||||
var req = { |
||||
clean: {} |
||||
}; |
||||
var expected = { |
||||
clean: { |
||||
'focus.point.lat': 12.12121212, |
||||
'focus.point.lon': 21.21212121 |
||||
} |
||||
}; |
||||
|
||||
locationBias(req, undefined, () => { |
||||
t.deepEqual(req, expected, 'focus point should be set'); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
test('undefined req.clean', t => { |
||||
var defaultParameters = { |
||||
'focus.point.lat': 12.12121212, |
||||
'focus.point.lon': 21.21212121 |
||||
}; |
||||
var locationBias = setup(defaultParameters); |
||||
var req = {}; |
||||
var expected = {}; |
||||
|
||||
locationBias(req, undefined, () => { |
||||
t.deepEqual(req, expected, 'should be unmodified' ); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
test('focusPointLat is undefined', t => { |
||||
var defaultParameters = { |
||||
'focus.point.lon': 12.2121212 |
||||
}; |
||||
var locationBias = setup(defaultParameters); |
||||
var req = { |
||||
clean: {} |
||||
}; |
||||
var expected = { |
||||
clean: {} |
||||
}; |
||||
|
||||
locationBias(req, undefined, () => { |
||||
t.deepEqual(req, expected, 'should be unmodified' ); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
test('focusPointLon is undefined', t => { |
||||
var defaultParameters = { |
||||
'focus.point.lat': 12.2121212 |
||||
}; |
||||
var locationBias = setup(defaultParameters); |
||||
var req = { |
||||
clean: {} |
||||
}; |
||||
var expected = { |
||||
clean: {} |
||||
}; |
||||
|
||||
locationBias(req, undefined, () => { |
||||
t.deepEqual(req, expected, 'should be unmodified' ); |
||||
t.end(); |
||||
}); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('[middleware] locationBias: ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue