diff --git a/test/acceptance/buffer-size-format.js b/test/acceptance/buffer-size-format.js index b8f40766..50a11329 100644 --- a/test/acceptance/buffer-size-format.js +++ b/test/acceptance/buffer-size-format.js @@ -196,13 +196,13 @@ describe('buffer size per format for named maps', function () { testCases.forEach(function (test) { 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 options = { format: test.format, 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); // To generate images use: //tile.save('./test/fixtures/buffer-size/tile-7.64.48-buffer-size-0-test.png'); diff --git a/test/support/test-client.js b/test/support/test-client.js index 3e3edf55..b2a326b8 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -16,15 +16,23 @@ var serverOptions = require('../../lib/cartodb/server_options'); serverOptions.analysis.batch.inlineExecution = true; var server = new CartodbWindshaft(serverOptions); -function TestClient(mapConfig, apiKey, template) { - this.mapConfig = mapConfig; +function TestClient(config, apiKey) { + this.mapConfig = isMapConfig(config) ? config : null; + this.template = isTemplate(config) ? config : null; this.apiKey = apiKey; - this.template = template; this.keysToDelete = {}; } module.exports = TestClient; +function isMapConfig(config) { + return config && config.layers; +} + +function isTemplate(config) { + return config && config.layergroup; +} + module.exports.RESPONSE = { ERROR: { status: 400, @@ -407,6 +415,7 @@ TestClient.prototype.getTile = function(z, x, y, params, callback) { } var url = '/api/v1/map'; + var urlNamed = url + '/named'; if (this.apiKey) { url += '?' + qs.stringify({api_key: this.apiKey}); @@ -414,17 +423,60 @@ TestClient.prototype.getTile = function(z, x, y, params, callback) { var layergroupId; step( - function createLayergroup() { + function createTemplate () { 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, { - url: url, + url: urlNamed + '?' + qs.stringify({ api_key: self.apiKey }), method: 'POST', headers: { host: 'localhost', '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, @@ -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); - } - ); -};