Merge branch 'master' into dgaubert/ch87185/log-system-revamp-sql-api

This commit is contained in:
Daniel García Aubert 2020-06-30 17:50:10 +02:00
commit c1ed382949
5 changed files with 65 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

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

View File

@ -10,6 +10,7 @@ const initLogger = require('../middlewares/logger');
const profiler = require('../middlewares/profiler');
const cors = require('../middlewares/cors');
const servedByHostHeader = require('../middlewares/served-by-host-header');
const clientHeader = require('../middlewares/client-header');
const QueryController = require('./query-controller');
const CopyController = require('./copy-controller');
@ -63,6 +64,7 @@ module.exports = class SqlRouter {
sqlRouter.use(initLogger({ logger: this.logger }));
sqlRouter.use(profiler({ statsClient: this.statsClient }));
sqlRouter.use(cors());
sqlRouter.use(clientHeader());
sqlRouter.use(servedByHostHeader());
this.queryController.route(sqlRouter);

View File

@ -0,0 +1,42 @@
'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 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) {