Skip to content

Commit

Permalink
Allow examples to use local files for tiles instead of external data.
Browse files Browse the repository at this point in the history
This removes the network dependency to openstreetmap.org in the tests.

As additional examples are added, the tiles.tgz file will need to be updated.
  • Loading branch information
manthey committed Jan 4, 2017
1 parent 04f04cf commit fd89026
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 13 deletions.
4 changes: 3 additions & 1 deletion examples/lines/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ $(function () {
lineFeature.draw();
var text = 'Shown: ' + segments;
$('#lines-shown').text(text).attr('title', text);
$('#map').addClass('ready');
map.onIdle(function () {
$('#map').addClass('ready');
});
}

/**
Expand Down
26 changes: 24 additions & 2 deletions karma-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,27 @@ var notes_middleware = function (config) {
};
};

/**
* Express style middleware to handle REST requests for OSM tiles on the test
* server.
*/
var osmtiles_middleware = function (config) {
return function (request, response, next) {
var match = request.url.match(/.*http:\/\/[a-c]\.tile.openstreetmap.org\/([0-9]+\/[0-9]+\/[0-9]+.png)$/);
/* Serve tiles if they have been proxied */
if (match && request.method === 'GET') {
var imagePath = 'dist/data/tiles/' + match[1];
var img = new Buffer(fs.readFileSync(imagePath));
response.setHeader('Content-Type', 'image/png');
response.setHeader('Content-Length', img.length);
response.setHeader('Access-Control-Allow-Origin', '*');
response.writeHead(200);
return response.end(img);
}
next();
};
};

module.exports = function (config) {
var newConfig = {
autoWatch: false,
Expand Down Expand Up @@ -218,10 +239,12 @@ module.exports = function (config) {
'kjhtml'
],
middleware: [
'notes'
'notes',
'osmtiles'
],
plugins: [
{'middleware:notes': ['factory', notes_middleware]},
{'middleware:osmtiles': ['factory', osmtiles_middleware]},
'karma-*'
],
preprocessors: {},
Expand All @@ -239,6 +262,5 @@ module.exports = function (config) {
}
};
newConfig.preprocessors[test_case] = ['webpack', 'sourcemap'];

return newConfig;
};
2 changes: 1 addition & 1 deletion testing/test-data/base-images.tgz.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2f4ea11110deac8f5de053c49d09ce1e
914356846f8c541a813bb7e53efc6c57
2 changes: 1 addition & 1 deletion testing/test-data/base-images.tgz.url
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://data.kitware.com/api/v1/file/586bfea38d777f05f44a5c6f/download
https://data.kitware.com/api/v1/file/586d1ff18d777f05f44a5c75/download
2 changes: 1 addition & 1 deletion testing/test-data/tiles.tgz.md5
Original file line number Diff line number Diff line change
@@ -1 +1 @@
73d3e6e8800e5d82d5f668413379373a
765a50e781a0f400068cead89837483e
2 changes: 1 addition & 1 deletion testing/test-data/tiles.tgz.url
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://data.kitware.com/api/v1/file/560a89528d777f7bfaadd3f8/download
https://data.kitware.com/api/v1/file/586d1e958d777f05f44a5c72/download
18 changes: 13 additions & 5 deletions tests/data/proxy-for-tests.pac
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/* global dnsDomainIs */

function FindProxyForURL(url, host) {
// Don't serve certain remote addresses
if (dnsDomainIs(host, "fonts.googleapis.com")) {
return "PROXY http://192.0.2.0";
}
return "DIRECT";
// Don't serve certain remote addresses
if (dnsDomainIs(host, 'fonts.googleapis.com')) {
// If we use a testing address such as 192.0.2.0, requests will take a
// long time to fail. Using an address starting with 0 fails promptly.
return 'PROXY 0.0.0.1';
}
// Redirect tiles to our test server
if (dnsDomainIs(host, '.tile.openstreetmap.org')) {
return 'PROXY 127.0.0.1:9876';
}
return 'DIRECT';
}
2 changes: 1 addition & 1 deletion tests/example-cases/lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('lines example', function () {
});

it('basic', function (done) {
$('#map').attr('src', '/examples/lines/index.html?showmap=false');
$('#map').attr('src', '/examples/lines/index.html');
imageTest.imageTest('exampleLines', '#map', 0.0015, done, null, 0, 2, '#map.ready');
}, 10000);
it('more lines', function (done) {
Expand Down

0 comments on commit fd89026

Please sign in to comment.