summaryrefslogtreecommitdiff
path: root/test/parallel/test-http2-pack-end-stream-flag.js
blob: f6bb4452d95a77ee830c5fbca629c506782e0a62 (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
52
53
54
55
56
57
58
59
60
61
'use strict';

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

const { PerformanceObserver } = require('perf_hooks');

const server = http2.createServer();

server.on('stream', (stream, headers) => {
  stream.respond({
    'content-type': 'text/html',
    ':status': 200
  });
  switch (headers[':path']) {
    case '/singleEnd':
      stream.end('OK');
      break;
    case '/sequentialEnd':
      stream.write('OK');
      stream.end();
      break;
    case '/delayedEnd':
      stream.write('OK', () => stream.end());
      break;
  }
});

function testRequest(path, targetFrameCount, callback) {
  const obs = new PerformanceObserver((list, observer) => {
    const entry = list.getEntries()[0];
    if (entry.name !== 'Http2Session') return;
    if (entry.type !== 'client') return;
    assert.strictEqual(entry.framesReceived, targetFrameCount);
    observer.disconnect();
    callback();
  });
  obs.observe({ entryTypes: ['http2'] });
  const client = http2.connect(`http://localhost:${server.address().port}`, () => {
    const req = client.request({ ':path': path });
    req.resume();
    req.end();
    req.on('end', () => client.close());
  });
}

// SETTINGS => SETTINGS => HEADERS => DATA
const MIN_FRAME_COUNT = 4;

server.listen(0, () => {
  testRequest('/singleEnd', MIN_FRAME_COUNT, () => {
    testRequest('/sequentialEnd', MIN_FRAME_COUNT, () => {
      testRequest('/delayedEnd', MIN_FRAME_COUNT + 1, () => {
        server.close();
      });
    });
  });
});