From 9f8fbc68ba4ac8138a2df746472c8ab137cc8c1a Mon Sep 17 00:00:00 2001 From: Arni Sumarlidason Date: Thu, 11 Jul 2019 18:32:29 -0400 Subject: [PATCH 1/3] baseApiUrl w/ path & normalizeTileURL functionality - adds regex to trim tileURL prefix to /v4/... - adds filter to makeAPIURL preventing duplication of param 'access_token' - reorders params on test case -> sku,access_token closes #8458 --- src/util/mapbox.js | 3 +++ test/unit/util/mapbox.test.js | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/src/util/mapbox.js b/src/util/mapbox.js index d7eb2f5134f..e5f6e551c93 100644 --- a/src/util/mapbox.js +++ b/src/util/mapbox.js @@ -110,6 +110,7 @@ export class RequestManager { const urlObject = parseUrl(tileURL); const imageExtensionRe = /(\.(png|jpg)\d*)(?=$)/; + const tileURLAPIPrefixRe = /^.+\/v4\//; // The v4 mapbox tile API supports 512x512 image tiles only when @2x // is appended to the tile URL. If `tileSize: 512` is specified for @@ -117,6 +118,7 @@ export class RequestManager { const suffix = browser.devicePixelRatio >= 2 || tileSize === 512 ? '@2x' : ''; const extension = webpSupported.supported ? '.webp' : '$1'; urlObject.path = urlObject.path.replace(imageExtensionRe, `${suffix}${extension}`); + urlObject.path = urlObject.path.replace(tileURLAPIPrefixRe, '/'); urlObject.path = `/v4${urlObject.path}`; if (config.REQUIRE_ACCESS_TOKEN && (config.ACCESS_TOKEN || this._customAccessToken) && this._skuToken) { @@ -176,6 +178,7 @@ export class RequestManager { if (accessToken[0] === 's') throw new Error(`Use a public access token (pk.*) with Mapbox GL, not a secret access token (sk.*). ${help}`); + urlObject.params = urlObject.params.filter((d) => !d.includes('access_token')); urlObject.params.push(`access_token=${accessToken}`); return formatUrl(urlObject); } diff --git a/test/unit/util/mapbox.test.js b/test/unit/util/mapbox.test.js index d068a24cfec..169c5595b1c 100644 --- a/test/unit/util/mapbox.test.js +++ b/test/unit/util/mapbox.test.js @@ -357,6 +357,15 @@ test("mapbox", (t) => { t.end(); }); + t.test('.normalizeTileURL ignores non-mapbox:// sources', (t) => { + // Add a path to the config: + config.API_URL = 'http://localhost:8080/mbx'; + const input = `https://localhost:8080/mbx/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/10/184/401.vector.pbf?access_token=${config.ACCESS_TOKEN}`; + const expected = `http://localhost:8080/mbx/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/10/184/401.vector.pbf?sku=${manager._skuToken}&access_token=${config.ACCESS_TOKEN}`; + t.equal(manager.normalizeTileURL(input, 'mapbox://mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7'), expected); + t.end(); + }); + t.test('.normalizeTileURL ignores undefined sources', (t) => { t.equal(manager.normalizeTileURL('http://path.png'), 'http://path.png'); t.end(); From 15ed9909af743778bd881070d9389d2b694a7442 Mon Sep 17 00:00:00 2001 From: Arni Sumarlidason Date: Thu, 11 Jul 2019 18:35:53 -0400 Subject: [PATCH 2/3] trigger ci From 1a817c24eaae6cf2245d5399cc4d9f15afda61c6 Mon Sep 17 00:00:00 2001 From: Arni Sumarlidason Date: Thu, 18 Jul 2019 10:24:26 -0400 Subject: [PATCH 3/3] Update test description --- test/unit/util/mapbox.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/util/mapbox.test.js b/test/unit/util/mapbox.test.js index 169c5595b1c..4684f416cb3 100644 --- a/test/unit/util/mapbox.test.js +++ b/test/unit/util/mapbox.test.js @@ -357,7 +357,7 @@ test("mapbox", (t) => { t.end(); }); - t.test('.normalizeTileURL ignores non-mapbox:// sources', (t) => { + t.test('.normalizeTileURL accounts for tileURLs w/ paths', (t) => { // Add a path to the config: config.API_URL = 'http://localhost:8080/mbx'; const input = `https://localhost:8080/mbx/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/10/184/401.vector.pbf?access_token=${config.ACCESS_TOKEN}`;