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:
parent
29f5db47f8
commit
a4a100e65a
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();
|
||||||
|
};
|
||||||
|
};
|
@ -19,6 +19,7 @@ module.exports = function user (metadataBackend) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
res.locals.userId = userId;
|
res.locals.userId = userId;
|
||||||
|
res.set('Carto-User-Id', `${userId}`);
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -19,6 +19,7 @@ const surrogateKey = require('../middlewares/surrogate-key');
|
|||||||
const lastModified = require('../middlewares/last-modified');
|
const lastModified = require('../middlewares/last-modified');
|
||||||
const formatter = require('../middlewares/formatter');
|
const formatter = require('../middlewares/formatter');
|
||||||
const content = require('../middlewares/content');
|
const content = require('../middlewares/content');
|
||||||
|
const clientHeader = require('../middlewares/client-header');
|
||||||
|
|
||||||
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimits;
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimits;
|
||||||
const PSQL = require('cartodb-psql');
|
const PSQL = require('cartodb-psql');
|
||||||
@ -55,6 +56,7 @@ module.exports = class QueryController {
|
|||||||
lastModified(),
|
lastModified(),
|
||||||
formatter(),
|
formatter(),
|
||||||
content(),
|
content(),
|
||||||
|
clientHeader(),
|
||||||
handleQuery({ stats: this.stats })
|
handleQuery({ stats: this.stats })
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
43
test/acceptance/client-headers-test.js
Normal file
43
test/acceptance/client-headers-test.js
Normal 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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
@ -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