• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const {
5  Writable,
6  Readable,
7  destroy
8} = require('stream');
9const assert = require('assert');
10const http = require('http');
11
12{
13  const r = new Readable({ read() {} });
14  destroy(r);
15  assert.strictEqual(r.destroyed, true);
16  r.on('error', common.mustCall((err) => {
17    assert.strictEqual(err.name, 'AbortError');
18  }));
19  r.on('close', common.mustCall());
20}
21
22{
23  const r = new Readable({ read() {} });
24  destroy(r, new Error('asd'));
25  assert.strictEqual(r.destroyed, true);
26  r.on('error', common.mustCall((err) => {
27    assert.strictEqual(err.message, 'asd');
28  }));
29  r.on('close', common.mustCall());
30}
31
32{
33  const w = new Writable({ write() {} });
34  destroy(w);
35  assert.strictEqual(w.destroyed, true);
36  w.on('error', common.mustCall((err) => {
37    assert.strictEqual(err.name, 'AbortError');
38  }));
39  w.on('close', common.mustCall());
40}
41
42{
43  const w = new Writable({ write() {} });
44  destroy(w, new Error('asd'));
45  assert.strictEqual(w.destroyed, true);
46  w.on('error', common.mustCall((err) => {
47    assert.strictEqual(err.message, 'asd');
48  }));
49  w.on('close', common.mustCall());
50}
51
52{
53  const server = http.createServer((req, res) => {
54    destroy(req);
55    req.on('error', common.mustCall((err) => {
56      assert.strictEqual(err.name, 'AbortError');
57    }));
58    req.on('close', common.mustCall(() => {
59      res.end('hello');
60    }));
61  });
62
63  server.listen(0, () => {
64    const req = http.request({
65      port: server.address().port
66    });
67
68    req.write('asd');
69    req.on('response', (res) => {
70      const buf = [];
71      res.on('data', (data) => buf.push(data));
72      res.on('end', common.mustCall(() => {
73        assert.deepStrictEqual(
74          Buffer.concat(buf),
75          Buffer.from('hello')
76        );
77        server.close();
78      }));
79    });
80  });
81}
82
83{
84  const server = http.createServer((req, res) => {
85    req
86      .resume()
87      .on('end', () => {
88        destroy(req);
89      })
90      .on('error', common.mustNotCall());
91
92    req.on('close', common.mustCall(() => {
93      res.end('hello');
94    }));
95  });
96
97  server.listen(0, () => {
98    const req = http.request({
99      port: server.address().port
100    });
101
102    req.write('asd');
103    req.on('response', (res) => {
104      const buf = [];
105      res.on('data', (data) => buf.push(data));
106      res.on('end', common.mustCall(() => {
107        assert.deepStrictEqual(
108          Buffer.concat(buf),
109          Buffer.from('hello')
110        );
111        server.close();
112      }));
113    });
114  });
115}
116