Skip to content

Commit 0ff40e7

Browse files
host= query param takes precedence
1 parent a483bdf commit 0ff40e7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ function parse(str) {
3535
config.client_encoding = result.query.encoding;
3636
return config;
3737
}
38-
config.host = result.hostname;
38+
if (!config.host) {
39+
// Only set the host if there is no equivalent query param.
40+
config.host = result.hostname;
41+
}
3942

4043
// result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls)
4144
// only strip the slash if it is present.

test/parse.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ describe('parse', function(){
120120
(subject.database === null).should.equal(true);
121121
});
122122

123+
it('configuration parameter host', function() {
124+
var subject = parse('pg://user:pass@/dbname?host=/unix/socket');
125+
subject.user.should.equal('user');
126+
subject.password.should.equal('pass');
127+
subject.host.should.equal('/unix/socket');
128+
subject.database.should.equal('dbname');
129+
});
130+
131+
it('configuration parameter host overrides url host', function() {
132+
var subject = parse('pg://user:pass@localhost/dbname?host=/unix/socket');
133+
subject.host.should.equal('/unix/socket');
134+
});
135+
123136
it('configuration parameter application_name', function(){
124137
var connectionString = 'pg:///?application_name=TheApp';
125138
var subject = parse(connectionString);

0 commit comments

Comments
 (0)