@@ -8,7 +8,7 @@ const { Client } = require('../../..')
8
8
const { createServer } = require ( 'node:http' )
9
9
10
10
test ( 'Diagnostics channel - post stream' , ( t ) => {
11
- const assert = tspl ( t , { plan : 33 } )
11
+ const assert = tspl ( t , { plan : 43 } )
12
12
const server = createServer ( { joinDuplicateHeaders : true } , ( req , res ) => {
13
13
req . resume ( )
14
14
res . setHeader ( 'Content-Type' , 'text/plain' )
@@ -107,20 +107,43 @@ test('Diagnostics channel - post stream', (t) => {
107
107
assert . equal ( response . statusText , 'OK' )
108
108
} )
109
109
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
+ } )
110
118
diagnosticsChannel . channel ( 'undici:request:bodySent' ) . subscribe ( ( { request } ) => {
111
119
assert . equal ( _req , request )
120
+ bodySent = true
121
+
122
+ const requestBody = Buffer . concat ( bodyChunks )
123
+ assert . deepStrictEqual ( requestBody , Buffer . from ( 'hello world' ) )
112
124
} )
113
125
114
126
let endEmitted = false
115
127
116
128
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
+
117
135
diagnosticsChannel . channel ( 'undici:request:trailers' ) . subscribe ( ( { request, trailers } ) => {
136
+ assert . equal ( bodySent , true )
118
137
assert . equal ( request . completed , true )
119
138
assert . equal ( _req , request )
120
139
// This event is emitted after the last chunk has been added to the body stream,
121
140
// not when it was consumed by the application
122
141
assert . equal ( endEmitted , false )
123
142
assert . deepStrictEqual ( trailers , [ Buffer . from ( 'foo' ) , Buffer . from ( 'oof' ) ] )
143
+
144
+ const respData = Buffer . concat ( respChunks )
145
+ assert . deepStrictEqual ( respData , Buffer . from ( 'hello' ) )
146
+
124
147
resolve ( )
125
148
} )
126
149
0 commit comments