Skip to content

Commit

Permalink
bug: enhance API_URL logic
Browse files Browse the repository at this point in the history
fixes #7944
  • Loading branch information
Arni Sumarlidason authored Feb 27, 2019
1 parent a2dc230 commit f5c2ae0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/util/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down
7 changes: 5 additions & 2 deletions src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -297,7 +298,8 @@ export class MapLoadEvent extends TelemetryEvent {
postMapLoadEvent(tileUrls: Array<string>, 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()});
Expand Down Expand Up @@ -336,7 +338,8 @@ export class TurnstileEvent extends TelemetryEvent {
postTurnstileEvent(tileUrls: Array<string>) {
//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());
Expand Down
42 changes: 29 additions & 13 deletions test/unit/util/mapbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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();
});

Expand All @@ -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();
});

Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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);
Expand All @@ -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();
});

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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();
});

Expand Down

0 comments on commit f5c2ae0

Please sign in to comment.