Skip to content

Commit c502985

Browse files
committed
Merge pull request #1 from slickmb/task/relative_url
Support usage of relative urls to set database on the default host
2 parents cb9bee1 + ba511f7 commit c502985

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Diff for: index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ function parse(str) {
3636
return config;
3737
}
3838
config.host = result.hostname;
39-
config.database = result.pathname ? decodeURI(result.pathname.slice(1)) : null;
39+
40+
// result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls)
41+
// only strip the slash if it is present.
42+
var pathname = result.pathname;
43+
if (pathname && pathname.charAt(0) === '/') {
44+
pathname = result.pathname.slice(1) || null;
45+
}
46+
config.database = pathname && decodeURI(pathname);
47+
4048
var auth = (result.auth || ':').split(':');
4149
config.user = auth[0];
4250
config.password = auth[1];

Diff for: test/parse.js

+23
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,26 @@ test('url is properly encoded', function(t){
8787
t.equal(subject.database, ' u%20rl');
8888
t.end();
8989
});
90+
91+
test('relative url sets database', function(t){
92+
var relative = 'different_db_on_default_host';
93+
var subject = parse(relative);
94+
t.equal(subject.database, 'different_db_on_default_host');
95+
t.end();
96+
});
97+
98+
test('no pathname returns null database', function (t) {
99+
var subject = parse('pg://myhost');
100+
t.equal(subject.host, 'myhost');
101+
t.type(subject.database, 'null');
102+
103+
t.end();
104+
});
105+
106+
test('pathname of "/" returns null database', function (t) {
107+
var subject = parse('pg://myhost/');
108+
t.equal(subject.host, 'myhost');
109+
t.type(subject.database, 'null');
110+
111+
t.end();
112+
});

0 commit comments

Comments
 (0)