• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const events = require('events');
6const { createServer, request } = require('http');
7
8events.captureRejections = true;
9
10{
11  const server = createServer(common.mustCall((req, res) => {
12    const _err = new Error('kaboom');
13    res.on('drain', common.mustCall(async () => {
14      throw _err;
15    }));
16
17    res.socket.on('error', common.mustCall((err) => {
18      assert.strictEqual(err, _err);
19    }));
20
21    // Write until there is space in the buffer
22    while (res.write('hello'));
23  }));
24
25  server.listen(0, common.mustCall(() => {
26    const req = request({
27      method: 'GET',
28      host: server.address().host,
29      port: server.address().port
30    });
31
32    req.end();
33
34    req.on('response', common.mustCall((res) => {
35      res.on('aborted', common.mustCall());
36      res.on('error', common.expectsError({
37        code: 'ECONNRESET'
38      }));
39      res.resume();
40      server.close();
41    }));
42  }));
43}
44
45{
46  let _res;
47  let shouldEnd = false;
48  // Not using mustCall here, because it is OS-dependant.
49  const server = createServer((req, res) => {
50    // So that we cleanly stop
51    _res = res;
52
53    if (shouldEnd) {
54      res.end();
55    }
56  });
57
58  server.listen(0, common.mustCall(() => {
59    const _err = new Error('kaboom');
60
61    const req = request({
62      method: 'POST',
63      host: server.address().host,
64      port: server.address().port
65    });
66
67    req.on('response', common.mustNotCall((res) => {
68      // So that we cleanly stop
69      res.resume();
70      server.close();
71    }));
72
73    req.on('error', common.mustCall((err) => {
74      server.close();
75      // On some variants of Windows, this can happen before
76      // the server has received the request.
77      if (_res) {
78        _res.end();
79      } else {
80        shouldEnd = true;
81      }
82      assert.strictEqual(err, _err);
83    }));
84
85    req.on('drain', common.mustCall(async () => {
86      throw _err;
87    }));
88
89    // Write until there is space in the buffer
90    while (req.write('hello'));
91  }));
92}
93