Refactor imageryBlacklists so it can be called without triggering a GET

Previous code was problematic because we need the function to
to properly blacklist custom imagery passed in through the url hash,
before iD is completely initialized.  Triggering GET causes tests to
break because osm service testing has side effects :-/
This commit is contained in:
Bryan Housel
2016-12-04 21:34:34 -05:00
parent c353684ad3
commit a7ac44f6e8
5 changed files with 35 additions and 52 deletions
+4 -5
View File
@@ -381,7 +381,6 @@ export function coreContext() {
ui = uiInit(context);
connection = services.osm;
background = rendererBackground(context);
features = rendererFeatures(context);
presets = presetIndex();
@@ -396,16 +395,16 @@ export function coreContext() {
context.zoomOutFurther = map.zoomOutFurther;
context.redrawEnable = map.redrawEnable;
background.init();
presets.init();
areaKeys = presets.areaKeys();
_.each(services, function(service) {
if (service && typeof service.init === 'function') {
service.init(context);
}
});
background.init();
presets.init();
areaKeys = presets.areaKeys();
return utilRebind(context, dispatch, 'on');
}
+3 -7
View File
@@ -12,8 +12,6 @@ export function rendererBackground(context) {
var dispatch = d3.dispatch('change'),
baseLayer = rendererTileLayer(context).projection(context.projection),
overlayLayers = [],
defaultBlacklist = '.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*',
blacklists = [defaultBlacklist],
backgroundSources;
@@ -130,10 +128,8 @@ export function rendererBackground(context) {
background.baseLayerSource = function(d) {
if (!arguments.length) return baseLayer.source();
// // test source against OSM imagery blacklists..
// context.connection().imageryBlacklists(function(err, val) {
// if (!err) blacklists = val;
// });
// test source against OSM imagery blacklists..
var blacklists = context.connection().imageryBlacklists();
var fail = false,
tested = 0,
@@ -152,7 +148,7 @@ export function rendererBackground(context) {
// ensure at least one test was run.
if (!tested) {
regex = new RegExp(defaultBlacklist);
regex = new RegExp('.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*');
fail = regex.test(d.template);
}
+17 -29
View File
@@ -13,6 +13,7 @@ var dispatch = d3.dispatch('authLoading', 'authDone', 'change', 'loading', 'load
useHttps = window.location.protocol === 'https:',
protocol = useHttps ? 'https:' : 'http:',
urlroot = protocol + '//www.openstreetmap.org',
blacklists = ['.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*'],
inflight = {},
loadedTiles = {},
tileZoom = 16,
@@ -25,7 +26,6 @@ var dispatch = d3.dispatch('authLoading', 'authDone', 'change', 'loading', 'load
}),
rateLimitError,
userDetails,
capabilities,
off;
@@ -162,7 +162,6 @@ export default {
reset: function() {
userDetails = undefined;
rateLimitError = undefined;
capabilities = undefined;
_.forEach(inflight, abortRequest);
loadedTiles = {};
inflight = {};
@@ -431,15 +430,26 @@ export default {
},
// `status` will always request the `api/capabilities` resource..
status: function(callback) {
function done(xml) {
capabilities = xml;
// update blacklists
var elements = xml.getElementsByTagName('blacklist'),
regexes = [];
for (var i = 0; i < elements.length; i++) {
var regex = elements[i].getAttribute('regex'); // needs unencode?
if (regex) {
regexes.push(regex);
}
}
if (regexes.length) {
blacklists = regexes;
}
if (rateLimitError) {
callback(rateLimitError, 'rateLimited');
} else {
var apiStatus = capabilities.getElementsByTagName('status'),
var apiStatus = xml.getElementsByTagName('status'),
val = apiStatus[0].getAttribute('api');
callback(undefined, val);
@@ -452,30 +462,8 @@ export default {
},
// `imageryBlacklists` will reuse the previous `api/capabilities` result, if available..
imageryBlacklists: function(callback) {
function done(xml) {
capabilities = xml;
var elements = capabilities.getElementsByTagName('blacklist'),
blacklists = [];
for (var i = 0; i < elements.length; i++) {
var regex = elements[i].getAttribute('regex'); // needs unencode?
if (regex) {
blacklists.push(regex);
}
}
callback(undefined, blacklists);
}
if (capabilities) {
done(capabilities);
} else {
d3.xml(urlroot + '/api/capabilities').get()
.on('load', done)
.on('error', callback);
}
imageryBlacklists: function() {
return blacklists;
},
+3 -6
View File
@@ -109,14 +109,11 @@ export function uiBackground(context) {
function editCustom() {
d3.event.preventDefault();
var template = window.prompt(t('background.custom_prompt'), customTemplate);
if (!template ||
template.indexOf('google.com') !== -1 ||
template.indexOf('googleapis.com') !== -1 ||
template.indexOf('google.ru') !== -1) {
if (template) {
setCustom(template);
} else {
selectLayer();
return;
}
setCustom(template);
}
+8 -5
View File
@@ -458,13 +458,15 @@ describe('iD.serviceOsm', function () {
'<timeout seconds="300"/>' +
'<status database="online" api="online" gpx="online"/>' +
'</api>' +
'<policy><imagery><blacklist regex=".*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*"/></imagery></policy>' +
'<policy><imagery>' +
'<blacklist regex="\.foo\.com"/>' +
'<blacklist regex="\.bar\.org"/>' +
'</imagery></policy>' +
'</osm>';
beforeEach(function() {
server = sinon.fakeServer.create();
// connection.reset();
});
afterEach(function() {
@@ -485,9 +487,10 @@ describe('iD.serviceOsm', function () {
});
describe('#imageryBlacklists', function() {
it('gets imagery blacklists', function(done) {
connection.imageryBlacklists(function(err, val) {
expect(val).to.eql(['.*\.google(apis)?\..*/(vt|kh)[\?/].*([xyz]=.*){3}.*']);
it('updates imagery blacklists', function(done) {
connection.status(function() {
var blacklists = connection.imageryBlacklists();
expect(blacklists).to.deep.equal(['\.foo\.com','\.bar\.org']);
done();
});