CartoDB-SQL-API/test/acceptance/frontend-abort-test.js

79 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-10-24 21:42:33 +08:00
'use strict';
require('../helper');
2015-05-13 17:21:44 +08:00
var assert = require('../support/assert');
var step = require('step');
var net = require('net');
2019-12-26 23:10:41 +08:00
var sqlServerDataHandler;
var sqlServerPort = 5556;
var sqlServer = net.createServer(function (c) {
2019-12-24 01:19:08 +08:00
c.on('data', function (d) {
console.log('SQL Server got data: ' + d);
2019-12-26 23:10:41 +08:00
if (sqlServerDataHandler) {
console.log('Sending data to sqlServerDataHandler');
sqlServerDataHandler(null, d);
2019-12-24 01:19:08 +08:00
}
c.destroy();
});
});
2019-12-24 01:19:08 +08:00
describe('frontend abort', function () {
before(function (done) {
2019-12-26 23:10:41 +08:00
sqlServer.listen(sqlServerPort, done);
2019-12-24 01:19:08 +08:00
});
2019-12-24 01:19:08 +08:00
// See https://github.com/CartoDB/CartoDB-SQL-API/issues/129
it('aborts request', function (done) {
// console.log("settings:"); console.dir(global.settings);
2019-12-26 23:10:41 +08:00
var dbHostBackup = global.settings.db_host;
var dbPortBackup = global.settings.db_port;
2019-12-24 01:19:08 +08:00
global.settings.db_host = 'localhost';
2019-12-26 23:10:41 +08:00
global.settings.db_port = sqlServerPort;
2019-12-24 01:19:08 +08:00
var server = require('../../lib/server')();
var timeout;
step(
function sendQuery () {
assert.response(server, {
url: '/api/v1/sql?q=SELECT+1',
method: 'GET',
timeout: 1,
headers: { host: 'vizzuality.localhost' }
}, {}, this);
},
function checkResponse (err/*, res */) {
assert(err); // expect timeout
assert.ok(('' + err).match(/socket/), err);
2019-12-26 23:10:41 +08:00
sqlServerDataHandler = this;
2019-12-24 01:19:08 +08:00
var next = this;
// If a call does not arrive to the sql server within
// the given timeout we're confident it means the request
// was successfully aborted
timeout = setTimeout(function () { next(null); }, 500);
},
function checkSqlServerData (err, data) {
2019-12-26 23:10:41 +08:00
assert.ifError(err);
2019-12-24 01:19:08 +08:00
clearTimeout(timeout);
assert.ok(!data, 'SQL Server was contacted no matter client abort');
// TODO: intercept logs ?
return null;
},
function finish (err) {
2019-12-26 23:10:41 +08:00
global.settings.db_host = dbHostBackup;
global.settings.db_port = dbPortBackup;
2019-12-24 01:19:08 +08:00
done(err);
}
);
});
2019-12-24 01:19:08 +08:00
after(function (done) {
try {
2019-12-26 23:10:41 +08:00
sqlServer.close(done);
2019-12-24 01:19:08 +08:00
} catch (er) {
console.log(er);
done(); // error expected as server is probably closed already
}
});
});