@@ -6,7 +6,10 @@ import {
6
6
Connection ,
7
7
LEGACY_HELLO_COMMAND ,
8
8
MongoServerError ,
9
- MongoServerSelectionError
9
+ MongoServerSelectionError ,
10
+ OpMsgRequest ,
11
+ OpQueryRequest ,
12
+ ServerApiVersion
10
13
} from '../../mongodb' ;
11
14
12
15
describe ( 'MongoDB Handshake' , ( ) => {
@@ -48,6 +51,7 @@ describe('MongoDB Handshake', () => {
48
51
49
52
context ( 'when compressors are provided on the mongo client' , ( ) => {
50
53
let spy : Sinon . SinonSpy ;
54
+
51
55
before ( ( ) => {
52
56
spy = sinon . spy ( Connection . prototype , 'command' ) ;
53
57
} ) ;
@@ -56,10 +60,78 @@ describe('MongoDB Handshake', () => {
56
60
57
61
it ( 'constructs a handshake with the specified compressors' , async function ( ) {
58
62
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 } ) ;
60
67
expect ( spy . called ) . to . be . true ;
61
68
const handshakeDoc = spy . getCall ( 0 ) . args [ 1 ] ;
62
69
expect ( handshakeDoc ) . to . have . property ( 'compression' ) . to . deep . equal ( [ 'snappy' ] ) ;
63
70
} ) ;
64
71
} ) ;
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
+ } ) ;
65
137
} ) ;
0 commit comments