Skip to content

Commit cc1f36c

Browse files
asdf
1 parent e71e98e commit cc1f36c

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

test/integration/node-specific/bson-options/utf8_validation.test.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { expect } from 'chai';
2+
import * as net from 'net';
23
import * as sinon from 'sinon';
4+
import { buffer } from 'stream/consumers';
35

46
import {
57
BSON,
@@ -9,7 +11,8 @@ import {
911
MongoDBResponse,
1012
MongoError,
1113
MongoServerError,
12-
OpMsgResponse
14+
OpMsgResponse,
15+
serialize
1316
} from '../../../mongodb';
1417

1518
const EXPECTED_VALIDATION_DISABLED_ARGUMENT = {
@@ -157,15 +160,36 @@ describe('class MongoDBResponse', () => {
157160
);
158161
});
159162

160-
describe('utf8 validation with cursors', function () {
163+
describe.only('utf8 validation with cursors', function () {
161164
let client: MongoClient;
162165
let collection: Collection;
163166

164167
beforeEach(async function () {
165168
client = this.configuration.newClient();
166169
await client.connect();
167170
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();
169193
});
170194

171195
afterEach(async function () {
@@ -275,7 +299,8 @@ describe('utf8 validation with cursors', function () {
275299
it('a for-await loop throw a BSON error', async function () {
276300
await expectReject(
277301
async () => {
278-
for await (const doc of collection.find({}));
302+
for await (const doc of collection.find({})) {
303+
}
279304
},
280305
{ errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ }
281306
);

0 commit comments

Comments
 (0)