summaryrefslogtreecommitdiff
path: root/test/parallel/test-http-socket-error-listeners.js
blob: f0d220a5d9337aec33803c2293e97defc2b39787 (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
// Flags: --no-warnings

'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');

// This test sends an invalid character to a HTTP server and purposely
// does not handle clientError (even if it sets an event handler).
//
// The idea is to let the server emit multiple errors on the socket,
// mostly due to parsing error, and make sure they don't result
// in leaking event listeners.

{
  let i = 0;
  let socket;
  process.on('warning', common.mustCall());

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

  server.on('clientError', common.mustCallAtLeast((err) => {
    assert.strictEqual(err.code, 'HPE_INVALID_METHOD');
    assert.strictEqual(err.rawPacket.toString(), '*');

    if (i === 20) {
      socket.end();
    } else {
      socket.write('*');
      i++;
    }
  }, 1));

  server.listen(0, () => {
    socket = net.createConnection({ port: server.address().port });

    socket.on('connect', () => {
      socket.write('*');
    });

    socket.on('close', () => {
      server.close();
    });
  });
}