From 69b3e18fe5bc8b4b961ed2ce1bfc97e154a04ef4 Mon Sep 17 00:00:00 2001 From: Arni Sumarlidason Date: Thu, 18 Jul 2019 14:46:14 -0400 Subject: [PATCH] allow normalizeTileURL to handle API URLs with paths (#8466) --- 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 fbeb85b8eec..1d6b466371b 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 88554c13f1e..5460aa91a18 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 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}`; + 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();