|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var test = require('tap').test; |
| 4 | + |
| 5 | +var parse = require('../').parse; |
| 6 | + |
| 7 | +test('using connection string in client constructor', function(t){ |
| 8 | + var subject = parse('postgres://brian:pw@boom:381/lala'); |
| 9 | + t.equal(subject.user,'brian'); |
| 10 | + t.equal(subject.password, 'pw'); |
| 11 | + t.equal(subject.host, 'boom'); |
| 12 | + t.equal(subject.port, '381'); |
| 13 | + t.equal(subject.database, 'lala'); |
| 14 | + t.end(); |
| 15 | +}); |
| 16 | + |
| 17 | +test('escape spaces if present', function(t){ |
| 18 | + var subject = parse('postgres://localhost/post gres'); |
| 19 | + t.equal(subject.database, 'post gres'); |
| 20 | + t.end(); |
| 21 | +}); |
| 22 | + |
| 23 | +test('do not double escape spaces', function(t){ |
| 24 | + var subject = parse('postgres://localhost/post%20gres'); |
| 25 | + t.equal(subject.database, 'post gres'); |
| 26 | + t.end(); |
| 27 | +}); |
| 28 | + |
| 29 | +test('initializing with unix domain socket', function(t){ |
| 30 | + var subject = parse('/var/run/'); |
| 31 | + t.equal(subject.host, '/var/run/'); |
| 32 | + t.end(); |
| 33 | +}); |
| 34 | + |
| 35 | +test('initializing with unix domain socket and a specific database, the simple way', function(t){ |
| 36 | + var subject = parse('/var/run/ mydb'); |
| 37 | + t.equal(subject.host, '/var/run/'); |
| 38 | + t.equal(subject.database, 'mydb'); |
| 39 | + t.end(); |
| 40 | +}); |
| 41 | + |
| 42 | +test('initializing with unix domain socket, the health way', function(t){ |
| 43 | + var subject = parse('socket:/some path/?db=my[db]&encoding=utf8'); |
| 44 | + t.equal(subject.host, '/some path/'); |
| 45 | + t.equal(subject.database, 'my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"'); |
| 46 | + t.equal(subject.client_encoding, 'utf8'); |
| 47 | + t.end(); |
| 48 | +}); |
| 49 | + |
| 50 | +test('initializing with unix domain socket, the escaped health way', function(t){ |
| 51 | + var subject = parse('socket:/some%20path/?db=my%2Bdb&encoding=utf8'); |
| 52 | + t.equal(subject.host, '/some path/'); |
| 53 | + t.equal(subject.database, 'my+db'); |
| 54 | + t.equal(subject.client_encoding, 'utf8'); |
| 55 | + t.end(); |
| 56 | +}); |
| 57 | + |
| 58 | +test('password contains < and/or > characters', function(t){ |
| 59 | + var sourceConfig = { |
| 60 | + user:'brian', |
| 61 | + password: 'hello<ther>e', |
| 62 | + port: 5432, |
| 63 | + host: 'localhost', |
| 64 | + database: 'postgres' |
| 65 | + }; |
| 66 | + var connectionString = 'postgres://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database; |
| 67 | + var subject = parse(connectionString); |
| 68 | + t.equal(subject.password, sourceConfig.password); |
| 69 | + t.end(); |
| 70 | +}); |
| 71 | + |
| 72 | +test('username or password contains weird characters', function(t){ |
| 73 | + var strang = 'pg://my f%irst name:is&%awesome!@localhost:9000'; |
| 74 | + var subject = parse(strang); |
| 75 | + t.equal(subject.user, 'my f%irst name'); |
| 76 | + t.equal(subject.password, 'is&%awesome!'); |
| 77 | + t.equal(subject.host, 'localhost'); |
| 78 | + t.end(); |
| 79 | +}); |
| 80 | + |
| 81 | +test('url is properly encoded', function(t){ |
| 82 | + var encoded = 'pg://bi%25na%25%25ry%20:s%40f%23@localhost/%20u%2520rl'; |
| 83 | + var subject = parse(encoded); |
| 84 | + t.equal(subject.user, 'bi%na%%ry '); |
| 85 | + t.equal(subject.password, 's@f#'); |
| 86 | + t.equal(subject.host, 'localhost'); |
| 87 | + t.equal(subject.database, ' u%20rl'); |
| 88 | + t.end(); |
| 89 | +}); |
0 commit comments