@@ -8,7 +8,7 @@ const { Client } = require('../../..')
88const { createServer } = require ( 'node:http' )
99
1010test ( 'Diagnostics channel - post stream' , ( t ) => {
11- const assert = tspl ( t , { plan : 33 } )
11+ const assert = tspl ( t , { plan : 43 } )
1212 const server = createServer ( { joinDuplicateHeaders : true } , ( req , res ) => {
1313 req . resume ( )
1414 res . setHeader ( 'Content-Type' , 'text/plain' )
@@ -107,20 +107,43 @@ test('Diagnostics channel - post stream', (t) => {
107107 assert . equal ( response . statusText , 'OK' )
108108 } )
109109
110+ let bodySent = false
111+ const bodyChunks = [ ]
112+ diagnosticsChannel . channel ( 'undici:request:bodyChunkSent' ) . subscribe ( ( { request, chunk } ) => {
113+ assert . equal ( _req , request )
114+ // Chunk can be a string or a Buffer, depending on the stream writer.
115+ assert . equal ( typeof chunk , 'string' )
116+ bodyChunks . push ( Buffer . from ( chunk ) )
117+ } )
110118 diagnosticsChannel . channel ( 'undici:request:bodySent' ) . subscribe ( ( { request } ) => {
111119 assert . equal ( _req , request )
120+ bodySent = true
121+
122+ const requestBody = Buffer . concat ( bodyChunks )
123+ assert . deepStrictEqual ( requestBody , Buffer . from ( 'hello world' ) )
112124 } )
113125
114126 let endEmitted = false
115127
116128 return new Promise ( ( resolve ) => {
129+ const respChunks = [ ]
130+ diagnosticsChannel . channel ( 'undici:request:bodyChunkReceived' ) . subscribe ( ( { request, chunk } ) => {
131+ assert . equal ( _req , request )
132+ respChunks . push ( chunk )
133+ } )
134+
117135 diagnosticsChannel . channel ( 'undici:request:trailers' ) . subscribe ( ( { request, trailers } ) => {
136+ assert . equal ( bodySent , true )
118137 assert . equal ( request . completed , true )
119138 assert . equal ( _req , request )
120139 // This event is emitted after the last chunk has been added to the body stream,
121140 // not when it was consumed by the application
122141 assert . equal ( endEmitted , false )
123142 assert . deepStrictEqual ( trailers , [ Buffer . from ( 'foo' ) , Buffer . from ( 'oof' ) ] )
143+
144+ const respData = Buffer . concat ( respChunks )
145+ assert . deepStrictEqual ( respData , Buffer . from ( 'hello' ) )
146+
124147 resolve ( )
125148 } )
126149
0 commit comments