• 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.mustCall((err, pushedStream) => {
24    assert.ifError(err);
25    pushedStream.respond();
26    pushedStream.end();
27    pushedStream.on('aborted', common.mustNotCall());
28  }));
29
30  // The second pushStream will be aborted because the client
31  // will reject it due to the maxReservedRemoteStreams option
32  // being set to only 1
33  stream.pushStream({
34    ':path': '/foobar',
35  }, common.mustCall((err, pushedStream) => {
36    assert.ifError(err);
37    pushedStream.respond();
38    pushedStream.on('aborted', common.mustCall());
39    pushedStream.on('error', common.mustNotCall());
40    pushedStream.on('close', common.mustCall(() => {
41      assert.strictEqual(pushedStream.rstCode, 8);
42      countdown.dec();
43    }));
44  }));
45
46  stream.respond();
47  stream.end('hello world');
48}));
49server.listen(0);
50
51server.on('listening', common.mustCall(() => {
52  client = h2.connect(`http://localhost:${server.address().port}`,
53                      { maxReservedRemoteStreams: 1 });
54
55  const req = client.request();
56
57  // Because maxReservedRemoteStream is 1, the stream event
58  // must only be emitted once, even tho the server sends
59  // two push streams.
60  client.on('stream', common.mustCall((stream) => {
61    stream.resume();
62    stream.on('push', common.mustCall());
63    stream.on('end', common.mustCall());
64    stream.on('close', common.mustCall(() => countdown.dec()));
65  }));
66
67  req.on('response', common.mustCall());
68  req.resume();
69  req.on('end', common.mustCall());
70  req.on('close', common.mustCall(() => countdown.dec()));
71}));
72