respond with user-id and client in the headers

added headers to the test-client callback to be able to check them
This commit is contained in:
Álvaro Manera 2020-06-22 09:47:22 +02:00
parent 29f5db47f8
commit a4a100e65a
5 changed files with 66 additions and 3 deletions

View File

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

View File

@ -19,6 +19,7 @@ module.exports = function user (metadataBackend) {
}
res.locals.userId = userId;
res.set('Carto-User-Id', `${userId}`);
return next();
});
};

View File

@ -19,6 +19,7 @@ const surrogateKey = require('../middlewares/surrogate-key');
const lastModified = require('../middlewares/last-modified');
const formatter = require('../middlewares/formatter');
const content = require('../middlewares/content');
const clientHeader = require('../middlewares/client-header');
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimits;
const PSQL = require('cartodb-psql');
@ -55,6 +56,7 @@ module.exports = class QueryController {
lastModified(),
formatter(),
content(),
clientHeader(),
handleQuery({ stats: this.stats })
];
};

View File

@ -0,0 +1,43 @@
'use strict';
const assert = require('../support/assert');
const TestClient = require('../support/test-client');
describe('SQL api metric headers', function () {
const publicSQL = 'select * from untitle_table_4';
it('should get client header if client param is present', function (done) {
this.testClient = new TestClient();
const params = { client: 'test' };
this.testClient.getResult(publicSQL, params, (err, result, headers) => {
assert.ifError(err);
assert.strictEqual(result.length, 6);
assert.strictEqual(headers['carto-client'], 'test');
done();
});
});
it('should not get the client header if no client is provided', function (done) {
this.testClient = new TestClient();
this.testClient.getResult(publicSQL, (err, result, headers) => {
assert.ifError(err);
assert.strictEqual(result.length, 6);
assert.strictEqual(headers['carto-client'], undefined);
done();
});
});
it('should not get the user id in the response header', function (done) {
this.testClient = new TestClient();
this.testClient.getResult(publicSQL, (err, result, headers) => {
assert.ifError(err);
assert.strictEqual(result.length, 6);
assert.strictEqual(headers['carto-user-id'], '1');
done();
});
});
});

View File

@ -56,10 +56,10 @@ TestClient.prototype.getResult = function (query, override, callback) {
var result = JSON.parse(res.body);
if (res.statusCode > 299) {
return callback(null, result);
return callback(null, result, res.headers);
}
return callback(null, result.rows || [], result);
return callback(null, result.rows, res.headers || [], result, res.headers);
}
);
};
@ -89,7 +89,11 @@ TestClient.prototype.getUrl = function (override) {
return '/api/v1/sql?';
}
return '/api/v2/sql?api_key=' + (override.apiKey || this.config.apiKey || '1234');
let url = '/api/v2/sql?api_key=' + (override.apiKey || this.config.apiKey || '1234');
if (override.client) {
url = url + '&client=' + override.client;
}
return url;
};
TestClient.prototype.getExpectedResponse = function (override) {