host= query param takes precedence

This commit is contained in:
Daniel Rozenberg 2020-01-28 19:28:03 -05:00
parent a483bdf5d9
commit 0ff40e733b
2 changed files with 17 additions and 1 deletions

View File

@ -35,7 +35,10 @@ function parse(str) {
config.client_encoding = result.query.encoding;
return config;
}
config.host = result.hostname;
if (!config.host) {
// Only set the host if there is no equivalent query param.
config.host = result.hostname;
}
// result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls)
// only strip the slash if it is present.

View File

@ -120,6 +120,19 @@ describe('parse', function(){
(subject.database === null).should.equal(true);
});
it('configuration parameter host', function() {
var subject = parse('pg://user:pass@/dbname?host=/unix/socket');
subject.user.should.equal('user');
subject.password.should.equal('pass');
subject.host.should.equal('/unix/socket');
subject.database.should.equal('dbname');
});
it('configuration parameter host overrides url host', function() {
var subject = parse('pg://user:pass@localhost/dbname?host=/unix/socket');
subject.host.should.equal('/unix/socket');
});
it('configuration parameter application_name', function(){
var connectionString = 'pg:///?application_name=TheApp';
var subject = parse(connectionString);