summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-client-close-event.js
blob: 7573931ac48ef69758705d578cc7a9b80af899f1 (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
'use strict';
const common = require('../common');

// This test ensures that the `'close'` event is emitted after the `'error'`
// event when a request is made and the socket is closed before we started to
// receive a response.

const assert = require('assert');
const http = require('http');

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

server.listen(0, common.mustCall(() => {
  const req = http.get({ port: server.address().port }, common.mustNotCall());
  let errorEmitted = false;

  req.on('error', (err) => {
    errorEmitted = true;
    assert.strictEqual(err.constructor, Error);
    assert.strictEqual(err.message, 'socket hang up');
    assert.strictEqual(err.code, 'ECONNRESET');
  });

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

  req.destroy();
}));