summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-abort-destroy.js
blob: 11b1390a839b6868e6f1366866d314a6630a95a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
const { getEventListeners } = require('events');

{
  // abort

  const server = http.createServer(common.mustCall((req, res) => {
    res.end('Hello');
  }));

  server.listen(0, common.mustCall(() => {
    const options = { port: server.address().port };
    const req = http.get(options, common.mustCall((res) => {
      res.on('data', (data) => {
        req.abort();
        assert.strictEqual(req.aborted, true);
        assert.strictEqual(req.destroyed, true);
        server.close();
      });
    }));
    req.on('error', common.mustNotCall());
    assert.strictEqual(req.aborted, false);
    assert.strictEqual(req.destroyed, false);
  }));
}

{
  // destroy + res

  const server = http.createServer(common.mustCall((req, res) => {
    res.end('Hello');
  }));

  server.listen(0, common.mustCall(() => {
    const options = { port: server.address().port };
    const req = http.get(options, common.mustCall((res) => {
      res.on('data', (data) => {
        req.destroy();
        assert.strictEqual(req.aborted, false);
        assert.strictEqual(req.destroyed, true);
        server.close();
      });
    }));
    req.on('error', common.mustNotCall());
    assert.strictEqual(req.aborted, false);
    assert.strictEqual(req.destroyed, false);
  }));
}

{
  // destroy

  const server = http.createServer(common.mustNotCall());

  server.listen(0, common.mustCall(() => {
    const options = { port: server.address().port };
    const req = http.get(options, common.mustNotCall());
    req.on('error', common.mustCall((err) => {
      assert.strictEqual(err.code, 'ECONNRESET');
      server.close();
    }));
    assert.strictEqual(req.aborted, false);
    assert.strictEqual(req.destroyed, false);
    req.destroy();
    assert.strictEqual(req.aborted, false);
    assert.strictEqual(req.destroyed, true);
  }));
}


{
  // Destroy post-abort sync with AbortSignal

  const server = http.createServer(common.mustNotCall());
  const controller = new AbortController();
  const { signal } = controller;
  server.listen(0, common.mustCall(() => {
    const options = { port: server.address().port, signal };
    const req = http.get(options, common.mustNotCall());
    req.on('error', common.mustCall((err) => {
      assert.strictEqual(err.code, 'ABORT_ERR');
      assert.strictEqual(err.name, 'AbortError');
      server.close();
    }));
    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
    assert.strictEqual(req.aborted, false);
    assert.strictEqual(req.destroyed, false);
    controller.abort();
    assert.strictEqual(req.aborted, false);
    assert.strictEqual(req.destroyed, true);
  }));
}

{
  // Use post-abort async AbortSignal
  const server = http.createServer(common.mustNotCall());
  const controller = new AbortController();
  const { signal } = controller;
  server.listen(0, common.mustCall(() => {
    const options = { port: server.address().port, signal };
    const req = http.get(options, common.mustNotCall());
    req.on('error', common.mustCall((err) => {
      assert.strictEqual(err.code, 'ABORT_ERR');
      assert.strictEqual(err.name, 'AbortError');
    }));

    req.on('close', common.mustCall(() => {
      assert.strictEqual(req.aborted, false);
      assert.strictEqual(req.destroyed, true);
      server.close();
    }));

    assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
    process.nextTick(() => controller.abort());
  }));
}

{
  // Use pre-aborted AbortSignal
  const server = http.createServer(common.mustNotCall());
  const controller = new AbortController();
  const { signal } = controller;
  server.listen(0, common.mustCall(() => {
    controller.abort();
    const options = { port: server.address().port, signal };
    const req = http.get(options, common.mustNotCall());
    assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
    req.on('error', common.mustCall((err) => {
      assert.strictEqual(err.code, 'ABORT_ERR');
      assert.strictEqual(err.name, 'AbortError');
      server.close();
    }));
    assert.strictEqual(req.aborted, false);
    assert.strictEqual(req.destroyed, true);
  }));
}