http2: add diagnostics channel 'http2.server.stream.error' · nodejs/node@2b868e8

1+

'use strict';

2+3+

const common = require('../common');

4+

if (!common.hasCrypto)

5+

common.skip('missing crypto');

6+7+

// This test ensures that the built-in HTTP/2 diagnostics channels are reporting

8+

// the diagnostics messages for the 'http2.server.stream.error' channel when

9+

// an error occurs during the processing of a ServerHttp2Stream.

10+11+

const assert = require('assert');

12+

const dc = require('diagnostics_channel');

13+

const http2 = require('http2');

14+

const { Duplex } = require('stream');

15+16+

let expectedError = null;

17+18+

dc.subscribe('http2.server.stream.error', common.mustCall(({ stream, error }) => {

19+

// Since ServerHttp2Stream is not exported from any module, this just checks

20+

// if the stream is an instance of Duplex and the constructor name is

21+

// 'ServerHttp2Stream'.

22+

assert.ok(stream instanceof Duplex);

23+

assert.strictEqual(stream.constructor.name, 'ServerHttp2Stream');

24+

assert.strictEqual(stream.closed, true);

25+

assert.strictEqual(stream.destroyed, true);

26+27+

assert.ok(error);

28+

assert.strictEqual(error, expectedError);

29+

}));

30+31+

const server = http2.createServer();

32+33+

server.on('stream', common.mustCall((stream) => {

34+

stream.on('error', common.mustCall((err) => {

35+

assert.strictEqual(err, expectedError);

36+

}));

37+38+

expectedError = new Error('HTTP/2 server stream error');

39+

stream.destroy(expectedError);

40+

}));

41+42+

server.listen(0, common.mustCall(() => {

43+

const port = server.address().port;

44+

const client = http2.connect(`http://localhost:${port}`);

45+46+

const stream = client.request({});

47+

stream.on('error', common.mustCall((err) => {

48+

assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_ERROR');

49+50+

client.close();

51+

server.close();

52+

}));

53+

}));