From 71c146a21b8b7c834097bf1a5a8351eae6abe4e4 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Wed, 4 Jan 2017 11:13:04 -0500 Subject: [PATCH] Allow examples to use local files for tiles instead of external data. 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. --- examples/lines/main.js | 4 +++- karma-base.js | 26 ++++++++++++++++++++++++-- testing/test-data/base-images.tgz.md5 | 2 +- testing/test-data/base-images.tgz.url | 2 +- testing/test-data/tiles.tgz.md5 | 2 +- testing/test-data/tiles.tgz.url | 2 +- tests/data/proxy-for-tests.pac | 18 +++++++++++++----- tests/example-cases/lines.js | 2 +- 8 files changed, 45 insertions(+), 13 deletions(-) diff --git a/examples/lines/main.js b/examples/lines/main.js index 4a50e0d27d..0a3846ec7e 100644 --- a/examples/lines/main.js +++ b/examples/lines/main.js @@ -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'); + }); } /** diff --git a/karma-base.js b/karma-base.js index 95661ad6a7..7c21470962 100644 --- a/karma-base.js +++ b/karma-base.js @@ -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, @@ -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: {}, @@ -239,6 +262,5 @@ module.exports = function (config) { } }; newConfig.preprocessors[test_case] = ['webpack', 'sourcemap']; - return newConfig; }; diff --git a/testing/test-data/base-images.tgz.md5 b/testing/test-data/base-images.tgz.md5 index 66cd14f990..0e365a4194 100644 --- a/testing/test-data/base-images.tgz.md5 +++ b/testing/test-data/base-images.tgz.md5 @@ -1 +1 @@ -2f4ea11110deac8f5de053c49d09ce1e \ No newline at end of file +914356846f8c541a813bb7e53efc6c57 \ No newline at end of file diff --git a/testing/test-data/base-images.tgz.url b/testing/test-data/base-images.tgz.url index 9163801cec..5f1e29b242 100644 --- a/testing/test-data/base-images.tgz.url +++ b/testing/test-data/base-images.tgz.url @@ -1 +1 @@ -https://data.kitware.com/api/v1/file/586bfea38d777f05f44a5c6f/download \ No newline at end of file +https://data.kitware.com/api/v1/file/586d1ff18d777f05f44a5c75/download \ No newline at end of file diff --git a/testing/test-data/tiles.tgz.md5 b/testing/test-data/tiles.tgz.md5 index 87ea977455..a0da2d8b27 100644 --- a/testing/test-data/tiles.tgz.md5 +++ b/testing/test-data/tiles.tgz.md5 @@ -1 +1 @@ -73d3e6e8800e5d82d5f668413379373a \ No newline at end of file +910b4894cc906dc3762dc9e314fd4025 diff --git a/testing/test-data/tiles.tgz.url b/testing/test-data/tiles.tgz.url index 0a8bcc380b..ac4e8d8f97 100644 --- a/testing/test-data/tiles.tgz.url +++ b/testing/test-data/tiles.tgz.url @@ -1 +1 @@ -https://data.kitware.com/api/v1/file/560a89528d777f7bfaadd3f8/download +https://data.kitware.com/api/v1/file/586d1e958d777f05f44a5c72/download diff --git a/tests/data/proxy-for-tests.pac b/tests/data/proxy-for-tests.pac index 01b6754f6c..c2f9327b0b 100644 --- a/tests/data/proxy-for-tests.pac +++ b/tests/data/proxy-for-tests.pac @@ -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'; } diff --git a/tests/example-cases/lines.js b/tests/example-cases/lines.js index d5ee14b0b0..eb233e72ae 100644 --- a/tests/example-cases/lines.js +++ b/tests/example-cases/lines.js @@ -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) {