CartoDB-SQL-API/test/acceptance/backend-crash-test.js

78 lines
2.5 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:57:24 +08:00
var sqlServerPort = 5540;
var sqlServer = net.createServer(function (c) {
2019-12-24 01:19:08 +08:00
c.destroy();
2019-12-26 23:57:24 +08:00
sqlServer.close(function () {
2019-12-24 01:19:08 +08:00
});
});
2019-12-24 01:19:08 +08:00
describe('backend crash', function () {
before(function (done) {
2019-12-26 23:57:24 +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/135
it('does not hang server', function (done) {
2019-12-26 23:57:24 +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:57:24 +08:00
global.settings.db_port = sqlServerPort;
2019-12-24 01:19:08 +08:00
var server = require('../../lib/server')();
step(
function sendQuery () {
assert.response(server, {
url: '/api/v1/sql?q=SELECT+1',
method: 'GET',
headers: { host: 'vizzuality.localhost' }
}, {}, this);
},
function checkResponse (err, res) {
assert.ifError(err);
2019-12-26 21:01:18 +08:00
assert.strictEqual(res.statusCode, 500, res.statusCode + ': ' + res.body);
2019-12-24 01:19:08 +08:00
var parsed = JSON.parse(res.body);
assert.ok(parsed.error);
var msg = parsed.error[0];
assert.ok(msg.match(/unexpected.*end/), msg);
return null;
},
function sendAnotherQuery () {
assert.response(server, {
url: '/api/v1/sql?q=SELECT+2',
method: 'GET',
headers: { host: 'vizzuality.localhost' }
}, {}, this);
},
function checkResponse (err, res) {
assert.ifError(err);
2019-12-26 21:01:18 +08:00
assert.strictEqual(res.statusCode, 500, res.statusCode + ': ' + res.body);
2019-12-24 01:19:08 +08:00
var parsed = JSON.parse(res.body);
assert.ok(parsed.error);
var msg = parsed.error[0];
assert.ok(msg.match(/connect/), msg);
return null;
},
function finish (err) {
2019-12-26 23:57:24 +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) {
2019-12-26 23:57:24 +08:00
// be sure the sqlServer is closed
if (sqlServer.listening) {
return sqlServer.close(done);
2019-12-24 01:19:08 +08:00
}
2016-12-13 18:10:14 +08:00
2019-12-24 01:19:08 +08:00
done();
});
});