summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-padding-callback.js
blob: 6d6a6b27221b07e0e3cce2d43c9ea5ede5bb0c24 (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
51
'use strict';

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

function selectPadding(frameLen, max) {
  assert.strictEqual(typeof frameLen, 'number');
  assert.strictEqual(typeof max, 'number');
  assert(max >= frameLen);
  return max;
}

// selectPadding will be called three times:
// 1. For the client request headers frame
// 2. For the server response headers frame
// 3. For the server response data frame
const options = {
  paddingStrategy: PADDING_STRATEGY_CALLBACK,
  selectPadding: common.mustCall(selectPadding, 3)
};

const server = h2.createServer(options);
server.on('stream', common.mustCall(onStream));

function onStream(stream, headers, flags) {
  stream.respond({
    'content-type': 'text/html',
    ':status': 200
  });
  stream.end('hello world');
}

server.listen(0);

server.on('listening', common.mustCall(() => {
  const client = h2.connect(`http://localhost:${server.address().port}`,
                            options);

  const req = client.request({ ':path': '/' });
  req.on('response', common.mustCall());
  req.resume();
  req.on('end', common.mustCall(() => {
    server.close();
    client.close();
  }));
  req.end();
}));