1'use strict';
2const common = require('../common');
3if (!common.hasCrypto)
4  common.skip('missing crypto');
5const { readSync } = require('../common/fixtures');
6const net = require('net');
7const http2 = require('http2');
8const { once } = require('events');
9
10async function main() {
11  const blobWithEmptyFrame = readSync('emptyframe.http2');
12  const server = net.createServer((socket) => {
13    socket.once('data', () => {
14      socket.end(blobWithEmptyFrame);
15    });
16  }).listen(0);
17  await once(server, 'listening');
18
19  for (const maxSessionInvalidFrames of [0, 2]) {
20    const client = http2.connect(`http://localhost:${server.address().port}`, {
21      maxSessionInvalidFrames
22    });
23    const stream = client.request({
24      ':method': 'GET',
25      ':path': '/'
26    });
27    if (maxSessionInvalidFrames) {
28      stream.on('error', common.mustNotCall());
29      client.on('error', common.mustNotCall());
30    } else {
31      const expected = {
32        code: 'ERR_HTTP2_TOO_MANY_INVALID_FRAMES',
33        message: 'Too many invalid HTTP/2 frames'
34      };
35      stream.on('error', common.expectsError(expected));
36      client.on('error', common.expectsError(expected));
37    }
38    stream.resume();
39    await new Promise((resolve) => {
40      stream.once('close', resolve);
41    });
42    client.close();
43  }
44  server.close();
45}
46
47main().then(common.mustCall());
48