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