11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst http = require('http');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst server = http.createServer(common.mustCall((req, res) => {
81cb0ef41Sopenharmony_ci  if (req.url === '/first') {
91cb0ef41Sopenharmony_ci    res.end('ok');
101cb0ef41Sopenharmony_ci    return;
111cb0ef41Sopenharmony_ci  }
121cb0ef41Sopenharmony_ci  setTimeout(() => {
131cb0ef41Sopenharmony_ci    res.end('ok');
141cb0ef41Sopenharmony_ci  }, common.platformTimeout(500));
151cb0ef41Sopenharmony_ci}, 2));
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciserver.keepAliveTimeout = common.platformTimeout(200);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst agent = new http.Agent({
201cb0ef41Sopenharmony_ci  keepAlive: true,
211cb0ef41Sopenharmony_ci  maxSockets: 1
221cb0ef41Sopenharmony_ci});
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cifunction request(path, callback) {
251cb0ef41Sopenharmony_ci  const port = server.address().port;
261cb0ef41Sopenharmony_ci  const req = http.request({ agent, path, port }, common.mustCall((res) => {
271cb0ef41Sopenharmony_ci    assert.strictEqual(res.statusCode, 200);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    res.setEncoding('utf8');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    let result = '';
321cb0ef41Sopenharmony_ci    res.on('data', (chunk) => {
331cb0ef41Sopenharmony_ci      result += chunk;
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci    res.on('end', common.mustCall(() => {
371cb0ef41Sopenharmony_ci      assert.strictEqual(result, 'ok');
381cb0ef41Sopenharmony_ci      callback();
391cb0ef41Sopenharmony_ci    }));
401cb0ef41Sopenharmony_ci  }));
411cb0ef41Sopenharmony_ci  req.end();
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
451cb0ef41Sopenharmony_ci  request('/first', () => {
461cb0ef41Sopenharmony_ci    request('/second', () => {
471cb0ef41Sopenharmony_ci      server.close();
481cb0ef41Sopenharmony_ci    });
491cb0ef41Sopenharmony_ci  });
501cb0ef41Sopenharmony_ci}));
51