11cb0ef41Sopenharmony_ci// Flags: --expose-gc
21cb0ef41Sopenharmony_ci'use strict';
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_ciconst makeDuplexPair = require('../common/duplexpair');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Make sure the Http2Stream destructor works, since we don't clean the
111cb0ef41Sopenharmony_ci// stream up like we would otherwise do.
121cb0ef41Sopenharmony_ciprocess.on('exit', global.gc);
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci{
151cb0ef41Sopenharmony_ci  const { clientSide, serverSide } = makeDuplexPair();
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  let serverSideHttp2Stream;
181cb0ef41Sopenharmony_ci  let serverSideHttp2StreamDestroyed = false;
191cb0ef41Sopenharmony_ci  const server = http2.createServer();
201cb0ef41Sopenharmony_ci  server.on('stream', common.mustCall((stream, headers) => {
211cb0ef41Sopenharmony_ci    serverSideHttp2Stream = stream;
221cb0ef41Sopenharmony_ci    stream.respond({
231cb0ef41Sopenharmony_ci      'content-type': 'text/html',
241cb0ef41Sopenharmony_ci      ':status': 200
251cb0ef41Sopenharmony_ci    });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    const originalWrite = serverSide._write;
281cb0ef41Sopenharmony_ci    serverSide._write = (buf, enc, cb) => {
291cb0ef41Sopenharmony_ci      if (serverSideHttp2StreamDestroyed) {
301cb0ef41Sopenharmony_ci        serverSide.destroy();
311cb0ef41Sopenharmony_ci        serverSide.write = () => {};
321cb0ef41Sopenharmony_ci      } else {
331cb0ef41Sopenharmony_ci        setImmediate(() => {
341cb0ef41Sopenharmony_ci          originalWrite.call(serverSide, buf, enc, () => setImmediate(cb));
351cb0ef41Sopenharmony_ci        });
361cb0ef41Sopenharmony_ci      }
371cb0ef41Sopenharmony_ci    };
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    // Enough data to fit into a single *session* window,
401cb0ef41Sopenharmony_ci    // not enough data to fit into a single *stream* window.
411cb0ef41Sopenharmony_ci    stream.write(Buffer.alloc(40000));
421cb0ef41Sopenharmony_ci  }));
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  server.emit('connection', serverSide);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  const client = http2.connect('http://localhost:80', {
471cb0ef41Sopenharmony_ci    createConnection: common.mustCall(() => clientSide)
481cb0ef41Sopenharmony_ci  });
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  const req = client.request({ ':path': '/' });
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  req.on('response', common.mustCall((headers) => {
531cb0ef41Sopenharmony_ci    assert.strictEqual(headers[':status'], 200);
541cb0ef41Sopenharmony_ci  }));
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  req.on('data', common.mustCallAtLeast(() => {
571cb0ef41Sopenharmony_ci    if (!serverSideHttp2StreamDestroyed) {
581cb0ef41Sopenharmony_ci      serverSideHttp2Stream.destroy();
591cb0ef41Sopenharmony_ci      serverSideHttp2StreamDestroyed = true;
601cb0ef41Sopenharmony_ci    }
611cb0ef41Sopenharmony_ci  }));
621cb0ef41Sopenharmony_ci}
63