Skip to content

Commit c5617dd

Browse files
committed
test: add UTF integration test
1 parent 24413fe commit c5617dd

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { expect } from 'chai';
22
import * as sinon from 'sinon';
33

4-
import { MongoDBResponse } from '../../../mongodb';
4+
import {
5+
BSON,
6+
type MongoClient,
7+
MongoDBResponse,
8+
MongoServerError,
9+
OpMsgResponse
10+
} from '../../../mongodb';
511

612
const EXPECTED_VALIDATION_DISABLED_ARGUMENT = {
713
utf8: false
@@ -107,4 +113,43 @@ describe('class MongoDBResponse', () => {
107113
});
108114
}
109115
});
116+
117+
context(
118+
'when the server is given a long multibyte utf sequence and there is a writeError',
119+
() => {
120+
let client: MongoClient;
121+
beforeEach(async function () {
122+
client = this.configuration.newClient();
123+
});
124+
125+
afterEach(async function () {
126+
sinon.restore();
127+
await client.db('parsing').dropDatabase();
128+
await client.close();
129+
});
130+
131+
it('does not throw a UTF-8 parsing error', async () => {
132+
// Insert a large string of multibyte UTF-8 characters
133+
const _id = '\u{1F92A}'.repeat(1000);
134+
135+
const test = client.db('parsing').collection<{ _id: string }>('parsing');
136+
await test.insertOne({ _id });
137+
138+
const spy = sinon.spy(OpMsgResponse.prototype, 'parse');
139+
140+
const error = await test.insertOne({ _id }).catch(error => error);
141+
142+
// Check that the server sent us broken BSON (bad UTF)
143+
expect(() => {
144+
BSON.deserialize(spy.returnValues[0], { validation: { utf8: true } });
145+
}).to.throw(BSON.BSONError, /Invalid UTF/i);
146+
147+
// Assert the driver squashed it
148+
expect(error).to.be.instanceOf(MongoServerError);
149+
expect(error.message).to.match(/duplicate/i);
150+
expect(error.message).to.not.match(/utf/i);
151+
expect(error.errmsg).to.include('\uFFFD');
152+
});
153+
}
154+
);
110155
});

0 commit comments

Comments
 (0)