• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6const assert = require('assert');
7const h2 = require('http2');
8const Countdown = require('../common/countdown');
9
10const server = h2.createServer();
11let client;
12
13const countdown = new Countdown(3, () => {
14  server.close();
15  client.close();
16});
17
18// We use the lower-level API here
19server.on('stream', common.mustCall((stream) => {
20  // The first pushStream will complete as normal
21  stream.pushStream({
22    ':path': '/foobar',
23  }, common.mustSucceed((pushedStream) => {
24    pushedStream.respond();
25    pushedStream.end();
26    pushedStream.on('aborted', common.mustNotCall());
27  }));
28
29  // The second pushStream will be aborted because the client
30  // will reject it due to the maxReservedRemoteStreams option
31  // being set to only 1
32  stream.pushStream({
33    ':path': '/foobar',
34  }, common.mustSucceed((pushedStream) => {
35    pushedStream.respond();
36    pushedStream.on('aborted', common.mustCall());
37    pushedStream.on('error', common.mustNotCall());
38    pushedStream.on('close', common.mustCall(() => {
39      assert.strictEqual(pushedStream.rstCode, 8);
40      countdown.dec();
41    }));
42  }));
43
44  stream.respond();
45  stream.end('hello world');
46}));
47server.listen(0);
48
49server.on('listening', common.mustCall(() => {
50  client = h2.connect(`http://localhost:${server.address().port}`,
51                      { maxReservedRemoteStreams: 1 });
52
53  const req = client.request();
54
55  // Because maxReservedRemoteStream is 1, the stream event
56  // must only be emitted once, even tho the server sends
57  // two push streams.
58  client.on('stream', common.mustCall((stream) => {
59    stream.resume();
60    stream.on('push', common.mustCall());
61    stream.on('end', common.mustCall());
62    stream.on('close', common.mustCall(() => countdown.dec()));
63  }));
64
65  req.on('response', common.mustCall());
66  req.resume();
67  req.on('end', common.mustCall());
68  req.on('close', common.mustCall(() => countdown.dec()));
69}));
70