Merge pull request #660 from CartoDB/clientHeaders
respond with user-id and client in the headers
This commit is contained in:
commit
5caba983b1
13
lib/api/middlewares/client-header.js
Normal file
13
lib/api/middlewares/client-header.js
Normal 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();
|
||||||
|
};
|
||||||
|
};
|
@ -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();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -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);
|
||||||
|
42
test/acceptance/client-headers-test.js
Normal file
42
test/acceptance/client-headers-test.js
Normal 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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user