From d0feffa528881d7d731c94031e314156aba31fcc Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 19 Jul 2017 21:08:53 +0200 Subject: [PATCH 1/2] Handle configured API_URLs that have a path: For the case when a user has modified mapboxgl.config.API_URL that includes a path such as http://test.example.com/api.mapbox.com the path was getting removed when building the full URL as it was being overwritten by the original path from the request. This pull request checks if the generation of the apiUrlObject has a path other than '/' (which is the default) we need to prepend it to the original urlObject path. This fixes the case where users using a reverse proxy to mapbox have filtered on the specific path set in their API_URL, and when it has been removed the lack of a match causes the proxy to not forward the requests . --- src/util/mapbox.js | 4 ++++ test/unit/util/mapbox.test.js | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/util/mapbox.js b/src/util/mapbox.js index eebe86194db..e02155fdc57 100644 --- a/src/util/mapbox.js +++ b/src/util/mapbox.js @@ -17,6 +17,10 @@ function makeAPIURL(urlObject: UrlObject, accessToken: string | null | void): st urlObject.protocol = apiUrlObject.protocol; urlObject.authority = apiUrlObject.authority; + if (apiUrlObject.path !== '/') { + urlObject.authority = `${urlObject.authority}${apiUrlObject.path}`; + } + if (!config.REQUIRE_ACCESS_TOKEN) return formatUrl(urlObject); accessToken = accessToken || config.ACCESS_TOKEN; diff --git a/test/unit/util/mapbox.test.js b/test/unit/util/mapbox.test.js index ad08fb943a1..da8c5b4798c 100644 --- a/test/unit/util/mapbox.test.js +++ b/test/unit/util/mapbox.test.js @@ -30,6 +30,17 @@ test("mapbox", (t) => { t.end(); }); + t.test('handles custom API_URLs with paths', (t) => { + const previousUrl = config.API_URL; + config.API_URL = 'https://test.example.com/api.mapbox.com'; + t.equal( + mapbox.normalizeStyleURL('mapbox://styles/foo/bar'), + 'https://test.example.com/api.mapbox.com/styles/v1/foo/bar?access_token=key' + ); + config.API_URL = previousUrl; + t.end(); + }); + t.end(); }); @@ -73,6 +84,17 @@ test("mapbox", (t) => { t.end(); }); + t.test('handles custom API_URLs with paths', (t) => { + const previousUrl = config.API_URL; + config.API_URL = 'https://test.example.com/api.mapbox.com'; + t.equal( + mapbox.normalizeSourceURL('mapbox://one.a'), + 'https://test.example.com/api.mapbox.com/v4/one.a.json?secure&access_token=key' + ); + config.API_URL = previousUrl; + t.end(); + }); + t.end(); }); @@ -92,6 +114,17 @@ test("mapbox", (t) => { t.end(); }); + t.test('handles custom API_URLs with paths', (t) => { + const previousUrl = config.API_URL; + config.API_URL = 'https://test.example.com/api.mapbox.com'; + t.equal( + mapbox.normalizeGlyphsURL('mapbox://fonts/boxmap/{fontstack}/{range}.pbf'), + 'https://test.example.com/api.mapbox.com/fonts/v1/boxmap/{fontstack}/{range}.pbf?access_token=key' + ); + config.API_URL = previousUrl; + t.end(); + }); + t.end(); }); @@ -149,6 +182,17 @@ test("mapbox", (t) => { t.end(); }); + t.test('handles custom API_URLs with paths', (t) => { + const previousUrl = config.API_URL; + config.API_URL = 'https://test.example.com/api.mapbox.com'; + t.equal( + mapbox.normalizeSpriteURL('mapbox://sprites/mapbox/streets-v8', '', '.json'), + 'https://test.example.com/api.mapbox.com/styles/v1/mapbox/streets-v8/sprite.json?access_token=key' + ); + config.API_URL = previousUrl; + t.end(); + }); + t.end(); }); From 432270d442c241f1432e44c5830c2c927d360bfc Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 19 Jul 2017 23:22:48 +0200 Subject: [PATCH 2/2] Prepend API_URL path to urlObject.path --- src/util/mapbox.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/mapbox.js b/src/util/mapbox.js index e02155fdc57..2619132f371 100644 --- a/src/util/mapbox.js +++ b/src/util/mapbox.js @@ -18,7 +18,7 @@ function makeAPIURL(urlObject: UrlObject, accessToken: string | null | void): st urlObject.authority = apiUrlObject.authority; if (apiUrlObject.path !== '/') { - urlObject.authority = `${urlObject.authority}${apiUrlObject.path}`; + urlObject.path = `${apiUrlObject.path}${urlObject.path}`; } if (!config.REQUIRE_ACCESS_TOKEN) return formatUrl(urlObject);