Skip to content

Commit

Permalink
Merge pull request #20 from marcjansen/legendurl-followup-19
Browse files Browse the repository at this point in the history
Support sources with URLs containing parameters
  • Loading branch information
marcjansen committed Sep 6, 2018
2 parents 386865a + ab67850 commit 9dd61cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/MapUtil/MapUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export class MapUtil {
const queryString = UrlUtil.objectToRequestString(
Object.assign(params, extraParams));

return url.endsWith('?') ? `${url}${queryString}` : `${url}?${queryString}`;
return /\?/.test(url) ? `${url}&${queryString}` : `${url}?${queryString}`;
} else {
Logger.warn(`Source of "${layer.get('name')}" is currently not supported `
+ `by MapUtil.getLegendGraphicUrl.`);
Expand Down
24 changes: 23 additions & 1 deletion src/MapUtil/MapUtil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ describe('MapUtil', () => {
let layer1;
let layer2;
let layer3;
let layer4;

beforeEach(() => {
layer1 = new OlLayerTile({
Expand All @@ -494,6 +495,17 @@ describe('MapUtil', () => {
crossOrigin: 'anonymous'
})
});
layer4 = new OlLayerTile({
name: 'OSM-WMS',
source: new OlSourceTileWMS({
urls: [
'https://a.example.com/service?humpty=dumpty',
'https://b.example.com/service?foo=bar'
],
params: {'LAYERS': 'OSM-WMS', 'TILED': true},
serverType: 'geoserver'
})
});
});

it('logs an error if called without a layer', () => {
Expand Down Expand Up @@ -551,10 +563,20 @@ describe('MapUtil', () => {
});
});

it('Does not append multiple questionmarks in URL', () => {
it('does not append multiple questionmarks in URL', () => {
const legendUrl = MapUtil.getLegendGraphicUrl(layer1);
const numQuestionMarks = (legendUrl.match(/\?/g) || []).length;
expect(legendUrl).toEqual(expect.stringContaining('?'));
expect(legendUrl).toEqual(expect.not.stringContaining('??'));
expect(numQuestionMarks).toEqual(1);
});

it('works as expected when layer URL contains params', () => {
const legendUrl = MapUtil.getLegendGraphicUrl(layer4);
const numQuestionMarks = (legendUrl.match(/\?/g) || []).length;
const containsParams = /humpty=dumpty/.test(legendUrl);
expect(numQuestionMarks).toEqual(1);
expect(containsParams).toBe(true);
});

it('accepts extraParams for the request', () => {
Expand Down

0 comments on commit 9dd61cc

Please sign in to comment.