parse.js 3.8 KB

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