PageRenderTime 39ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/mongoose/node_modules/muri/test/index.js

https://gitlab.com/bethalice523/WebAPI
JavaScript | 328 lines | 303 code | 25 blank | 0 comment | 2 complexity | da1a55f6f971ff3f3ccf852bd2f16bfc MD5 | raw file
  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('host', function(){
  46. it('must be specified', function(done){
  47. assert.throws(function () {
  48. muri('mongodb://');
  49. }, /Missing host/)
  50. assert.throws(function () {
  51. muri('mongodb:///fake');
  52. }, /Missing host/)
  53. assert.throws(function () {
  54. muri('mongodb://?yep');
  55. }, /Missing host/)
  56. assert.throws(function () {
  57. muri('mongodb:///?yep');
  58. }, /Missing host/)
  59. var val = muri('mongodb://local');
  60. assert.ok(Array.isArray(val.hosts));
  61. assert.equal(1, val.hosts.length);
  62. assert.equal('local', val.hosts[0].host);
  63. done();
  64. })
  65. it('supports replica sets', function(done){
  66. var val = muri('mongodb://local:27017,remote:27018,japan:99999');
  67. assert.ok(Array.isArray(val.hosts));
  68. assert.equal(3, val.hosts.length);
  69. assert.equal('local', val.hosts[0].host);
  70. assert.equal(27017, val.hosts[0].port);
  71. assert.equal('remote', val.hosts[1].host);
  72. assert.equal(27018, val.hosts[1].port);
  73. assert.equal('japan', val.hosts[2].host);
  74. assert.equal(99999, val.hosts[2].port);
  75. done();
  76. })
  77. })
  78. describe('port', function(){
  79. describe('with single host', function(){
  80. it('defaults to 27017 if not specified', function(done){
  81. var val = muri('mongodb://local/');
  82. assert.equal(27017, val.hosts[0].port);
  83. done();
  84. })
  85. it('uses what is specified', function(done){
  86. var val = muri('mongodb://local:27018');
  87. assert.equal(27018, val.hosts[0].port);
  88. done();
  89. })
  90. })
  91. describe('with replica sets', function(){
  92. var val;
  93. before(function(){
  94. val = muri('mongodb://local,remote:27018,another');
  95. })
  96. it('defaults to 27017 if not specified', function(done){
  97. assert.equal(27017, val.hosts[0].port);
  98. assert.equal(27017, val.hosts[2].port);
  99. done();
  100. })
  101. it('uses what is specified', function(done){
  102. assert.equal(27018, val.hosts[1].port);
  103. done();
  104. })
  105. })
  106. })
  107. describe('database', function(){
  108. it('default', function(done){
  109. var val = muri('mongodb://localhost/');
  110. assert.equal('test', val.db);
  111. var val = muri('mongodb://localhost');
  112. assert.equal('test', val.db);
  113. done();
  114. })
  115. it('is overridable', function(done){
  116. var val = muri('mongodb://localhost,a,x:34343,b/muri');
  117. assert.equal('muri', val.db);
  118. done();
  119. })
  120. it('works with multiple specified protocols', function(done){
  121. var uri = 'mongodb://localhost:27020/testing,mongodb://localhost:27019,mongodb://localhost:27018'
  122. var val = muri(uri);
  123. assert.equal('testing', val.db);
  124. done();
  125. })
  126. })
  127. describe('querystring separator', function(){
  128. it('can be ; ', function(done){
  129. var val = muri('mongodb://muri/?replicaSet=myreplset;slaveOk=true;x=1');
  130. assert.ok(val.options);
  131. assert.equal(true, val.options.slaveOk);
  132. assert.equal('myreplset', val.options.replicaSet);
  133. assert.equal(1, val.options.x);
  134. done();
  135. })
  136. it('can be & ', function(done){
  137. var val = muri('mongodb://muri/?replicaSet=myreplset&slaveOk=true&x=1');
  138. assert.ok(val.options);
  139. assert.equal(true, val.options.slaveOk);
  140. assert.equal('myreplset', val.options.replicaSet);
  141. assert.equal(1, val.options.x);
  142. done();
  143. })
  144. })
  145. describe('readPref tags', function(){
  146. describe('with & ', function(){
  147. it('mongodb://localhost/?readPreferenceTags=dc:ny', function(done){
  148. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny');
  149. assert.equal('test', val.db);
  150. assert.deepEqual([{ dc: 'ny' }], val.options.readPreferenceTags);
  151. done();
  152. })
  153. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1', function(done){
  154. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1');
  155. assert.deepEqual([{ dc: 'ny', rack: 1 }], val.options.readPreferenceTags);
  156. done();
  157. })
  158. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:sf,rack:2', function(done){
  159. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:sf,rack:2');
  160. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'sf', rack: 2 }], val.options.readPreferenceTags);
  161. done();
  162. })
  163. it('mongodb://localhost/db?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:sf,rack:2&readPreferenceTags=', function(done){
  164. var val = muri('mongodb://localhost/db?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:sf,rack:2&readPreferenceTags=');
  165. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'sf', rack: 2 }], val.options.readPreferenceTags);
  166. done();
  167. })
  168. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:ny&readPreferenceTags=', function(done){
  169. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1&readPreferenceTags=dc:ny&readPreferenceTags=');
  170. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'ny' }], val.options.readPreferenceTags);
  171. done();
  172. })
  173. })
  174. describe('with ; ', function(){
  175. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:sf,rack:2', function(done){
  176. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:sf,rack:2');
  177. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'sf', rack: 2 }], val.options.readPreferenceTags);
  178. done();
  179. })
  180. it('mongodb://localhost/db?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:sf,rack:2;readPreferenceTags=', function(done){
  181. var val = muri('mongodb://localhost/db?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:sf,rack:2;readPreferenceTags=');
  182. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'sf', rack: 2 }], val.options.readPreferenceTags);
  183. done();
  184. })
  185. it('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:ny;readPreferenceTags=', function(done){
  186. var val = muri('mongodb://localhost/?readPreferenceTags=dc:ny,rack:1;readPreferenceTags=dc:ny;readPreferenceTags=');
  187. assert.deepEqual([{ dc: 'ny', rack: 1 }, { dc: 'ny' }], val.options.readPreferenceTags);
  188. done();
  189. })
  190. })
  191. })
  192. describe('unix domain sockets', function(){
  193. it('without auth', function(done){
  194. var val = muri('mongodb:///tmp/mongodb-27017.sock?safe=false');
  195. assert.equal(val.db, 'test')
  196. assert.ok(Array.isArray(val.hosts));
  197. assert.equal(1, val.hosts.length);
  198. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  199. assert.equal(val.hosts[0].host, undefined);
  200. assert.equal(val.hosts[0].port, undefined);
  201. assert.equal(false, val.options.safe);
  202. done();
  203. })
  204. it('without auth with a database name', function(done){
  205. var val = muri('mongodb:///tmp/mongodb-27017.sock/tester?safe=false');
  206. assert.equal(val.db, 'tester')
  207. assert.ok(Array.isArray(val.hosts));
  208. assert.equal(1, val.hosts.length);
  209. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  210. assert.equal(val.hosts[0].host, undefined);
  211. assert.equal(val.hosts[0].port, undefined);
  212. assert.equal(false, val.options.safe);
  213. done();
  214. })
  215. it('with auth', function(done){
  216. var val = muri('mongodb://user:password@/tmp/mongodb-27017.sock?safe=false');
  217. assert.equal(val.db, 'admin')
  218. assert.ok(Array.isArray(val.hosts));
  219. assert.equal(1, val.hosts.length);
  220. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  221. assert.equal(val.hosts[0].host, undefined);
  222. assert.equal(val.hosts[0].port, undefined);
  223. assert.equal(false, val.options.safe);
  224. done();
  225. })
  226. it('with auth with a db name', function(done){
  227. var val = muri('mongodb://user:password@/tmp/mongodb-27017.sock/tester?safe=false');
  228. assert.equal(val.db, 'tester')
  229. assert.ok(Array.isArray(val.hosts));
  230. assert.equal(1, val.hosts.length);
  231. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  232. assert.equal(val.hosts[0].host, undefined);
  233. assert.equal(val.hosts[0].port, undefined);
  234. assert.equal(false, val.options.safe);
  235. done();
  236. })
  237. it('with auth + repl sets', function(done){
  238. var val = muri('mongodb://user:password@/tmp/mongodb-27017.sock,/tmp/another-27018.sock?safe=false');
  239. assert.equal(val.db, 'admin')
  240. assert.ok(Array.isArray(val.hosts));
  241. assert.equal(2, val.hosts.length);
  242. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  243. assert.equal(val.hosts[0].host, undefined);
  244. assert.equal(val.hosts[0].port, undefined);
  245. assert.equal(val.hosts[1].ipc, '/tmp/another-27018.sock')
  246. assert.equal(val.hosts[1].host, undefined);
  247. assert.equal(val.hosts[1].port, undefined);
  248. assert.equal(false, val.options.safe);
  249. done();
  250. })
  251. it('with auth + repl sets with a db name', function(done){
  252. var val = muri('mongodb://user:password@/tmp/mongodb-27017.sock,/tmp/another-27018.sock/tester?safe=false');
  253. assert.equal(val.db, 'tester')
  254. assert.ok(Array.isArray(val.hosts));
  255. assert.equal(2, val.hosts.length);
  256. assert.equal(val.hosts[0].ipc, '/tmp/mongodb-27017.sock')
  257. assert.equal(val.hosts[0].host, undefined);
  258. assert.equal(val.hosts[0].port, undefined);
  259. assert.equal(val.hosts[1].ipc, '/tmp/another-27018.sock')
  260. assert.equal(val.hosts[1].host, undefined);
  261. assert.equal(val.hosts[1].port, undefined);
  262. assert.equal(false, val.options.safe);
  263. done();
  264. })
  265. })
  266. it('all together now', function(done){
  267. var uri = 'mongodb://u#ser:pas#s@local,remote:27018,japan:27019/neatdb'
  268. uri += '?replicaSet=myreplset&journal=true&w=2&wtimeoutMS=50'
  269. var val = muri(uri);
  270. assert.equal('u#ser', val.auth.user);
  271. assert.equal('pas#s', val.auth.pass);
  272. assert.equal('neatdb', val.db);
  273. assert.equal(3, val.hosts.length);
  274. assert.equal('local', val.hosts[0].host);
  275. assert.strictEqual(27017, val.hosts[0].port);
  276. assert.equal('remote', val.hosts[1].host);
  277. assert.strictEqual(27018, val.hosts[1].port);
  278. assert.equal('japan', val.hosts[2].host);
  279. assert.strictEqual(27019, val.hosts[2].port);
  280. assert.equal('myreplset', val.options.replicaSet);
  281. assert.equal(true, val.options.journal);
  282. assert.equal(50, val.options.wtimeoutMS);
  283. done();
  284. });
  285. it('ipv6', function(done) {
  286. var uri = 'mongodb://[::1]:27017/test';
  287. var val = muri(uri);
  288. assert.equal(1, val.hosts.length);
  289. assert.equal('::1', val.hosts[0].host);
  290. assert.equal(27017, val.hosts[0].port);
  291. done();
  292. });
  293. it('has a version', function(done){
  294. assert.ok(muri.version);
  295. done();
  296. })
  297. it('replica set name with a leading number', function(done){
  298. var uri = 'mongodb://localhost:27017/test?replicaSet=1800-shard-0';
  299. var val = muri(uri);
  300. assert.equal('1800-shard-0', val.options.replicaSet);
  301. done()
  302. })
  303. })