@@ -9,6 +9,7 @@ void describe('doRequest()', () => {
9
9
status : 200 ,
10
10
headers : new Map < string , string > ( [
11
11
[ 'content-type' , 'application/json' ] ,
12
+ [ 'content-length' , '42' ] ,
12
13
] ) ,
13
14
json : async ( ) => Promise . resolve ( { foo : 'bar' } ) ,
14
15
} ) ,
@@ -37,6 +38,34 @@ void describe('doRequest()', () => {
37
38
} )
38
39
} )
39
40
41
+ void it ( 'should only parse the response if there is content' , async ( ) => {
42
+ const mockFetch = mock . fn ( async ( ) =>
43
+ Promise . resolve ( {
44
+ status : 200 ,
45
+ headers : new Map < string , string > ( [
46
+ [ 'content-type' , 'application/json' ] ,
47
+ [ 'content-length' , '0' ] ,
48
+ ] ) ,
49
+ json : async ( ) => Promise . resolve ( JSON . parse ( '' ) ) , // Intentional empty JSON
50
+ } ) ,
51
+ )
52
+ const assertFn = mock . fn < AssertFn > ( async ( ) => Promise . resolve ( ) )
53
+
54
+ const inFlight = doRequest (
55
+ new URL ( 'https://example.com' ) ,
56
+ {
57
+ method : 'POST' ,
58
+ } ,
59
+ undefined ,
60
+ mockFetch as any ,
61
+ )
62
+
63
+ await inFlight . match ( assertFn )
64
+ assert . partialDeepStrictEqual ( assertFn . mock . calls [ 0 ] ?. arguments ?. [ 0 ] , {
65
+ body : undefined ,
66
+ } )
67
+ } )
68
+
40
69
void it ( 'should retry the request if the assert fails' , async ( ) => {
41
70
const mockFetch = mock . fn < ( ) => Promise < ReturnType < typeof fetch > > > ( )
42
71
mockFetch . mock . mockImplementationOnce (
@@ -53,6 +82,7 @@ void describe('doRequest()', () => {
53
82
status : 200 ,
54
83
headers : new Map < string , string > ( [
55
84
[ 'content-type' , 'application/json' ] ,
85
+ [ 'content-length' , '42' ] ,
56
86
] ) ,
57
87
json : async ( ) => Promise . resolve ( { foo : 'bar' } ) ,
58
88
} as any ) ,
0 commit comments