Windshaft-cartodb/test/acceptance/ported/blend-http-fallback-test.js

147 lines
4.6 KiB
JavaScript
Raw Normal View History

'use strict';
require('../../support/test-helper');
2015-07-08 05:46:58 +08:00
var assert = require('../../support/assert');
var testClient = require('./support/test-client');
2015-07-08 05:46:58 +08:00
var fs = require('fs');
var http = require('http');
var path = require('path');
2015-07-08 05:46:58 +08:00
2019-10-22 01:07:24 +08:00
describe('blend http fallback', function () {
2015-07-08 05:46:58 +08:00
var IMG_TOLERANCE_PER_MIL = 20;
var httpRendererResourcesServer;
2019-10-22 01:07:24 +08:00
before(function (done) {
2015-07-08 05:46:58 +08:00
// Start a server to test external resources
2019-10-22 01:07:24 +08:00
httpRendererResourcesServer = http.createServer(function (request, response) {
2015-07-08 05:46:58 +08:00
if (request.url.match(/^\/error404\//)) {
response.writeHead(404);
response.end();
} else {
var filename = path.join(__dirname, '/../../fixtures/http/light_nolabels-1-0-0.png');
2015-07-08 05:46:58 +08:00
if (request.url.match(/^\/dark\//)) {
filename = path.join(__dirname, '/../../fixtures/http/dark_nolabels-1-0-0.png');
2015-07-08 05:46:58 +08:00
}
2019-10-22 01:07:24 +08:00
fs.readFile(filename, { encoding: 'binary' }, function (err, file) {
2019-10-25 00:38:37 +08:00
if (err) {
return done(err);
}
2015-07-08 05:46:58 +08:00
response.writeHead(200);
2019-10-22 01:07:24 +08:00
response.write(file, 'binary');
2015-07-08 05:46:58 +08:00
response.end();
});
}
});
httpRendererResourcesServer.listen(8033, done);
});
2019-10-22 01:07:24 +08:00
after(function (done) {
2015-07-08 05:46:58 +08:00
httpRendererResourcesServer.close(done);
});
var mapConfig = {
version: '1.2.0',
layers: [
{
type: 'plain', // <- 0
options: {
color: '#fabada'
}
},
{
type: 'http', // <- 1
options: {
urlTemplate: 'http://127.0.0.1:8033/{s}/{z}/{x}/{y}.png',
subdomains: ['light']
}
},
{
type: 'http', // <- 2
options: {
urlTemplate: 'http://127.0.0.1:8033/{s}/{z}/{x}/{y}.png',
subdomains: ['dark']
}
},
{
type: 'http', // <- 3
options: {
urlTemplate: 'http://127.0.0.1:8033/{s}/{z}/{x}/{y}.png',
subdomains: ['error404']
}
},
{
type: 'mapnik', // <- 4
options: {
sql: 'SELECT * FROM populated_places_simple_reduced',
cartocss: '#layer { marker-fill:red; } #layer { marker-width: 2; }',
cartocss_version: '2.3.0',
geom_column: 'the_geom'
}
}
]
};
var filteredLayersSuite = [
2019-10-22 01:07:24 +08:00
// ['all'], // layers displayed: 2 + 4, skipping 3 as it fails
2015-07-08 05:46:58 +08:00
[0, 4],
[0, 3], // skips layer 3 as it fails
[1, 2],
[1, 3],
[2, 3],
[3, 4]
];
2019-10-22 01:07:24 +08:00
function blendPngFixture (layers) {
2015-07-08 05:46:58 +08:00
return './test/fixtures/blend/http_fallback/blend-layers-' + layers.join('.') + '-zxy-1.0.0.png';
}
2019-10-22 01:07:24 +08:00
filteredLayersSuite.forEach(function (filteredLayers) {
2015-07-08 05:46:58 +08:00
var layerFilter = filteredLayers.join(',');
var tileRequest = {
z: 1,
x: 0,
y: 0,
layer: layerFilter,
format: 'png'
};
it('should fallback on http error while blending layers ' + layerFilter + '/1/0/0.png', function (done) {
2019-10-22 01:07:24 +08:00
testClient.getTileLayer(mapConfig, tileRequest, function (err, res) {
2019-10-25 00:38:37 +08:00
assert.ifError(err);
2016-02-22 23:36:06 +08:00
assert.imageBufferIsSimilarToFile(res.body, blendPngFixture(filteredLayers), IMG_TOLERANCE_PER_MIL,
2019-10-22 01:07:24 +08:00
function (err) {
2016-02-22 23:36:06 +08:00
assert.ok(!err, err);
done();
}
);
2015-07-08 05:46:58 +08:00
});
});
});
2019-10-22 01:07:24 +08:00
it('should keep failing when http layer is requested individually', function (done) {
2015-07-08 05:46:58 +08:00
var tileRequest = {
z: 1,
x: 0,
y: 0,
layer: 3,
format: 'png'
};
var expectedResponse = {
status: 400,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
2019-10-22 01:07:24 +08:00
testClient.getTileLayer(mapConfig, tileRequest, expectedResponse, function (err, res) {
2015-07-08 05:46:58 +08:00
assert.ok(!err);
var parsedBody = JSON.parse(res.body);
assert.deepStrictEqual(parsedBody.errors, [
2019-10-22 01:07:24 +08:00
'Unable to fetch http tile: http://127.0.0.1:8033/error404/1/0/0.png [404]'
]);
2015-07-08 05:46:58 +08:00
done();
});
});
});