@@ -34,11 +34,39 @@ describe('gzip', () => {
3434 expect ( globalThis . CompressionStream ) . toBeDefined ( )
3535 expect ( isGzipSupported ( ) ) . toBe ( true )
3636 } )
37- it ( 'should return false if CompressStream not available' , ( ) => {
38- const CompressionStream = globalThis . CompressionStream
39- delete ( globalThis as any ) . CompressionStream
40- expect ( isGzipSupported ( ) ) . toBe ( false )
41- ; ( globalThis as any ) . CompressionStream = CompressionStream
37+ it . each ( [
38+ [
39+ 'CompressionStream' ,
40+ ( ) => {
41+ const CompressionStream = globalThis . CompressionStream
42+ delete ( globalThis as any ) . CompressionStream
43+ return ( ) => ( ( globalThis as any ) . CompressionStream = CompressionStream )
44+ } ,
45+ ] ,
46+ [
47+ 'TextEncoder' ,
48+ ( ) => {
49+ const TextEncoder = globalThis . TextEncoder
50+ delete ( globalThis as any ) . TextEncoder
51+ return ( ) => ( ( globalThis as any ) . TextEncoder = TextEncoder )
52+ } ,
53+ ] ,
54+ [
55+ 'Response.blob' ,
56+ ( ) => {
57+ const blob = globalThis . Response . prototype . blob
58+ delete ( globalThis . Response . prototype as any ) . blob
59+ return ( ) => ( ( globalThis . Response . prototype as any ) . blob = blob )
60+ } ,
61+ ] ,
62+ ] ) ( 'should return false if %s not available' , ( _name , removeDependency ) => {
63+ const restoreDependency = removeDependency ( )
64+
65+ try {
66+ expect ( isGzipSupported ( ) ) . toBe ( false )
67+ } finally {
68+ restoreDependency ( )
69+ }
4270 } )
4371 } )
4472 describe ( 'isNativeAsyncGzipReadError' , ( ) => {
@@ -60,6 +88,44 @@ describe('gzip', () => {
6088 ; ( globalThis as any ) . CompressionStream = CompressionStream
6189 } )
6290
91+ it ( 'does not read input using Blob.stream' , async ( ) => {
92+ const blobStream = Blob . prototype . stream
93+ Blob . prototype . stream = jest . fn ( ( ) => {
94+ throw new Error ( 'Blob.stream should not be used' )
95+ } )
96+
97+ try {
98+ const compressed = await gzipCompress ( API_TEST_INPUT , false , { rethrow : true } )
99+ expect ( compressed ) . not . toBe ( null )
100+ } finally {
101+ Blob . prototype . stream = blobStream
102+ }
103+ } )
104+
105+ it ( 'aborts the compression writer when writing input fails' , async ( ) => {
106+ const CompressionStream = globalThis . CompressionStream
107+ const writeError = new Error ( 'write failed' )
108+ const abort = jest . fn ( ( ) => Promise . resolve ( ) )
109+
110+ ; ( globalThis as any ) . CompressionStream = jest . fn ( ( ) => ( {
111+ writable : {
112+ getWriter : ( ) => ( {
113+ write : ( ) => Promise . reject ( writeError ) ,
114+ close : jest . fn ( ) ,
115+ abort,
116+ } ) ,
117+ } ,
118+ readable : new ReadableStream ( ) ,
119+ } ) )
120+
121+ try {
122+ await expect ( gzipCompress ( API_TEST_INPUT , false , { rethrow : true } ) ) . rejects . toBe ( writeError )
123+ expect ( abort ) . toHaveBeenCalledWith ( writeError )
124+ } finally {
125+ ; ( globalThis as any ) . CompressionStream = CompressionStream
126+ }
127+ } )
128+
63129 it ( 'compressed random data should match node' , async ( ) => {
64130 const compressed = await gzipCompress ( RANDOM_TEST_INPUT )
65131 expect ( compressed ) . not . toBe ( null )
0 commit comments