@@ -20,6 +20,7 @@ const makeRequest = (
2020 readonly headers ?: Record < string , string > ;
2121 readonly postData ?: string ;
2222 readonly errorText ?: string ;
23+ readonly responseBodySize ?: number ;
2324 } = { } ,
2425) => ( {
2526 url : ( ) => url ,
@@ -28,6 +29,10 @@ const makeRequest = (
2829 headers : ( ) => options . headers ?? { } ,
2930 postDataBuffer : ( ) => ( options . postData === undefined ? null : Buffer . from ( options . postData ) ) ,
3031 failure : ( ) => ( options . errorText === undefined ? null : { errorText : options . errorText } ) ,
32+ sizes : async ( ) => {
33+ if ( options . responseBodySize === undefined ) throw new Error ( "Unable to fetch sizes" ) ;
34+ return { responseBodySize : options . responseBodySize } ;
35+ } ,
3136} ) ;
3237
3338const makeResponse = (
@@ -201,6 +206,39 @@ describe("NetworkLog", () => {
201206 expect ( streamResponse . body ) . not . toHaveBeenCalled ( ) ;
202207 } ) ;
203208
209+ it ( "bounds a chunked body without a declared length by the bytes received" , async ( ) => {
210+ const network = setup ( ) ;
211+ const export_ = makeRequest ( "http://localhost:5173/api/export" , {
212+ responseBodySize : 6_000_000 ,
213+ } ) ;
214+ const exportResponse = makeResponse ( export_ , 200 , { "content-type" : "application/json" } ) ;
215+ const small = makeRequest ( "http://localhost:5173/api/small" , { responseBodySize : 11 } ) ;
216+ const smallResponse = makeResponse (
217+ small ,
218+ 500 ,
219+ { "content-type" : "application/json" } ,
220+ '{"ok":false}' ,
221+ ) ;
222+ for ( const [ request , response ] of [
223+ [ export_ , exportResponse ] ,
224+ [ small , smallResponse ] ,
225+ ] as const ) {
226+ network . start ( request ) ;
227+ network . respond ( response ) ;
228+ network . finish ( request ) ;
229+ }
230+
231+ const detail = async ( requestId : string ) =>
232+ ( await network . log . query ( "tab-1" , { requestId } ) ) . requests [ 0 ] ?. responseBody ;
233+ expect ( await detail ( "r1" ) ) . toEqual ( {
234+ sizeBytes : 6_000_000 ,
235+ truncated : true ,
236+ omitted : "unavailable" ,
237+ } ) ;
238+ expect ( exportResponse . body ) . not . toHaveBeenCalled ( ) ;
239+ expect ( await detail ( "r2" ) ) . toEqual ( { text : '{"ok":false}' , sizeBytes : 12 , truncated : false } ) ;
240+ } ) ;
241+
204242 it ( "reports a body Chromium no longer holds as unavailable" , async ( ) => {
205243 const network = setup ( ) ;
206244 const request = makeRequest ( "http://localhost:5173/api/old" ) ;
0 commit comments