Skip to content

Commit

Permalink
Merge pull request #487 from aurium/master
Browse files Browse the repository at this point in the history
Set database on socket string connection
  • Loading branch information
brianc committed Dec 20, 2013
2 parents 1884feb + c0fd4b1 commit aa32992
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/connection-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ var val = function(key, config) {
var url = require('url');
//parses a connection string
var parse = function(str) {
var config;
//unix socket
if(str.charAt(0) === '/') {
return { host: str };
config = str.split(' ');
return { host: config[0], database: config[1] };
}
// url parse expects spaces encoded as %20
str = encodeURI(str);
if(/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) str = encodeURI(str);
var result = url.parse(str, true);
var config = {};
config = {};
if(result.protocol == 'socket:') {
config.host = decodeURI(result.pathname);
config.database = result.query.db;
config.client_encoding = result.query.encoding;
return config;
}
config.host = result.hostname;
config.database = result.pathname ? result.pathname.slice(1) : null;
config.database = result.pathname ? decodeURI(result.pathname.slice(1)) : null;
var auth = (result.auth || ':').split(':');
config.user = auth[0];
config.password = auth[1];
Expand Down
34 changes: 34 additions & 0 deletions test/unit/connection-parameters/creation-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,44 @@ test('ConnectionParameters initializing from config', function() {
assert.ok(subject.isDomainSocket === false);
});

test('escape spaces if present', function() {
subject = new ConnectionParameters('postgres://localhost/post gres');
assert.equal(subject.database, 'post gres');
});

test('do not double escape spaces', function() {
subject = new ConnectionParameters('postgres://localhost/post%20gres');
assert.equal(subject.database, 'post gres');
});

test('initializing with unix domain socket', function() {
var subject = new ConnectionParameters('/var/run/');
assert.ok(subject.isDomainSocket);
assert.equal(subject.host, '/var/run/');
assert.equal(subject.database, defaults.user);
});

test('initializing with unix domain socket and a specific database, the simple way', function() {
var subject = new ConnectionParameters('/var/run/ mydb');
assert.ok(subject.isDomainSocket);
assert.equal(subject.host, '/var/run/');
assert.equal(subject.database, 'mydb');
});

test('initializing with unix domain socket, the health way', function() {
var subject = new ConnectionParameters('socket:/some path/?db=my[db]&encoding=utf8');
assert.ok(subject.isDomainSocket);
assert.equal(subject.host, '/some path/');
assert.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"');
assert.equal(subject.client_encoding, 'utf8');
});

test('initializing with unix domain socket, the escaped health way', function() {
var subject = new ConnectionParameters('socket:/some%20path/?db=my%2Bdb&encoding=utf8');
assert.ok(subject.isDomainSocket);
assert.equal(subject.host, '/some path/');
assert.equal(subject.database, 'my+db');
assert.equal(subject.client_encoding, 'utf8');
});

test('libpq connection string building', function() {
Expand Down

0 comments on commit aa32992

Please sign in to comment.