-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathclientConfig.js
125 lines (100 loc) · 3.82 KB
/
clientConfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict'
const chai = require('chai')
const expect = chai.expect
chai.should()
const { parse, toClientConfig, parseIntoClientConfig } = require('../')
describe('toClientConfig', function () {
it('converts connection info', function () {
const config = parse('postgres://brian:pw@boom:381/lala')
const clientConfig = toClientConfig(config)
clientConfig.user.should.equal('brian')
clientConfig.password.should.equal('pw')
clientConfig.host.should.equal('boom')
clientConfig.port.should.equal(381)
clientConfig.database.should.equal('lala')
})
it('converts query params', function () {
const config = parse(
'postgres:///?application_name=TheApp&fallback_application_name=TheAppFallback&client_encoding=utf8&options=-c geqo=off'
)
const clientConfig = toClientConfig(config)
clientConfig.application_name.should.equal('TheApp')
clientConfig.fallback_application_name.should.equal('TheAppFallback')
clientConfig.client_encoding.should.equal('utf8')
clientConfig.options.should.equal('-c geqo=off')
})
it('converts SSL boolean', function () {
const config = parse('pg:///?ssl=true')
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.equal(true)
})
it('converts sslmode=disable', function () {
const config = parse('pg:///?sslmode=disable')
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.equal(false)
})
it('converts sslmode=noverify', function () {
const config = parse('pg:///?sslmode=no-verify')
const clientConfig = toClientConfig(config)
clientConfig.ssl.rejectUnauthorized.should.equal(false)
})
it('converts other sslmode options', function () {
const config = parse('pg:///?sslmode=verify-ca')
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.deep.equal({})
})
it('converts other sslmode options', function () {
const config = parse('pg:///?sslmode=verify-ca')
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.deep.equal({})
})
it('converts ssl cert options', function () {
const connectionString =
'pg:///?sslcert=' +
__dirname +
'/example.cert&sslkey=' +
__dirname +
'/example.key&sslrootcert=' +
__dirname +
'/example.ca'
const config = parse(connectionString)
const clientConfig = toClientConfig(config)
clientConfig.ssl.should.deep.equal({
ca: 'example ca\n',
cert: 'example cert\n',
key: 'example key\n',
})
})
it('converts unix domain sockets', function () {
const config = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus')
const clientConfig = toClientConfig(config)
clientConfig.host.should.equal('/some path/')
clientConfig.database.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"')
clientConfig.client_encoding.should.equal('utf8')
})
it('handles invalid port', function () {
const config = parse('postgres://@boom:381/lala')
config.port = 'bogus'
expect(() => toClientConfig(config)).to.throw()
})
it('handles invalid sslconfig values', function () {
const config = parse('postgres://@boom/lala')
config.ssl = {}
config.ssl.cert = null
config.ssl.key = undefined
const clientConfig = toClientConfig(config)
clientConfig.host.should.equal('boom')
clientConfig.database.should.equal('lala')
clientConfig.ssl.should.deep.equal({})
})
})
describe('parseIntoClientConfig', function () {
it('converts url', function () {
const clientConfig = parseIntoClientConfig('postgres://brian:pw@boom:381/lala')
clientConfig.user.should.equal('brian')
clientConfig.password.should.equal('pw')
clientConfig.host.should.equal('boom')
clientConfig.port.should.equal(381)
clientConfig.database.should.equal('lala')
})
})