Refactor test-client in order to use same interface for named and anonymous maps

This commit is contained in:
Daniel García Aubert 2017-04-26 18:27:18 +02:00
parent c481d6473c
commit 2f4e4246a4
2 changed files with 60 additions and 170 deletions

View File

@ -196,13 +196,13 @@ describe('buffer size per format for named maps', function () {
testCases.forEach(function (test) { testCases.forEach(function (test) {
it(test.desc, function (done) { it(test.desc, function (done) {
var testClient = new TestClient(undefined, 1234, test.template); var testClient = new TestClient(test.template, 1234);
var coords = test.coords; var coords = test.coords;
var options = { var options = {
format: test.format, format: test.format,
placeholders: test.placeholders placeholders: test.placeholders
}; };
testClient.getNamedTile(coords.z, coords.x, coords.y, options, function (err, res, tile) { testClient.getTile(coords.z, coords.x, coords.y, options, function (err, res, tile) {
assert.ifError(err); assert.ifError(err);
// To generate images use: // To generate images use:
//tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-0-test.png'); //tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-0-test.png');

View File

@ -16,15 +16,23 @@ var serverOptions = require('../../lib/cartodb/server_options');
serverOptions.analysis.batch.inlineExecution = true; serverOptions.analysis.batch.inlineExecution = true;
var server = new CartodbWindshaft(serverOptions); var server = new CartodbWindshaft(serverOptions);
function TestClient(mapConfig, apiKey, template) { function TestClient(config, apiKey) {
this.mapConfig = mapConfig; this.mapConfig = isMapConfig(config) ? config : null;
this.template = isTemplate(config) ? config : null;
this.apiKey = apiKey; this.apiKey = apiKey;
this.template = template;
this.keysToDelete = {}; this.keysToDelete = {};
} }
module.exports = TestClient; module.exports = TestClient;
function isMapConfig(config) {
return config && config.layers;
}
function isTemplate(config) {
return config && config.layergroup;
}
module.exports.RESPONSE = { module.exports.RESPONSE = {
ERROR: { ERROR: {
status: 400, status: 400,
@ -407,6 +415,7 @@ TestClient.prototype.getTile = function(z, x, y, params, callback) {
} }
var url = '/api/v1/map'; var url = '/api/v1/map';
var urlNamed = url + '/named';
if (this.apiKey) { if (this.apiKey) {
url += '?' + qs.stringify({api_key: this.apiKey}); url += '?' + qs.stringify({api_key: this.apiKey});
@ -414,17 +423,60 @@ TestClient.prototype.getTile = function(z, x, y, params, callback) {
var layergroupId; var layergroupId;
step( step(
function createLayergroup() { function createTemplate () {
var next = this; var next = this;
if (!self.template) {
return next();
}
if (!self.apiKey) {
return next(new Error('apiKey param is mandatory to create a new template'));
}
params.placeholders = params.placeholders || {};
assert.response(server, assert.response(server,
{ {
url: url, url: urlNamed + '?' + qs.stringify({ api_key: self.apiKey }),
method: 'POST', method: 'POST',
headers: { headers: {
host: 'localhost', host: 'localhost',
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
data: JSON.stringify(self.mapConfig) data: JSON.stringify(self.template)
},
{
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
},
function (res, err) {
if (err) {
return next(err);
}
return next(null, JSON.parse(res.body).template_id);
}
);
},
function createLayergroup(err, templateId) {
var next = this;
var data = templateId ? params.placeholders : self.mapConfig
var path = templateId ?
urlNamed + '/' + templateId + '?' + qs.stringify({api_key: self.apiKey}) :
url;
assert.response(server,
{
url: path,
method: 'POST',
headers: {
host: 'localhost',
'Content-Type': 'application/json'
},
data: JSON.stringify(data)
}, },
{ {
status: 200, status: 200,
@ -697,165 +749,3 @@ module.exports.getStaticMap = function getStaticMap(templateName, params, callba
}); });
}); });
}; };
TestClient.prototype.getNamedTile = function(z, x, y, params, callback) {
if (!this.template) {
throw new Error('Template is not defined');
}
var self = this;
if (!callback) {
callback = params;
params = {};
}
if (!params.placeholders) {
params.placeholders = {};
}
var urlNamed = '/api/v1/map/named';
var url = '/api/v1/map';
if (this.apiKey) {
url += '?' + qs.stringify({api_key: this.apiKey});
}
var templateName;
var layergroupId;
step(
function createTemplate () {
var next = this;
console.log(urlNamed + '?' + qs.stringify({api_key: self.apiKey}))
assert.response(server,
{
url: urlNamed + '?' + qs.stringify({api_key: self.apiKey}),
method: 'POST',
headers: {
host: 'localhost',
'Content-Type': 'application/json'
},
data: JSON.stringify(self.template)
},
{
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
},
function (res, err) {
if (err) {
return next(err);
}
return next(null, JSON.parse(res.body).template_id);
}
);
},
function createLayergroup(err, templateId) {
var next = this;
console.log(urlNamed + '/' + templateId + '?' + qs.stringify({api_key: self.apiKey}))
assert.response(server,
{
url: urlNamed + '/' + templateId + '?' + qs.stringify({api_key: self.apiKey}),
method: 'POST',
headers: {
host: 'localhost',
'Content-Type': 'application/json'
},
data: JSON.stringify(params.placeholders)
},
{
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
},
function(res, err) {
if (err) {
return next(err);
}
return next(null, JSON.parse(res.body).layergroupid);
}
);
},
function getTileResult(err, _layergroupId) {
assert.ifError(err);
var next = this;
layergroupId = _layergroupId;
url = '/api/v1/map/' + layergroupId + '/';
var layers = params.layers;
if (layers !== undefined) {
layers = Array.isArray(layers) ? layers : [layers];
url += layers.join(',') + '/';
}
var format = params.format || 'png';
url += [z,x,y].join('/');
url += '.' + format;
if (self.apiKey) {
url += '?' + qs.stringify({api_key: self.apiKey});
}
var request = {
url: url,
method: 'GET',
headers: {
host: 'localhost'
}
};
var expectedResponse = {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
var isPng = format.match(/png$/);
if (isPng) {
request.encoding = 'binary';
expectedResponse.headers['Content-Type'] = 'image/png';
}
var isMvt = format.match(/mvt$/);
if (isMvt) {
request.encoding = 'binary';
expectedResponse.headers['Content-Type'] = 'application/x-protobuf';
}
assert.response(server, request, expectedResponse, function(res, err) {
assert.ifError(err);
var obj;
if (isPng) {
obj = mapnik.Image.fromBytes(new Buffer(res.body, 'binary'));
}
else if (isMvt) {
obj = new mapnik.VectorTile(z, x, y);
obj.setDataSync(new Buffer(res.body, 'binary'));
}
else {
obj = JSON.parse(res.body);
}
next(null, res, obj);
});
},
function finish(err, res, image) {
self.keysToDelete['map_cfg|' + LayergroupToken.parse(layergroupId).token] = 0;
self.keysToDelete['user:localhost:mapviews:global'] = 5;
return callback(err, res, image);
}
);
};