2018-10-24 00:39:02 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 16:44:45 +08:00
|
|
|
var testHelper = require('../../support/test-helper');
|
2015-07-08 05:46:58 +08:00
|
|
|
|
|
|
|
var assert = require('../../support/assert');
|
|
|
|
var fs = require('fs');
|
2019-10-07 16:44:45 +08:00
|
|
|
var PortedServerOptions = require('./support/ported-server-options');
|
2015-07-08 05:46:58 +08:00
|
|
|
var http = require('http');
|
2019-10-07 16:44:45 +08:00
|
|
|
var testClient = require('./support/test-client');
|
2015-09-18 00:12:45 +08:00
|
|
|
var nock = require('nock');
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
describe('external resources', function () {
|
2015-07-08 05:46:58 +08:00
|
|
|
var res_serv; // resources server
|
2019-10-22 01:07:24 +08:00
|
|
|
var res_serv_status = { numrequests: 0 }; // status of resources server
|
2018-04-17 16:44:44 +08:00
|
|
|
var res_serv_port;
|
2015-07-08 05:46:58 +08:00
|
|
|
|
|
|
|
var IMAGE_EQUALS_TOLERANCE_PER_MIL = 25;
|
|
|
|
|
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
|
|
|
res_serv = http.createServer(function (request, response) {
|
2015-07-08 05:46:58 +08:00
|
|
|
++res_serv_status.numrequests;
|
|
|
|
var filename = __dirname + '/../../fixtures/markers' + request.url;
|
2019-10-22 01:07:24 +08:00
|
|
|
fs.readFile(filename, 'binary', function (err, file) {
|
|
|
|
if (err) {
|
|
|
|
response.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
|
|
response.write('404 Not Found\n');
|
|
|
|
} else {
|
|
|
|
response.writeHead(200);
|
|
|
|
response.write(file, 'binary');
|
|
|
|
}
|
|
|
|
response.end();
|
2015-07-08 05:46:58 +08:00
|
|
|
});
|
|
|
|
});
|
2018-04-17 16:44:44 +08:00
|
|
|
|
|
|
|
const host = '127.0.0.1';
|
|
|
|
const markersServer = res_serv.listen(0);
|
|
|
|
|
|
|
|
res_serv_port = markersServer.address().port;
|
|
|
|
|
|
|
|
nock.disableNetConnect();
|
|
|
|
nock.enableNetConnect(host);
|
|
|
|
|
|
|
|
markersServer.on('listening', done);
|
2015-07-08 05:46:58 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
after(function (done) {
|
2015-09-26 01:56:28 +08:00
|
|
|
testHelper.rmdirRecursiveSync(global.environment.millstone.cache_basedir);
|
2015-09-17 00:09:39 +08:00
|
|
|
|
2018-04-17 16:44:44 +08:00
|
|
|
nock.cleanAll();
|
|
|
|
nock.enableNetConnect();
|
|
|
|
|
2015-09-26 01:05:37 +08:00
|
|
|
// Close the resources server
|
|
|
|
res_serv.close(done);
|
2015-09-25 20:42:30 +08:00
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function imageCompareFn (fixture, done) {
|
|
|
|
return function (err, res) {
|
2015-07-08 05:46:58 +08:00
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
2016-02-22 23:36:06 +08:00
|
|
|
var referenceImagePath = './test/fixtures/' + fixture;
|
|
|
|
assert.imageBufferIsSimilarToFile(res.body, referenceImagePath, IMAGE_EQUALS_TOLERANCE_PER_MIL, done);
|
2015-07-08 05:46:58 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('basic external resource', function (done) {
|
2015-09-17 23:51:34 +08:00
|
|
|
var circleStyle = "#test_table_3 { marker-file: url('http://127.0.0.1:" + res_serv_port +
|
2015-07-08 05:46:58 +08:00
|
|
|
"/circle.svg'); marker-transform:'scale(0.2)'; }";
|
|
|
|
|
|
|
|
testClient.getTile(testClient.defaultTableMapConfig('test_table_3', circleStyle), 13, 4011, 3088,
|
|
|
|
imageCompareFn('test_table_13_4011_3088_svg1.png', done));
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('different external resource', function (done) {
|
2015-09-17 23:51:34 +08:00
|
|
|
var squareStyle = "#test_table_3 { marker-file: url('http://127.0.0.1:" + res_serv_port +
|
2015-07-08 05:46:58 +08:00
|
|
|
"/square.svg'); marker-transform:'scale(0.2)'; }";
|
|
|
|
|
|
|
|
testClient.getTile(testClient.defaultTableMapConfig('test_table_3', squareStyle), 13, 4011, 3088,
|
|
|
|
imageCompareFn('test_table_13_4011_3088_svg2.png', done));
|
|
|
|
});
|
|
|
|
|
|
|
|
// See http://github.com/CartoDB/Windshaft/issues/107
|
2019-10-22 01:07:24 +08:00
|
|
|
it('external resources get localized on renderer creation if not locally cached', function (done) {
|
2015-07-08 05:46:58 +08:00
|
|
|
var options = {
|
|
|
|
serverOptions: PortedServerOptions
|
|
|
|
};
|
|
|
|
|
2015-09-17 23:36:23 +08:00
|
|
|
var externalResourceStyle = "#test_table_3{marker-file: url('http://127.0.0.1:" + res_serv_port +
|
2015-07-08 05:46:58 +08:00
|
|
|
"/square.svg'); marker-transform:'scale(0.2)'; }";
|
|
|
|
|
|
|
|
var externalResourceMapConfig = testClient.defaultTableMapConfig('test_table_3', externalResourceStyle);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.createLayergroup(externalResourceMapConfig, options, function () {
|
2015-07-08 05:46:58 +08:00
|
|
|
var externalResourceRequestsCount = res_serv_status.numrequests;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.createLayergroup(externalResourceMapConfig, options, function () {
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res_serv_status.numrequests, externalResourceRequestsCount);
|
2015-07-08 05:46:58 +08:00
|
|
|
|
|
|
|
// reset resources cache
|
2015-09-26 01:56:28 +08:00
|
|
|
testHelper.rmdirRecursiveSync(global.environment.millstone.cache_basedir);
|
2015-07-08 05:46:58 +08:00
|
|
|
|
|
|
|
externalResourceMapConfig = testClient.defaultTableMapConfig('test_table_3 ', externalResourceStyle);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.createLayergroup(externalResourceMapConfig, options, function () {
|
2019-10-22 01:41:03 +08:00
|
|
|
assert.strictEqual(res_serv_status.numrequests, externalResourceRequestsCount + 1);
|
2015-07-08 05:46:58 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
it('referencing unexistant external resources returns an error', function (done) {
|
|
|
|
var url = 'http://127.0.0.1:' + res_serv_port + '/notfound.png';
|
2015-07-08 05:46:58 +08:00
|
|
|
var style = "#test_table_3{marker-file: url('" + url + "'); marker-transform:'scale(0.2)'; }";
|
|
|
|
|
|
|
|
var mapConfig = testClient.defaultTableMapConfig('test_table_3', style);
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
testClient.createLayergroup(mapConfig, { statusCode: 400 }, function (err, res) {
|
2019-10-22 01:52:51 +08:00
|
|
|
assert.deepStrictEqual(JSON.parse(res.body).errors, [
|
2016-06-13 22:14:01 +08:00
|
|
|
"Unable to download '" + url + "' for 'style0' (server returned 404)"]
|
|
|
|
);
|
2015-07-08 05:46:58 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|