Browse Source

use url.resolve to eliminate duplicate slashes

pull/875/head
Stephen Hess 8 years ago
parent
commit
143971e43e
  1. 5
      service/configurations/PointInPolygon.js
  2. 33
      test/unit/service/configurations/PointInPolygon.js

5
service/configurations/PointInPolygon.js

@ -1,5 +1,7 @@
'use strict';
const url = require('url');
const _ = require('lodash');
const ServiceConfiguration = require('pelias-microservice-wrapper').ServiceConfiguration;
@ -10,7 +12,8 @@ class PointInPolygon extends ServiceConfiguration {
}
getUrl(req) {
return `${this.baseUrl}/${req.clean['point.lon']}/${req.clean['point.lat']}`;
// use resolve to eliminate possibility of duplicate /'s in URL
return url.resolve(this.baseUrl, `${req.clean['point.lon']}/${req.clean['point.lat']}`);
}
}

33
test/unit/service/configurations/PointInPolygon.js

@ -5,7 +5,7 @@ const PointInPolygon = require('../../../../service/configurations/PointInPolygo
module.exports.tests.all = (test, common) => {
test('getName should return \'pip\'', (t) => {
const configBlob = {
url: 'base url',
url: 'http://localhost:1234',
timeout: 17,
retries: 19
};
@ -13,7 +13,7 @@ module.exports.tests.all = (test, common) => {
const pointInPolygon = new PointInPolygon(configBlob);
t.equals(pointInPolygon.getName(), 'pip');
t.equals(pointInPolygon.getBaseUrl(), 'base url');
t.equals(pointInPolygon.getBaseUrl(), 'http://localhost:1234/');
t.equals(pointInPolygon.getTimeout(), 17);
t.equals(pointInPolygon.getRetries(), 19);
t.end();
@ -22,7 +22,7 @@ module.exports.tests.all = (test, common) => {
test('getUrl should return value formatted with point.lon/lat passed to constructor', (t) => {
const configBlob = {
url: 'base url'
url: 'http://localhost:1234'
};
const pointInPolygon = new PointInPolygon(configBlob);
@ -34,14 +34,14 @@ module.exports.tests.all = (test, common) => {
req.clean['point.lon'] = 21.212121;
req.clean['point.lat'] = 12.121212;
t.equals(pointInPolygon.getUrl(req), 'base url/21.212121/12.121212');
t.equals(pointInPolygon.getUrl(req), 'http://localhost:1234/21.212121/12.121212');
t.end();
});
test('getHeaders should return an empty object', (t) => {
const configBlob = {
url: 'base url',
url: 'http://localhost:1234',
timeout: 17,
retries: 19
};
@ -55,7 +55,7 @@ module.exports.tests.all = (test, common) => {
test('getParameters should return an empty object', (t) => {
const configBlob = {
url: 'base url',
url: 'http://localhost:1234',
timeout: 17,
retries: 19
};
@ -67,6 +67,27 @@ module.exports.tests.all = (test, common) => {
});
test('baseUrl ending in / should not have double /\'s return by getUrl', (t) => {
const configBlob = {
url: 'http://localhost:1234/',
timeout: 17,
retries: 19
};
const pointInPolygon = new PointInPolygon(configBlob);
const req = {
clean: { }
};
req.clean['point.lon'] = 21.212121;
req.clean['point.lat'] = 12.121212;
t.equals(pointInPolygon.getUrl(req), 'http://localhost:1234/21.212121/12.121212');
t.end();
});
};
module.exports.all = (tape, common) => {

Loading…
Cancel
Save