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