• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4// This test ensures that the `'close'` event is emitted after the `'error'`
5// event when a request is made and the socket is closed before we started to
6// receive a response.
7
8const assert = require('assert');
9const http = require('http');
10
11const server = http.createServer(common.mustNotCall());
12
13server.listen(0, common.mustCall(() => {
14  const req = http.get({ port: server.address().port }, common.mustNotCall());
15  let errorEmitted = false;
16
17  req.on('error', common.mustCall((err) => {
18    errorEmitted = true;
19    assert.strictEqual(err.constructor, Error);
20    assert.strictEqual(err.message, 'socket hang up');
21    assert.strictEqual(err.code, 'ECONNRESET');
22  }));
23
24  req.on('close', common.mustCall(() => {
25    assert.strictEqual(req.destroyed, true);
26    assert.strictEqual(errorEmitted, true);
27    server.close();
28  }));
29
30  req.destroy();
31}));
32