Browse Source

added placeholder config, added tests for http_json service

pull/850/head
Stephen Hess 8 years ago
parent
commit
5515fa4b6c
  1. 36
      service/configurations/placeholder.js
  2. 2
      service/http_json.js
  3. 1
      test/unit/run.js
  4. 94
      test/unit/service/configurations/placeholder.js
  5. 44
      test/unit/service/http_json.js

36
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;

2
service/http_json.js

@ -32,7 +32,7 @@ module.exports = function setup(serviceConfig) {
method: 'GET', method: 'GET',
url: serviceConfig.getUrl(req), url: serviceConfig.getUrl(req),
qs: serviceConfig.getParameters(req), qs: serviceConfig.getParameters(req),
headers: serviceConfig.getHeaders(req) headers: serviceConfig.getHeaders(req) || {}
}; };
const do_not_track = isDNT(req); const do_not_track = isDNT(req);

1
test/unit/run.js

@ -81,6 +81,7 @@ var tests = [
require('./sanitizer/search_fallback'), require('./sanitizer/search_fallback'),
require('./sanitizer/wrap'), require('./sanitizer/wrap'),
require('./service/http_json'), require('./service/http_json'),
require('./service/configurations/placeholder'),
require('./service/mget'), require('./service/mget'),
require('./service/search'), require('./service/search'),
require('./service/interpolation'), require('./service/interpolation'),

94
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);
}
};

44
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) => { module.exports.all = (tape, common) => {

Loading…
Cancel
Save