2010-10-24 01:42:01 +08:00
|
|
|
require(__dirname + '/test-helper');
|
2011-03-02 04:35:14 +08:00
|
|
|
var utils = require(__dirname + "/../../lib/utils");
|
|
|
|
var defaults = require(__dirname + "/../../lib").defaults;
|
2010-10-24 01:42:01 +08:00
|
|
|
|
|
|
|
//this tests the monkey patching
|
|
|
|
//to ensure comptability with older
|
|
|
|
//versions of node
|
|
|
|
test("EventEmitter.once", function() {
|
2010-12-10 08:10:42 +08:00
|
|
|
|
2010-10-24 01:42:01 +08:00
|
|
|
//an event emitter
|
|
|
|
var stream = new MemoryStream();
|
|
|
|
|
|
|
|
var callCount = 0;
|
|
|
|
stream.once('single', function() {
|
|
|
|
callCount++;
|
|
|
|
});
|
2010-12-10 08:10:42 +08:00
|
|
|
|
2010-10-24 01:42:01 +08:00
|
|
|
stream.emit('single');
|
|
|
|
stream.emit('single');
|
|
|
|
assert.equal(callCount, 1);
|
|
|
|
});
|
2010-12-10 08:10:42 +08:00
|
|
|
|
|
|
|
|
2011-03-02 04:35:14 +08:00
|
|
|
test('normalizing connection info', function() {
|
|
|
|
test('with objects', function() {
|
|
|
|
test('empty object uses defaults', function() {
|
|
|
|
var input = {};
|
|
|
|
var output = utils.normalizeConnectionInfo(input);
|
|
|
|
assert.equal(output.user, defaults.user);
|
|
|
|
assert.equal(output.database, defaults.database);
|
|
|
|
assert.equal(output.port, defaults.port);
|
|
|
|
assert.equal(output.host, defaults.host);
|
|
|
|
assert.equal(output.password, defaults.password);
|
|
|
|
});
|
2010-12-11 07:32:34 +08:00
|
|
|
|
2011-03-02 04:35:14 +08:00
|
|
|
test('full object ignores defaults', function() {
|
|
|
|
var input = {
|
|
|
|
user: 'test1',
|
|
|
|
database: 'test2',
|
|
|
|
port: 'test3',
|
|
|
|
host: 'test4',
|
|
|
|
password: 'test5'
|
|
|
|
};
|
|
|
|
assert.equal(utils.normalizeConnectionInfo(input), input);
|
|
|
|
});
|
2010-12-11 07:32:34 +08:00
|
|
|
|
2011-03-02 04:35:14 +08:00
|
|
|
test('connection string', function() {
|
|
|
|
test('non-unix socket', function() {
|
|
|
|
test('uses defaults', function() {
|
|
|
|
var input = "";
|
|
|
|
var output = utils.normalizeConnectionInfo(input);
|
|
|
|
assert.equal(output.user, defaults.user);
|
|
|
|
assert.equal(output.database, defaults.database);
|
|
|
|
assert.equal(output.port, defaults.port);
|
|
|
|
assert.equal(output.host, defaults.host);
|
|
|
|
assert.equal(output.password, defaults.password);
|
|
|
|
});
|
|
|
|
test('ignores defaults if string contains them all', function() {
|
|
|
|
var input = "tcp://user1:pass2@host3:3333/databaseName";
|
|
|
|
var output = utils.normalizeConnectionInfo(input);
|
|
|
|
assert.equal(output.user, 'user1');
|
|
|
|
assert.equal(output.database, 'databaseName');
|
|
|
|
assert.equal(output.port, 3333);
|
|
|
|
assert.equal(output.host, 'host3');
|
|
|
|
assert.equal(output.password, 'pass2');
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
test('unix socket', function() {
|
|
|
|
test('uses defaults', function() {
|
|
|
|
var input = "/var/run/postgresql";
|
|
|
|
var output = utils.normalizeConnectionInfo(input);
|
|
|
|
assert.equal(output.user, process.env.USER);
|
|
|
|
assert.equal(output.host, '/var/run/postgresql');
|
|
|
|
assert.equal(output.database, process.env.USER);
|
|
|
|
assert.equal(output.port, 5432);
|
|
|
|
});
|
2011-05-02 10:55:31 +08:00
|
|
|
|
2011-03-02 04:35:14 +08:00
|
|
|
test('uses overridden defaults', function() {
|
|
|
|
defaults.host = "/var/run/postgresql";
|
|
|
|
defaults.user = "boom";
|
|
|
|
defaults.password = "yeah";
|
|
|
|
defaults.port = 1234;
|
|
|
|
var output = utils.normalizeConnectionInfo("asdf");
|
|
|
|
assert.equal(output.user, "boom");
|
|
|
|
assert.equal(output.password, "yeah");
|
|
|
|
assert.equal(output.port, 1234);
|
|
|
|
assert.equal(output.host, "/var/run/postgresql");
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2011-05-06 08:13:43 +08:00
|
|
|
|
|
|
|
test('libpq connection string building', function() {
|
|
|
|
var checkForPart = function(array, part) {
|
|
|
|
assert.ok(array.indexOf(part) > -1, array.join(" ") + " did not contain " + part);
|
|
|
|
}
|
|
|
|
|
|
|
|
test('builds simple string', function() {
|
|
|
|
var config = {
|
|
|
|
user: 'brian',
|
|
|
|
password: 'xyz',
|
|
|
|
port: 888,
|
|
|
|
host: 'localhost',
|
|
|
|
database: 'bam'
|
|
|
|
}
|
|
|
|
utils.buildLibpqConnectionString(config, assert.calls(function(err, constring) {
|
|
|
|
assert.isNull(err)
|
|
|
|
var parts = constring.split(" ");
|
|
|
|
checkForPart(parts, "user='brian'")
|
|
|
|
checkForPart(parts, "password='xyz'")
|
|
|
|
checkForPart(parts, "port='888'")
|
|
|
|
checkForPart(parts, "hostaddr=127.0.0.1")
|
|
|
|
checkForPart(parts, "dbname='bam'")
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
test('builds dns string', function() {
|
|
|
|
var config = {
|
|
|
|
user: 'brian',
|
|
|
|
password: 'asdf',
|
|
|
|
port: 5432,
|
2011-07-11 02:34:25 +08:00
|
|
|
host: 'localhost'
|
2011-05-06 08:13:43 +08:00
|
|
|
}
|
|
|
|
utils.buildLibpqConnectionString(config, assert.calls(function(err, constring) {
|
|
|
|
assert.isNull(err);
|
|
|
|
var parts = constring.split(" ");
|
|
|
|
checkForPart(parts, "user='brian'")
|
2011-07-11 02:34:25 +08:00
|
|
|
checkForPart(parts, "hostaddr=127.0.0.1")
|
2011-05-06 08:13:43 +08:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('error when dns fails', function() {
|
|
|
|
var config = {
|
|
|
|
user: 'brian',
|
|
|
|
password: 'asf',
|
|
|
|
port: 5432,
|
|
|
|
host: 'asdlfkjasldfkksfd#!$!!!!..com'
|
|
|
|
}
|
|
|
|
utils.buildLibpqConnectionString(config, assert.calls(function(err, constring) {
|
|
|
|
assert.ok(err);
|
|
|
|
assert.isNull(constring)
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2012-06-14 03:46:47 +08:00
|
|
|
test('password contains < and/or > characters', function () {
|
2012-07-12 11:44:21 +08:00
|
|
|
return false;
|
2012-06-14 03:46:47 +08:00
|
|
|
var sourceConfig = {
|
|
|
|
user:'brian',
|
|
|
|
password: 'hello<ther>e',
|
|
|
|
port: 5432,
|
|
|
|
host: 'localhost',
|
|
|
|
database: 'postgres'
|
|
|
|
}
|
|
|
|
var connectionString = 'pg://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database;
|
|
|
|
var config = utils.parseConnectionString(connectionString);
|
|
|
|
assert.same(config, sourceConfig);
|
|
|
|
});
|
|
|
|
|
2011-05-06 08:13:43 +08:00
|
|
|
})
|