• 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 http2 = require('http2');
8
9const server = http2.createServer();
10
11server.on('stream', (s) => {
12  assert(s.pushAllowed);
13
14  s.pushStream({ ':path': '/file' }, common.mustSucceed((pushStream) => {
15    pushStream.respond();
16    pushStream.end('a push stream');
17  }));
18
19  s.respond();
20  s.end('hello world');
21});
22
23server.listen(0, () => {
24  server.unref();
25
26  const url = `http://localhost:${server.address().port}`;
27
28  const client = http2.connect(url);
29  const req = client.request();
30
31  let pushStream;
32
33  client.on('stream', common.mustCall((s, headers) => {
34    assert.strictEqual(headers[':path'], '/file');
35    pushStream = s;
36  }));
37
38  req.on('response', common.mustCall((headers) => {
39    let pushData = '';
40    pushStream.setEncoding('utf8');
41    pushStream.on('data', (d) => pushData += d);
42    pushStream.on('end', common.mustCall(() => {
43      assert.strictEqual(pushData, 'a push stream');
44
45      // Removing the setImmediate causes the test to pass
46      setImmediate(function() {
47        let data = '';
48        req.setEncoding('utf8');
49        req.on('data', (d) => data += d);
50        req.on('end', common.mustCall(() => {
51          assert.strictEqual(data, 'hello world');
52          client.close();
53        }));
54      });
55    }));
56  }));
57});
58