@@ -8,7 +8,7 @@ const { WriteConcern } = require('../../src/write_concern');
8
8
const { ReadPreference } = require ( '../../src/read_preference' ) ;
9
9
const { Logger } = require ( '../../src/logger' ) ;
10
10
const { MongoCredentials } = require ( '../../src/cmap/auth/mongo_credentials' ) ;
11
- const { MongoClient } = require ( '../../src' ) ;
11
+ const { MongoClient, MongoParseError } = require ( '../../src' ) ;
12
12
13
13
describe ( 'MongoOptions' , function ( ) {
14
14
it ( 'MongoClient should always freeze public options' , function ( ) {
@@ -138,9 +138,8 @@ describe('MongoOptions', function () {
138
138
zlibCompressionLevel : 2
139
139
} ;
140
140
141
- it ( 'All options' , function ( ) {
141
+ it ( 'should parse all options from the options object ' , function ( ) {
142
142
const options = parseOptions ( 'mongodb://localhost:27017/' , ALL_OPTIONS ) ;
143
-
144
143
// Check consolidated options
145
144
expect ( options ) . has . property ( 'writeConcern' ) ;
146
145
expect ( options . writeConcern ) . has . property ( 'w' , 2 ) ;
@@ -183,7 +182,7 @@ describe('MongoOptions', function () {
183
182
'zlibCompressionLevel=2'
184
183
] . join ( '&' ) ;
185
184
186
- it ( 'All URI options' , function ( ) {
185
+ it ( 'should parse all options from the URI string ' , function ( ) {
187
186
const options = parseOptions ( allURIOptions ) ;
188
187
expect ( options ) . has . property ( 'zlibCompressionLevel' , 2 ) ;
189
188
@@ -192,6 +191,40 @@ describe('MongoOptions', function () {
192
191
expect ( options . writeConcern ) . has . property ( 'wtimeout' , 2 ) ;
193
192
} ) ;
194
193
194
+ it ( 'should ignore undefined and null values in the options object' , function ( ) {
195
+ const options = parseOptions ( 'mongodb://localhost:27017/' , {
196
+ maxPoolSize : null ,
197
+ servername : undefined ,
198
+ randomopt : null ,
199
+ otherrandomopt : undefined
200
+ } ) ;
201
+
202
+ // test valid option key with default value
203
+ expect ( options ) . to . have . property ( 'maxPoolSize' , 100 ) ;
204
+
205
+ // test valid option key without default value
206
+ expect ( options ) . not . to . have . property ( 'servername' ) ;
207
+
208
+ // test invalid option keys that are null/undefined
209
+ expect ( options ) . not . to . have . property ( 'randomopt' ) ;
210
+ expect ( options ) . not . to . have . property ( 'otherrandomopt' ) ;
211
+ } ) ;
212
+
213
+ it ( 'should throw an error on unrecognized keys in the options object if they are defined' , function ( ) {
214
+ expect ( ( ) =>
215
+ parseOptions ( 'mongodb://localhost:27017/' , {
216
+ randomopt : 'test'
217
+ } )
218
+ ) . to . throw ( MongoParseError , 'option randomopt is not supported' ) ;
219
+
220
+ expect ( ( ) =>
221
+ parseOptions ( 'mongodb://localhost:27017/' , {
222
+ randomopt : 'test' ,
223
+ randomopt2 : 'test'
224
+ } )
225
+ ) . to . throw ( MongoParseError , 'options randomopt, randomopt2 are not supported' ) ;
226
+ } ) ;
227
+
195
228
it ( 'srvHost saved to options for later resolution' , function ( ) {
196
229
const options = parseOptions ( 'mongodb+srv://server.example.com/' ) ;
197
230
expect ( options ) . has . property ( 'srvHost' , 'server.example.com' ) ;
0 commit comments