11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst events = require('events');
51cb0ef41Sopenharmony_ciconst { createServer, request } = require('http');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cievents.captureRejections = true;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci{
111cb0ef41Sopenharmony_ci  const server = createServer(common.mustCall(async (req, res) => {
121cb0ef41Sopenharmony_ci    // We will test that this header is cleaned up before forwarding.
131cb0ef41Sopenharmony_ci    res.setHeader('content-type', 'application/json');
141cb0ef41Sopenharmony_ci    throw new Error('kaboom');
151cb0ef41Sopenharmony_ci  }));
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
181cb0ef41Sopenharmony_ci    const req = request({
191cb0ef41Sopenharmony_ci      method: 'GET',
201cb0ef41Sopenharmony_ci      host: server.address().host,
211cb0ef41Sopenharmony_ci      port: server.address().port
221cb0ef41Sopenharmony_ci    });
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    req.end();
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((res) => {
271cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 500);
281cb0ef41Sopenharmony_ci      assert.strictEqual(Object.hasOwn(res.headers, 'content-type'), false);
291cb0ef41Sopenharmony_ci      let data = '';
301cb0ef41Sopenharmony_ci      res.setEncoding('utf8');
311cb0ef41Sopenharmony_ci      res.on('data', common.mustCall((chunk) => {
321cb0ef41Sopenharmony_ci        data += chunk;
331cb0ef41Sopenharmony_ci      }));
341cb0ef41Sopenharmony_ci      res.on('end', common.mustCall(() => {
351cb0ef41Sopenharmony_ci        assert.strictEqual(data, 'Internal Server Error');
361cb0ef41Sopenharmony_ci        server.close();
371cb0ef41Sopenharmony_ci      }));
381cb0ef41Sopenharmony_ci    }));
391cb0ef41Sopenharmony_ci  }));
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci{
431cb0ef41Sopenharmony_ci  let resolve;
441cb0ef41Sopenharmony_ci  const latch = new Promise((_resolve) => {
451cb0ef41Sopenharmony_ci    resolve = _resolve;
461cb0ef41Sopenharmony_ci  });
471cb0ef41Sopenharmony_ci  const server = createServer(common.mustCall(async (req, res) => {
481cb0ef41Sopenharmony_ci    server.close();
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    // We will test that this header is cleaned up before forwarding.
511cb0ef41Sopenharmony_ci    res.setHeader('content-type', 'application/json');
521cb0ef41Sopenharmony_ci    res.write('{');
531cb0ef41Sopenharmony_ci    req.resume();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    // Wait so the data is on the wire
561cb0ef41Sopenharmony_ci    await latch;
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    throw new Error('kaboom');
591cb0ef41Sopenharmony_ci  }));
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
621cb0ef41Sopenharmony_ci    const req = request({
631cb0ef41Sopenharmony_ci      method: 'GET',
641cb0ef41Sopenharmony_ci      host: server.address().host,
651cb0ef41Sopenharmony_ci      port: server.address().port
661cb0ef41Sopenharmony_ci    });
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    req.end();
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((res) => {
711cb0ef41Sopenharmony_ci      assert.strictEqual(res.statusCode, 200);
721cb0ef41Sopenharmony_ci      assert.strictEqual(res.headers['content-type'], 'application/json');
731cb0ef41Sopenharmony_ci      resolve();
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci      let data = '';
761cb0ef41Sopenharmony_ci      res.setEncoding('utf8');
771cb0ef41Sopenharmony_ci      res.on('data', common.mustCall((chunk) => {
781cb0ef41Sopenharmony_ci        data += chunk;
791cb0ef41Sopenharmony_ci      }));
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci      req.on('close', common.mustCall(() => {
821cb0ef41Sopenharmony_ci        assert.strictEqual(data, '{');
831cb0ef41Sopenharmony_ci      }));
841cb0ef41Sopenharmony_ci    }));
851cb0ef41Sopenharmony_ci  }));
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci{
891cb0ef41Sopenharmony_ci  const server = createServer(common.mustCall(async (req, res) => {
901cb0ef41Sopenharmony_ci    // We will test that this header is cleaned up before forwarding.
911cb0ef41Sopenharmony_ci    res.writeHead(200);
921cb0ef41Sopenharmony_ci    throw new Error('kaboom');
931cb0ef41Sopenharmony_ci  }));
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
961cb0ef41Sopenharmony_ci    const req = request({
971cb0ef41Sopenharmony_ci      method: 'GET',
981cb0ef41Sopenharmony_ci      host: server.address().host,
991cb0ef41Sopenharmony_ci      port: server.address().port
1001cb0ef41Sopenharmony_ci    });
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci    req.end();
1031cb0ef41Sopenharmony_ci    req.on('error', common.mustCall((err) => {
1041cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 'ECONNRESET');
1051cb0ef41Sopenharmony_ci      server.close();
1061cb0ef41Sopenharmony_ci    }));
1071cb0ef41Sopenharmony_ci  }));
1081cb0ef41Sopenharmony_ci}
109