File tree 2 files changed +32
-1
lines changed
2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,15 @@ function parse(str) {
36
36
return config ;
37
37
}
38
38
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
+
40
48
var auth = ( result . auth || ':' ) . split ( ':' ) ;
41
49
config . user = auth [ 0 ] ;
42
50
config . password = auth [ 1 ] ;
Original file line number Diff line number Diff line change @@ -87,3 +87,26 @@ test('url is properly encoded', function(t){
87
87
t . equal ( subject . database , ' u%20rl' ) ;
88
88
t . end ( ) ;
89
89
} ) ;
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments