Stop adding X-Cache-Channel header when no tables were identified in SQL query

Fixes #238 and fixes #157
This commit is contained in:
Raul Ochoa 2015-09-07 18:22:24 +02:00
parent 920cb350cc
commit 875eccba62
3 changed files with 28 additions and 2 deletions

View File

@ -488,7 +488,7 @@ function handleQuery(req, res) {
} }
// Only set an X-Cache-Channel for responses we want Varnish to cache. // Only set an X-Cache-Channel for responses we want Varnish to cache.
if ( tableCacheItem && ! tableCacheItem.may_write ) { if ( tableCacheItem && tableCacheItem.affected_tables.length > 0 && !tableCacheItem.may_write ) {
res.header('X-Cache-Channel', generateCacheKey(dbopts.dbname, tableCacheItem, authenticated)); res.header('X-Cache-Channel', generateCacheKey(dbopts.dbname, tableCacheItem, authenticated));
} }

View File

@ -1082,7 +1082,7 @@ it('numeric arrays are rendered as such', function(done){
assert.equal(out.rows[0].x.length, 2); assert.equal(out.rows[0].x.length, 2);
assert.equal(out.rows[0].x[0], '8.7'); assert.equal(out.rows[0].x[0], '8.7');
assert.equal(out.rows[0].x[1], '4.3'); assert.equal(out.rows[0].x[1], '4.3');
assert.equal(res.headers['x-cache-channel'], 'cartodb_test_user_1_db:'); // keep forever assert.equal(res.headers.hasOwnProperty('x-cache-channel'), false);
done(); done();
}); });
}); });

View File

@ -84,4 +84,30 @@ describe('X-Cache-Channel header', function() {
], done)); ], done));
}); });
it('should not add header for functions', function(done) {
var sql = "SELECT format('%s', 'wadus')";
assert.response(app, createGetRequest(sql), RESPONSE_OK, function(res) {
assert.ok(!res.headers.hasOwnProperty('x-cache-channel'), res.headers['x-cache-channel']);
done();
});
});
it('should not add header for CDB_QueryTables', function(done) {
var sql = "SELECT CDB_QueryTablesText('select * from untitle_table_4')";
assert.response(app, createGetRequest(sql), RESPONSE_OK, function(res) {
assert.ok(!res.headers.hasOwnProperty('x-cache-channel'), res.headers['x-cache-channel']);
done();
});
});
it('should not add header for non table results', function(done) {
var sql = "SELECT 'wadus'::text";
assert.response(app, createGetRequest(sql), RESPONSE_OK, function(res) {
assert.ok(!res.headers.hasOwnProperty('x-cache-channel'), res.headers['x-cache-channel']);
done();
});
});
}); });