2013-01-16 16:58:09 +08:00
|
|
|
require('../../helper');
|
|
|
|
require('../../support/assert');
|
|
|
|
|
|
|
|
|
2014-01-31 17:55:30 +08:00
|
|
|
var app = require(global.settings.app_root + '/app/controllers/app')()
|
2013-01-16 16:58:09 +08:00
|
|
|
, assert = require('assert')
|
|
|
|
, querystring = require('querystring')
|
|
|
|
, _ = require('underscore')
|
|
|
|
, zipfile = require('zipfile')
|
|
|
|
, fs = require('fs')
|
|
|
|
, libxmljs = require('libxmljs')
|
2013-04-11 22:15:51 +08:00
|
|
|
, http = require('http')
|
|
|
|
, server_utils = require('../../support/server_utils');
|
2013-01-16 16:58:09 +08:00
|
|
|
;
|
|
|
|
|
|
|
|
// allow lots of emitters to be set to silence warning
|
|
|
|
app.setMaxListeners(0);
|
|
|
|
|
|
|
|
suite('export.kml', function() {
|
|
|
|
|
|
|
|
var expected_cache_control = 'no-cache,max-age=3600,must-revalidate,public';
|
|
|
|
var expected_cache_control_persist = 'public,max-age=31536000';
|
|
|
|
|
|
|
|
// use dec_sep for internationalization
|
|
|
|
var checkDecimals = function(x, dec_sep){
|
|
|
|
var tmp='' + x;
|
|
|
|
if (tmp.indexOf(dec_sep)>-1)
|
|
|
|
return tmp.length-tmp.indexOf(dec_sep)-1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-04 18:39:49 +08:00
|
|
|
// Check if an attribute is in the KML output
|
|
|
|
//
|
2013-09-04 18:56:48 +08:00
|
|
|
// NOTE: "name" and "description" attributes are threated specially
|
|
|
|
// in that they are matched in case-insensitive way
|
2013-09-04 18:39:49 +08:00
|
|
|
//
|
|
|
|
var hasAttribute = function(kml, att) {
|
|
|
|
|
|
|
|
// Strip namespace:
|
|
|
|
//https://github.com/polotek/libxmljs/issues/212
|
|
|
|
kml = kml.replace(/ xmlns=[^>]*>/, '>');
|
|
|
|
|
|
|
|
var doc = libxmljs.parseXmlString(kml);
|
|
|
|
//console.log("doc: " + doc);
|
|
|
|
var xpath;
|
|
|
|
|
|
|
|
xpath = "//SimpleField[@name='" + att + "']";
|
|
|
|
if ( doc.get(xpath) ) return true;
|
|
|
|
|
|
|
|
xpath = "//Placemark/" + att;
|
|
|
|
if ( doc.get(xpath) ) return true;
|
|
|
|
|
|
|
|
var lcatt = att.toLowerCase();
|
2013-09-04 18:56:48 +08:00
|
|
|
if ( lcatt == 'name' || lcatt == 'description' ) {
|
2013-09-04 18:39:49 +08:00
|
|
|
xpath = "//Placemark/" + lcatt;
|
|
|
|
if ( doc.get(xpath) ) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//if ( lowerkml.indexOf('simplefield name="'+ loweratt + '"') != -1 ) return true;
|
|
|
|
//if ( lowerkml.indexOf('<'+loweratt+'>') != -1 ) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-05 23:56:07 +08:00
|
|
|
// Return the first coordinate array found in KML
|
|
|
|
var extractCoordinates = function(kml) {
|
|
|
|
|
|
|
|
// Strip namespace:
|
|
|
|
//https://github.com/polotek/libxmljs/issues/212
|
|
|
|
kml = kml.replace(/ xmlns=[^>]*>/, '>');
|
|
|
|
|
|
|
|
var doc = libxmljs.parseXmlString(kml);
|
|
|
|
//console.log("doc: " + doc);
|
|
|
|
if ( ! doc ) return;
|
|
|
|
var coo = doc.get("//coordinates");
|
|
|
|
//console.log("coo: " + coo);
|
|
|
|
if ( ! coo ) return;
|
|
|
|
coo = coo.text();
|
|
|
|
//console.log("coo: " + coo);
|
|
|
|
if ( ! coo ) return;
|
|
|
|
coo = coo.split(' ');
|
|
|
|
//console.log("coo: " + coo);
|
|
|
|
for (var i=0; i<coo.length; ++i) {
|
|
|
|
coo[i] = coo[i].split(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
return coo;
|
|
|
|
}
|
|
|
|
|
2013-11-06 00:29:02 +08:00
|
|
|
// Return the first folder name in KML
|
|
|
|
var extractFolderName = function(kml) {
|
|
|
|
|
|
|
|
// Strip namespace:
|
|
|
|
//https://github.com/polotek/libxmljs/issues/212
|
|
|
|
kml = kml.replace(/ xmlns=[^>]*>/, '>');
|
|
|
|
|
|
|
|
var doc = libxmljs.parseXmlString(kml);
|
|
|
|
//console.log("doc: " + doc);
|
|
|
|
if ( ! doc ) return;
|
|
|
|
var coo = doc.get("//Document/Folder/name");
|
|
|
|
//console.log("coo: " + coo);
|
|
|
|
if ( ! coo ) return;
|
|
|
|
coo = coo.text();
|
|
|
|
//console.log("coo: " + coo);
|
|
|
|
if ( ! coo ) return;
|
|
|
|
return coo;
|
|
|
|
}
|
|
|
|
|
2013-01-16 16:58:09 +08:00
|
|
|
// KML tests
|
|
|
|
|
|
|
|
test('KML format, unauthenticated', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /^attachment/.test(cd), 'KML is not disposed as attachment: ' + cd);
|
|
|
|
assert.equal(true, /filename=cartodb-query.kml/gi.test(cd), 'Unexpected KML filename: ' + cd);
|
|
|
|
var row0 = res.body;
|
|
|
|
var checkfields = {'Name':1, 'address':1, 'cartodb_id':1, 'the_geom':0, 'the_geom_webmercator':0};
|
|
|
|
for ( var f in checkfields ) {
|
|
|
|
if ( checkfields[f] ) {
|
2013-09-04 18:39:49 +08:00
|
|
|
assert.ok(hasAttribute(row0, f), "result does not include '" + f + "': " + row0);
|
2013-01-16 16:58:09 +08:00
|
|
|
} else {
|
2013-09-04 18:39:49 +08:00
|
|
|
assert.ok(!hasAttribute(row0, f), "result includes '" + f + "'");
|
2013-01-16 16:58:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('KML format, unauthenticated, POST', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql',
|
|
|
|
data: 'q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /^attachment/.test(cd), 'KML is not disposed as attachment: ' + cd);
|
|
|
|
assert.equal(true, /filename=cartodb-query.kml/gi.test(cd), 'Unexpected KML filename: ' + cd);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('KML format, bigger than 81920 bytes', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql',
|
|
|
|
data: querystring.stringify({
|
|
|
|
q: 'SELECT 0 as fname FROM generate_series(0,81920)',
|
|
|
|
format: 'kml'
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
|
|
method: 'POST'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /^attachment/.test(cd), 'KML is not disposed as attachment: ' + cd);
|
|
|
|
assert.equal(true, /filename=cartodb-query.kml/gi.test(cd), 'Unexpected KML filename: ' + cd);
|
|
|
|
assert.ok(res.body.length > 81920, 'KML smaller than expected: ' + res.body.length);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('KML format, skipfields', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml&skipfields=address,cartodb_id',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /^attachment/.test(cd), 'KML is not disposed as attachment: ' + cd);
|
|
|
|
assert.equal(true, /filename=cartodb-query.kml/gi.test(cd), 'Unexpected KML filename: ' + cd);
|
|
|
|
var row0 = res.body;
|
|
|
|
var checkfields = {'Name':1, 'address':0, 'cartodb_id':0, 'the_geom':0, 'the_geom_webmercator':0};
|
|
|
|
for ( var f in checkfields ) {
|
|
|
|
if ( checkfields[f] ) {
|
2013-09-04 18:39:49 +08:00
|
|
|
assert.ok(hasAttribute(row0, f), "result does not include '" + f + "': " + row0);
|
2013-01-16 16:58:09 +08:00
|
|
|
} else {
|
2013-09-04 18:39:49 +08:00
|
|
|
assert.ok(!hasAttribute(row0, f), "result includes '" + f + "'");
|
2013-01-16 16:58:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('KML format, unauthenticated, custom filename', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml&filename=kmltest',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /^attachment/.test(cd), 'KML is not disposed as attachment: ' + cd);
|
|
|
|
assert.equal(true, /filename=kmltest.kml/gi.test(cd), 'Unexpected KML filename: ' + cd);
|
2013-11-06 00:29:02 +08:00
|
|
|
var name = extractFolderName(res.body);
|
|
|
|
assert.equal(name, "kmltest");
|
2013-01-16 16:58:09 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('KML format, authenticated', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20*%20FROM%20untitle_table_4%20LIMIT%201&format=kml&api_key=1234',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.header('Content-Disposition');
|
|
|
|
assert.equal(true, /filename=cartodb-query.kml/gi.test(cd), 'Unexpected KML filename: ' + cd);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-03-26 23:59:37 +08:00
|
|
|
test('KML format, unauthenticated, concurrent requests', function(done){
|
|
|
|
var query = querystring.stringify({
|
2013-11-05 23:56:07 +08:00
|
|
|
q: "SELECT 'val', x, y, st_setsrid(st_makepoint(x,y),4326) as the_geom FROM generate_series(-180, 180) as x, generate_series(-90,90) y",
|
2013-03-26 23:59:37 +08:00
|
|
|
format: 'kml',
|
|
|
|
filename: 'multi'
|
|
|
|
});
|
|
|
|
|
2013-03-27 23:09:31 +08:00
|
|
|
var concurrency = 4;
|
|
|
|
var waiting = concurrency;
|
2013-04-11 22:15:51 +08:00
|
|
|
server_utils.startOnNextPort(app, function() {
|
|
|
|
var port = app.address().port;
|
|
|
|
//console.log("Listening on port " + port);
|
|
|
|
for (var i=0; i<concurrency; ++i) {
|
|
|
|
//console.log("Sending request");
|
|
|
|
var req = http.request({
|
|
|
|
host: '127.0.0.1',
|
|
|
|
port: port,
|
|
|
|
path: '/api/v1/sql?' + query,
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
agent: false // or should this be true ?
|
|
|
|
}).on('response', function(res) {
|
|
|
|
//console.log("Response started");
|
|
|
|
//res.body = '';
|
|
|
|
//res.setEncoding('binary');
|
|
|
|
//res.on('data', function(chunk){ res.body += chunk; });
|
|
|
|
res.on('end', function(){
|
|
|
|
//console.log("Response ended");
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var cd = res.headers['content-disposition'];
|
|
|
|
assert.equal(true, /^attachment/.test(cd), 'KML is not disposed as attachment: ' + cd);
|
|
|
|
assert.equal(true, /filename=multi.kml/gi.test(cd), 'Unexpected KML filename: ' + cd);
|
|
|
|
if ( ! --waiting ) {
|
|
|
|
app.close();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).on('error', function(err) {
|
|
|
|
console.log("Response error" + err);
|
|
|
|
}).end();
|
|
|
|
}
|
|
|
|
});
|
2013-03-26 23:59:37 +08:00
|
|
|
});
|
|
|
|
|
2013-03-27 01:16:27 +08:00
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/60
|
|
|
|
test('GET /api/v1/sql as kml with no rows', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?q=SELECT%20true%20WHERE%20false&format=kml',
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2013-11-06 00:29:02 +08:00
|
|
|
var body = '<?xml version="1.0" encoding="utf-8" ?><kml xmlns="http://www.opengis.net/kml/2.2"><Document><Folder><name>cartodb_query</name></Folder></Document></kml>';
|
2013-08-21 00:06:47 +08:00
|
|
|
assert.equal(res.body.replace(/\n/g,''), body);
|
2013-03-27 01:16:27 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-04-10 23:21:42 +08:00
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/90
|
|
|
|
test('GET /api/v1/sql as kml with ending semicolon', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: 'SELECT true WHERE false;',
|
|
|
|
format: 'kml'
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
2013-11-06 00:29:02 +08:00
|
|
|
var body = '<?xml version="1.0" encoding="utf-8" ?><kml xmlns="http://www.opengis.net/kml/2.2"><Document><Folder><name>cartodb_query</name></Folder></Document></kml>';
|
2013-08-21 00:06:47 +08:00
|
|
|
assert.equal(res.body.replace(/\n/g,''), body);
|
2013-04-10 23:21:42 +08:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-05 23:56:07 +08:00
|
|
|
// See https://github.com/CartoDB/cartodb/issues/276
|
|
|
|
test('check point coordinates, unauthenticated', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: 'SELECT * from untitle_table_4 WHERE cartodb_id = -1',
|
|
|
|
format: 'kml'
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var coords = extractCoordinates(res.body);
|
|
|
|
assert(coords, 'No coordinates in ' + res.body);
|
|
|
|
assert.deepEqual(coords, [[33,16]]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// See https://github.com/CartoDB/cartodb/issues/276
|
|
|
|
test('check point coordinates, authenticated', function(done){
|
|
|
|
assert.response(app, {
|
|
|
|
url: '/api/v1/sql?' + querystring.stringify({
|
|
|
|
q: 'SELECT * from untitle_table_4 WHERE cartodb_id = -1',
|
|
|
|
api_key: 1234,
|
|
|
|
format: 'kml'
|
|
|
|
}),
|
|
|
|
headers: {host: 'vizzuality.cartodb.com'},
|
|
|
|
method: 'GET'
|
|
|
|
},{ }, function(res){
|
|
|
|
assert.equal(res.statusCode, 200, res.body);
|
|
|
|
var coords = extractCoordinates(res.body);
|
|
|
|
assert(coords, 'No coordinates in ' + res.body);
|
|
|
|
assert.deepEqual(coords, [[33,16]]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-01-16 16:58:09 +08:00
|
|
|
});
|