@@ -44,6 +44,13 @@ describe('Connection String', function () {
44
44
} ) ;
45
45
} ) ;
46
46
47
+ it ( 'throws an error related to the option that was given an empty value' , function ( ) {
48
+ expect ( ( ) => parseOptions ( 'mongodb://localhost?tls=' , { } ) ) . to . throw (
49
+ MongoAPIError ,
50
+ / t l s " c a n n o t / i
51
+ ) ;
52
+ } ) ;
53
+
47
54
it ( 'should provide a default port if one is not provided' , function ( ) {
48
55
const options = parseOptions ( 'mongodb://hostname' ) ;
49
56
expect ( options . hosts [ 0 ] . socketPath ) . to . be . undefined ;
@@ -122,21 +129,18 @@ describe('Connection String', function () {
122
129
123
130
describe ( 'readPreferenceTags option' , function ( ) {
124
131
context ( 'when the option is passed in the uri' , ( ) => {
125
- it ( 'should throw an error if no value is passed for readPreferenceTags' , ( ) => {
126
- expect ( ( ) => parseOptions ( 'mongodb://hostname?readPreferenceTags=' ) ) . to . throw (
127
- MongoAPIError
128
- ) ;
129
- } ) ;
130
132
it ( 'should parse a single read preference tag' , ( ) => {
131
133
const options = parseOptions ( 'mongodb://hostname?readPreferenceTags=bar:foo' ) ;
132
134
expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } ] ) ;
133
135
} ) ;
136
+
134
137
it ( 'should parse multiple readPreferenceTags' , ( ) => {
135
138
const options = parseOptions (
136
139
'mongodb://hostname?readPreferenceTags=bar:foo&readPreferenceTags=baz:bar'
137
140
) ;
138
141
expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } , { baz : 'bar' } ] ) ;
139
142
} ) ;
143
+
140
144
it ( 'should parse multiple readPreferenceTags for the same key' , ( ) => {
141
145
const options = parseOptions (
142
146
'mongodb://hostname?readPreferenceTags=bar:foo&readPreferenceTags=bar:banana&readPreferenceTags=baz:bar'
@@ -147,6 +151,19 @@ describe('Connection String', function () {
147
151
{ baz : 'bar' }
148
152
] ) ;
149
153
} ) ;
154
+
155
+ it ( 'should parse multiple and empty readPreferenceTags' , ( ) => {
156
+ const options = parseOptions (
157
+ 'mongodb://hostname?readPreferenceTags=bar:foo&readPreferenceTags=baz:bar&readPreferenceTags='
158
+ ) ;
159
+ expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } , { baz : 'bar' } , { } ] ) ;
160
+ } ) ;
161
+
162
+ it ( 'will set "__proto__" as own property on readPreferenceTag' , ( ) => {
163
+ const options = parseOptions ( 'mongodb://hostname?readPreferenceTags=__proto__:foo' ) ;
164
+ expect ( options . readPreference . tags ?. [ 0 ] ) . to . have . own . property ( '__proto__' , 'foo' ) ;
165
+ expect ( Object . getPrototypeOf ( options . readPreference . tags ?. [ 0 ] ) ) . to . be . null ;
166
+ } ) ;
150
167
} ) ;
151
168
152
169
context ( 'when the option is passed in the options object' , ( ) => {
@@ -156,12 +173,14 @@ describe('Connection String', function () {
156
173
} ) ;
157
174
expect ( options . readPreference . tags ) . to . deep . equal ( [ ] ) ;
158
175
} ) ;
176
+
159
177
it ( 'should parse a single readPreferenceTags object' , ( ) => {
160
178
const options = parseOptions ( 'mongodb://hostname?' , {
161
179
readPreferenceTags : [ { bar : 'foo' } ]
162
180
} ) ;
163
181
expect ( options . readPreference . tags ) . to . deep . equal ( [ { bar : 'foo' } ] ) ;
164
182
} ) ;
183
+
165
184
it ( 'should parse multiple readPreferenceTags' , ( ) => {
166
185
const options = parseOptions ( 'mongodb://hostname?' , {
167
186
readPreferenceTags : [ { bar : 'foo' } , { baz : 'bar' } ]
@@ -493,6 +512,18 @@ describe('Connection String', function () {
493
512
) ;
494
513
} ) ;
495
514
515
+ it ( 'throws an error for repeated options that can only appear once' , function ( ) {
516
+ // At the time of writing, readPreferenceTags is the only options that can be repeated
517
+ expect ( ( ) => parseOptions ( 'mongodb://localhost/?compressors=zstd&compressors=zstd' ) ) . to . throw (
518
+ MongoInvalidArgumentError ,
519
+ / c a n n o t a p p e a r m o r e t h a n o n c e /
520
+ ) ;
521
+ expect ( ( ) => parseOptions ( 'mongodb://localhost/?tls=true&tls=true' ) ) . to . throw (
522
+ MongoInvalidArgumentError ,
523
+ / c a n n o t a p p e a r m o r e t h a n o n c e /
524
+ ) ;
525
+ } ) ;
526
+
496
527
it ( 'should validate authMechanism' , function ( ) {
497
528
expect ( ( ) => parseOptions ( 'mongodb://localhost/?authMechanism=DOGS' ) ) . to . throw (
498
529
MongoParseError ,
0 commit comments