diff --git a/src/util/config.js b/src/util/config.js index 0d8ad3017f8..58a55f04359 100644 --- a/src/util/config.js +++ b/src/util/config.js @@ -2,7 +2,7 @@ type Config = {| API_URL: string, - EVENTS_URL: string, + EVENTS_URL: ?string, FEEDBACK_URL: string, REQUIRE_ACCESS_TOKEN: boolean, ACCESS_TOKEN: ?string, @@ -12,10 +12,13 @@ type Config = {| const config: Config = { API_URL: 'https://api.mapbox.com', get EVENTS_URL() { + if (!this.API_URL) { return null; } if (this.API_URL.indexOf('https://api.mapbox.cn') === 0) { return 'https://events.mapbox.cn/events/v2'; - } else { + } else if (this.API_URL.indexOf('https://api.mapbox.com') === 0) { return 'https://events.mapbox.com/events/v2'; + } else { + return null; } }, FEEDBACK_URL: 'https://apps.mapbox.com/feedback', diff --git a/src/util/mapbox.js b/src/util/mapbox.js index a0b9c89bcd9..9752b2a4aec 100644 --- a/src/util/mapbox.js +++ b/src/util/mapbox.js @@ -253,6 +253,7 @@ class TelemetryEvent { * to TelemetryEvent#saveData */ postEvent(timestamp: number, additionalPayload: {[string]: any}, callback: (err: ?Error) => void) { + if (!config.EVENTS_URL) return; const eventsUrlObject: UrlObject = parseUrl(config.EVENTS_URL); eventsUrlObject.params.push(`access_token=${config.ACCESS_TOKEN || ''}`); const payload: Object = { @@ -297,7 +298,8 @@ export class MapLoadEvent extends TelemetryEvent { postMapLoadEvent(tileUrls: Array, mapId: number) { //Enabled only when Mapbox Access Token is set and a source uses // mapbox tiles. - if (config.ACCESS_TOKEN && + if (config.EVENTS_URL && + config.ACCESS_TOKEN && Array.isArray(tileUrls) && tileUrls.some(url => isMapboxURL(url) || isMapboxHTTPURL(url))) { this.queueRequest({id: mapId, timestamp: Date.now()}); @@ -336,7 +338,8 @@ export class TurnstileEvent extends TelemetryEvent { postTurnstileEvent(tileUrls: Array) { //Enabled only when Mapbox Access Token is set and a source uses // mapbox tiles. - if (config.ACCESS_TOKEN && + if (config.EVENTS_URL && + config.ACCESS_TOKEN && Array.isArray(tileUrls) && tileUrls.some(url => isMapboxURL(url) || isMapboxHTTPURL(url))) { this.queueRequest(Date.now()); diff --git a/test/unit/util/mapbox.test.js b/test/unit/util/mapbox.test.js index 5fa3f3a45b2..9e37fdfa4ea 100644 --- a/test/unit/util/mapbox.test.js +++ b/test/unit/util/mapbox.test.js @@ -29,6 +29,7 @@ test("mapbox", (t) => { t.beforeEach((callback) => { config.ACCESS_TOKEN = 'key'; config.REQUIRE_ACCESS_TOKEN = true; + config.API_URL = 'https://api.mapbox.com'; callback(); }); @@ -64,13 +65,11 @@ test("mapbox", (t) => { }); 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(); }); @@ -118,13 +117,11 @@ test("mapbox", (t) => { }); 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(); }); @@ -148,13 +145,11 @@ test("mapbox", (t) => { }); 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(); }); @@ -216,13 +211,11 @@ test("mapbox", (t) => { }); 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(); }); @@ -412,7 +405,6 @@ test("mapbox", (t) => { }); t.test('POSTs cn event when API_URL change to cn endpoint', (t) => { - const previousUrl = config.API_URL; config.API_URL = 'https://api.mapbox.cn'; event.postTurnstileEvent(mapboxTileURLs); @@ -421,7 +413,20 @@ test("mapbox", (t) => { req.respond(200); t.true(req.url.indexOf('https://events.mapbox.cn') > -1); - config.API_URL = previousUrl; + t.end(); + }); + + t.test('POSTs no event when API_URL unavailable', (t) => { + config.API_URL = null; + event.postTurnstileEvent(mapboxTileURLs); + t.equal(window.server.requests.length, 0, 'no events posted'); + t.end(); + }); + + t.test('POSTs no event when API_URL non-standard', (t) => { + config.API_URL = 'https://api.example.com'; + event.postTurnstileEvent(mapboxTileURLs); + t.equal(window.server.requests.length, 0, 'no events posted'); t.end(); }); @@ -465,7 +470,6 @@ test("mapbox", (t) => { t.test('POSTs event when previously stored anonId is not a valid uuid', (t) => { const now = +Date.now(); - window.localStorage.setItem(`mapbox.eventData.uuid:${config.ACCESS_TOKEN}`, 'anonymous'); window.localStorage.setItem(`mapbox.eventData:${config.ACCESS_TOKEN}`, JSON.stringify({ lastSuccess: now @@ -720,7 +724,6 @@ test("mapbox", (t) => { }); t.test('POSTs cn event when API_URL changes to cn endpoint', (t) => { - const previousUrl = config.API_URL; config.API_URL = 'https://api.mapbox.cn'; event.postMapLoadEvent(mapboxTileURLs, 1); @@ -729,7 +732,20 @@ test("mapbox", (t) => { req.respond(200); t.true(req.url.indexOf('https://events.mapbox.cn') > -1); - config.API_URL = previousUrl; + t.end(); + }); + + t.test('POSTs no event when API_URL unavailable', (t) => { + config.API_URL = null; + event.postMapLoadEvent(mapboxTileURLs, 1); + t.equal(window.server.requests.length, 0, 'no events posted'); + t.end(); + }); + + t.test('POSTs no event when API_URL is non-standard', (t) => { + config.API_URL = "https://api.example.com"; + event.postMapLoadEvent(mapboxTileURLs, 1); + t.equal(window.server.requests.length, 0, 'no events posted'); t.end(); });