@@ -46,16 +46,138 @@ describe('Connection String', function () {
46
46
expect ( options . hosts [ 0 ] . port ) . to . equal ( 27017 ) ;
47
47
} ) ;
48
48
49
- context ( 'readPreferenceTags' , function ( ) {
50
- it ( 'should parse multiple readPreferenceTags when passed in the uri' , ( ) => {
51
- const options = parseOptions (
52
- 'mongodb://hostname?readPreferenceTags=bar:foo&readPreferenceTags=baz:bar'
53
- ) ;
54
- expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } , { baz : 'bar' } ] ) ;
49
+ describe ( 'ca option' , ( ) => {
50
+ context ( 'when set in the options object' , ( ) => {
51
+ it ( 'should parse a string' , ( ) => {
52
+ const options = parseOptions ( 'mongodb://localhost' , {
53
+ ca : 'hello'
54
+ } ) ;
55
+ expect ( options ) . to . have . property ( 'ca' ) . to . equal ( 'hello' ) ;
56
+ } ) ;
57
+
58
+ it ( 'should parse a NodeJS buffer' , ( ) => {
59
+ const options = parseOptions ( 'mongodb://localhost' , {
60
+ ca : Buffer . from ( [ 1 , 2 , 3 , 4 ] )
61
+ } ) ;
62
+
63
+ expect ( options )
64
+ . to . have . property ( 'ca' )
65
+ . to . deep . equal ( Buffer . from ( [ 1 , 2 , 3 , 4 ] ) ) ;
66
+ } ) ;
67
+
68
+ it ( 'should parse arrays with a single element' , ( ) => {
69
+ const options = parseOptions ( 'mongodb://localhost' , {
70
+ ca : [ 'hello' ]
71
+ } ) ;
72
+ expect ( options ) . to . have . property ( 'ca' ) . to . deep . equal ( [ 'hello' ] ) ;
73
+ } ) ;
74
+
75
+ it ( 'should parse an empty array' , ( ) => {
76
+ const options = parseOptions ( 'mongodb://localhost' , {
77
+ ca : [ ]
78
+ } ) ;
79
+ expect ( options ) . to . have . property ( 'ca' ) . to . deep . equal ( [ ] ) ;
80
+ } ) ;
81
+
82
+ it ( 'should parse arrays with multiple elements' , ( ) => {
83
+ const options = parseOptions ( 'mongodb://localhost' , {
84
+ ca : [ 'hello' , 'world' ]
85
+ } ) ;
86
+ expect ( options ) . to . have . property ( 'ca' ) . to . deep . equal ( [ 'hello' , 'world' ] ) ;
87
+ } ) ;
88
+ } ) ;
89
+
90
+ // TODO(NODE-4172): align uri behavior with object options behavior
91
+ context ( 'when set in the uri' , ( ) => {
92
+ it ( 'should parse a string value' , ( ) => {
93
+ const options = parseOptions ( 'mongodb://localhost?ca=hello' , { } ) ;
94
+ expect ( options ) . to . have . property ( 'ca' ) . to . equal ( 'hello' ) ;
95
+ } ) ;
96
+
97
+ it ( 'should throw an error with a buffer value' , ( ) => {
98
+ const buffer = Buffer . from ( [ 1 , 2 , 3 , 4 ] ) ;
99
+ expect ( ( ) => {
100
+ parseOptions ( `mongodb://localhost?ca=${ buffer . toString ( ) } ` , { } ) ;
101
+ } ) . to . throw ( MongoAPIError ) ;
102
+ } ) ;
103
+
104
+ it ( 'should not parse multiple string values (array of options)' , ( ) => {
105
+ const options = parseOptions ( 'mongodb://localhost?ca=hello,world' , { } ) ;
106
+ expect ( options ) . to . have . property ( 'ca' ) . to . equal ( 'hello,world' ) ;
107
+ } ) ;
108
+ } ) ;
109
+
110
+ it ( 'should prioritize options set in the object over those set in the URI' , ( ) => {
111
+ const options = parseOptions ( 'mongodb://localhost?ca=hello' , {
112
+ ca : [ 'world' ]
113
+ } ) ;
114
+ expect ( options ) . to . have . property ( 'ca' ) . to . deep . equal ( [ 'world' ] ) ;
55
115
} ) ;
116
+ } ) ;
56
117
57
- it ( 'should parse multiple readPreferenceTags when passed in options object' , ( ) => {
58
- const options = parseOptions ( 'mongodb://hostname?' , {
118
+ describe ( 'readPreferenceTags option' , function ( ) {
119
+ context ( 'when the option is passed in the uri' , ( ) => {
120
+ it ( 'should throw an error if no value is passed for readPreferenceTags' , ( ) => {
121
+ expect ( ( ) => parseOptions ( 'mongodb://hostname?readPreferenceTags=' ) ) . to . throw (
122
+ MongoAPIError
123
+ ) ;
124
+ } ) ;
125
+ it ( 'should parse a single read preference tag' , ( ) => {
126
+ const options = parseOptions ( 'mongodb://hostname?readPreferenceTags=bar:foo' ) ;
127
+ expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } ] ) ;
128
+ } ) ;
129
+ it ( 'should parse multiple readPreferenceTags' , ( ) => {
130
+ const options = parseOptions (
131
+ 'mongodb://hostname?readPreferenceTags=bar:foo&readPreferenceTags=baz:bar'
132
+ ) ;
133
+ expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } , { baz : 'bar' } ] ) ;
134
+ } ) ;
135
+ it ( 'should parse multiple readPreferenceTags for the same key' , ( ) => {
136
+ const options = parseOptions (
137
+ 'mongodb://hostname?readPreferenceTags=bar:foo&readPreferenceTags=bar:banana&readPreferenceTags=baz:bar'
138
+ ) ;
139
+ expect ( options . readPreference . tags ) . to . deep . equal ( [
140
+ { bar : 'foo' } ,
141
+ { bar : 'banana' } ,
142
+ { baz : 'bar' }
143
+ ] ) ;
144
+ } ) ;
145
+ } ) ;
146
+
147
+ context ( 'when the option is passed in the options object' , ( ) => {
148
+ it ( 'should not parse an empty readPreferenceTags object' , ( ) => {
149
+ const options = parseOptions ( 'mongodb://hostname?' , {
150
+ readPreferenceTags : [ ]
151
+ } ) ;
152
+ expect ( options . readPreference . tags ) . to . deep . equal ( [ ] ) ;
153
+ } ) ;
154
+ it ( 'should parse a single readPreferenceTags object' , ( ) => {
155
+ const options = parseOptions ( 'mongodb://hostname?' , {
156
+ readPreferenceTags : [ { bar : 'foo' } ]
157
+ } ) ;
158
+ expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } ] ) ;
159
+ } ) ;
160
+ it ( 'should parse multiple readPreferenceTags' , ( ) => {
161
+ const options = parseOptions ( 'mongodb://hostname?' , {
162
+ readPreferenceTags : [ { bar : 'foo' } , { baz : 'bar' } ]
163
+ } ) ;
164
+ expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } , { baz : 'bar' } ] ) ;
165
+ } ) ;
166
+
167
+ it ( 'should parse multiple readPreferenceTags for the same key' , ( ) => {
168
+ const options = parseOptions ( 'mongodb://hostname?' , {
169
+ readPreferenceTags : [ { bar : 'foo' } , { bar : 'banana' } , { baz : 'bar' } ]
170
+ } ) ;
171
+ expect ( options . readPreference . tags ) . to . deep . equal ( [
172
+ { bar : 'foo' } ,
173
+ { bar : 'banana' } ,
174
+ { baz : 'bar' }
175
+ ] ) ;
176
+ } ) ;
177
+ } ) ;
178
+
179
+ it ( 'should prioritize options from the options object over the uri options' , ( ) => {
180
+ const options = parseOptions ( 'mongodb://hostname?readPreferenceTags=a:b' , {
59
181
readPreferenceTags : [ { bar : 'foo' } , { baz : 'bar' } ]
60
182
} ) ;
61
183
expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } , { baz : 'bar' } ] ) ;
@@ -174,25 +296,25 @@ describe('Connection String', function () {
174
296
context ( 'when the options are provided in the URI' , function ( ) {
175
297
context ( 'when the options are equal' , function ( ) {
176
298
context ( 'when both options are true' , function ( ) {
177
- const options = parseOptions ( 'mongodb://localhost/?tls=true&ssl=true' ) ;
178
-
179
299
it ( 'sets the tls option' , function ( ) {
300
+ const options = parseOptions ( 'mongodb://localhost/?tls=true&ssl=true' ) ;
180
301
expect ( options . tls ) . to . be . true ;
181
302
} ) ;
182
303
183
304
it ( 'does not set the ssl option' , function ( ) {
305
+ const options = parseOptions ( 'mongodb://localhost/?tls=true&ssl=true' ) ;
184
306
expect ( options ) . to . not . have . property ( 'ssl' ) ;
185
307
} ) ;
186
308
} ) ;
187
309
188
310
context ( 'when both options are false' , function ( ) {
189
- const options = parseOptions ( 'mongodb://localhost/?tls=false&ssl=false' ) ;
190
-
191
311
it ( 'sets the tls option' , function ( ) {
312
+ const options = parseOptions ( 'mongodb://localhost/?tls=false&ssl=false' ) ;
192
313
expect ( options . tls ) . to . be . false ;
193
314
} ) ;
194
315
195
316
it ( 'does not set the ssl option' , function ( ) {
317
+ const options = parseOptions ( 'mongodb://localhost/?tls=false&ssl=false' ) ;
196
318
expect ( options ) . to . not . have . property ( 'ssl' ) ;
197
319
} ) ;
198
320
} ) ;
@@ -210,38 +332,38 @@ describe('Connection String', function () {
210
332
context ( 'when the options are provided in the options' , function ( ) {
211
333
context ( 'when the options are equal' , function ( ) {
212
334
context ( 'when both options are true' , function ( ) {
213
- const options = parseOptions ( 'mongodb://localhost/' , { tls : true , ssl : true } ) ;
214
-
215
335
it ( 'sets the tls option' , function ( ) {
336
+ const options = parseOptions ( 'mongodb://localhost/' , { tls : true , ssl : true } ) ;
216
337
expect ( options . tls ) . to . be . true ;
217
338
} ) ;
218
339
219
340
it ( 'does not set the ssl option' , function ( ) {
341
+ const options = parseOptions ( 'mongodb://localhost/' , { tls : true , ssl : true } ) ;
220
342
expect ( options ) . to . not . have . property ( 'ssl' ) ;
221
343
} ) ;
222
344
} ) ;
223
345
224
346
context ( 'when both options are false' , function ( ) {
225
347
context ( 'when the URI is an SRV URI' , function ( ) {
226
- const options = parseOptions ( 'mongodb+srv://localhost/' , { tls : false , ssl : false } ) ;
227
-
228
348
it ( 'overrides the tls option' , function ( ) {
349
+ const options = parseOptions ( 'mongodb+srv://localhost/' , { tls : false , ssl : false } ) ;
229
350
expect ( options . tls ) . to . be . false ;
230
351
} ) ;
231
352
232
353
it ( 'does not set the ssl option' , function ( ) {
354
+ const options = parseOptions ( 'mongodb+srv://localhost/' , { tls : false , ssl : false } ) ;
233
355
expect ( options ) . to . not . have . property ( 'ssl' ) ;
234
356
} ) ;
235
357
} ) ;
236
358
237
359
context ( 'when the URI is not SRV' , function ( ) {
238
- const options = parseOptions ( 'mongodb://localhost/' , { tls : false , ssl : false } ) ;
239
-
240
360
it ( 'sets the tls option' , function ( ) {
361
+ const options = parseOptions ( 'mongodb://localhost/' , { tls : false , ssl : false } ) ;
241
362
expect ( options . tls ) . to . be . false ;
242
363
} ) ;
243
364
244
365
it ( 'does not set the ssl option' , function ( ) {
366
+ const options = parseOptions ( 'mongodb://localhost/' , { tls : false , ssl : false } ) ;
245
367
expect ( options ) . to . not . have . property ( 'ssl' ) ;
246
368
} ) ;
247
369
} ) ;
0 commit comments