From ecd33e5561c54b0a174948158e6054e171ffa184 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 2 Mar 2016 12:40:53 +0100 Subject: [PATCH] TestClient with method to retrieve tiles --- test/support/test-client.js | 113 +++++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-) diff --git a/test/support/test-client.js b/test/support/test-client.js index d2df611b..1a29f564 100644 --- a/test/support/test-client.js +++ b/test/support/test-client.js @@ -3,6 +3,8 @@ var qs = require('querystring'); var step = require('step'); +var mapnik = require('windshaft').mapnik; + var LayergroupToken = require('../../lib/cartodb/models/layergroup_token'); var assert = require('./assert'); @@ -13,8 +15,9 @@ var serverOptions = require('../../lib/cartodb/server_options'); var server = new CartodbWindshaft(serverOptions); -function TestClient(mapConfig) { +function TestClient(mapConfig, apiKey) { this.mapConfig = mapConfig; + this.apiKey = apiKey; this.keysToDelete = {}; } @@ -114,6 +117,114 @@ TestClient.prototype.getWidget = function(widgetName, params, callback) { ); }; +TestClient.prototype.getTile = function(z, x, y, params, callback) { + var self = this; + + if (!callback) { + callback = params; + params = {}; + } + + var url = '/api/v1/map'; + + if (this.apiKey) { + url += '?' + qs.stringify({api_key: this.apiKey}); + } + + var layergroupId; + step( + function createLayergroup() { + var next = this; + assert.response(server, + { + url: url, + method: 'POST', + headers: { + host: 'localhost', + 'Content-Type': 'application/json' + }, + data: JSON.stringify(self.mapConfig) + }, + { + 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) { + 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 === 'png'; + + if (isPng) { + request.encoding = 'binary'; + expectedResponse.headers['Content-Type'] = 'image/png'; + } + + assert.response(server, request, expectedResponse, function(res, err) { + assert.ifError(err); + + var image; + + if (isPng) { + image = mapnik.Image.fromBytes(new Buffer(res.body, 'binary')); + } + + next(null, res, image); + }); + }, + 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); + } + ); +}; + TestClient.prototype.drain = function(callback) { helper.deleteRedisKeys(this.keysToDelete, callback); };