index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. var muri = require('../')
  2. var assert = require('assert')
  3. describe('muri', function(){
  4. it('must begin with mongodb://', function(done){
  5. assert.throws(function () {
  6. muri('localhost:27017');
  7. }, /Invalid mongodb uri/);
  8. assert.doesNotThrow(function () {
  9. muri('mongodb://localhost:27017');
  10. })
  11. done();
  12. })
  13. describe('user:password', function(done){
  14. it('is optional', function(done){
  15. var uri = 'mongodb://local:27017';
  16. var val = muri(uri);
  17. assert.ok(!val.auth);
  18. done();
  19. })
  20. it('parses properly', function(done){
  21. var uri = 'mongodb://user:password@local:27017';
  22. var val = muri(uri);
  23. assert.ok(val.auth);
  24. assert.equal('user', val.auth.user);
  25. assert.equal('password', val.auth.pass);
  26. done();
  27. })
  28. it('handles # in the username', function(done){
  29. var uri = 'mongodb://us#er:password@local:27017';
  30. var val = muri(uri);
  31. assert.ok(val.auth);
  32. assert.equal('us#er', val.auth.user);
  33. assert.equal('password', val.auth.pass);
  34. done();
  35. })
  36. it('handles # in the password', function(done){
  37. var uri = 'mongodb://user:pa#ssword@local:27017';
  38. var val = muri(uri);
  39. assert.ok(val.auth);
  40. assert.equal('user', val.auth.user);
  41. assert.equal('pa#ssword', val.auth.pass);
  42. done();
  43. })
  44. })
  45. describe('user@', function(done){
  46. it('works', function(done){
  47. var uri = 'mongodb://username@local:27017/test?a=b';
  48. var val = muri(uri);
  49. assert.ok(val.auth);
  50. assert.equal(val.auth.user, 'username');
  51. assert.ok(!val.auth.pass);
  52. done();
  53. })
  54. })
  55. describe('host', function(){
  56. it('must be specified', function(done){
  57. assert.throws(function () {
  58. muri('mongodb://');
  59. }, /Missing host/)
  60. assert.throws(function () {
  61. muri('mongodb:///fake');
  62. }, /Missing host/)
  63. assert.throws(function () {
  64. muri('mongodb://?yep');
  65. }, /Missing host/)
  66. assert.throws(function () {
  67. muri('mongodb:///?yep');
  68. }, /Missing host/)
  69. var val = muri('mongodb://local');
  70. assert.ok(Array.isArray(val.hosts));
  71. assert.equal(1, val.hosts.length);
  72. assert.equal('local', val.hosts[0].host);
  73. done();
  74. })
  75. it('supports replica sets', function(done){
  76. var val = muri('mongodb://local:27017,remote:27018,japan:99999');
  77. assert.ok(Array.isArray(val.hosts));
  78. assert.equal(3, val.hosts.length);
  79. assert.equal('local', val.hosts[0].host);
  80. assert.equal(27017, val.hosts[0].port);
  81. assert.equal('remote', val.hosts[1].host);
  82. assert.equal(27018, val.hosts[1].port);
  83. assert.equal('japan', val.hosts[2].host);
  84. assert.equal(99999, val.hosts[2].port);
  85. done();
  86. })
  87. })
  88. describe('port', function(){
  89. describe('with single host', function(){
  90. it('defaults to 27017 if not specified', function(done){
  91. var val = muri('mongodb://local/');
  92. assert.equal(27017, val.hosts[0].port);
  93. done();
  94. })
  95. it('uses what is specified', function(done){
  96. var val = muri('mongodb://local:27018');
  97. assert.equal(27018, val.hosts[0].port);
  98. done();
  99. })
  100. })
  101. describe('with replica sets', function(){
  102. var val;
  103. before(function(){
  104. val = muri('mongodb://local,remote:27018,another');
  105. })
  106. it('defaults to 27017 if not specified', function(done){
  107. assert.equal(27017, val.hosts[0].port);
  108. assert.equal(27017, val.hosts[2].port);
  109. done();
  110. })
  111. it('uses what is specified', function(done){
  112. assert.equal(27018, val.hosts[1].port);
  113. done();
  114. })
  115. })
  116. })
  117. describe('database', function(){
  118. it('default', function(done){
  119. var val = muri('mongodb://localhost/');
  120. assert.equal('test', val.db);
  121. var val = muri('mongodb://localhost');
  122. assert.equal('test', val.db);
  123. done();
  124. })
  125. it('is overridable', function(done){
  126. var val = muri('mongodb://localhost,a,x:34343,b/muri');
  127. assert.equal('muri', val.db);
  128. done();
  129. })
  130. it('works with multiple specified protocols', function(done){
  131. var uri = 'mongodb://localhost:27020/testing,mongodb://localhost:27019,mongodb://localhost:27018'
  132. var val = muri(uri);
  133. assert.equal('testing', val.db);
  134. done();
  135. })
  136. })
  137. describe('querystring separator', function(){
  138. it('can be ; ', function(done){
  139. var val = muri('mongodb://muri/?replicaSet=myreplset;slaveOk=true;x=1');
  140. assert.ok(val.options);
  141. assert.equal(true, val.options.slaveOk);
  142. assert.equal('myreplset', val.options.replicaSet);
  143. assert.equal(1, val.options.x);
  144. done();
  145. })
  146. it('can be & ', function(done){
  147. var val = muri('mongodb://muri/?replicaSet=myreplset&slaveOk=true&x=1');
  148. assert.ok(val.options);
  149. assert.equal(true, val.options.slaveOk);
  150. assert.equal('myreplset', val.options.replicaSet);
  151. assert.equal(1, val.options.x);
  152. done();
  153. })
  154. })
  155. describe('readPref tags', function(){
  156. describe('with & ', function(){
  157. it('mongodb://localhost/?readPreferenceTags=dc:ny', function(done){
  158. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny');
  159. assert.equal('test', val.db);
  160. assert.deepEqual([{ dc: 'ny' }], val.options.readPreferenceTags);
  161. done();
  162. })
  163. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1', function(done){
  164. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1');
  165. assert.deepEqual([{ dc: 'ny', rack: 1 }], val.options.readPreferenceTags);
  166. done();
  167. })
  168. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:sf,rack:2', function(done){
  169. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:sf,rack:2');
  170. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'sf', rack: 2 }], val.options.readPreferenceTags);
  171. done();
  172. })
  173. it('mongodb://localhost/db?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:sf,rack:2&readPreferenceTags=', function(done){
  174. var val = muri('mongodb://localhost/db?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:sf,rack:2&readPreferenceTags=');
  175. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'sf', rack: 2 }], val.options.readPreferenceTags);
  176. done();
  177. })
  178. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:ny&readPreferenceTags=', function(done){
  179. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:ny&readPreferenceTags=');
  180. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'ny' }], val.options.readPreferenceTags);
  181. done();
  182. })
  183. })
  184. describe('with ; ', function(){
  185. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:sf,rack:2', function(done){
  186. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:sf,rack:2');
  187. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'sf', rack: 2 }], val.options.readPreferenceTags);
  188. done();
  189. })
  190. it('mongodb://localhost/db?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:sf,rack:2;readPreferenceTags=', function(done){
  191. var val = muri('mongodb://localhost/db?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:sf,rack:2;readPreferenceTags=');
  192. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'sf', rack: 2 }], val.options.readPreferenceTags);
  193. done();
  194. })
  195. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:ny;readPreferenceTags=', function(done){
  196. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:ny;readPreferenceTags=');
  197. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'ny' }], val.options.readPreferenceTags);
  198. done();
  199. })
  200. })
  201. })
  202. describe('unix domain sockets', function(){
  203. it('without auth', function(done){
  204. var val = muri('mongodb:///tmp/mongodb-27017.sock?safe=false');
  205. assert.equal(val.db, 'test')
  206. assert.ok(Array.isArray(val.hosts));
  207. assert.equal(1, val.hosts.length);
  208. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  209. assert.equal(val.hosts[0].host, undefined);
  210. assert.equal(val.hosts[0].port, undefined);
  211. assert.equal(false, val.options.safe);
  212. done();
  213. })
  214. it('without auth with a database name', function(done){
  215. var val = muri('mongodb:///tmp/mongodb-27017.sock/tester?safe=false');
  216. assert.equal(val.db, 'tester')
  217. assert.ok(Array.isArray(val.hosts));
  218. assert.equal(1, val.hosts.length);
  219. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  220. assert.equal(val.hosts[0].host, undefined);
  221. assert.equal(val.hosts[0].port, undefined);
  222. assert.equal(false, val.options.safe);
  223. done();
  224. })
  225. it('with auth', function(done){
  226. var val = muri('mongodb://user:password@/tmp/mongodb-27017.sock?safe=false');
  227. assert.equal(val.db, 'admin')
  228. assert.ok(Array.isArray(val.hosts));
  229. assert.equal(1, val.hosts.length);
  230. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  231. assert.equal(val.hosts[0].host, undefined);
  232. assert.equal(val.hosts[0].port, undefined);
  233. assert.equal(false, val.options.safe);
  234. done();
  235. })
  236. it('with auth with a db name', function(done){
  237. var val = muri('mongodb://user:password@/tmp/mongodb-27017.sock/tester?safe=false');
  238. assert.equal(val.db, 'tester')
  239. assert.ok(Array.isArray(val.hosts));
  240. assert.equal(1, val.hosts.length);
  241. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  242. assert.equal(val.hosts[0].host, undefined);
  243. assert.equal(val.hosts[0].port, undefined);
  244. assert.equal(false, val.options.safe);
  245. done();
  246. })
  247. it('with auth + repl sets', function(done){
  248. var val = muri('mongodb://user:password@/tmp/mongodb-27017.sock,/tmp/another-27018.sock?safe=false');
  249. assert.equal(val.db, 'admin')
  250. assert.ok(Array.isArray(val.hosts));
  251. assert.equal(2, val.hosts.length);
  252. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  253. assert.equal(val.hosts[0].host, undefined);
  254. assert.equal(val.hosts[0].port, undefined);
  255. assert.equal(val.hosts[1].ipc, '/tmp/another-27018.sock')
  256. assert.equal(val.hosts[1].host, undefined);
  257. assert.equal(val.hosts[1].port, undefined);
  258. assert.equal(false, val.options.safe);
  259. done();
  260. })
  261. it('with auth + repl sets with a db name', function(done){
  262. var val = muri('mongodb://user:password@/tmp/mongodb-27017.sock,/tmp/another-27018.sock/tester?safe=false');
  263. assert.equal(val.db, 'tester')
  264. assert.ok(Array.isArray(val.hosts));
  265. assert.equal(2, val.hosts.length);
  266. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  267. assert.equal(val.hosts[0].host, undefined);
  268. assert.equal(val.hosts[0].port, undefined);
  269. assert.equal(val.hosts[1].ipc, '/tmp/another-27018.sock')
  270. assert.equal(val.hosts[1].host, undefined);
  271. assert.equal(val.hosts[1].port, undefined);
  272. assert.equal(false, val.options.safe);
  273. done();
  274. })
  275. })
  276. it('all together now', function(done){
  277. var uri = 'mongodb://u#ser:pas#s@local,remote:27018,japan:27019/neatdb'
  278. uri += '?replicaSet=myreplset&journal=true&w=2&wtimeoutMS=50'
  279. var val = muri(uri);
  280. assert.equal('u#ser', val.auth.user);
  281. assert.equal('pas#s', val.auth.pass);
  282. assert.equal('neatdb', val.db);
  283. assert.equal(3, val.hosts.length);
  284. assert.equal('local', val.hosts[0].host);
  285. assert.strictEqual(27017, val.hosts[0].port);
  286. assert.equal('remote', val.hosts[1].host);
  287. assert.strictEqual(27018, val.hosts[1].port);
  288. assert.equal('japan', val.hosts[2].host);
  289. assert.strictEqual(27019, val.hosts[2].port);
  290. assert.equal('myreplset', val.options.replicaSet);
  291. assert.equal(true, val.options.journal);
  292. assert.equal(50, val.options.wtimeoutMS);
  293. done();
  294. });
  295. it('ipv6', function(done) {
  296. var uri = 'mongodb://[::1]:27017/test';
  297. var val = muri(uri);
  298. assert.equal(1, val.hosts.length);
  299. assert.equal('::1', val.hosts[0].host);
  300. assert.equal(27017, val.hosts[0].port);
  301. done();
  302. });
  303. it('has a version', function(done){
  304. assert.ok(muri.version);
  305. done();
  306. })
  307. it('replica set name with a leading number', function(done){
  308. var uri = 'mongodb://localhost:27017/test?replicaSet=1800-shard-0';
  309. var val = muri(uri);
  310. assert.equal('1800-shard-0', val.options.replicaSet);
  311. done();
  312. });
  313. it('parses x509-style uris', function(done) {
  314. var userName = 'CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US%';
  315. var uri = 'mongodb://' + encodeURIComponent(userName) + '@server:27017/test?authMechanism=%s&ssl=true';
  316. var val = muri(uri);
  317. assert.equal(val.auth.user, userName);
  318. assert.equal(val.auth.pass, void 0);
  319. done();
  320. });
  321. })