11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciif (!common.hasCrypto)
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst https = require('https');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst serverOptions = {
121cb0ef41Sopenharmony_ci  key: fixtures.readKey('agent1-key.pem'),
131cb0ef41Sopenharmony_ci  cert: fixtures.readKey('agent1-cert.pem')
141cb0ef41Sopenharmony_ci};
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cilet requests = 0;
171cb0ef41Sopenharmony_cilet responses = 0;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst headers = {};
201cb0ef41Sopenharmony_ciconst N = 100;
211cb0ef41Sopenharmony_cifor (let i = 0; i < N; ++i) {
221cb0ef41Sopenharmony_ci  headers[`key${i}`] = i;
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst maxAndExpected = [ // for server
261cb0ef41Sopenharmony_ci  [50, 50],
271cb0ef41Sopenharmony_ci  [1500, 102],
281cb0ef41Sopenharmony_ci  [0, N + 2], // Host and Connection
291cb0ef41Sopenharmony_ci];
301cb0ef41Sopenharmony_cilet max = maxAndExpected[requests][0];
311cb0ef41Sopenharmony_cilet expected = maxAndExpected[requests][1];
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst server = https.createServer(serverOptions, common.mustCall((req, res) => {
341cb0ef41Sopenharmony_ci  assert.strictEqual(Object.keys(req.headers).length, expected);
351cb0ef41Sopenharmony_ci  if (++requests < maxAndExpected.length) {
361cb0ef41Sopenharmony_ci    max = maxAndExpected[requests][0];
371cb0ef41Sopenharmony_ci    expected = maxAndExpected[requests][1];
381cb0ef41Sopenharmony_ci    server.maxHeadersCount = max;
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci  res.writeHead(200, headers);
411cb0ef41Sopenharmony_ci  res.end();
421cb0ef41Sopenharmony_ci}, 3));
431cb0ef41Sopenharmony_ciserver.maxHeadersCount = max;
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(() => {
461cb0ef41Sopenharmony_ci  const maxAndExpected = [ // for client
471cb0ef41Sopenharmony_ci    [20, 20],
481cb0ef41Sopenharmony_ci    [1200, 103],
491cb0ef41Sopenharmony_ci    [0, N + 3], // Connection, Date and Transfer-Encoding
501cb0ef41Sopenharmony_ci  ];
511cb0ef41Sopenharmony_ci  const doRequest = common.mustCall(() => {
521cb0ef41Sopenharmony_ci    const max = maxAndExpected[responses][0];
531cb0ef41Sopenharmony_ci    const expected = maxAndExpected[responses][1];
541cb0ef41Sopenharmony_ci    const req = https.request({
551cb0ef41Sopenharmony_ci      port: server.address().port,
561cb0ef41Sopenharmony_ci      headers: headers,
571cb0ef41Sopenharmony_ci      rejectUnauthorized: false
581cb0ef41Sopenharmony_ci    }, (res) => {
591cb0ef41Sopenharmony_ci      assert.strictEqual(Object.keys(res.headers).length, expected);
601cb0ef41Sopenharmony_ci      res.on('end', () => {
611cb0ef41Sopenharmony_ci        if (++responses < maxAndExpected.length) {
621cb0ef41Sopenharmony_ci          doRequest();
631cb0ef41Sopenharmony_ci        } else {
641cb0ef41Sopenharmony_ci          server.close();
651cb0ef41Sopenharmony_ci        }
661cb0ef41Sopenharmony_ci      });
671cb0ef41Sopenharmony_ci      res.resume();
681cb0ef41Sopenharmony_ci    });
691cb0ef41Sopenharmony_ci    req.maxHeadersCount = max;
701cb0ef41Sopenharmony_ci    req.end();
711cb0ef41Sopenharmony_ci  }, 3);
721cb0ef41Sopenharmony_ci  doRequest();
731cb0ef41Sopenharmony_ci}));
74