|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { parse, toClientConfig } = require('../') |
| 4 | + |
| 5 | +describe('toClientConfig', function () { |
| 6 | + it('converts connection info', function () { |
| 7 | + const config = parse('postgres://brian:pw@boom:381/lala') |
| 8 | + const clientConfig = toClientConfig(config) |
| 9 | + |
| 10 | + clientConfig.user.should.equal('brian') |
| 11 | + clientConfig.password.should.equal('pw') |
| 12 | + clientConfig.host.should.equal('boom') |
| 13 | + clientConfig.port.should.equal(381) |
| 14 | + clientConfig.database.should.equal('lala') |
| 15 | + }) |
| 16 | + |
| 17 | + it('converts query params', function () { |
| 18 | + const config = parse( |
| 19 | + 'postgres:///?application_name=TheApp&fallback_application_name=TheAppFallback&client_encoding=utf8&options=-c geqo=off' |
| 20 | + ) |
| 21 | + const clientConfig = toClientConfig(config) |
| 22 | + |
| 23 | + clientConfig.application_name.should.equal('TheApp') |
| 24 | + clientConfig.fallback_application_name.should.equal('TheAppFallback') |
| 25 | + clientConfig.client_encoding.should.equal('utf8') |
| 26 | + clientConfig.options.should.equal('-c geqo=off') |
| 27 | + }) |
| 28 | + |
| 29 | + it('converts SSL boolean', function () { |
| 30 | + const config = parse('pg:///?ssl=true') |
| 31 | + const clientConfig = toClientConfig(config) |
| 32 | + |
| 33 | + clientConfig.ssl.should.equal(true) |
| 34 | + }) |
| 35 | + |
| 36 | + it('converts sslmode=disable', function () { |
| 37 | + const config = parse('pg:///?sslmode=disable') |
| 38 | + const clientConfig = toClientConfig(config) |
| 39 | + |
| 40 | + clientConfig.ssl.should.equal(false) |
| 41 | + }) |
| 42 | + |
| 43 | + it('converts sslmode=noverify', function () { |
| 44 | + const config = parse('pg:///?sslmode=no-verify') |
| 45 | + const clientConfig = toClientConfig(config) |
| 46 | + |
| 47 | + clientConfig.ssl.rejectUnauthorized.should.equal(false) |
| 48 | + }) |
| 49 | + |
| 50 | + it('converts other sslmode options', function () { |
| 51 | + const config = parse('pg:///?sslmode=verify-ca') |
| 52 | + const clientConfig = toClientConfig(config) |
| 53 | + |
| 54 | + clientConfig.ssl.should.deep.equal({}) |
| 55 | + }) |
| 56 | + |
| 57 | + it('converts other sslmode options', function () { |
| 58 | + const config = parse('pg:///?sslmode=verify-ca') |
| 59 | + const clientConfig = toClientConfig(config) |
| 60 | + |
| 61 | + clientConfig.ssl.should.deep.equal({}) |
| 62 | + }) |
| 63 | + |
| 64 | + it('converts ssl cert options', function () { |
| 65 | + const connectionString = |
| 66 | + 'pg:///?sslcert=' + |
| 67 | + __dirname + |
| 68 | + '/example.cert&sslkey=' + |
| 69 | + __dirname + |
| 70 | + '/example.key&sslrootcert=' + |
| 71 | + __dirname + |
| 72 | + '/example.ca' |
| 73 | + const config = parse(connectionString) |
| 74 | + const clientConfig = toClientConfig(config) |
| 75 | + |
| 76 | + clientConfig.ssl.should.deep.equal({ |
| 77 | + ca: 'example ca\n', |
| 78 | + cert: 'example cert\n', |
| 79 | + key: 'example key\n', |
| 80 | + }) |
| 81 | + }) |
| 82 | + |
| 83 | + it('converts unix domain sockets', function () { |
| 84 | + const config = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus') |
| 85 | + const clientConfig = toClientConfig(config) |
| 86 | + clientConfig.host.should.equal('/some path/') |
| 87 | + clientConfig.database.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"') |
| 88 | + clientConfig.client_encoding.should.equal('utf8') |
| 89 | + }) |
| 90 | +}) |
0 commit comments