From fdd4c4aaa0a4961e8f61c78c3f7bcd3f2ec220e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 2 Mar 2018 18:22:53 +0100 Subject: [PATCH] Going red: get default named map vector tile --- test/acceptance/mvt.js | 42 ++++++++++++++++++ test/support/test-client.js | 86 ++++++++++++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 1 deletion(-) diff --git a/test/acceptance/mvt.js b/test/acceptance/mvt.js index fa9189f2..d21ae629 100644 --- a/test/acceptance/mvt.js +++ b/test/acceptance/mvt.js @@ -34,6 +34,48 @@ return function () { serverOptions.renderer.mvt.usePostGIS = originalUsePostGIS; }); + describe('named map tile', function () { + it('should get default named vector tile', function (done) { + const apikeyToken = 1234; + const templateName = 'mvt-template'; + const template = { + version: '0.0.1', + name: templateName, + placeholders: { + buffersize: { + type: 'number', + default: 0 + } + }, + layergroup: { + version: '1.7.0', + layers: [{ + type: 'cartodb', + options: { + sql: 'select * from populated_places_simple_reduced limit 10', + cartocss: TestClient.CARTOCSS.POINTS, + cartocss_version: '2.3.0', + } + }] + } + }; + + const testClient = new TestClient(template, apikeyToken); + + testClient.getNamedTile(templateName, 0, 0, 0, 'mvt', {}, (err, res, tile) => { + if (err) { + return done(err); + } + + const tileJSON = tile.toJSON(); + + assert.equal(tileJSON[0].features.length, 10); + + done(); + }); + }); + }); + describe('analysis-layers-dataviews-mvt', function () { function createMapConfig(layers, dataviews, analysis) { diff --git a/test/support/test-client.js b/test/support/test-client.js index 6b200f2e..3e919175 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -414,7 +414,7 @@ TestClient.prototype.getDataview = function(dataviewName, params, callback) { var urlParams = {}; if (params.hasOwnProperty('no_filters')) { urlParams.no_filters = params.no_filters; - } + } if (params.hasOwnProperty('own_filter')) { urlParams.own_filter = params.own_filter; } @@ -1253,3 +1253,87 @@ TestClient.prototype.getAnalysesCatalog = function (params, callback) { } ); }; + +TestClient.prototype.getNamedTile = function (name, z, x, y, format, options, callback) { + const { params } = options; + + if (!this.apiKey) { + return callback(new Error('apiKey param is mandatory to create a new template')); + } + + const createTemplateRequest = { + url: `/api/v1/map/named?${qs.stringify({ api_key: this.apiKey })}`, + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(this.template) + }; + + const createTemplateResponse = { + status: 200, + headers: { + 'Content-Type': 'application/json; charset=utf-8' + } + }; + + assert.response(this.server, createTemplateRequest, createTemplateResponse, (res, err) => { + if (err) { + return callback(err); + } + + const templateId = JSON.parse(res.body).template_id; + const queryParams = params ? `?${qs.stringify(params)}` : ''; + const url = `/api/v1/map/named/${templateId}/all/${[z,x,y].join('/')}.${format}${queryParams}`; + const namedTileRequest = { + url, + method: 'GET', + headers: { + host: 'localhost' + }, + encoding: 'binary' + }; + + let contentType; + switch (format) { + case 'png': + contentType = 'image/png'; + break; + case 'mvt': + contentType = 'application/x-protobuf'; + break; + default: + contentType = 'application/json'; + break; + } + + const namedTileResponse = Object.assign({ + status: 200, + headers: { + 'content-type': contentType + } + }, options.response); + + assert.response(this.server, namedTileRequest, namedTileResponse, (res, err) => { + let body; + switch (res.headers['content-type']) { + case 'image/png': + body = mapnik.Image.fromBytes(new Buffer(res.body, 'binary')); + break; + case 'application/x-protobuf': + body = new mapnik.VectorTile(z, x, y); + body.setDataSync(new Buffer(res.body, 'binary')); + break; + case 'application/json; charset=utf-8': + body = JSON.parse(res.body); + break; + default: + body = res.body; + break; + } + + return callback(err, res, body); + }); + }); +};