/
uri_spec.rb
517 lines (397 loc) · 13 KB
/
uri_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
require 'spec_helper'
describe Mongo::URI do
let(:scheme) { 'mongodb://' }
let(:uri) { described_class.new(string) }
describe '#initialize' do
context 'string is not uri' do
let(:string) { 'tyler' }
it 'raises an error' do
expect { uri }.to raise_error(Mongo::Error::InvalidURI)
end
end
end
describe '#servers' do
let(:string) { "#{scheme}#{servers}" }
context 'single server' do
let(:servers) { 'localhost' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([servers])
end
end
context 'single server with port' do
let(:servers) { 'localhost:27017' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([servers])
end
end
context 'numerical ipv4 server' do
let(:servers) { '127.0.0.1' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([servers])
end
end
context 'numerical ipv6 server' do
let(:servers) { '[::1]:27107' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([servers])
end
end
context 'unix socket server' do
let(:servers) { '/tmp/mongodb-27017.sock' }
it 'returns an array with the parsed server' do
expect(uri.servers).to eq([servers])
end
end
context 'multiple servers' do
let(:servers) { 'localhost,127.0.0.1' }
it 'returns an array with the parsed servers' do
expect(uri.servers).to eq(servers.split(','))
end
end
context 'multiple servers with ports' do
let(:servers) { '127.0.0.1:27107,localhost:27018' }
it 'returns an array with the parsed servers' do
expect(uri.servers).to eq(servers.split(','))
end
end
end
describe '#client_options' do
let(:db) { TEST_DB }
let(:servers) { 'localhost' }
let(:string) { "#{scheme}#{credentials}@#{servers}/#{db}" }
let(:user) { 'tyler' }
let(:password) { 's3kr4t' }
let(:credentials) { "#{user}:#{password}" }
let(:options) do
uri.client_options
end
it 'includes the database in the options' do
expect(options[:database]).to eq(TEST_DB)
end
it 'includes the credentials in the options' do
expect(options[:user]).to eq(user)
end
it 'includes the options in the options' do
expect(options[:password]).to eq(password)
end
end
describe '#credentials' do
let(:servers) { 'localhost' }
let(:string) { "#{scheme}#{credentials}@#{servers}" }
let(:user) { 'tyler' }
context 'username provided' do
let(:credentials) { "#{user}:" }
it 'returns the username' do
expect(uri.credentials[:user]).to eq(user)
end
end
context 'username and password provided' do
let(:password) { 's3kr4t' }
let(:credentials) { "#{user}:#{password}" }
it 'returns the username' do
expect(uri.credentials[:user]).to eq(user)
end
it 'returns the password' do
expect(uri.credentials[:password]).to eq(password)
end
end
end
describe '#database' do
let(:servers) { 'localhost' }
let(:string) { "#{scheme}#{servers}/#{db}" }
let(:db) { TEST_DB }
context 'database provided' do
it 'returns the database name' do
expect(uri.database).to eq(TEST_DB)
end
end
end
describe '#options' do
let(:servers) { 'localhost' }
let(:string) { "#{scheme}#{servers}/?#{options}" }
context 'when no options were provided' do
let(:string) { "#{scheme}#{servers}" }
it 'returns an empty hash' do
expect(uri.options).to be_empty
end
end
context 'write concern options provided' do
context 'numerical w value' do
let(:options) { 'w=1' }
let(:concern) { { :w => 1 } }
it 'sets the write concern options' do
expect(uri.options[:write]).to eq(concern)
end
end
context 'w=majority' do
let(:options) { 'w=majority' }
let(:concern) { { :w => :majority } }
it 'sets the write concern options' do
expect(uri.options[:write]).to eq(concern)
end
end
context 'journal' do
let(:options) { 'j=true' }
let(:concern) { { :j => true } }
it 'sets the write concern options' do
expect(uri.options[:write]).to eq(concern)
end
end
context 'fsync' do
let(:options) { 'fsync=true' }
let(:concern) { { :fsync => true } }
it 'sets the write concern options' do
expect(uri.options[:write]).to eq(concern)
end
end
context 'wtimeoutMS' do
let(:timeout) { 1234 }
let(:options) { "w=2&wtimeoutMS=#{timeout}" }
let(:concern) { { :w => 2, :timeout => timeout } }
it 'sets the write concern options' do
expect(uri.options[:write]).to eq(concern)
end
end
end
context 'read preference option provided' do
let(:options) { "readPreference=#{mode}" }
context 'primary' do
let(:mode) { 'primary' }
let(:read) { { :mode => :primary } }
it 'sets the read preference' do
expect(uri.options[:read]).to eq(read)
end
end
context 'primaryPreferred' do
let(:mode) { 'primaryPreferred' }
let(:read) { { :mode => :primary_preferred } }
it 'sets the read preference' do
expect(uri.options[:read]).to eq(read)
end
end
context 'secondary' do
let(:mode) { 'secondary' }
let(:read) { { :mode => :secondary } }
it 'sets the read preference' do
expect(uri.options[:read]).to eq(read)
end
end
context 'secondaryPreferred' do
let(:mode) { 'secondaryPreferred' }
let(:read) { { :mode => :secondary_preferred } }
it 'sets the read preference' do
expect(uri.options[:read]).to eq(read)
end
end
context 'nearest' do
let(:mode) { 'nearest' }
let(:read) { { :mode => :nearest } }
it 'sets the read preference' do
expect(uri.options[:read]).to eq(read)
end
end
end
context 'read preferece tags provided' do
context 'single read preference tag set' do
let(:options) do
'readPreferenceTags=dc:ny,rack:1'
end
let(:read) do
{ :tag_sets => [{ :dc => 'ny', :rack => '1' }] }
end
it 'sets the read preference tag set' do
expect(uri.options[:read]).to eq(read)
end
end
context 'multiple read preference tag sets' do
let(:options) do
'readPreferenceTags=dc:ny&readPreferenceTags=dc:bos'
end
let(:read) do
{ :tag_sets => [{ :dc => 'ny' }, { :dc => 'bos' }] }
end
it 'sets the read preference tag sets' do
expect(uri.options[:read]).to eq(read)
end
end
end
context 'replica set option provided' do
let(:rs_name) { TEST_SET }
let(:options) { "replicaSet=#{rs_name}" }
it 'sets the replica set option' do
expect(uri.options[:replica_set]).to eq(rs_name)
end
end
context 'auth mechanism provided' do
let(:options) { "authMechanism=#{mechanism}" }
context 'plain' do
let(:mechanism) { 'PLAIN' }
let(:auth) { { :mechanism => :plain } }
it 'sets the auth mechanism to :plain' do
expect(uri.options[:auth]).to eq(auth)
end
end
context 'mongodb-cr' do
let(:mechanism) { 'MONGODB-CR' }
let(:auth) { { :mechanism => :mongodb_cr } }
it 'sets the auth mechanism to :mongodb_cr' do
expect(uri.options[:auth]).to eq(auth)
end
end
context 'gssapi' do
let(:mechanism) { 'GSSAPI' }
let(:auth) { { :mechanism => :gssapi } }
it 'sets the auth mechanism to :gssapi' do
expect(uri.options[:auth]).to eq(auth)
end
end
end
context 'auth source provided' do
let(:options) { "authSource=#{source}" }
context 'regular db' do
let(:source) { 'foo' }
let(:auth) { { :source => 'foo' } }
it 'sets the auth source to the database' do
expect(uri.options[:auth]).to eq(auth)
end
end
context '$external' do
let(:source) { '$external' }
let(:auth) { { :source => :external } }
it 'sets the auth source to :external' do
expect(uri.options[:auth]).to eq(auth)
end
end
end
context 'auth mechanism properties provided' do
context 'service_name' do
let(:options) do
"authMechanismProperties=SERVICE_NAME:#{service_name}"
end
let(:service_name) { 'foo' }
let(:auth) do
{ auth_mech_properties: { service_name: service_name } }
end
it 'sets the auth mechanism properties' do
expect(uri.options[:auth]).to eq(auth)
end
end
context 'canonicalize_host_name' do
let(:options) do
"authMechanismProperties=CANONICALIZE_HOST_NAME:#{canonicalize_host_name}"
end
let(:canonicalize_host_name) { 'true' }
let(:auth) do
{ auth_mech_properties: { canonicalize_host_name: true } }
end
it 'sets the auth mechanism properties' do
expect(uri.options[:auth]).to eq(auth)
end
end
context 'service_realm' do
let(:options) do
"authMechanismProperties=SERVICE_REALM:#{service_realm}"
end
let(:service_realm) { 'dumdum' }
let(:auth) do
{ auth_mech_properties: { service_realm: service_realm } }
end
it 'sets the auth mechanism properties' do
expect(uri.options[:auth]).to eq(auth)
end
end
context 'multiple properties' do
let(:options) do
"authMechanismProperties=SERVICE_REALM:#{service_realm}," +
"CANONICALIZE_HOST_NAME:#{canonicalize_host_name}," +
"SERVICE_NAME:#{service_name}"
end
let(:service_name) { 'foo' }
let(:canonicalize_host_name) { 'true' }
let(:service_realm) { 'dumdum' }
let(:auth) do
{ auth_mech_properties: { service_name: service_name,
canonicalize_host_name: true,
service_realm: service_realm } }
end
it 'sets the auth mechanism properties' do
expect(uri.options[:auth]).to eq(auth)
end
end
end
context 'connectTimeoutMS' do
let(:options) { "connectTimeoutMS=4567" }
it 'sets the the connect timeout' do
expect(uri.options[:connect_timeout]).to eq(4.567)
end
end
context 'socketTimeoutMS' do
let(:options) { "socketTimeoutMS=8910" }
it 'sets the socket timeout' do
expect(uri.options[:socket_timeout]).to eq(8.910)
end
end
context 'when providing serverSelectionTimeoutMS' do
let(:options) { "serverSelectionTimeoutMS=3561" }
it 'sets the the connect timeout' do
expect(uri.options[:server_selection_timeout]).to eq(3.561)
end
end
context 'when providing localThresholdMS' do
let(:options) { "localThresholdMS=3561" }
it 'sets the the connect timeout' do
expect(uri.options[:local_threshold]).to eq(3.561)
end
end
context 'when providing maxPoolSize' do
let(:max_pool_size) { 10 }
let(:options) { "maxPoolSize=#{max_pool_size}" }
it 'sets the max pool size option' do
expect(uri.options[:max_pool_size]).to eq(max_pool_size)
end
end
context 'when providing minPoolSize' do
let(:min_pool_size) { 5 }
let(:options) { "minPoolSize=#{min_pool_size}" }
it 'sets the min pool size option' do
expect(uri.options[:min_pool_size]).to eq(min_pool_size)
end
end
context 'when providing waitQueueTimeoutMS' do
let(:wait_queue_timeout) { 500 }
let(:options) { "waitQueueTimeoutMS=#{wait_queue_timeout}" }
it 'sets the wait queue timeout option' do
expect(uri.options[:wait_queue_timeout]).to eq(0.5)
end
end
context 'ssl' do
let(:options) { "ssl=#{ssl}" }
context 'true' do
let(:ssl) { true }
it 'sets the ssl option to true' do
expect(uri.options[:ssl]).to be true
end
end
context 'false' do
let(:ssl) { false }
it 'sets the ssl option to false' do
expect(uri.options[:ssl]).to be false
end
end
end
context 'grouped and non-grouped options provided' do
let(:options) { 'w=1&ssl=true' }
it 'do not overshadow top level options' do
expect(uri.options).not_to be_empty
end
end
context 'when an invalid option is provided' do
let(:options) { 'iDontKnow=10' }
it 'raises an exception' do
expect {
uri.options
}.to raise_error(Mongo::Error::InvalidURIOption)
end
end
end
end