Windshaft-cartodb/test/acceptance/ported/static-maps-test.js

146 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 http = require('http');
var fs = require('fs');
var path = require('path');
2015-07-08 05:46:58 +08:00
2019-10-22 01:07:24 +08:00
describe('static_maps', function () {
2015-07-08 05:46:58 +08:00
var validUrlTemplate = 'http://127.0.0.1:8033/{s}/{z}/{x}/{y}.png';
var invalidUrlTemplate = 'http://127.0.0.1:8033/INVALID/{z}/{x}/{y}.png';
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) {
var filename = path.join(__dirname, '/../../fixtures/http/basemap.png');
2019-10-22 01:07:24 +08:00
fs.readFile(filename, { encoding: 'binary' }, function (err, file) {
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);
});
2019-10-22 01:07:24 +08:00
function staticMapConfig (urlTemplate, cartocss) {
2015-07-08 05:46:58 +08:00
return {
version: '1.2.0',
layers: [
{
type: 'http',
options: {
urlTemplate: urlTemplate,
subdomains: ['abcd']
}
},
{
type: 'mapnik',
options: {
sql: 'SELECT * FROM populated_places_simple_reduced',
cartocss: cartocss || '#layer { marker-fill:red; } #layer { marker-width: 2; }',
cartocss_version: '2.3.0'
}
}
]
};
}
2019-10-22 01:07:24 +08:00
var zoom = 3;
var lat = 0;
var lon = 0;
var width = 400;
var height = 300;
2015-07-08 05:46:58 +08:00
it('center image', function (done) {
var mapConfig = staticMapConfig(validUrlTemplate);
2019-10-22 01:07:24 +08:00
testClient.getStaticCenter(mapConfig, zoom, lat, lon, width, height, function (err, res, image) {
2015-07-08 05:46:58 +08:00
if (err) {
return done(err);
}
assert.strictEqual(image.width(), width);
assert.strictEqual(image.height(), height);
2015-07-08 05:46:58 +08:00
done();
});
});
it('center image with invalid basemap', function (done) {
var mapConfig = staticMapConfig(invalidUrlTemplate);
2019-10-22 01:07:24 +08:00
testClient.getStaticCenter(mapConfig, zoom, lat, lon, width, height, function (err, res, image) {
2015-07-08 05:46:58 +08:00
if (err) {
return done(err);
}
assert.strictEqual(image.width(), width);
assert.strictEqual(image.height(), height);
2015-07-08 05:46:58 +08:00
done();
});
});
2019-10-22 01:07:24 +08:00
var west = -90;
var south = -45;
var east = 90;
var north = 45;
var bbWidth = 640;
var bbHeight = 480;
2015-07-08 05:46:58 +08:00
it('bbox', function (done) {
var mapConfig = staticMapConfig(validUrlTemplate);
2019-10-22 01:07:24 +08:00
testClient.getStaticBbox(mapConfig, west, south, east, north, bbWidth, bbHeight, function (err, res, image) {
2015-07-08 05:46:58 +08:00
if (err) {
return done(err);
}
assert.strictEqual(image.width(), bbWidth);
assert.strictEqual(image.height(), bbHeight);
2015-07-08 05:46:58 +08:00
done();
});
});
it('should not fail for coordinates out of range', function (done) {
var outOfRangeHeight = 3000;
var mapConfig = staticMapConfig(validUrlTemplate);
2019-10-22 01:07:24 +08:00
testClient.getStaticCenter(mapConfig, 1, lat, lon, width, outOfRangeHeight, function (err, res, image) {
2015-07-08 05:46:58 +08:00
if (err) {
return done(err);
}
assert.strictEqual(image.width(), width);
assert.strictEqual(image.height(), outOfRangeHeight);
2015-07-08 05:46:58 +08:00
done();
});
});
it('should keep failing for other errors', function (done) {
2019-10-22 01:07:24 +08:00
var invalidStyleForZoom = '#layer { marker-fill:red; } #layer[zoom=' + zoom + '] { marker-width: [wadus] * 2; }';
2015-07-08 05:46:58 +08:00
var mapConfig = staticMapConfig(validUrlTemplate, invalidStyleForZoom);
var expectedResponse = {
statusCode: 400,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
2019-10-22 01:07:24 +08:00
testClient.getStaticCenter(mapConfig, zoom, lat, lon, width, height, expectedResponse, function (err, res) {
2015-07-08 05:46:58 +08:00
assert.ok(!err);
var parsedBody = JSON.parse(res.body);
assert.ok(parsedBody.errors);
assert.ok(parsedBody.errors.length);
assert.ok(parsedBody.errors[0].match(/column \"wadus\" does not exist/));
done();
});
});
});