|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import * as sinon from 'sinon';
|
3 | 3 |
|
4 |
| -import { MongoDBResponse } from '../../../mongodb'; |
| 4 | +import { |
| 5 | + BSON, |
| 6 | + type MongoClient, |
| 7 | + MongoDBResponse, |
| 8 | + MongoServerError, |
| 9 | + OpMsgResponse |
| 10 | +} from '../../../mongodb'; |
5 | 11 |
|
6 | 12 | const EXPECTED_VALIDATION_DISABLED_ARGUMENT = {
|
7 | 13 | utf8: false
|
@@ -107,4 +113,43 @@ describe('class MongoDBResponse', () => {
|
107 | 113 | });
|
108 | 114 | }
|
109 | 115 | });
|
| 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 | + ); |
110 | 155 | });
|
0 commit comments