2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2013-02-14 00:31:53 +08:00
|
|
|
require('../helper');
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var server = require('../../lib/server')();
|
2015-05-13 17:21:44 +08:00
|
|
|
var assert = require('../support/assert');
|
|
|
|
var querystring = require('querystring');
|
|
|
|
var _ = require('underscore');
|
2013-02-14 00:31:53 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
describe('X-Cache-Channel header', function () {
|
|
|
|
function createGetRequest (sqlQuery) {
|
2015-09-08 00:09:42 +08:00
|
|
|
var query = querystring.stringify({
|
|
|
|
q: sqlQuery,
|
|
|
|
api_key: 1234
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
url: '/api/v1/sql?' + query,
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
};
|
|
|
|
}
|
2013-02-14 00:31:53 +08:00
|
|
|
|
2015-09-08 00:09:42 +08:00
|
|
|
var RESPONSE_OK = {
|
|
|
|
statusCode: 200
|
|
|
|
};
|
2013-02-14 00:31:53 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function xCacheChannelHeaderHasTables (xCacheChannel, expectedTablesNames) {
|
2015-09-08 00:09:42 +08:00
|
|
|
var databaseAndTables = xCacheChannel.split(':');
|
|
|
|
var databaseName = databaseAndTables[0];
|
|
|
|
|
|
|
|
assert.equal(databaseName, 'cartodb_test_user_1_db');
|
|
|
|
|
|
|
|
var headerTableNames = databaseAndTables[1].split(',');
|
|
|
|
assert.equal(headerTableNames.length, expectedTablesNames.length);
|
|
|
|
|
|
|
|
var tablesDiff = _.difference(expectedTablesNames, headerTableNames);
|
|
|
|
assert.equal(tablesDiff.length, 0, 'X-Cache-Channel header missing tables: ' + tablesDiff.join(','));
|
|
|
|
}
|
2013-02-14 00:31:53 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function tableNamesInCacheChannelHeader (expectedTableNames, done) {
|
|
|
|
return function (err, res) {
|
2015-09-08 00:09:42 +08:00
|
|
|
xCacheChannelHeaderHasTables(res.headers['x-cache-channel'], expectedTableNames);
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('supports joins', function (done) {
|
|
|
|
var sql = 'SELECT a.name as an, b.name as bn FROM untitle_table_4 a ' +
|
|
|
|
'left join private_table b ON (a.cartodb_id = b.cartodb_id)';
|
2015-09-08 00:09:42 +08:00
|
|
|
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, createGetRequest(sql), RESPONSE_OK, tableNamesInCacheChannelHeader([
|
2015-09-08 00:09:42 +08:00
|
|
|
'public.private_table',
|
|
|
|
'public.untitle_table_4'
|
|
|
|
], done));
|
2013-02-14 00:31:53 +08:00
|
|
|
});
|
2015-09-08 00:09:42 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('supports multistatements', function (done) {
|
|
|
|
var sql = 'SELECT * FROM untitle_table_4; SELECT * FROM private_table';
|
2015-09-08 00:09:42 +08:00
|
|
|
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, createGetRequest(sql), RESPONSE_OK, tableNamesInCacheChannelHeader([
|
2015-09-08 00:09:42 +08:00
|
|
|
'public.private_table',
|
|
|
|
'public.untitle_table_4'
|
|
|
|
], done));
|
2013-02-14 00:31:53 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('supports explicit transactions', function (done) {
|
|
|
|
var sql = 'BEGIN; SELECT * FROM untitle_table_4; COMMIT; BEGIN; SELECT * FROM private_table; COMMIT;';
|
2015-09-08 00:09:42 +08:00
|
|
|
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, createGetRequest(sql), RESPONSE_OK, tableNamesInCacheChannelHeader([
|
2015-09-08 00:09:42 +08:00
|
|
|
'public.private_table',
|
|
|
|
'public.untitle_table_4'
|
|
|
|
], done));
|
2013-02-14 00:31:53 +08:00
|
|
|
});
|
2015-09-08 00:09:42 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('survives partial transactions', function (done) {
|
|
|
|
var sql = 'BEGIN; SELECT * FROM untitle_table_4';
|
2015-09-08 00:09:42 +08:00
|
|
|
|
2016-09-15 02:54:24 +08:00
|
|
|
assert.response(server, createGetRequest(sql), RESPONSE_OK, tableNamesInCacheChannelHeader([
|
2015-09-08 00:09:42 +08:00
|
|
|
'public.untitle_table_4'
|
|
|
|
], done));
|
2013-02-14 00:31:53 +08:00
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should not add header for functions', function (done) {
|
2015-09-08 00:22:24 +08:00
|
|
|
var sql = "SELECT format('%s', 'wadus')";
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.response(server, createGetRequest(sql), RESPONSE_OK, function (err, res) {
|
2015-09-08 00:22:24 +08:00
|
|
|
assert.ok(!res.headers.hasOwnProperty('x-cache-channel'), res.headers['x-cache-channel']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should not add header for CDB_QueryTables', function (done) {
|
2015-09-08 00:22:24 +08:00
|
|
|
var sql = "SELECT CDB_QueryTablesText('select * from untitle_table_4')";
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.response(server, createGetRequest(sql), RESPONSE_OK, function (err, res) {
|
2015-09-08 00:22:24 +08:00
|
|
|
assert.ok(!res.headers.hasOwnProperty('x-cache-channel'), res.headers['x-cache-channel']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
it('should not add header for non table results', function (done) {
|
2015-09-08 00:22:24 +08:00
|
|
|
var sql = "SELECT 'wadus'::text";
|
2019-12-24 01:19:08 +08:00
|
|
|
assert.response(server, createGetRequest(sql), RESPONSE_OK, function (err, res) {
|
2015-09-08 00:22:24 +08:00
|
|
|
assert.ok(!res.headers.hasOwnProperty('x-cache-channel'), res.headers['x-cache-channel']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-02-14 00:31:53 +08:00
|
|
|
});
|