11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst events = require('events');
61cb0ef41Sopenharmony_ciconst { createServer, request } = require('http');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cievents.captureRejections = true;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci{
111cb0ef41Sopenharmony_ci  const server = createServer(common.mustCall((req, res) => {
121cb0ef41Sopenharmony_ci    const _err = new Error('kaboom');
131cb0ef41Sopenharmony_ci    res.on('drain', common.mustCall(async () => {
141cb0ef41Sopenharmony_ci      throw _err;
151cb0ef41Sopenharmony_ci    }));
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci    res.socket.on('error', common.mustCall((err) => {
181cb0ef41Sopenharmony_ci      assert.strictEqual(err, _err);
191cb0ef41Sopenharmony_ci    }));
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    // Write until there is space in the buffer
221cb0ef41Sopenharmony_ci    while (res.write('hello'));
231cb0ef41Sopenharmony_ci  }));
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
261cb0ef41Sopenharmony_ci    const req = request({
271cb0ef41Sopenharmony_ci      method: 'GET',
281cb0ef41Sopenharmony_ci      host: server.address().host,
291cb0ef41Sopenharmony_ci      port: server.address().port
301cb0ef41Sopenharmony_ci    });
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    req.end();
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    req.on('response', common.mustCall((res) => {
351cb0ef41Sopenharmony_ci      res.on('aborted', common.mustCall());
361cb0ef41Sopenharmony_ci      res.on('error', common.expectsError({
371cb0ef41Sopenharmony_ci        code: 'ECONNRESET'
381cb0ef41Sopenharmony_ci      }));
391cb0ef41Sopenharmony_ci      res.resume();
401cb0ef41Sopenharmony_ci      server.close();
411cb0ef41Sopenharmony_ci    }));
421cb0ef41Sopenharmony_ci  }));
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci{
461cb0ef41Sopenharmony_ci  let _res;
471cb0ef41Sopenharmony_ci  let shouldEnd = false;
481cb0ef41Sopenharmony_ci  // Not using mustCall here, because it is OS-dependant.
491cb0ef41Sopenharmony_ci  const server = createServer((req, res) => {
501cb0ef41Sopenharmony_ci    // So that we cleanly stop
511cb0ef41Sopenharmony_ci    _res = res;
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    if (shouldEnd) {
541cb0ef41Sopenharmony_ci      res.end();
551cb0ef41Sopenharmony_ci    }
561cb0ef41Sopenharmony_ci  });
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(() => {
591cb0ef41Sopenharmony_ci    const _err = new Error('kaboom');
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    const req = request({
621cb0ef41Sopenharmony_ci      method: 'POST',
631cb0ef41Sopenharmony_ci      host: server.address().host,
641cb0ef41Sopenharmony_ci      port: server.address().port
651cb0ef41Sopenharmony_ci    });
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci    req.on('response', common.mustNotCall((res) => {
681cb0ef41Sopenharmony_ci      // So that we cleanly stop
691cb0ef41Sopenharmony_ci      res.resume();
701cb0ef41Sopenharmony_ci      server.close();
711cb0ef41Sopenharmony_ci    }));
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci    req.on('error', common.mustCall((err) => {
741cb0ef41Sopenharmony_ci      server.close();
751cb0ef41Sopenharmony_ci      // On some variants of Windows, this can happen before
761cb0ef41Sopenharmony_ci      // the server has received the request.
771cb0ef41Sopenharmony_ci      if (_res) {
781cb0ef41Sopenharmony_ci        _res.end();
791cb0ef41Sopenharmony_ci      } else {
801cb0ef41Sopenharmony_ci        shouldEnd = true;
811cb0ef41Sopenharmony_ci      }
821cb0ef41Sopenharmony_ci      assert.strictEqual(err, _err);
831cb0ef41Sopenharmony_ci    }));
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    req.on('drain', common.mustCall(async () => {
861cb0ef41Sopenharmony_ci      throw _err;
871cb0ef41Sopenharmony_ci    }));
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci    // Write until there is space in the buffer
901cb0ef41Sopenharmony_ci    while (req.write('hello'));
911cb0ef41Sopenharmony_ci  }));
921cb0ef41Sopenharmony_ci}
93