@@ -8,7 +8,7 @@ const { WriteConcern } = require('../../src/write_concern');
88const { ReadPreference } = require ( '../../src/read_preference' ) ;
99const { Logger } = require ( '../../src/logger' ) ;
1010const { MongoCredentials } = require ( '../../src/cmap/auth/mongo_credentials' ) ;
11- const { MongoClient, MongoParseError } = require ( '../../src' ) ;
11+ const { MongoClient, MongoParseError, ServerApiVersion } = require ( '../../src' ) ;
1212
1313describe ( 'MongoOptions' , function ( ) {
1414 it ( 'MongoClient should always freeze public options' , function ( ) {
@@ -122,6 +122,7 @@ describe('MongoOptions', function () {
122122 serializeFunctions : true ,
123123 serverSelectionTimeoutMS : 3 ,
124124 servername : 'some tls option' ,
125+ serverApi : { version : '1' } ,
125126 socketTimeoutMS : 3 ,
126127 ssl : true ,
127128 sslPass : 'pass' ,
@@ -369,4 +370,71 @@ describe('MongoOptions', function () {
369370 expect ( optionsUndefined . rejectUnauthorized ) . to . equal ( undefined ) ;
370371 expect ( optionsUndefined . checkServerIdentity ) . to . equal ( undefined ) ;
371372 } ) ;
373+
374+ describe ( 'serverApi' , function ( ) {
375+ it ( 'is supported as a client option when it is a valid ServerApiVersion string' , function ( ) {
376+ const validVersions = Object . values ( ServerApiVersion ) ;
377+ expect ( validVersions . length ) . to . be . at . least ( 1 ) ;
378+ for ( const version of validVersions ) {
379+ const result = parseOptions ( 'mongodb://localhost/' , {
380+ serverApi : version
381+ } ) ;
382+ expect ( result ) . to . have . property ( 'serverApi' ) . deep . equal ( { version } ) ;
383+ }
384+ } ) ;
385+
386+ it ( 'is supported as a client option when it is an object with a valid version property' , function ( ) {
387+ const validVersions = Object . values ( ServerApiVersion ) ;
388+ expect ( validVersions . length ) . to . be . at . least ( 1 ) ;
389+ for ( const version of validVersions ) {
390+ const result = parseOptions ( 'mongodb://localhost/' , {
391+ serverApi : { version }
392+ } ) ;
393+ expect ( result ) . to . have . property ( 'serverApi' ) . deep . equal ( { version } ) ;
394+ }
395+ } ) ;
396+
397+ it ( 'is not supported as a client option when it is an invalid string' , function ( ) {
398+ expect ( ( ) =>
399+ parseOptions ( 'mongodb://localhost/' , {
400+ serverApi : 'bad'
401+ } )
402+ ) . to . throw ( / ^ I n v a l i d s e r v e r A P I v e r s i o n = b a d ; / ) ;
403+ } ) ;
404+
405+ it ( 'is not supported as a client option when it is a number' , function ( ) {
406+ expect ( ( ) =>
407+ parseOptions ( 'mongodb://localhost/' , {
408+ serverApi : 1
409+ } )
410+ ) . to . throw ( / ^ I n v a l i d ` s e r v e r A p i ` p r o p e r t y ; / ) ;
411+ } ) ;
412+
413+ it ( 'is not supported as a client option when it is an object without a specified version' , function ( ) {
414+ expect ( ( ) =>
415+ parseOptions ( 'mongodb://localhost/' , {
416+ serverApi : { }
417+ } )
418+ ) . to . throw ( / ^ I n v a l i d ` s e r v e r A p i ` p r o p e r t y ; / ) ;
419+ } ) ;
420+
421+ it ( 'is not supported as a client option when it is an object with an invalid specified version' , function ( ) {
422+ expect ( ( ) =>
423+ parseOptions ( 'mongodb://localhost/' , {
424+ serverApi : { version : 1 }
425+ } )
426+ ) . to . throw ( / ^ I n v a l i d s e r v e r A P I v e r s i o n = 1 ; / ) ;
427+ expect ( ( ) =>
428+ parseOptions ( 'mongodb://localhost/' , {
429+ serverApi : { version : 'bad' }
430+ } )
431+ ) . to . throw ( / ^ I n v a l i d s e r v e r A P I v e r s i o n = b a d ; / ) ;
432+ } ) ;
433+
434+ it ( 'is not supported as a URI option even when it is a valid ServerApiVersion string' , function ( ) {
435+ expect ( ( ) => parseOptions ( 'mongodb://localhost/?serverApi=1' ) ) . to . throw (
436+ 'URI cannot contain `serverApi`, it can only be passed to the client'
437+ ) ;
438+ } ) ;
439+ } ) ;
372440} ) ;
0 commit comments