File tree Expand file tree Collapse file tree 2 files changed +24
-6
lines changed Expand file tree Collapse file tree 2 files changed +24
-6
lines changed Original file line number Diff line number Diff line change @@ -82,16 +82,18 @@ export function deserializeBodyReader(
82
82
return buildBodyReader ( encodedBody , headers , decoder ) ;
83
83
}
84
84
85
- function failIfDecodingRequired ( error : string | undefined , buffer : Buffer , headers : Headers ) {
85
+ function failIfDecodingRequired ( errorMessage : string | undefined , buffer : Buffer , headers : Headers ) {
86
86
if ( ! headers [ 'content-encoding' ] || headers [ 'content-encoding' ] === 'identity' ) {
87
87
return buffer ;
88
88
}
89
89
90
- if ( error ) {
91
- throw new Error ( `Decoding error: ${ error } ` ) ;
92
- } else {
93
- throw new Error ( "Can't read encoded message body as client-side decoding has been disabled" ) ;
94
- }
90
+ const error = errorMessage
91
+ ? new Error ( `Decoding error (${ headers [ 'content-encoding' ] } ): ${ errorMessage } ` )
92
+ : new Error ( "Can't read encoded message body without server-side decoding" ) ;
93
+
94
+ console . warn ( error . message ) ;
95
+
96
+ throw error ;
95
97
}
96
98
97
99
/**
Original file line number Diff line number Diff line change @@ -478,6 +478,22 @@ nodeOnly(() => {
478
478
expect ( bodyText ) . to . equal ( 'Hello world' ) ;
479
479
} ) ;
480
480
481
+ it ( "should not be able to read non-decodeable bodies" , async ( ) => {
482
+ const responseDeferred = getDeferred < CompletedResponse > ( ) ;
483
+ await remoteServer . on ( 'response' , ( res ) => responseDeferred . resolve ( res ) ) ;
484
+
485
+ await remoteServer . forAnyRequest ( ) . thenReply ( 200 , 'INVALID DATA' , {
486
+ 'Content-Encoding' : 'gzip'
487
+ } ) ;
488
+
489
+ await fetch ( remoteServer . url ) ;
490
+
491
+ const response = await responseDeferred ;
492
+ expect ( response . statusCode ) . to . equal ( 200 ) ;
493
+ const bodyText = await response . body . getText ( ) ;
494
+ expect ( bodyText ) . to . equal ( undefined ) ;
495
+ } ) ;
496
+
481
497
it ( "should allow resetting the mock server configured responses" , async ( ) => {
482
498
await remoteServer . forGet ( "/mocked-endpoint" ) . thenReply ( 200 , "mocked data" ) ;
483
499
You can’t perform that action at this time.
0 commit comments