Use inline disposition when no format and no filename are given

See #61
This commit is contained in:
Sandro Santilli 2012-11-12 19:44:16 +01:00
parent 120cf3f0c5
commit 0ef13f08c2
2 changed files with 23 additions and 4 deletions

View File

@ -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){

View File

@ -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',