diff --git a/service/configurations/placeholder.js b/service/configurations/placeholder.js new file mode 100644 index 00000000..631b64b4 --- /dev/null +++ b/service/configurations/placeholder.js @@ -0,0 +1,36 @@ +const _ = require('lodash'); + +function PlaceHolder( baseUrl ){ + this.baseUrl = baseUrl; +} + +PlaceHolder.prototype.getName = function() { + return 'placeholder'; +}; + +PlaceHolder.prototype.getBaseUrl = function() { + return this.baseUrl; +}; + +PlaceHolder.prototype.getUrl = function(req) { + return `${this.baseUrl}/search`; +}; + +PlaceHolder.prototype.getParameters = function(req) { + const parameters = { + text: req.clean.text + }; + + if (_.has(req.clean, 'lang.iso6393')) { + parameters.lang = req.clean.lang.iso6393; + } + + return parameters; +}; + +PlaceHolder.prototype.getHeaders = function(req) { + return {}; +}; + +// export +module.exports = PlaceHolder; diff --git a/service/http_json.js b/service/http_json.js index ac1db802..969c5baf 100644 --- a/service/http_json.js +++ b/service/http_json.js @@ -32,7 +32,7 @@ module.exports = function setup(serviceConfig) { method: 'GET', url: serviceConfig.getUrl(req), qs: serviceConfig.getParameters(req), - headers: serviceConfig.getHeaders(req) + headers: serviceConfig.getHeaders(req) || {} }; const do_not_track = isDNT(req); diff --git a/test/unit/run.js b/test/unit/run.js index 4fb1e5a2..878a1b58 100644 --- a/test/unit/run.js +++ b/test/unit/run.js @@ -81,6 +81,7 @@ var tests = [ require('./sanitizer/search_fallback'), require('./sanitizer/wrap'), require('./service/http_json'), + require('./service/configurations/placeholder'), require('./service/mget'), require('./service/search'), require('./service/interpolation'), diff --git a/test/unit/service/configurations/placeholder.js b/test/unit/service/configurations/placeholder.js new file mode 100644 index 00000000..cb1191bb --- /dev/null +++ b/test/unit/service/configurations/placeholder.js @@ -0,0 +1,94 @@ +module.exports.tests = {}; + +const PlaceHolder = require('../../../../service/configurations/placeholder'); + +module.exports.tests.all = (test, common) => { + test('getName should return \'placeholder\'', (t) => { + const placeholder = new PlaceHolder('base url'); + + t.equals(placeholder.getName(), 'placeholder'); + t.end(); + + }); + + test('getBaseUrl should return value passed to constructor', (t) => { + const placeholder = new PlaceHolder('base url'); + + t.equals(placeholder.getBaseUrl(), 'base url'); + t.end(); + + }); + + test('getUrl should return value passed to constructor', (t) => { + const placeholder = new PlaceHolder('base url'); + + t.equals(placeholder.getUrl('this is not an object'), 'base url/search'); + t.end(); + + }); + + test('getParameters should return object with text and lang from req', (t) => { + const placeholder = new PlaceHolder('base url'); + + const req = { + clean: { + text: 'text value', + lang: { + iso6393: 'lang value' + } + } + }; + + t.deepEquals(placeholder.getParameters(req), { text: 'text value', lang: 'lang value' }); + t.end(); + + }); + + test('getHeaders should return empty object', (t) => { + const placeholder = new PlaceHolder('base url'); + + t.deepEquals(placeholder.getHeaders('this is not an object'), {}); + t.end(); + + }); + + test('getParameters should not include lang if req.clean.lang is unavailable', (t) => { + const placeholder = new PlaceHolder('base url'); + + const req = { + clean: { + text: 'text value' + } + }; + + t.deepEquals(placeholder.getParameters(req), { text: 'text value' }); + t.end(); + + }); + + test('getParameters should not include lang if req.clean.lang.iso6393 is unavailable', (t) => { + const placeholder = new PlaceHolder('base url'); + + const req = { + clean: { + text: 'text value', + lang: {} + } + }; + + t.deepEquals(placeholder.getParameters(req), { text: 'text value' }); + t.end(); + + }); + +}; + +module.exports.all = (tape, common) => { + function test(name, testFunction) { + return tape(`SERVICE CONFIGURATION /placeholder ${name}`, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/unit/service/http_json.js b/test/unit/service/http_json.js index 98de94ce..b9c4d528 100644 --- a/test/unit/service/http_json.js +++ b/test/unit/service/http_json.js @@ -422,6 +422,50 @@ module.exports.tests.success_conditions = (test, common) => { }); + test('getHeaders returning undefined should use empty headers object', (t) => { + const webServer = express(); + webServer.get('/some_endpoint', (req, res, next) => { + t.equals(req.headers.dnt, '1'); + + t.deepEquals(req.query, { param1: 'param1 value', param2: 'param2 value' }); + + res.status(200).send('[1, 2, 3]'); + }); + + const server = webServer.listen(); + const port = server.address().port; + + const logger = require('pelias-mock-logger')(); + + const serviceConfig = { + getName: () => { return 'foo'; }, + getBaseUrl: () => { return `http://localhost:${port}`; }, + getUrl: (req) => { return `http://localhost:${port}/some_endpoint`; }, + getParameters: (req) => { return { param1: 'param1 value', param2: 'param2 value' }; }, + getHeaders: (req) => { } + }; + + const service = proxyquire('../../../service/http_json', { + 'pelias-logger': logger, + '../helper/logging': { + isDNT: () => { return true; } + } + })(serviceConfig); + + t.ok(logger.isInfoMessage(new RegExp(`using foo service at http://localhost:${port}`))); + + service({}, (err, results) => { + t.notOk(err, 'should be no error'); + t.deepEquals(results, [1, 2, 3]); + t.notOk(logger.hasErrorMessages()); + t.end(); + + server.close(); + + }); + + }); + }; module.exports.all = (tape, common) => {