11cb0ef41Sopenharmony_ci// Flags: --allow_natives_syntax
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst http = require('http');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst server =
91cb0ef41Sopenharmony_ci    http.createServer(onrequest).listen(0, common.localhostIPv4, () => next(0));
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction onrequest(req, res) {
121cb0ef41Sopenharmony_ci  res.end('ok');
131cb0ef41Sopenharmony_ci  onrequest.requests.push(req);
141cb0ef41Sopenharmony_ci  onrequest.responses.push(res);
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_cionrequest.requests = [];
171cb0ef41Sopenharmony_cionrequest.responses = [];
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cifunction next(n) {
201cb0ef41Sopenharmony_ci  const { address: host, port } = server.address();
211cb0ef41Sopenharmony_ci  const req = http.get({ host, port });
221cb0ef41Sopenharmony_ci  req.once('response', (res) => onresponse(n, req, res));
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cifunction onresponse(n, req, res) {
261cb0ef41Sopenharmony_ci  res.resume();
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  if (n < 3) {
291cb0ef41Sopenharmony_ci    res.once('end', () => next(n + 1));
301cb0ef41Sopenharmony_ci  } else {
311cb0ef41Sopenharmony_ci    server.close();
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  onresponse.requests.push(req);
351cb0ef41Sopenharmony_ci  onresponse.responses.push(res);
361cb0ef41Sopenharmony_ci}
371cb0ef41Sopenharmony_cionresponse.requests = [];
381cb0ef41Sopenharmony_cionresponse.responses = [];
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cifunction allSame(list) {
411cb0ef41Sopenharmony_ci  assert(list.length >= 2);
421cb0ef41Sopenharmony_ci  // eslint-disable-next-line no-unused-vars
431cb0ef41Sopenharmony_ci  for (const elt of list) eval('%DebugPrint(elt)');
441cb0ef41Sopenharmony_ci  // eslint-disable-next-line no-unused-vars
451cb0ef41Sopenharmony_ci  for (const elt of list) assert(eval('%HaveSameMap(list[0], elt)'));
461cb0ef41Sopenharmony_ci}
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciprocess.on('exit', () => {
491cb0ef41Sopenharmony_ci  eval('%CollectGarbage(0)');
501cb0ef41Sopenharmony_ci  // TODO(bnoordhuis) Investigate why the first IncomingMessage ends up
511cb0ef41Sopenharmony_ci  // with a deprecated map.  The map is stable after the first request.
521cb0ef41Sopenharmony_ci  allSame(onrequest.requests.slice(1));
531cb0ef41Sopenharmony_ci  allSame(onrequest.responses);
541cb0ef41Sopenharmony_ci  allSame(onresponse.requests);
551cb0ef41Sopenharmony_ci  allSame(onresponse.responses);
561cb0ef41Sopenharmony_ci});
57