2019-07-04 18:24:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
const server = require('../../lib/server')();
|
2019-07-04 18:24:42 +08:00
|
|
|
const assert = require('../support/assert');
|
|
|
|
const qs = require('querystring');
|
|
|
|
|
|
|
|
describe('cache headers', function () {
|
|
|
|
it('should return a Vary header', function (done) {
|
|
|
|
assert.response(server, {
|
|
|
|
url: `/api/v1/sql?${qs.encode({
|
|
|
|
api_key: '1234',
|
|
|
|
q: 'select * from untitle_table_4'
|
|
|
|
})}`,
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
function (err, res) {
|
2019-12-27 00:16:29 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.headers.vary, 'Authorization');
|
2019-07-04 18:24:42 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-05 22:12:09 +08:00
|
|
|
it('should return a proper max-age when CDB_TableMetadata table includes the last updated time', function (done) {
|
2019-07-04 18:24:42 +08:00
|
|
|
const ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365;
|
|
|
|
const noTtl = 0;
|
2019-10-15 19:42:11 +08:00
|
|
|
const fallbackTtl = global.settings.cache.fallbackTtl || 300;
|
2019-07-04 18:24:42 +08:00
|
|
|
const ttl = global.settings.cache.ttl || ONE_YEAR_IN_SECONDS;
|
|
|
|
const tableName = `wadus_table_${Date.now()}`;
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: `/api/v1/sql?${qs.encode({
|
|
|
|
api_key: '1234',
|
|
|
|
q: `create table ${tableName}()`
|
|
|
|
})}`,
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
{},
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-12-27 00:16:29 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.headers['cache-control'], `no-cache,max-age=${noTtl},must-revalidate,public`);
|
2019-07-04 18:24:42 +08:00
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: `/api/v1/sql?${qs.encode({
|
|
|
|
api_key: '1234',
|
|
|
|
q: `select * from ${tableName}`
|
|
|
|
})}`,
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {},
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-12-27 00:16:29 +08:00
|
|
|
assert.ifError(err);
|
2019-10-15 19:11:49 +08:00
|
|
|
const cacheControl = res.headers['cache-control'];
|
2019-12-24 01:19:08 +08:00
|
|
|
const [, maxAge] = cacheControl.split(',');
|
|
|
|
const [, value] = maxAge.split('=');
|
2019-10-15 19:11:49 +08:00
|
|
|
|
|
|
|
assert.ok(Number(value) <= fallbackTtl);
|
2019-07-04 18:24:42 +08:00
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: `/api/v1/sql?${qs.encode({
|
|
|
|
api_key: '1234',
|
2020-04-17 00:35:29 +08:00
|
|
|
q: `select cartodb.CDB_TableMetadataTouch('${tableName}'::regclass)`
|
2019-07-04 18:24:42 +08:00
|
|
|
})}`,
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {},
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-12-27 00:16:29 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.headers['cache-control'], `no-cache,max-age=${ttl},must-revalidate,public`);
|
2019-07-04 18:24:42 +08:00
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: `/api/v1/sql?${qs.encode({
|
|
|
|
api_key: '1234',
|
|
|
|
q: `select * from ${tableName}`
|
|
|
|
})}`,
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
}, {},
|
2019-12-24 01:19:08 +08:00
|
|
|
function (err, res) {
|
2019-12-27 00:16:29 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.headers['cache-control'], `no-cache,max-age=${ttl},must-revalidate,public`);
|
2019-07-04 18:24:42 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-07-05 22:12:09 +08:00
|
|
|
|
|
|
|
it('should return a proper max-age when the query doesn\'t use any table', function (done) {
|
|
|
|
const ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365;
|
|
|
|
const ttl = global.settings.cache.ttl || ONE_YEAR_IN_SECONDS;
|
|
|
|
|
|
|
|
assert.response(server, {
|
|
|
|
url: `/api/v1/sql?${qs.encode({
|
|
|
|
api_key: '1234',
|
2019-12-24 01:19:08 +08:00
|
|
|
q: 'select 1'
|
2019-07-05 22:12:09 +08:00
|
|
|
})}`,
|
|
|
|
headers: {
|
|
|
|
host: 'vizzuality.cartodb.com'
|
|
|
|
},
|
|
|
|
method: 'GET'
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
function (err, res) {
|
2019-12-27 00:16:29 +08:00
|
|
|
assert.ifError(err);
|
2019-12-26 21:01:18 +08:00
|
|
|
assert.strictEqual(res.headers['cache-control'], `no-cache,max-age=${ttl},must-revalidate,public`);
|
2019-07-05 22:12:09 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2019-07-04 18:24:42 +08:00
|
|
|
});
|