@@ -4,6 +4,7 @@ import * as utils from '../lib/utils.mjs'
4
4
import * as config from './config.mjs'
5
5
import * as clientUtils from './utils.mjs'
6
6
import * as fetching from './fetching.mjs'
7
+ import { getGlobalDispatcher } from 'undici'
7
8
const assert = utils . assert
8
9
const setupCheck = clientUtils . setupCheck
9
10
@@ -32,11 +33,19 @@ export async function makeTest (test) {
32
33
controller . abort ( )
33
34
} , config . requestTimeout * 1000 )
34
35
init . signal = controller . signal
36
+
37
+ const interimResponses = [ ]
38
+ if ( 'expected_interim_responses' in reqConfig ) {
39
+ const globalDispatcher = getGlobalDispatcher ( )
40
+ const dispatcher = globalDispatcher . compose ( clientUtils . interimResponsesCollectingInterceptor ( interimResponses ) )
41
+ init . dispatcher = dispatcher
42
+ }
43
+
35
44
if ( test . dump === true ) clientUtils . logRequest ( url , init , reqNum )
36
45
return fetch ( url , init )
37
46
. then ( response => {
38
47
responses . push ( response )
39
- return checkResponse ( test , requests , idx , response )
48
+ return checkResponse ( test , requests , idx , response , interimResponses )
40
49
} )
41
50
. finally ( ( ) => {
42
51
clearTimeout ( timeout )
@@ -84,7 +93,7 @@ export async function makeTest (test) {
84
93
} )
85
94
}
86
95
87
- function checkResponse ( test , requests , idx , response ) {
96
+ function checkResponse ( test , requests , idx , response , interimResponses ) {
88
97
const reqNum = idx + 1
89
98
const reqConfig = requests [ idx ]
90
99
const resNum = parseInt ( response . headers . get ( 'Server-Request-Count' ) )
@@ -185,6 +194,37 @@ function checkResponse (test, requests, idx, response) {
185
194
}
186
195
} )
187
196
}
197
+
198
+ // check interim responses
199
+ if ( 'expected_interim_responses' in reqConfig ) {
200
+ const isSetup = setupCheck ( reqConfig , 'expected_interim_responses' )
201
+
202
+ reqConfig . expected_interim_responses . forEach ( ( [ statusCode , headers = [ ] ] , idx ) => {
203
+ if ( interimResponses [ idx ] == null ) {
204
+ assert ( isSetup , false , `Interim response ${ idx + 1 } not received` )
205
+ } else {
206
+ assert ( isSetup , interimResponses [ idx ] [ 0 ] === statusCode , `Interim response ${ idx + 1 } status is ${ interimResponses [ idx ] [ 0 ] } , not ${ statusCode } ` )
207
+
208
+ const receivedHeaders = interimResponses [ idx ] [ 1 ]
209
+ headers . forEach ( ( [ header , value ] ) => {
210
+ if ( typeof header === 'string' ) {
211
+ assert ( isSetup , header in receivedHeaders ,
212
+ `Interim response ${ idx + 1 } ${ header } header not present.` )
213
+ } else if ( header . length > 2 ) {
214
+ assert ( isSetup , header in receivedHeaders ,
215
+ `Interim response ${ idx + 1 } ${ header } header not present.` )
216
+
217
+ const receivedValue = receivedHeaders [ header ]
218
+ assert ( isSetup , value === receivedValue ,
219
+ `Interim response ${ idx + 1 } header ${ header } is ${ receivedValue } , should ${ value } ` )
220
+ } else {
221
+ console . log ( 'ERROR: Unknown header item in expected_interim_responses' , header )
222
+ }
223
+ } )
224
+ }
225
+ } )
226
+ }
227
+
188
228
return response . text ( ) . then ( makeCheckResponseBody ( test , reqConfig , response . status ) )
189
229
}
190
230
0 commit comments