|
1 | 1 | import { expect, AssertionError } from 'chai'; |
2 | 2 | import sinon, { SinonStub } from 'sinon'; |
| 3 | +import { execFileSync } from 'child_process'; |
3 | 4 | import http from 'http'; |
4 | 5 | import { PassThrough } from 'stream'; |
5 | 6 | import { AddressInfo } from 'net'; |
@@ -808,6 +809,41 @@ describe('CloudFetchResultHandler', () => { |
808 | 809 | expect(body.listenerCount('end')).to.eq(0); |
809 | 810 | expect(body.listeners('error')).to.deep.eq(errorsBefore); |
810 | 811 | }); |
| 812 | + |
| 813 | + it('handles late body errors without application error handlers or unhandled rejections', function () { |
| 814 | + this.timeout(15000); |
| 815 | + const script = ` |
| 816 | + const assert = require('assert').strict; |
| 817 | + const { PassThrough } = require('stream'); |
| 818 | + const { Response } = require('node-fetch'); |
| 819 | + const Handler = require('./lib/result/CloudFetchResultHandler').default; |
| 820 | + (async () => { |
| 821 | + const body = new PassThrough(); |
| 822 | + const response = new Response(body); |
| 823 | + const originalErrors = body.listeners('error'); |
| 824 | + assert.equal(originalErrors.length, 1); |
| 825 | + const reading = Handler.prototype.readBody.call({}, response); |
| 826 | + body.end(Buffer.from('result bytes')); |
| 827 | + assert.equal((await reading).toString(), 'result bytes'); |
| 828 | + assert.equal(response.bodyUsed, true); |
| 829 | + assert.equal(body.listenerCount('data'), 0); |
| 830 | + assert.equal(body.listenerCount('end'), 0); |
| 831 | + await new Promise((resolve) => setImmediate(() => { |
| 832 | + body.emit('error', new Error('late body error')); |
| 833 | + resolve(); |
| 834 | + })); |
| 835 | + await new Promise((resolve) => setImmediate(resolve)); |
| 836 | + assert.deepEqual(body.listeners('error'), originalErrors); |
| 837 | + process.stdout.write('late body error handled\\n'); |
| 838 | + })().catch((error) => { console.error(error); process.exitCode = 1; }); |
| 839 | + `; |
| 840 | + const output = execFileSync( |
| 841 | + process.execPath, |
| 842 | + ['--unhandled-rejections=strict', '--require', 'ts-node/register', '-e', script], |
| 843 | + { cwd: process.cwd(), encoding: 'utf8', timeout: 10000, stdio: 'pipe' }, |
| 844 | + ); |
| 845 | + expect(output).to.eq('late body error handled\n'); |
| 846 | + }); |
811 | 847 | }); |
812 | 848 |
|
813 | 849 | describe('download deadline', () => { |
|
0 commit comments