Add test to check the headers exist while instantiating a map

remotes/origin/es6
Daniel García Aubert 4 years ago
parent ddefb1a6ca
commit aff5c9a614

@ -0,0 +1,11 @@
'use strict';
module.exports = function clientHeader () {
return function clientHeaderMiddleware (req, res, next) {
const { client } = req.query;
res.set('Carto-Client', client);
return next();
};
};

@ -0,0 +1,97 @@
'use strict';
require('../support/test-helper');
const assert = require('../support/assert');
const TestClient = require('../support/test-client');
const defaultLayers = [{
type: 'cartodb',
options: {
sql: TestClient.SQL.ONE_POINT
}
}];
const defaultStatTag = 'wadus';
function createMapConfig (layers = defaultLayers, statTag = defaultStatTag) {
return {
version: '1.8.0',
layers: layers,
stat_tag: defaultStatTag
};
}
describe('map view headers', function () {
it('anonymous map instantiation should respond with map-view headers', function (done) {
const mapConfig = createMapConfig();
const testClient = new TestClient(mapConfig);
const params = { client: 'test' };
testClient.getLayergroup(params, (err, body, res) => {
if (err) {
return done(err);
}
assert.strictEqual(res.headers['carto-stat-tag'], defaultStatTag);
assert.strictEqual(res.headers['carto-client'], params.client);
assert.strictEqual(res.headers['carto-user-id'], '1');
testClient.drain(done);
});
});
it('named map instantiation should respond with map-view headers', function (done) {
const templateid = `map-view-headers-test-${Date.now()}`;
const template = {
version: '0.0.1',
name: templateid,
layergroup: createMapConfig()
};
const testClient = new TestClient(template, 1234);
const params = { client: 'test' };
testClient.getLayergroup(params, (err, body, res) => {
if (err) {
return done(err);
}
assert.strictEqual(res.headers['carto-stat-tag'], defaultStatTag);
assert.strictEqual(res.headers['carto-client'], params.client);
assert.strictEqual(res.headers['carto-user-id'], '1');
testClient.drain(done);
});
});
it('preview should respond with map-view headers', function (done) {
const templateid = `map-view-headers-test-${Date.now()}`;
const template = {
version: '0.0.1',
name: templateid,
layergroup: createMapConfig([{
type: 'cartodb',
options: {
sql: TestClient.SQL.ONE_POINT,
cartocss: TestClient.CARTOCSS.POINTS,
cartocss_version: '2.3.0'
}
}])
};
const testClient = new TestClient(template, 1234);
const params = { client: 'test' };
testClient.getPreview(640, 480, params, (err, res) => {
if (err) {
return done(err);
}
assert.strictEqual(res.headers['carto-stat-tag'], defaultStatTag);
assert.strictEqual(res.headers['carto-client'], params.client);
assert.strictEqual(res.headers['carto-user-id'], '1');
testClient.drain(done);
});
});
});

@ -1033,9 +1033,13 @@ TestClient.prototype.getLayergroup = function (params, callback) {
queryParams.aggregation = params.aggregation; queryParams.aggregation = params.aggregation;
} }
if (params.client !== undefined) {
queryParams.client = params.client;
}
const path = templateId const path = templateId
? urlNamed + '/' + templateId + '?' + qs.stringify(queryParams) ? `${urlNamed}/${templateId}?${qs.stringify(queryParams)}`
: url; : `${url}?${qs.stringify(queryParams)}`;
assert.response(self.server, assert.response(self.server,
{ {
@ -1058,12 +1062,15 @@ TestClient.prototype.getLayergroup = function (params, callback) {
if (res.statusCode === 200 && self.template && self.template.layergroup && self.template.layergroup.stat_tag) { if (res.statusCode === 200 && self.template && self.template.layergroup && self.template.layergroup.stat_tag) {
self.keysToDelete[`user:localhost:mapviews:stat_tag:${self.template.layergroup.stat_tag}`] = 5; self.keysToDelete[`user:localhost:mapviews:stat_tag:${self.template.layergroup.stat_tag}`] = 5;
} }
if (res.statusCode === 200 && self.mapConfig && self.mapConfig.stat_tag) {
self.keysToDelete[`user:localhost:mapviews:stat_tag:${self.mapConfig.stat_tag}`] = 5;
}
} }
if (err) { if (err) {
return callback(err); return callback(err);
} }
return callback(null, parsedBody); return callback(null, parsedBody, res);
} }
); );
} }

Loading…
Cancel
Save