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 h2 = require('http2');
81cb0ef41Sopenharmony_ciconst Countdown = require('../common/countdown');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst server = h2.createServer();
111cb0ef41Sopenharmony_cilet client;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst countdown = new Countdown(3, () => {
141cb0ef41Sopenharmony_ci  server.close();
151cb0ef41Sopenharmony_ci  client.close();
161cb0ef41Sopenharmony_ci});
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// We use the lower-level API here
191cb0ef41Sopenharmony_ciserver.on('stream', common.mustCall((stream) => {
201cb0ef41Sopenharmony_ci  // The first pushStream will complete as normal
211cb0ef41Sopenharmony_ci  stream.pushStream({
221cb0ef41Sopenharmony_ci    ':path': '/foobar',
231cb0ef41Sopenharmony_ci  }, common.mustSucceed((pushedStream) => {
241cb0ef41Sopenharmony_ci    pushedStream.respond();
251cb0ef41Sopenharmony_ci    pushedStream.end();
261cb0ef41Sopenharmony_ci    pushedStream.on('aborted', common.mustNotCall());
271cb0ef41Sopenharmony_ci  }));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  // The second pushStream will be aborted because the client
301cb0ef41Sopenharmony_ci  // will reject it due to the maxReservedRemoteStreams option
311cb0ef41Sopenharmony_ci  // being set to only 1
321cb0ef41Sopenharmony_ci  stream.pushStream({
331cb0ef41Sopenharmony_ci    ':path': '/foobar',
341cb0ef41Sopenharmony_ci  }, common.mustSucceed((pushedStream) => {
351cb0ef41Sopenharmony_ci    pushedStream.respond();
361cb0ef41Sopenharmony_ci    pushedStream.on('aborted', common.mustCall());
371cb0ef41Sopenharmony_ci    pushedStream.on('error', common.mustNotCall());
381cb0ef41Sopenharmony_ci    pushedStream.on('close', common.mustCall(() => {
391cb0ef41Sopenharmony_ci      assert.strictEqual(pushedStream.rstCode, 8);
401cb0ef41Sopenharmony_ci      countdown.dec();
411cb0ef41Sopenharmony_ci    }));
421cb0ef41Sopenharmony_ci  }));
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  stream.respond();
451cb0ef41Sopenharmony_ci  stream.end('hello world');
461cb0ef41Sopenharmony_ci}));
471cb0ef41Sopenharmony_ciserver.listen(0);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciserver.on('listening', common.mustCall(() => {
501cb0ef41Sopenharmony_ci  client = h2.connect(`http://localhost:${server.address().port}`,
511cb0ef41Sopenharmony_ci                      { maxReservedRemoteStreams: 1 });
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  const req = client.request();
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  // Because maxReservedRemoteStream is 1, the stream event
561cb0ef41Sopenharmony_ci  // must only be emitted once, even tho the server sends
571cb0ef41Sopenharmony_ci  // two push streams.
581cb0ef41Sopenharmony_ci  client.on('stream', common.mustCall((stream) => {
591cb0ef41Sopenharmony_ci    stream.resume();
601cb0ef41Sopenharmony_ci    stream.on('push', common.mustCall());
611cb0ef41Sopenharmony_ci    stream.on('end', common.mustCall());
621cb0ef41Sopenharmony_ci    stream.on('close', common.mustCall(() => countdown.dec()));
631cb0ef41Sopenharmony_ci  }));
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  req.on('response', common.mustCall());
661cb0ef41Sopenharmony_ci  req.resume();
671cb0ef41Sopenharmony_ci  req.on('end', common.mustCall());
681cb0ef41Sopenharmony_ci  req.on('close', common.mustCall(() => countdown.dec()));
691cb0ef41Sopenharmony_ci}));
70