Merge pull request #660 from CartoDB/clientHeaders

respond with user-id and client in the headers
This commit is contained in:
Álvaro 2020-06-23 17:56:20 +02:00 committed by GitHub
commit 5caba983b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 4 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

@ -15,10 +15,11 @@ module.exports = function user (metadataBackend) {
error.subtype = 'user-not-found'; error.subtype = 'user-not-found';
error.http_status = 404; error.http_status = 404;
error.message = errorUserNotFoundMessageTemplate(res.locals.user); error.message = errorUserNotFoundMessageTemplate(res.locals.user);
next(error); return next(error);
} }
res.locals.userId = userId; res.locals.userId = userId;
res.set('Carto-User-Id', `${userId}`);
return next(); return next();
}); });
}; };

View File

@ -10,6 +10,7 @@ const logger = require('../middlewares/logger');
const profiler = require('../middlewares/profiler'); const profiler = require('../middlewares/profiler');
const cors = require('../middlewares/cors'); const cors = require('../middlewares/cors');
const servedByHostHeader = require('../middlewares/served-by-host-header'); const servedByHostHeader = require('../middlewares/served-by-host-header');
const clientHeader = require('../middlewares/client-header');
const QueryController = require('./query-controller'); const QueryController = require('./query-controller');
const CopyController = require('./copy-controller'); const CopyController = require('./copy-controller');
@ -61,6 +62,7 @@ module.exports = class SqlRouter {
sqlRouter.use(logger()); sqlRouter.use(logger());
sqlRouter.use(profiler({ statsClient: this.statsClient })); sqlRouter.use(profiler({ statsClient: this.statsClient }));
sqlRouter.use(cors()); sqlRouter.use(cors());
sqlRouter.use(clientHeader());
sqlRouter.use(servedByHostHeader()); sqlRouter.use(servedByHostHeader());
this.queryController.route(sqlRouter); 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); var result = JSON.parse(res.body);
if (res.statusCode > 299) { 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/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) { TestClient.prototype.getExpectedResponse = function (override) {