From 875eccba62131a7632b2657b178551e0cebb7902 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 7 Sep 2015 18:22:24 +0200 Subject: [PATCH] Stop adding X-Cache-Channel header when no tables were identified in SQL query Fixes #238 and fixes #157 --- app/controllers/app.js | 2 +- test/acceptance/app.test.js | 2 +- test/acceptance/x-cache-channel.js | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/controllers/app.js b/app/controllers/app.js index 158b2827..46636ac8 100755 --- a/app/controllers/app.js +++ b/app/controllers/app.js @@ -488,7 +488,7 @@ function handleQuery(req, res) { } // 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)); } diff --git a/test/acceptance/app.test.js b/test/acceptance/app.test.js index 78644c90..6db04df3 100644 --- a/test/acceptance/app.test.js +++ b/test/acceptance/app.test.js @@ -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[0], '8.7'); 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(); }); }); diff --git a/test/acceptance/x-cache-channel.js b/test/acceptance/x-cache-channel.js index b06bb420..53c58cc1 100644 --- a/test/acceptance/x-cache-channel.js +++ b/test/acceptance/x-cache-channel.js @@ -84,4 +84,30 @@ describe('X-Cache-Channel header', function() { ], 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(); + }); + }); + + + });