Skip to content

Commit

Permalink
extract isMapboxHTTPURL function
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaefer committed Oct 5, 2018
1 parent db24652 commit 1af27d8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ function isMapboxURL(url: string) {
return url.indexOf('mapbox:') === 0;
}

export { isMapboxURL };
const mapboxHTTPURLRe = /^((https?:)?\/\/)?([^\/]+\.)?mapbox\.c(n|om)(\/|\?|$)/i;
function isMapboxHTTPURL(url: string): boolean {
return mapboxHTTPURLRe.test(url);
}

export { isMapboxURL, isMapboxHTTPURL };

export const normalizeStyleURL = function(url: string, accessToken?: string): string {
if (!isMapboxURL(url)) return url;
Expand Down Expand Up @@ -146,7 +151,7 @@ export class TurnstileEvent {
// mapbox tiles.
if (config.ACCESS_TOKEN &&
Array.isArray(tileUrls) &&
tileUrls.some((url) => { return /(mapbox\.c)(n|om)/i.test(url); })) {
tileUrls.some(url => isMapboxHTTPURL(url))) {
this.queueRequest(browser.now());
}
}
Expand Down
66 changes: 44 additions & 22 deletions test/unit/util/mapbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import window from '../../../src/util/window';
import { uuid } from '../../../src/util/util';
import { version } from '../../../package.json';

const mapboxTileURLs = [
'https://a.tiles.mapbox.com/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf',
'https://b.tiles.mapbox.com/v4/mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7/{z}/{x}/{y}.vector.pbf'
];

const nonMapboxTileURLs = [
'https://a.example.com/tiles/{z}/{x}/{y}.mvt',
'https://b.example.com/tiles/{z}/{x}/{y}.mvt'
];

test("mapbox", (t) => {
const mapboxSource = 'mapbox://user.map';
const nonMapboxSource = 'http://www.example.com/tiles.json';
Expand All @@ -15,6 +25,18 @@ test("mapbox", (t) => {
callback();
});

t.test('.isMapboxHTTPURL', (t) => {
t.ok(mapbox.isMapboxHTTPURL('http://mapbox.com'));
t.ok(mapbox.isMapboxHTTPURL('https://mapbox.com'));
t.ok(mapbox.isMapboxHTTPURL('https://mapbox.com/'));
t.ok(mapbox.isMapboxHTTPURL('https://mapbox.com?'));
t.ok(mapbox.isMapboxHTTPURL('https://api.mapbox.com/tiles'));
t.ok(mapbox.isMapboxHTTPURL('https://api.mapbox.cn/tiles'));
t.ok(mapbox.isMapboxHTTPURL('http://a.tiles.mapbox.cn/tiles'));
t.notOk(mapbox.isMapboxHTTPURL('http://example.com/mapbox.com'));
t.end();
});

t.test('.normalizeStyleURL', (t) => {
t.test('returns an API URL with access_token parameter when no query string', (t) => {
t.equal(mapbox.normalizeStyleURL('mapbox://styles/user/style'), 'https://api.mapbox.com/styles/v1/user/style?access_token=key');
Expand Down Expand Up @@ -300,14 +322,14 @@ test("mapbox", (t) => {
t.test('does not POST when mapboxgl.ACCESS_TOKEN is not set', (t) => {
config.ACCESS_TOKEN = null;

event.postTurnstileEvent([' a.tiles.mapxbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

t.equal(window.server.requests.length, 0);
t.end();
});

t.test('does not POST when url does not point to mapbox.com', (t) => {
event.postTurnstileEvent(['a.tiles.boxmap.com']);
event.postTurnstileEvent(nonMapboxTileURLs);

t.equal(window.server.requests.length, 0);
t.end();
Expand All @@ -317,7 +339,7 @@ test("mapbox", (t) => {
const previousUrl = config.API_URL;
config.API_URL = 'https://api.mapbox.cn';

event.postTurnstileEvent(['a.tiles.mapbox.cn']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand Down Expand Up @@ -361,7 +383,7 @@ test("mapbox", (t) => {

t.stub(browser, 'now').callsFake(() => now + 5); // A bit later

event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

t.false(window.server.requests.length);
t.end();
Expand All @@ -377,7 +399,7 @@ test("mapbox", (t) => {

t.stub(browser, 'now').callsFake(() => now + ms25Hours); // next day

event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -397,7 +419,7 @@ test("mapbox", (t) => {

t.stub(browser, 'now').callsFake(() => now); // Past relative ot lastSuccess

event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -410,12 +432,12 @@ test("mapbox", (t) => {
t.test('does not POST appuserTurnstile event second time within same calendar day', (t) => {
let now = +Date.now();
t.stub(browser, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

//Post second event
const firstEvent = now;
now += (60 * 1000); // A bit later
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -431,12 +453,12 @@ test("mapbox", (t) => {
t.test('does not POST appuserTurnstile event second time when clock goes backwards less than a day', (t) => {
let now = +Date.now();
t.stub(browser, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

//Post second event
const firstEvent = now;
now -= (60 * 1000); // A bit earlier
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -452,7 +474,7 @@ test("mapbox", (t) => {
t.test('POSTs appuserTurnstile event when access token changes', (t) => {
config.ACCESS_TOKEN = 'pk.new.*';

event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -467,7 +489,7 @@ test("mapbox", (t) => {

t.test('when LocalStorage is not available', (t) => {
t.test('POSTs appuserTurnstile event', (t) => {
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -486,11 +508,11 @@ test("mapbox", (t) => {
let now = +Date.now();
const firstEvent = now;
t.stub(browser, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

//Post second event
now += (60 * 1000); // A bit later
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -507,11 +529,11 @@ test("mapbox", (t) => {
let now = +Date.now();
const firstEvent = now;
t.stub(browser, 'now').callsFake(() => now);
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

//Post second event
now -= (60 * 1000); // A bit earlier
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -527,7 +549,7 @@ test("mapbox", (t) => {
t.test('POSTs appuserTurnstile event when access token changes', (t) => {
config.ACCESS_TOKEN = 'pk.new.*';

event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const req = window.server.requests[0];
req.respond(200);
Expand All @@ -546,11 +568,11 @@ test("mapbox", (t) => {
let now = +Date.now();
t.stub(browser, 'now').callsFake(() => now);

event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

now += ms25Hours; // Add a day
const tomorrow = now;
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

let req = window.server.requests[0];
req.respond(200);
Expand All @@ -573,15 +595,15 @@ test("mapbox", (t) => {
t.stub(browser, 'now').callsFake(() => now);

const today = now;
event.postTurnstileEvent(['a.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const laterToday = now + 1;
now = laterToday;
event.postTurnstileEvent(['b.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const tomorrow = laterToday + ms25Hours; // Add a day
now = tomorrow;
event.postTurnstileEvent(['c.tiles.mapbox.com']);
event.postTurnstileEvent(mapboxTileURLs);

const reqToday = window.server.requests[0];
reqToday.respond(200);
Expand Down

0 comments on commit 1af27d8

Please sign in to comment.