11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst http = require('http');
51cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst MAX_REQUESTS = 13;
81cb0ef41Sopenharmony_cilet reqNum = 0;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cifunction test(res, header, code) {
111cb0ef41Sopenharmony_ci  assert.throws(() => {
121cb0ef41Sopenharmony_ci    res.writeHead(header);
131cb0ef41Sopenharmony_ci  }, {
141cb0ef41Sopenharmony_ci    code: 'ERR_HTTP_INVALID_STATUS_CODE',
151cb0ef41Sopenharmony_ci    name: 'RangeError',
161cb0ef41Sopenharmony_ci    message: `Invalid status code: ${code}`
171cb0ef41Sopenharmony_ci  });
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst server = http.Server(common.mustCall(function(req, res) {
211cb0ef41Sopenharmony_ci  switch (reqNum) {
221cb0ef41Sopenharmony_ci    case 0:
231cb0ef41Sopenharmony_ci      test(res, -1, '-1');
241cb0ef41Sopenharmony_ci      break;
251cb0ef41Sopenharmony_ci    case 1:
261cb0ef41Sopenharmony_ci      test(res, Infinity, 'Infinity');
271cb0ef41Sopenharmony_ci      break;
281cb0ef41Sopenharmony_ci    case 2:
291cb0ef41Sopenharmony_ci      test(res, NaN, 'NaN');
301cb0ef41Sopenharmony_ci      break;
311cb0ef41Sopenharmony_ci    case 3:
321cb0ef41Sopenharmony_ci      test(res, {}, '{}');
331cb0ef41Sopenharmony_ci      break;
341cb0ef41Sopenharmony_ci    case 4:
351cb0ef41Sopenharmony_ci      test(res, 99, '99');
361cb0ef41Sopenharmony_ci      break;
371cb0ef41Sopenharmony_ci    case 5:
381cb0ef41Sopenharmony_ci      test(res, 1000, '1000');
391cb0ef41Sopenharmony_ci      break;
401cb0ef41Sopenharmony_ci    case 6:
411cb0ef41Sopenharmony_ci      test(res, '1000', '1000');
421cb0ef41Sopenharmony_ci      break;
431cb0ef41Sopenharmony_ci    case 7:
441cb0ef41Sopenharmony_ci      test(res, null, 'null');
451cb0ef41Sopenharmony_ci      break;
461cb0ef41Sopenharmony_ci    case 8:
471cb0ef41Sopenharmony_ci      test(res, true, 'true');
481cb0ef41Sopenharmony_ci      break;
491cb0ef41Sopenharmony_ci    case 9:
501cb0ef41Sopenharmony_ci      test(res, [], '[]');
511cb0ef41Sopenharmony_ci      break;
521cb0ef41Sopenharmony_ci    case 10:
531cb0ef41Sopenharmony_ci      test(res, 'this is not valid', 'this is not valid');
541cb0ef41Sopenharmony_ci      break;
551cb0ef41Sopenharmony_ci    case 11:
561cb0ef41Sopenharmony_ci      test(res, '404 this is not valid either', '404 this is not valid either');
571cb0ef41Sopenharmony_ci      break;
581cb0ef41Sopenharmony_ci    case 12:
591cb0ef41Sopenharmony_ci      assert.throws(() => { res.writeHead(); },
601cb0ef41Sopenharmony_ci                    {
611cb0ef41Sopenharmony_ci                      code: 'ERR_HTTP_INVALID_STATUS_CODE',
621cb0ef41Sopenharmony_ci                      name: 'RangeError',
631cb0ef41Sopenharmony_ci                      message: 'Invalid status code: undefined'
641cb0ef41Sopenharmony_ci                    });
651cb0ef41Sopenharmony_ci      this.close();
661cb0ef41Sopenharmony_ci      break;
671cb0ef41Sopenharmony_ci    default:
681cb0ef41Sopenharmony_ci      throw new Error('Unexpected request');
691cb0ef41Sopenharmony_ci  }
701cb0ef41Sopenharmony_ci  res.statusCode = 200;
711cb0ef41Sopenharmony_ci  res.end();
721cb0ef41Sopenharmony_ci}, MAX_REQUESTS));
731cb0ef41Sopenharmony_ciserver.listen();
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciconst countdown = new Countdown(MAX_REQUESTS, () => server.close());
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciserver.on('listening', function makeRequest() {
781cb0ef41Sopenharmony_ci  http.get({
791cb0ef41Sopenharmony_ci    port: this.address().port
801cb0ef41Sopenharmony_ci  }, (res) => {
811cb0ef41Sopenharmony_ci    assert.strictEqual(res.statusCode, 200);
821cb0ef41Sopenharmony_ci    res.on('end', () => {
831cb0ef41Sopenharmony_ci      countdown.dec();
841cb0ef41Sopenharmony_ci      reqNum = MAX_REQUESTS - countdown.remaining;
851cb0ef41Sopenharmony_ci      if (countdown.remaining > 0)
861cb0ef41Sopenharmony_ci        makeRequest.call(this);
871cb0ef41Sopenharmony_ci    });
881cb0ef41Sopenharmony_ci    res.resume();
891cb0ef41Sopenharmony_ci  });
901cb0ef41Sopenharmony_ci});
91