11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst { expectsError, mustCall } = require('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Test that the request socket is destroyed if the `'clientError'` event is
61cb0ef41Sopenharmony_ci// emitted and there is no listener for it.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst { createServer } = require('http');
101cb0ef41Sopenharmony_ciconst { createConnection } = require('net');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst server = createServer();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciserver.on('connection', mustCall((socket) => {
151cb0ef41Sopenharmony_ci  socket.on('error', expectsError({
161cb0ef41Sopenharmony_ci    name: 'Error',
171cb0ef41Sopenharmony_ci    message: 'Parse Error: Invalid method encountered',
181cb0ef41Sopenharmony_ci    code: 'HPE_INVALID_METHOD',
191cb0ef41Sopenharmony_ci    bytesParsed: 1,
201cb0ef41Sopenharmony_ci    rawPacket: Buffer.from('FOO /\r\n')
211cb0ef41Sopenharmony_ci  }));
221cb0ef41Sopenharmony_ci}));
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciserver.listen(0, () => {
251cb0ef41Sopenharmony_ci  const chunks = [];
261cb0ef41Sopenharmony_ci  const socket = createConnection({
271cb0ef41Sopenharmony_ci    allowHalfOpen: true,
281cb0ef41Sopenharmony_ci    port: server.address().port
291cb0ef41Sopenharmony_ci  });
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  socket.on('connect', mustCall(() => {
321cb0ef41Sopenharmony_ci    socket.write('FOO /\r\n');
331cb0ef41Sopenharmony_ci  }));
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  socket.on('data', (chunk) => {
361cb0ef41Sopenharmony_ci    chunks.push(chunk);
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  socket.on('end', mustCall(() => {
401cb0ef41Sopenharmony_ci    const expected = Buffer.from(
411cb0ef41Sopenharmony_ci      'HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n'
421cb0ef41Sopenharmony_ci    );
431cb0ef41Sopenharmony_ci    assert(Buffer.concat(chunks).equals(expected));
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci    server.close();
461cb0ef41Sopenharmony_ci  }));
471cb0ef41Sopenharmony_ci});
48