parse.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. var chai = require('chai');
  3. var expect = chai.expect;
  4. chai.should();
  5. var parse = require('../').parse;
  6. describe('parse', function(){
  7. it('using connection string in client constructor', function(){
  8. var subject = parse('postgres://brian:pw@boom:381/lala');
  9. subject.user.should.equal('brian');
  10. subject.password.should.equal( 'pw');
  11. subject.host.should.equal( 'boom');
  12. subject.port.should.equal( '381');
  13. subject.database.should.equal( 'lala');
  14. });
  15. it('escape spaces if present', function(){
  16. var subject = parse('postgres://localhost/post gres');
  17. subject.database.should.equal('post gres');
  18. });
  19. it('do not double escape spaces', function(){
  20. var subject = parse('postgres://localhost/post%20gres');
  21. subject.database.should.equal('post gres');
  22. });
  23. it('initializing with unix domain socket', function(){
  24. var subject = parse('/var/run/');
  25. subject.host.should.equal('/var/run/');
  26. });
  27. it('initializing with unix domain socket and a specific database, the simple way', function(){
  28. var subject = parse('/var/run/ mydb');
  29. subject.host.should.equal('/var/run/');
  30. subject.database.should.equal('mydb');
  31. });
  32. it('initializing with unix domain socket, the health way', function(){
  33. var subject = parse('socket:/some path/?db=my[db]&encoding=utf8');
  34. subject.host.should.equal('/some path/');
  35. subject.database.should.equal('my[db]', 'must to be escaped and unescaped trough "my%5Bdb%5D"');
  36. subject.client_encoding.should.equal('utf8');
  37. });
  38. it('initializing with unix domain socket, the escaped health way', function(){
  39. var subject = parse('socket:/some%20path/?db=my%2Bdb&encoding=utf8');
  40. subject.host.should.equal('/some path/');
  41. subject.database.should.equal('my+db');
  42. subject.client_encoding.should.equal('utf8');
  43. });
  44. it('password contains < and/or > characters', function(){
  45. var sourceConfig = {
  46. user:'brian',
  47. password: 'hello<ther>e',
  48. port: 5432,
  49. host: 'localhost',
  50. database: 'postgres'
  51. };
  52. var connectionString = 'postgres://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database;
  53. var subject = parse(connectionString);
  54. subject.password.should.equal(sourceConfig.password);
  55. });
  56. it('password contains colons', function(){
  57. var sourceConfig = {
  58. user:'brian',
  59. password: 'hello:pass:world',
  60. port: 5432,
  61. host: 'localhost',
  62. database: 'postgres'
  63. };
  64. var connectionString = 'postgres://' + sourceConfig.user + ':' + sourceConfig.password + '@' + sourceConfig.host + ':' + sourceConfig.port + '/' + sourceConfig.database;
  65. var subject = parse(connectionString);
  66. subject.password.should.equal(sourceConfig.password);
  67. });
  68. it('username or password contains weird characters', function(){
  69. var strang = 'pg://my f%irst name:is&%awesome!@localhost:9000';
  70. var subject = parse(strang);
  71. subject.user.should.equal('my f%irst name');
  72. subject.password.should.equal('is&%awesome!');
  73. subject.host.should.equal('localhost');
  74. });
  75. it('url is properly encoded', function(){
  76. var encoded = 'pg://bi%25na%25%25ry%20:s%40f%23@localhost/%20u%2520rl';
  77. var subject = parse(encoded);
  78. subject.user.should.equal('bi%na%%ry ');
  79. subject.password.should.equal('s@f#');
  80. subject.host.should.equal('localhost');
  81. subject.database.should.equal(' u%20rl');
  82. });
  83. it('relative url sets database', function(){
  84. var relative = 'different_db_on_default_host';
  85. var subject = parse(relative);
  86. subject.database.should.equal('different_db_on_default_host');
  87. });
  88. it('no pathname returns null database', function () {
  89. var subject = parse('pg://myhost');
  90. (subject.database === null).should.equal(true);
  91. });
  92. it('pathname of "/" returns null database', function () {
  93. var subject = parse('pg://myhost/');
  94. subject.host.should.equal('myhost');
  95. (subject.database === null).should.equal(true);
  96. });
  97. it('configuration parameter application_name', function(){
  98. var connectionString = 'pg:///?application_name=TheApp';
  99. var subject = parse(connectionString);
  100. subject.application_name.should.equal('TheApp');
  101. });
  102. it('configuration parameter fallback_application_name', function(){
  103. var connectionString = 'pg:///?fallback_application_name=TheAppFallback';
  104. var subject = parse(connectionString);
  105. subject.fallback_application_name.should.equal('TheAppFallback');
  106. });
  107. it('configuration parameter fallback_application_name', function(){
  108. var connectionString = 'pg:///?fallback_application_name=TheAppFallback';
  109. var subject = parse(connectionString);
  110. subject.fallback_application_name.should.equal('TheAppFallback');
  111. });
  112. it('configuration parameter ssl=true', function(){
  113. var connectionString = 'pg:///?ssl=true';
  114. var subject = parse(connectionString);
  115. subject.ssl.should.equal(true);
  116. });
  117. it('configuration parameter ssl=1', function(){
  118. var connectionString = 'pg:///?ssl=1';
  119. var subject = parse(connectionString);
  120. subject.ssl.should.equal(true);
  121. });
  122. it('set ssl', function () {
  123. var subject = parse('pg://myhost/db?ssl=1');
  124. subject.ssl.should.equal(true);
  125. });
  126. it('allow other params like max, ...', function () {
  127. var subject = parse('pg://myhost/db?max=18&min=4');
  128. subject.max.should.equal('18');
  129. subject.min.should.equal('4');
  130. });
  131. it('configuration parameter keepalives', function(){
  132. var connectionString = 'pg:///?keepalives=1';
  133. var subject = parse(connectionString);
  134. subject.keepalives.should.equal('1');
  135. });
  136. it('unknown configuration parameter is passed into client', function(){
  137. var connectionString = 'pg:///?ThereIsNoSuchPostgresParameter=1234';
  138. var subject = parse(connectionString);
  139. subject.ThereIsNoSuchPostgresParameter.should.equal('1234');
  140. });
  141. it('do not override a config field with value from query string', function(){
  142. var subject = parse('socket:/some path/?db=my[db]&encoding=utf8&client_encoding=bogus');
  143. subject.host.should.equal('/some path/');
  144. subject.database.should.equal('my[db]', 'must to be escaped and unescaped through "my%5Bdb%5D"');
  145. subject.client_encoding.should.equal('utf8');
  146. });
  147. it('return last value of repeated parameter', function(){
  148. var connectionString = 'pg:///?keepalives=1&keepalives=0';
  149. var subject = parse(connectionString);
  150. subject.keepalives.should.equal('0');
  151. });
  152. });