Skip to content

Commit 5806afc

Browse files
committed
Merge pull request brianc#276 from bryanburgers/connection-url-ssl
Add ssl query string to the connection string parser brianc#275
2 parents 95da124 + 79f85a4 commit 5806afc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/connection-parameters.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ var parse = function(str) {
1818
}
1919
// url parse expects spaces encoded as %20
2020
str = encodeURI(str);
21-
var result = url.parse(str);
21+
var result = url.parse(str, true);
2222
var config = {};
2323
config.host = result.hostname;
2424
config.database = result.pathname ? result.pathname.slice(1) : null;
2525
var auth = (result.auth || ':').split(':');
2626
config.user = auth[0];
2727
config.password = auth[1];
2828
config.port = result.port;
29+
30+
var ssl = result.query.ssl;
31+
if (ssl === 'true' || ssl === '1') {
32+
config.ssl = true;
33+
}
34+
2935
return config;
3036
};
3137

test/unit/connection-parameters/environment-variable-tests.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ test('connection string parsing', function(t) {
5454
assert.equal(subject.database, 'lala', 'string database');
5555
});
5656

57+
test('connection string parsing - ssl', function(t) {
58+
var string = 'postgres://brian:pw@boom:381/lala?ssl=true';
59+
var subject = new ConnectionParameters(string);
60+
assert.equal(subject.ssl, true, 'ssl');
61+
62+
string = 'postgres://brian:pw@boom:381/lala?ssl=1';
63+
subject = new ConnectionParameters(string);
64+
assert.equal(subject.ssl, true, 'ssl');
65+
66+
string = 'postgres://brian:pw@boom:381/lala?other&ssl=true';
67+
subject = new ConnectionParameters(string);
68+
assert.equal(subject.ssl, true, 'ssl');
69+
70+
string = 'postgres://brian:pw@boom:381/lala?ssl=0';
71+
subject = new ConnectionParameters(string);
72+
assert.equal(!!subject.ssl, false, 'ssl');
73+
74+
string = 'postgres://brian:pw@boom:381/lala';
75+
subject = new ConnectionParameters(string);
76+
assert.equal(!!subject.ssl, false, 'ssl');
77+
});
78+
5779
//restore process.env
5880
for(var key in realEnv) {
5981
process.env[key] = realEnv[key];

0 commit comments

Comments
 (0)