summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-compat-short-stream-client-server.js
blob: f7ef9412106f59ee66a267bf229dc6362860d113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';

const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const { Readable } = require('stream');

const server = http2.createServer(common.mustCall((req, res) => {
  res.setHeader('content-type', 'text/html');
  const input = new Readable({
    read() {
      this.push('test');
      this.push(null);
    }
  });
  input.pipe(res);
}));

server.listen(0, common.mustCall(() => {
  const port = server.address().port;
  const client = http2.connect(`http://localhost:${port}`);

  const req = client.request();

  req.on('response', common.mustCall((headers) => {
    assert.strictEqual(headers[':status'], 200);
    assert.strictEqual(headers['content-type'], 'text/html');
  }));

  let data = '';

  const notCallClose = common.mustNotCall();

  setTimeout(() => {
    req.setEncoding('utf8');
    req.removeListener('close', notCallClose);
    req.on('close', common.mustCall(() => {
      server.close();
      client.close();
    }));
    req.on('data', common.mustCallAtLeast((d) => data += d));
    req.on('end', common.mustCall(() => {
      assert.strictEqual(data, 'test');
    }));
  }, common.platformTimeout(100));

  req.on('close', notCallClose);
}));