|
| 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.client.stream.bodyChunkSent' and |
| 9 | +// 'http2.client.stream.bodySent' channels when ClientHttp2Streams bodies are |
| 10 | +// being sent with multiple Buffers and strings. |
| 11 | + |
| 12 | +const assert = require('assert'); |
| 13 | +const dc = require('diagnostics_channel'); |
| 14 | +const http2 = require('http2'); |
| 15 | +const { Duplex } = require('stream'); |
| 16 | + |
| 17 | +let bodyChunkSent = false; |
| 18 | + |
| 19 | +dc.subscribe('http2.client.stream.bodyChunkSent', common.mustCall(({ stream, writev, data, encoding }) => { |
| 20 | + // Since ClientHttp2Stream is not exported from any module, this just checks |
| 21 | + // if the stream is an instance of Duplex. |
| 22 | + assert.ok(stream instanceof Duplex); |
| 23 | + assert.strictEqual(stream.constructor.name, 'ClientHttp2Stream'); |
| 24 | + |
| 25 | + assert.strictEqual(writev, true); |
| 26 | + |
| 27 | + assert.ok(Array.isArray(data)); |
| 28 | + assert.strictEqual(data.length, 3); |
| 29 | + |
| 30 | + assert.strictEqual(data[0].chunk, 'héllo'); |
| 31 | + assert.strictEqual(data[0].encoding, 'latin1'); |
| 32 | + |
| 33 | + assert.ok(Buffer.from('foo').equals(data[1].chunk)); |
| 34 | + assert.strictEqual(data[1].encoding, 'buffer'); |
| 35 | + |
| 36 | + assert.ok(Buffer.from('bar').equals(data[2].chunk)); |
| 37 | + assert.strictEqual(data[2].encoding, 'buffer'); |
| 38 | + |
| 39 | + assert.strictEqual(encoding, ''); |
| 40 | + |
| 41 | + bodyChunkSent = true; |
| 42 | +})); |
| 43 | + |
| 44 | +dc.subscribe('http2.client.stream.bodySent', common.mustCall(({ stream }) => { |
| 45 | + // 'http2.client.stream.bodyChunkSent' must run first. |
| 46 | + assert.ok(bodyChunkSent); |
| 47 | + |
| 48 | + // Since ClientHttp2Stream is not exported from any module, this just checks |
| 49 | + // if the stream is an instance of Duplex. |
| 50 | + assert.ok(stream instanceof Duplex); |
| 51 | + assert.strictEqual(stream.constructor.name, 'ClientHttp2Stream'); |
| 52 | +})); |
| 53 | + |
| 54 | +const server = http2.createServer(); |
| 55 | +server.on('stream', common.mustCall((stream) => { |
| 56 | + stream.respond({}, { endStream: true }); |
| 57 | +})); |
| 58 | + |
| 59 | +server.listen(0, common.mustCall(() => { |
| 60 | + const port = server.address().port; |
| 61 | + const client = http2.connect(`http://localhost:${port}`); |
| 62 | + |
| 63 | + const stream = client.request({ [http2.constants.HTTP2_HEADER_METHOD]: 'POST' }); |
| 64 | + stream.write('héllo', 'latin1'); |
| 65 | + stream.write(Buffer.from('foo')); |
| 66 | + stream.write(new TextEncoder().encode('bar')); |
| 67 | + stream.end(); |
| 68 | + |
| 69 | + stream.on('response', common.mustCall(() => { |
| 70 | + client.close(); |
| 71 | + server.close(); |
| 72 | + })); |
| 73 | +}, 1)); |
0 commit comments