11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (!common.hasCrypto)
51cb0ef41Sopenharmony_ci  common.skip('missing crypto');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst http2 = require('http2');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst { PerformanceObserver } = require('perf_hooks');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst server = http2.createServer();
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciserver.on('stream', (stream, headers) => {
141cb0ef41Sopenharmony_ci  stream.respond({
151cb0ef41Sopenharmony_ci    'content-type': 'text/html',
161cb0ef41Sopenharmony_ci    ':status': 200
171cb0ef41Sopenharmony_ci  });
181cb0ef41Sopenharmony_ci  switch (headers[':path']) {
191cb0ef41Sopenharmony_ci    case '/singleEnd':
201cb0ef41Sopenharmony_ci      stream.end('OK');
211cb0ef41Sopenharmony_ci      break;
221cb0ef41Sopenharmony_ci    case '/sequentialEnd':
231cb0ef41Sopenharmony_ci      stream.write('OK');
241cb0ef41Sopenharmony_ci      stream.end();
251cb0ef41Sopenharmony_ci      break;
261cb0ef41Sopenharmony_ci    case '/delayedEnd':
271cb0ef41Sopenharmony_ci      stream.write('OK', () => stream.end());
281cb0ef41Sopenharmony_ci      break;
291cb0ef41Sopenharmony_ci  }
301cb0ef41Sopenharmony_ci});
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cifunction testRequest(path, targetFrameCount, callback) {
331cb0ef41Sopenharmony_ci  const obs = new PerformanceObserver(
341cb0ef41Sopenharmony_ci    common.mustCallAtLeast((list, observer) => {
351cb0ef41Sopenharmony_ci      const entries = list.getEntries();
361cb0ef41Sopenharmony_ci      for (let n = 0; n < entries.length; n++) {
371cb0ef41Sopenharmony_ci        const entry = entries[n];
381cb0ef41Sopenharmony_ci        if (entry.name !== 'Http2Session') continue;
391cb0ef41Sopenharmony_ci        if (entry.detail.type !== 'client') continue;
401cb0ef41Sopenharmony_ci        assert.strictEqual(entry.detail.framesReceived, targetFrameCount);
411cb0ef41Sopenharmony_ci        observer.disconnect();
421cb0ef41Sopenharmony_ci        callback();
431cb0ef41Sopenharmony_ci      }
441cb0ef41Sopenharmony_ci    }));
451cb0ef41Sopenharmony_ci  obs.observe({ type: 'http2' });
461cb0ef41Sopenharmony_ci  const client =
471cb0ef41Sopenharmony_ci    http2.connect(`http://localhost:${server.address().port}`, () => {
481cb0ef41Sopenharmony_ci      const req = client.request({ ':path': path });
491cb0ef41Sopenharmony_ci      req.resume();
501cb0ef41Sopenharmony_ci      req.end();
511cb0ef41Sopenharmony_ci      req.on('end', () => client.close());
521cb0ef41Sopenharmony_ci    });
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci// SETTINGS => SETTINGS => HEADERS => DATA
561cb0ef41Sopenharmony_ciconst MIN_FRAME_COUNT = 4;
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciserver.listen(0, () => {
591cb0ef41Sopenharmony_ci  testRequest('/singleEnd', MIN_FRAME_COUNT, () => {
601cb0ef41Sopenharmony_ci    testRequest('/sequentialEnd', MIN_FRAME_COUNT, () => {
611cb0ef41Sopenharmony_ci      testRequest('/delayedEnd', MIN_FRAME_COUNT + 1, () => {
621cb0ef41Sopenharmony_ci        server.close();
631cb0ef41Sopenharmony_ci      });
641cb0ef41Sopenharmony_ci    });
651cb0ef41Sopenharmony_ci  });
661cb0ef41Sopenharmony_ci});
67