11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci'use strict';
41cb0ef41Sopenharmony_ciconst { expectsError, mustCall } = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst { createServer, maxHeaderSize } = require('http');
71cb0ef41Sopenharmony_ciconst { createConnection } = require('net');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst CRLF = '\r\n';
101cb0ef41Sopenharmony_ciconst DUMMY_HEADER_NAME = 'Cookie: ';
111cb0ef41Sopenharmony_ciconst DUMMY_HEADER_VALUE = 'a'.repeat(
121cb0ef41Sopenharmony_ci  // Plus one is to make it 1 byte too big
131cb0ef41Sopenharmony_ci  maxHeaderSize - DUMMY_HEADER_NAME.length + 2
141cb0ef41Sopenharmony_ci);
151cb0ef41Sopenharmony_ciconst PAYLOAD_GET = 'GET /blah HTTP/1.1';
161cb0ef41Sopenharmony_ciconst PAYLOAD = PAYLOAD_GET + CRLF +
171cb0ef41Sopenharmony_ci  DUMMY_HEADER_NAME + DUMMY_HEADER_VALUE + CRLF.repeat(2);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst server = createServer();
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciserver.on('connection', mustCall((socket) => {
221cb0ef41Sopenharmony_ci  socket.on('error', expectsError({
231cb0ef41Sopenharmony_ci    name: 'Error',
241cb0ef41Sopenharmony_ci    message: 'Parse Error: Header overflow',
251cb0ef41Sopenharmony_ci    code: 'HPE_HEADER_OVERFLOW',
261cb0ef41Sopenharmony_ci    bytesParsed: maxHeaderSize + PAYLOAD_GET.length + (CRLF.length * 2) + 1,
271cb0ef41Sopenharmony_ci    rawPacket: Buffer.from(PAYLOAD)
281cb0ef41Sopenharmony_ci  }));
291cb0ef41Sopenharmony_ci}));
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciserver.listen(0, mustCall(() => {
321cb0ef41Sopenharmony_ci  const c = createConnection(server.address().port);
331cb0ef41Sopenharmony_ci  let received = '';
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  c.on('connect', mustCall(() => {
361cb0ef41Sopenharmony_ci    c.write(PAYLOAD);
371cb0ef41Sopenharmony_ci  }));
381cb0ef41Sopenharmony_ci  c.on('data', mustCall((data) => {
391cb0ef41Sopenharmony_ci    received += data.toString();
401cb0ef41Sopenharmony_ci  }));
411cb0ef41Sopenharmony_ci  c.on('end', mustCall(() => {
421cb0ef41Sopenharmony_ci    assert.strictEqual(
431cb0ef41Sopenharmony_ci      received,
441cb0ef41Sopenharmony_ci      'HTTP/1.1 431 Request Header Fields Too Large\r\n' +
451cb0ef41Sopenharmony_ci      'Connection: close\r\n\r\n'
461cb0ef41Sopenharmony_ci    );
471cb0ef41Sopenharmony_ci    c.end();
481cb0ef41Sopenharmony_ci  }));
491cb0ef41Sopenharmony_ci  c.on('close', mustCall(() => server.close()));
501cb0ef41Sopenharmony_ci}));
51