1
1
import { expect } from 'chai' ;
2
+ import * as net from 'net' ;
2
3
import * as sinon from 'sinon' ;
4
+ import { buffer } from 'stream/consumers' ;
3
5
4
6
import {
5
7
BSON ,
@@ -9,7 +11,8 @@ import {
9
11
MongoDBResponse ,
10
12
MongoError ,
11
13
MongoServerError ,
12
- OpMsgResponse
14
+ OpMsgResponse ,
15
+ serialize
13
16
} from '../../../mongodb' ;
14
17
15
18
const EXPECTED_VALIDATION_DISABLED_ARGUMENT = {
@@ -157,15 +160,36 @@ describe('class MongoDBResponse', () => {
157
160
) ;
158
161
} ) ;
159
162
160
- describe ( 'utf8 validation with cursors' , function ( ) {
163
+ describe . only ( 'utf8 validation with cursors' , function ( ) {
161
164
let client : MongoClient ;
162
165
let collection : Collection ;
163
166
164
167
beforeEach ( async function ( ) {
165
168
client = this . configuration . newClient ( ) ;
166
169
await client . connect ( ) ;
167
170
const db = client . db ( 'test' ) ;
168
- collection = db . collection ( 'invalidutf8' ) ;
171
+ collection = db . collection ( 'invalidutf' ) ;
172
+
173
+ await collection . deleteMany ( { } ) ;
174
+
175
+ const stub = sinon . stub ( net . Socket . prototype , 'write' ) . callsFake ( function ( ...args ) {
176
+ if ( args [ 0 ] . toString ( 'hex' ) . includes ( 'c3a9' ) ) {
177
+ const buffer = Buffer . from ( args [ 0 ] . toString ( 'hex' ) . replace ( 'c3a9' , 'c301' ) , 'hex' ) ;
178
+ const result = stub . wrappedMethod . apply ( this , [ buffer ] ) ;
179
+ sinon . restore ( ) ;
180
+ return result ;
181
+ }
182
+ const result = stub . wrappedMethod . apply ( this , args ) ;
183
+ return result ;
184
+ } ) ;
185
+
186
+ const document = {
187
+ field : 'é'
188
+ } ;
189
+
190
+ await collection . insertOne ( document ) ;
191
+
192
+ sinon . restore ( ) ;
169
193
} ) ;
170
194
171
195
afterEach ( async function ( ) {
@@ -275,7 +299,8 @@ describe('utf8 validation with cursors', function () {
275
299
it ( 'a for-await loop throw a BSON error' , async function ( ) {
276
300
await expectReject (
277
301
async ( ) => {
278
- for await ( const doc of collection . find ( { } ) ) ;
302
+ for await ( const doc of collection . find ( { } ) ) {
303
+ }
279
304
} ,
280
305
{ errorClass : BSONError , regex : / I n v a l i d U T F - 8 s t r i n g i n B S O N d o c u m e n t / }
281
306
) ;
0 commit comments