File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff 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 ] ;
Original file line number Diff line number Diff 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+ } ) ;
You can’t perform that action at this time.
0 commit comments