diff --git a/lib/client.js b/lib/client.js index 3093888..5f18b63 100644 --- a/lib/client.js +++ b/lib/client.js @@ -12,7 +12,7 @@ var Connection = require(__dirname + '/connection'); var parseConnectionString = function(str) { var result = url.parse(str); result.host = result.hostname; - result.database = result.pathname.slice(1); + result.database = result.pathname ? result.pathname.slice(1) : null var auth = (result.auth || ':').split(':'); result.user = auth[0]; result.password = auth[1]; diff --git a/test/unit/client/configuration-tests.js b/test/unit/client/configuration-tests.js index 7c7a826..4a90811 100644 --- a/test/unit/client/configuration-tests.js +++ b/test/unit/client/configuration-tests.js @@ -19,7 +19,7 @@ test('client settings', function() { port: 321, password: password }); - + assert.equal(client.user, user); assert.equal(client.database, database); assert.equal(client.port, 321); @@ -27,3 +27,26 @@ test('client settings', function() { }); }); + +test('initializing from a config string', function() { + + test('uses the correct values from the config string', function() { + var client = new Client("pg://brian:pass@host1:333/databasename") + assert.equal(client.user, 'brian') + assert.equal(client.password, "pass") + assert.equal(client.host, "host1") + assert.equal(client.port, 333) + assert.equal(client.database, "databasename") + }) + + test('when not including all values the defaults are used', function() { + var client = new Client("pg://host1") + assert.equal(client.user, "") + assert.equal(client.password, "") + assert.equal(client.host, "host1") + assert.equal(client.port, 5432) + assert.equal(client.database, "") + }) + + +})