Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set database on socket string connection #487

Merged
merged 1 commit into from Dec 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/connection-parameters.js
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
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');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bonus points for this!

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