|
9 | 9 | pipeline
|
10 | 10 | } = require('stream');
|
11 | 11 | const assert = require('assert');
|
| 12 | +const http = require('http'); |
12 | 13 |
|
13 | 14 | async function tests() {
|
14 | 15 | {
|
@@ -44,9 +45,11 @@ async function tests() {
|
44 | 45 | const iter = Readable.prototype[Symbol.asyncIterator].call(stream);
|
45 | 46 | await iter.next();
|
46 | 47 | await iter.next();
|
47 |
| - await iter.next().catch(common.mustCall((err) => { |
48 |
| - assert.strictEqual(err.message, 'asd'); |
49 |
| - })); |
| 48 | + await iter.next() |
| 49 | + .then(common.mustNotCall()) |
| 50 | + .catch(common.mustCall((err) => { |
| 51 | + assert.strictEqual(err.message, 'asd'); |
| 52 | + })); |
50 | 53 | }
|
51 | 54 |
|
52 | 55 | {
|
@@ -581,6 +584,61 @@ async function tests() {
|
581 | 584 | assert.strictEqual(err, _err);
|
582 | 585 | }));
|
583 | 586 | }
|
| 587 | + |
| 588 | + { |
| 589 | + // Don't destroy if no auto destroy. |
| 590 | + // https://github.com/nodejs/node/issues/35116 |
| 591 | + |
| 592 | + const r = new Readable({ |
| 593 | + autoDestroy: false, |
| 594 | + read() { |
| 595 | + this.push('asd'); |
| 596 | + this.push(null); |
| 597 | + } |
| 598 | + }); |
| 599 | + |
| 600 | + for await (const chunk of r) { |
| 601 | + chunk; |
| 602 | + } |
| 603 | + assert.strictEqual(r.destroyed, false); |
| 604 | + } |
| 605 | + |
| 606 | + { |
| 607 | + // Destroy if no auto destroy and premature break. |
| 608 | + // https://github.com/nodejs/node/pull/35122/files#r485678318 |
| 609 | + |
| 610 | + const r = new Readable({ |
| 611 | + autoDestroy: false, |
| 612 | + read() { |
| 613 | + this.push('asd'); |
| 614 | + } |
| 615 | + }); |
| 616 | + |
| 617 | + for await (const chunk of r) { |
| 618 | + chunk; |
| 619 | + break; |
| 620 | + } |
| 621 | + assert.strictEqual(r.destroyed, true); |
| 622 | + } |
| 623 | + |
| 624 | + { |
| 625 | + // Don't destroy before 'end'. |
| 626 | + |
| 627 | + const r = new Readable({ |
| 628 | + read() { |
| 629 | + this.push('asd'); |
| 630 | + this.push(null); |
| 631 | + } |
| 632 | + }).on('end', () => { |
| 633 | + assert.strictEqual(r.destroyed, false); |
| 634 | + }); |
| 635 | + |
| 636 | + for await (const chunk of r) { |
| 637 | + chunk; |
| 638 | + } |
| 639 | + |
| 640 | + assert.strictEqual(r.destroyed, true); |
| 641 | + } |
584 | 642 | }
|
585 | 643 |
|
586 | 644 | {
|
@@ -643,5 +701,78 @@ async function tests() {
|
643 | 701 | });
|
644 | 702 | }
|
645 | 703 |
|
| 704 | +{ |
| 705 | + let _req; |
| 706 | + const server = http.createServer((request, response) => { |
| 707 | + response.statusCode = 404; |
| 708 | + response.write('never ends'); |
| 709 | + }); |
| 710 | + |
| 711 | + server.listen(() => { |
| 712 | + _req = http.request(`http://localhost:${server.address().port}`) |
| 713 | + .on('response', common.mustCall(async (res) => { |
| 714 | + setTimeout(() => { |
| 715 | + _req.destroy(new Error('something happened')); |
| 716 | + }, 100); |
| 717 | + |
| 718 | + res.on('error', common.mustCall()); |
| 719 | + |
| 720 | + let _err; |
| 721 | + try { |
| 722 | + for await (const chunk of res) { |
| 723 | + chunk; |
| 724 | + } |
| 725 | + } catch (err) { |
| 726 | + _err = err; |
| 727 | + } |
| 728 | + |
| 729 | + assert.strictEqual(_err.code, 'ECONNRESET'); |
| 730 | + server.close(); |
| 731 | + })) |
| 732 | + .on('error', common.mustCall()) |
| 733 | + .end(); |
| 734 | + }); |
| 735 | +} |
| 736 | + |
| 737 | +{ |
| 738 | + async function getParsedBody(request) { |
| 739 | + let body = ''; |
| 740 | + |
| 741 | + for await (const data of request) { |
| 742 | + body += data; |
| 743 | + } |
| 744 | + |
| 745 | + try { |
| 746 | + return JSON.parse(body); |
| 747 | + } catch { |
| 748 | + return {}; |
| 749 | + } |
| 750 | + } |
| 751 | + |
| 752 | + const str = JSON.stringify({ asd: true }); |
| 753 | + const server = http.createServer(async (request, response) => { |
| 754 | + const body = await getParsedBody(request); |
| 755 | + response.statusCode = 200; |
| 756 | + assert.strictEqual(JSON.stringify(body), str); |
| 757 | + response.end(JSON.stringify(body)); |
| 758 | + }).listen(() => { |
| 759 | + http |
| 760 | + .request({ |
| 761 | + method: 'POST', |
| 762 | + hostname: 'localhost', |
| 763 | + port: server.address().port, |
| 764 | + }) |
| 765 | + .end(str) |
| 766 | + .on('response', async (res) => { |
| 767 | + let body = ''; |
| 768 | + for await (const chunk of res) { |
| 769 | + body += chunk; |
| 770 | + } |
| 771 | + assert.strictEqual(body, str); |
| 772 | + server.close(); |
| 773 | + }); |
| 774 | + }); |
| 775 | +} |
| 776 | + |
646 | 777 | // To avoid missing some tests if a promise does not resolve
|
647 | 778 | tests().then(common.mustCall());
|
0 commit comments