From f89fd98ed73cf4cecea47878bc2f8754a637c56a Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Wed, 15 Jan 2014 11:53:19 +0100 Subject: [PATCH] Expect malformed response objects (#109) Include test for sql errors on layergroup creation Closes #109 --- lib/cartodb/cartodb_windshaft.js | 8 ++++--- test/acceptance/multilayer.js | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/cartodb/cartodb_windshaft.js b/lib/cartodb/cartodb_windshaft.js index d18cf4e3..6ba8e031 100644 --- a/lib/cartodb/cartodb_windshaft.js +++ b/lib/cartodb/cartodb_windshaft.js @@ -46,13 +46,15 @@ var CartodbWindshaft = function(serverOptions) { // See http://github.com/CartoDB/Windshaft-cartodb/issues/107 var ws_sendError = ws.sendError; ws.sendError = function(res) { + // NOTE: the "res" object will have no _headers when + // faked by Windshaft, see + // http://github.com/CartoDB/Windshaft-cartodb/issues/109 + // if ( res._headers ) { delete res._headers['cache-control']; delete res._headers['last-modified']; delete res._headers['x-cache-channel']; - } else { - console.log("WARNING: response has no _headers: "); console.dir(res); - } + } ws_sendError.apply(this, arguments); }; diff --git a/test/acceptance/multilayer.js b/test/acceptance/multilayer.js index c7d6f3bd..c5d30856 100644 --- a/test/acceptance/multilayer.js +++ b/test/acceptance/multilayer.js @@ -19,6 +19,14 @@ var serverOptions = require(__dirname + '/../../lib/cartodb/server_options'); var server = new CartodbWindshaft(serverOptions); server.setMaxListeners(0); +// Check that the response headers do not request caching +// Throws on failure +function checkNoCache(res) { + assert.ok(!res.headers.hasOwnProperty('x-cache-channel')); + assert.ok(!res.headers.hasOwnProperty('cache-control')); // is this correct ? + assert.ok(!res.headers.hasOwnProperty('last-modified')); // is this correct ? +} + suite('multilayer', function() { var redis_client = redis.createClient(global.environment.redis.port); @@ -460,6 +468,35 @@ suite('multilayer', function() { }); }); + // Also tests that server doesn't crash: + // see http://github.com/CartoDB/Windshaft-cartodb/issues/109 + test("layergroup creation fails if sql is bogus", function(done) { + var layergroup = { + stat_tag: 'random_tag', + version: '1.0.0', + layers: [ + { options: { + sql: 'select bogus(0,0) as the_geom_webmercator', + cartocss: '#layer { polygon-fill:red; }', + cartocss_version: '2.0.1' + } } + ] + }; + assert.response(server, { + url: '/tiles/layergroup', + method: 'POST', + headers: {host: 'localhost', 'Content-Type': 'application/json' }, + data: JSON.stringify(layergroup) + }, {}, function(res) { + assert.equal(res.statusCode, 400, res.body); + var parsed = JSON.parse(res.body); + var msg = parsed.errors[0]; + assert.ok(msg.match(/bogus.*exist/), msg); + checkNoCache(res); + done(); + }); + }); + test("layergroup with 2 private-table layers", function(done) { var layergroup = {