CartoDB-SQL-API/test/acceptance/export/kml.js

405 lines
14 KiB
JavaScript
Raw Normal View History

2013-01-16 16:58:09 +08:00
require('../../helper');
2015-05-13 17:53:14 +08:00
var app = require(global.settings.app_root + '/app/controllers/app')();
var assert = require('../../support/assert');
var querystring = require('querystring');
var libxmljs = require('libxmljs');
var http = require('http');
var 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);
2015-05-13 17:53:14 +08:00
describe('export.kml', function() {
2013-01-16 16:58:09 +08:00
// Check if an attribute is in the KML output
//
// NOTE: "name" and "description" attributes are threated specially
// in that they are matched in case-insensitive way
//
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 + "']";
2015-05-13 17:53:14 +08:00
if ( doc.get(xpath) ) {
return true;
}
xpath = "//Placemark/" + att;
2015-05-13 17:53:14 +08:00
if ( doc.get(xpath) ) {
return true;
}
var lcatt = att.toLowerCase();
2015-05-13 17:53:14 +08:00
if ( lcatt === 'name' || lcatt === 'description' ) {
xpath = "//Placemark/" + lcatt;
2015-05-13 17:53:14 +08:00
if ( doc.get(xpath) ) {
return true;
}
}
//if ( lowerkml.indexOf('simplefield name="'+ loweratt + '"') != -1 ) return true;
//if ( lowerkml.indexOf('<'+loweratt+'>') != -1 ) return true;
return false;
2014-09-22 21:05:38 +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);
2015-05-13 17:53:14 +08:00
if ( ! doc ) {
return;
}
var coo = doc.get("//coordinates");
//console.log("coo: " + coo);
2015-05-13 17:53:14 +08:00
if ( ! coo ) {
return;
}
coo = coo.text();
//console.log("coo: " + coo);
2015-05-13 17:53:14 +08:00
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;
2014-09-22 21:05:38 +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);
2015-05-13 17:53:14 +08:00
if ( ! doc ) {
return;
}
var coo = doc.get("//Document/Folder/name");
//console.log("coo: " + coo);
2015-05-13 17:53:14 +08:00
if ( ! coo ) {
return;
}
coo = coo.text();
//console.log("coo: " + coo);
2015-05-13 17:53:14 +08:00
if ( ! coo ) {
return;
}
return coo;
2014-09-22 21:05:38 +08:00
};
2013-01-16 16:58:09 +08:00
// KML tests
2015-05-13 17:53:14 +08:00
it('KML format, unauthenticated', function(done){
2013-01-16 16:58:09 +08:00
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};
2014-09-22 21:05:38 +08:00
Object.keys(checkfields).forEach(function(f) {
2013-01-16 16:58:09 +08:00
if ( checkfields[f] ) {
assert.ok(hasAttribute(row0, f), "result does not include '" + f + "': " + row0);
2013-01-16 16:58:09 +08:00
} else {
assert.ok(!hasAttribute(row0, f), "result includes '" + f + "'");
2013-01-16 16:58:09 +08:00
}
2014-09-22 21:05:38 +08:00
});
2013-01-16 16:58:09 +08:00
done();
});
});
2015-05-13 17:53:14 +08:00
it('KML format, unauthenticated, POST', function(done){
2013-01-16 16:58:09 +08:00
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();
});
});
2015-05-13 17:53:14 +08:00
it('KML format, bigger than 81920 bytes', function(done){
2013-01-16 16:58:09 +08:00
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();
});
});
2015-05-13 17:53:14 +08:00
it('KML format, skipfields', function(done){
2013-01-16 16:58:09 +08:00
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;
2014-09-22 21:05:38 +08:00
var checkFields = {'Name':1, 'address':0, 'cartodb_id':0, 'the_geom':0, 'the_geom_webmercator':0};
Object.keys(checkFields).forEach(function(f) {
if ( checkFields[f] ) {
assert.ok(hasAttribute(row0, f), "result does not include '" + f + "': " + row0);
2013-01-16 16:58:09 +08:00
} else {
assert.ok(!hasAttribute(row0, f), "result includes '" + f + "'");
2013-01-16 16:58:09 +08:00
}
2014-09-22 21:05:38 +08:00
});
2013-01-16 16:58:09 +08:00
done();
});
});
2015-05-13 17:53:14 +08:00
it('KML format, unauthenticated, custom filename', function(done){
2013-01-16 16:58:09 +08:00
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);
var name = extractFolderName(res.body);
assert.equal(name, "kmltest");
2013-01-16 16:58:09 +08:00
done();
});
});
2015-05-13 17:53:14 +08:00
it('KML format, authenticated', function(done){
2013-01-16 16:58:09 +08:00
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();
});
});
2015-05-13 17:53:14 +08:00
it('KML format, unauthenticated, concurrent requests', function(done){
2013-03-26 23:59:37 +08:00
var query = querystring.stringify({
2015-05-13 17:53:14 +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'
});
var concurrency = 4;
var waiting = concurrency;
2015-05-13 17:53:14 +08:00
function onResponse(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);
assert.ok(res.body);
var snippet = res.body.substr(0, 5);
assert.equal(snippet, "<?xml");
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();
}
});
}
function onError(err) {
console.log("Response error" + err);
}
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");
2014-09-22 21:05:38 +08:00
http.request({
host: 'localhost',
port: port,
path: '/api/v1/sql?' + query,
headers: {host: 'vizzuality.cartodb.com'},
agent: false // or should this be true ?
2015-05-13 17:53:14 +08:00
})
.on('response', onResponse)
.on('error', onError)
.end();
}
});
2013-03-26 23:59:37 +08:00
});
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/60
2015-05-13 17:53:14 +08:00
it('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);
// NOTE: GDAL-1.11+ added 'id="root_doc"' attribute to the output
2015-05-13 17:53:14 +08:00
var pat = new RegExp('^<\\?xml version="1.0" encoding="utf-8" \\?>' +
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document( id="root_doc")?><Folder><name>cartodb_query</name></Folder></Document>' +
'</kml>$');
var body = res.body.replace(/\n/g,'');
assert.ok(body.match(pat),
"Response:\n" + body + '\ndoes not match pattern:\n' + pat);
done();
});
});
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/90
2015-05-13 17:53:14 +08:00
it('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);
// NOTE: GDAL-1.11+ added 'id="root_doc"' attribute to the output
2015-05-13 17:53:14 +08:00
var pat = new RegExp('^<\\?xml version="1.0" encoding="utf-8" \\?>' +
'<kml xmlns="http://www.opengis.net/kml/2.2">' +
'<Document( id="root_doc")?><Folder><name>cartodb_query</name></Folder></Document>' +
'</kml>$');
var body = res.body.replace(/\n/g,'');
assert.ok(body.match(pat),
"Response:\n" + body + '\ndoes not match pattern:\n' + pat);
done();
});
});
// See https://github.com/CartoDB/cartodb/issues/276
2015-05-13 17:53:14 +08:00
it('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
2015-05-13 17:53:14 +08:00
it('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();
});
});
2015-05-13 17:53:14 +08:00
it('expects 1000 placemarks in public table', function(done){
var numberOfRowsInPublicTable = 6,
seriesLimit = 200,
expectedRows = numberOfRowsInPublicTable * seriesLimit;
assert.response(app, {
url: '/api/v1/sql',
data: querystring.stringify({
q: "SELECT x, untitle_table_4.* FROM untitle_table_4, generate_series(1," + seriesLimit + ") x",
format: 'kml'
}),
headers: {host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST'
},
{
status: 200
},
function(res) {
assert.equal(res.body.match(/<Placemark>/g).length, expectedRows);
done();
}
);
});
2015-05-13 17:53:14 +08:00
it('expects 1000 placemarks in private table using the API KEY', function(done){
var numberOfRowsInPrivateTable = 5,
seriesLimit = 200,
expectedRows = numberOfRowsInPrivateTable * seriesLimit;
assert.response(app, {
url: '/api/v1/sql',
data: querystring.stringify({
q: "SELECT x, private_table.* FROM private_table, generate_series(1," + seriesLimit + ") x",
api_key: 1234,
format: 'kml'
}),
headers: {host: 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST'
},
{
status: 200
},
function(res) {
assert.equal(res.body.match(/<Placemark>/g).length, expectedRows);
done();
}
);
});
2013-01-16 16:58:09 +08:00
});