• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const { expectsError, mustCall } = require('../common');
4
5// Test that the request socket is destroyed if the `'clientError'` event is
6// emitted and there is no listener for it.
7
8const assert = require('assert');
9const { createServer } = require('http');
10const { createConnection } = require('net');
11
12const server = createServer();
13
14server.on('connection', mustCall((socket) => {
15  socket.on('error', expectsError({
16    name: 'Error',
17    message: 'Parse Error: Invalid method encountered',
18    code: 'HPE_INVALID_METHOD',
19    bytesParsed: 0,
20    rawPacket: Buffer.from('FOO /\r\n')
21  }));
22}));
23
24server.listen(0, () => {
25  const chunks = [];
26  const socket = createConnection({
27    allowHalfOpen: true,
28    port: server.address().port
29  });
30
31  socket.on('connect', mustCall(() => {
32    socket.write('FOO /\r\n');
33  }));
34
35  socket.on('data', (chunk) => {
36    chunks.push(chunk);
37  });
38
39  socket.on('end', mustCall(() => {
40    const expected = Buffer.from(
41      'HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'
42    );
43    assert(Buffer.concat(chunks).equals(expected));
44
45    server.close();
46  }));
47});
48