@@ -6,7 +6,10 @@ import {
66 Connection ,
77 LEGACY_HELLO_COMMAND ,
88 MongoServerError ,
9- MongoServerSelectionError
9+ MongoServerSelectionError ,
10+ OpMsgRequest ,
11+ OpQueryRequest ,
12+ ServerApiVersion
1013} from '../../mongodb' ;
1114
1215describe ( 'MongoDB Handshake' , ( ) => {
@@ -48,6 +51,7 @@ describe('MongoDB Handshake', () => {
4851
4952 context ( 'when compressors are provided on the mongo client' , ( ) => {
5053 let spy : Sinon . SinonSpy ;
54+
5155 before ( ( ) => {
5256 spy = sinon . spy ( Connection . prototype , 'command' ) ;
5357 } ) ;
@@ -56,10 +60,78 @@ describe('MongoDB Handshake', () => {
5660
5761 it ( 'constructs a handshake with the specified compressors' , async function ( ) {
5862 client = this . configuration . newClient ( { compressors : [ 'snappy' ] } ) ;
59- await client . connect ( ) ;
63+ // The load-balanced mode doesn’t perform SDAM,
64+ // so `connect` doesn’t do anything unless authentication is enabled.
65+ // Force the driver to send a command to the server in the noauth mode.
66+ await client . db ( 'admin' ) . command ( { ping : 1 } ) ;
6067 expect ( spy . called ) . to . be . true ;
6168 const handshakeDoc = spy . getCall ( 0 ) . args [ 1 ] ;
6269 expect ( handshakeDoc ) . to . have . property ( 'compression' ) . to . deep . equal ( [ 'snappy' ] ) ;
6370 } ) ;
6471 } ) ;
72+
73+ context ( 'when load-balanced' , function ( ) {
74+ let opMsgRequestToBinSpy : Sinon . SinonSpy ;
75+
76+ beforeEach ( ( ) => {
77+ opMsgRequestToBinSpy = sinon . spy ( OpMsgRequest . prototype , 'toBin' ) ;
78+ } ) ;
79+
80+ afterEach ( ( ) => sinon . restore ( ) ) ;
81+
82+ it ( 'sends the hello command as OP_MSG' , {
83+ metadata : { requires : { topology : 'load-balanced' } } ,
84+ test : async function ( ) {
85+ client = this . configuration . newClient ( { loadBalanced : true } ) ;
86+ await client . db ( 'admin' ) . command ( { ping : 1 } ) ;
87+ expect ( opMsgRequestToBinSpy ) . to . have . been . called ;
88+ }
89+ } ) ;
90+ } ) ;
91+
92+ context ( 'when serverApi version is present' , function ( ) {
93+ let opMsgRequestToBinSpy : Sinon . SinonSpy ;
94+
95+ beforeEach ( ( ) => {
96+ opMsgRequestToBinSpy = sinon . spy ( OpMsgRequest . prototype , 'toBin' ) ;
97+ } ) ;
98+
99+ afterEach ( ( ) => sinon . restore ( ) ) ;
100+
101+ it ( 'sends the hello command as OP_MSG' , {
102+ metadata : { requires : { topology : '!load-balanced' , mongodb : '>=5.0' } } ,
103+ test : async function ( ) {
104+ client = this . configuration . newClient ( { } , { serverApi : { version : ServerApiVersion . v1 } } ) ;
105+ await client . connect ( ) ;
106+ expect ( opMsgRequestToBinSpy ) . to . have . been . called ;
107+ }
108+ } ) ;
109+ } ) ;
110+
111+ context ( 'when not load-balanced and serverApi version is not present' , function ( ) {
112+ let opQueryRequestToBinSpy : Sinon . SinonSpy ;
113+ let opMsgRequestToBinSpy : Sinon . SinonSpy ;
114+
115+ beforeEach ( ( ) => {
116+ opQueryRequestToBinSpy = sinon . spy ( OpQueryRequest . prototype , 'toBin' ) ;
117+ opMsgRequestToBinSpy = sinon . spy ( OpMsgRequest . prototype , 'toBin' ) ;
118+ } ) ;
119+
120+ afterEach ( ( ) => sinon . restore ( ) ) ;
121+
122+ it ( 'sends the hello command as OP_MSG' , {
123+ metadata : { requires : { topology : '!load-balanced' , mongodb : '>=5.0' } } ,
124+ test : async function ( ) {
125+ if ( this . configuration . serverApi ) {
126+ this . skipReason = 'Test requires serverApi to NOT be enabled' ;
127+ return this . skip ( ) ;
128+ }
129+ client = this . configuration . newClient ( ) ;
130+ await client . db ( 'admin' ) . command ( { ping : 1 } ) ;
131+ expect ( opQueryRequestToBinSpy ) . to . have . been . called ;
132+ expect ( opMsgRequestToBinSpy ) . to . have . been . called ;
133+ opMsgRequestToBinSpy . calledAfter ( opQueryRequestToBinSpy ) ;
134+ }
135+ } ) ;
136+ } ) ;
65137} ) ;
0 commit comments