From 0ef13f08c2d9ed7f5bbdf812bb36cf1e8666cb25 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Mon, 12 Nov 2012 19:44:16 +0100 Subject: [PATCH] Use inline disposition when no format and no filename are given See #61 --- app/controllers/app.js | 7 ++++--- test/acceptance/app.test.js | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/controllers/app.js b/app/controllers/app.js index 612b2513..80c8da3e 100755 --- a/app/controllers/app.js +++ b/app/controllers/app.js @@ -217,7 +217,8 @@ function handleQuery(req, res) { if (err) throw err; // configure headers for given format - res.header("Content-Disposition", getContentDisposition(format, filename)); + var use_inline = !req.query.hasOwnProperty('format') && !req.query.hasOwnProperty('filename'); + res.header("Content-Disposition", getContentDisposition(format, filename, use_inline)); res.header("Content-Type", getContentType(format)); // allow cross site post @@ -713,7 +714,7 @@ function toKML(dbname, user_id, gcol, sql, res, callback) { ); } -function getContentDisposition(format, filename) { +function getContentDisposition(format, filename, inline) { var ext = 'json'; if (format === 'geojson'){ ext = 'geojson'; @@ -731,7 +732,7 @@ function getContentDisposition(format, filename) { ext = 'kml'; } var time = new Date().toUTCString(); - return 'attachment; filename=' + filename + '.' + ext + '; modification-date="' + time + '";'; + return ( inline ? 'inline' : 'attachment' ) +'; filename=' + filename + '.' + ext + '; modification-date="' + time + '";'; } function getContentType(format){ diff --git a/test/acceptance/app.test.js b/test/acceptance/app.test.js index c9e520b2..56b22867 100644 --- a/test/acceptance/app.test.js +++ b/test/acceptance/app.test.js @@ -440,13 +440,31 @@ test('GET /api/v1/sql with SQL parameter and no format, ensuring content-disposi method: 'GET' },{ }, function(res){ assert.equal(res.statusCode, 200, res.body); + var ct = res.header('Content-Type'); + assert.ok(/json/.test(ct), 'Default format is not JSON: ' + ct); var cd = res.header('Content-Disposition'); - assert.equal(true, /^attachment/.test(cd), 'JSON is not disposed as attachment: ' + cd); + assert.equal(true, /^inline/.test(cd), 'Default format is not disposed inline: ' + cd); assert.equal(true, /filename=cartodb-query.json/gi.test(cd), 'Unexpected JSON filename: ' + cd); done(); }); }); +test('GET /api/v1/sql with SQL parameter and no format, but a filename', function(done){ + assert.response(app, { + url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4&filename=x', + headers: {host: 'vizzuality.cartodb.com'}, + method: 'GET' + },{ }, function(res){ + assert.equal(res.statusCode, 200, res.body); + var ct = res.header('Content-Type'); + assert.ok(/json/.test(ct), 'Default format is not JSON: ' + ct); + var cd = res.header('Content-Disposition'); + assert.equal(true, /^attachment/.test(cd), 'Format with filename is not disposed as attachment: ' + cd); + assert.equal(true, /filename=x.json/gi.test(cd), 'Unexpected JSON filename: ' + cd); + done(); + }); +}); + test('field named "the_geom_webmercator" is not skipped by default', function(done){ assert.response(app, { url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4',