|
8 | 8 | type Collection,
|
9 | 9 | type MongoClient,
|
10 | 10 | MongoDBResponse,
|
11 |
| - MongoError, |
12 | 11 | MongoServerError,
|
13 | 12 | OpMsgResponse
|
14 | 13 | } from '../../../mongodb';
|
@@ -240,127 +239,95 @@ describe('utf8 validation with cursors', function () {
|
240 | 239 | });
|
241 | 240 | });
|
242 | 241 |
|
243 |
| - async function expectReject(fn: () => Promise<void>, options?: { regex?: RegExp; errorClass }) { |
244 |
| - const regex = options?.regex ?? /.*/; |
245 |
| - const errorClass = options?.errorClass ?? MongoError; |
| 242 | + async function expectReject(fn: () => Promise<void>) { |
246 | 243 | try {
|
247 | 244 | await fn();
|
248 | 245 | expect.fail('expected the provided callback function to reject, but it did not.');
|
249 | 246 | } catch (error) {
|
250 |
| - expect(error).to.match(regex); |
251 |
| - expect(error).to.be.instanceOf(errorClass); |
| 247 | + expect(error).to.match(/Invalid UTF-8 string in BSON document/); |
| 248 | + expect(error).to.be.instanceOf(BSONError); |
252 | 249 | }
|
253 | 250 | }
|
254 | 251 |
|
255 | 252 | context('when utf-8 validation is explicitly enabled', function () {
|
256 | 253 | it('a for-await loop throw a BSON error', async function () {
|
257 |
| - await expectReject( |
258 |
| - async () => { |
259 |
| - for await (const _doc of collection.find({}, { enableUtf8Validation: true })); |
260 |
| - }, |
261 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
262 |
| - ); |
| 254 | + await expectReject(async () => { |
| 255 | + for await (const _doc of collection.find({}, { enableUtf8Validation: true })); |
| 256 | + }); |
263 | 257 | });
|
264 | 258 | it('next() throws a BSON error', async function () {
|
265 |
| - await expectReject( |
266 |
| - async () => { |
267 |
| - const cursor = collection.find({}, { enableUtf8Validation: true }); |
268 |
| - |
269 |
| - while (await cursor.hasNext()) { |
270 |
| - await cursor.next(); |
271 |
| - } |
272 |
| - }, |
273 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
274 |
| - ); |
| 259 | + await expectReject(async () => { |
| 260 | + const cursor = collection.find({}, { enableUtf8Validation: true }); |
| 261 | + |
| 262 | + while (await cursor.hasNext()) { |
| 263 | + await cursor.next(); |
| 264 | + } |
| 265 | + }); |
275 | 266 | });
|
276 | 267 |
|
277 | 268 | it('toArray() throws a BSON error', async function () {
|
278 |
| - await expectReject( |
279 |
| - async () => { |
280 |
| - const cursor = collection.find({}, { enableUtf8Validation: true }); |
281 |
| - await cursor.toArray(); |
282 |
| - }, |
283 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
284 |
| - ); |
| 269 | + await expectReject(async () => { |
| 270 | + const cursor = collection.find({}, { enableUtf8Validation: true }); |
| 271 | + await cursor.toArray(); |
| 272 | + }); |
285 | 273 | });
|
286 | 274 |
|
287 | 275 | it('.stream() throws a BSONError', async function () {
|
288 |
| - await expectReject( |
289 |
| - async () => { |
290 |
| - const cursor = collection.find({}, { enableUtf8Validation: true }); |
291 |
| - await cursor.stream().toArray(); |
292 |
| - }, |
293 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
294 |
| - ); |
| 276 | + await expectReject(async () => { |
| 277 | + const cursor = collection.find({}, { enableUtf8Validation: true }); |
| 278 | + await cursor.stream().toArray(); |
| 279 | + }); |
295 | 280 | });
|
296 | 281 |
|
297 | 282 | it('tryNext() throws a BSONError', async function () {
|
298 |
| - await expectReject( |
299 |
| - async () => { |
300 |
| - const cursor = collection.find({}, { enableUtf8Validation: true }); |
301 |
| - |
302 |
| - while (await cursor.hasNext()) { |
303 |
| - await cursor.tryNext(); |
304 |
| - } |
305 |
| - }, |
306 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
307 |
| - ); |
| 283 | + await expectReject(async () => { |
| 284 | + const cursor = collection.find({}, { enableUtf8Validation: true }); |
| 285 | + |
| 286 | + while (await cursor.hasNext()) { |
| 287 | + await cursor.tryNext(); |
| 288 | + } |
| 289 | + }); |
308 | 290 | });
|
309 | 291 | });
|
310 | 292 |
|
311 | 293 | context('utf-8 validation defaults to enabled', function () {
|
312 | 294 | it('a for-await loop throw a BSON error', async function () {
|
313 |
| - await expectReject( |
314 |
| - async () => { |
315 |
| - for await (const _doc of collection.find({})); |
316 |
| - }, |
317 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
318 |
| - ); |
| 295 | + await expectReject(async () => { |
| 296 | + for await (const _doc of collection.find({})); |
| 297 | + }); |
319 | 298 | });
|
320 | 299 | it('next() throws a BSON error', async function () {
|
321 |
| - await expectReject( |
322 |
| - async () => { |
323 |
| - const cursor = collection.find({}); |
324 |
| - |
325 |
| - while (await cursor.hasNext()) { |
326 |
| - await cursor.next(); |
327 |
| - } |
328 |
| - }, |
329 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
330 |
| - ); |
| 300 | + await expectReject(async () => { |
| 301 | + const cursor = collection.find({}); |
| 302 | + |
| 303 | + while (await cursor.hasNext()) { |
| 304 | + await cursor.next(); |
| 305 | + } |
| 306 | + }); |
331 | 307 | });
|
332 | 308 |
|
333 | 309 | it('toArray() throws a BSON error', async function () {
|
334 |
| - await expectReject( |
335 |
| - async () => { |
336 |
| - const cursor = collection.find({}); |
337 |
| - await cursor.toArray(); |
338 |
| - }, |
339 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
340 |
| - ); |
| 310 | + await expectReject(async () => { |
| 311 | + const cursor = collection.find({}); |
| 312 | + await cursor.toArray(); |
| 313 | + }); |
341 | 314 | });
|
342 | 315 |
|
343 | 316 | it('.stream() throws a BSONError', async function () {
|
344 |
| - await expectReject( |
345 |
| - async () => { |
346 |
| - const cursor = collection.find({}); |
347 |
| - await cursor.stream().toArray(); |
348 |
| - }, |
349 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
350 |
| - ); |
| 317 | + await expectReject(async () => { |
| 318 | + const cursor = collection.find({}); |
| 319 | + await cursor.stream().toArray(); |
| 320 | + }); |
351 | 321 | });
|
352 | 322 |
|
353 | 323 | it('tryNext() throws a BSONError', async function () {
|
354 |
| - await expectReject( |
355 |
| - async () => { |
356 |
| - const cursor = collection.find({}, { enableUtf8Validation: true }); |
357 |
| - |
358 |
| - while (await cursor.hasNext()) { |
359 |
| - await cursor.tryNext(); |
360 |
| - } |
361 |
| - }, |
362 |
| - { errorClass: BSONError, regex: /Invalid UTF-8 string in BSON document/ } |
363 |
| - ); |
| 324 | + await expectReject(async () => { |
| 325 | + const cursor = collection.find({}, { enableUtf8Validation: true }); |
| 326 | + |
| 327 | + while (await cursor.hasNext()) { |
| 328 | + await cursor.tryNext(); |
| 329 | + } |
| 330 | + }); |
364 | 331 | });
|
365 | 332 | });
|
366 | 333 | });
|
0 commit comments