@@ -149,7 +149,7 @@ describe('Native Fetch', () => {
149
149
await fetch ( 'https://api.test.com/data' , { headers } )
150
150
} )
151
151
152
- describe . skip ( 'content-encoding' , ( ) => {
152
+ describe ( 'content-encoding' , ( ) => {
153
153
it ( 'should accept gzipped content' , async ( ) => {
154
154
const message = 'Lorem ipsum dolor sit amet'
155
155
const compressed = zlib . gzipSync ( message )
@@ -222,6 +222,24 @@ describe('Native Fetch', () => {
222
222
scope . done ( )
223
223
} )
224
224
225
+ it ( 'should accept gzip and deflate content' , async ( ) => {
226
+ const message = 'Lorem ipsum dolor sit amet'
227
+ const compressed = zlib . deflateSync ( zlib . gzipSync ( message ) )
228
+
229
+ const scope = nock ( 'http://example.test' )
230
+ . get ( '/foo' )
231
+ . reply ( 200 , compressed , {
232
+ 'X-Transfer-Length' : String ( compressed . length ) ,
233
+ 'Content-Length' : undefined ,
234
+ 'Content-Encoding' : 'gzip, deflate' ,
235
+ } )
236
+ const response = await fetch ( 'http://example.test/foo' )
237
+
238
+ expect ( response . status ) . to . equal ( 200 )
239
+ expect ( await response . text ( ) ) . to . equal ( message )
240
+ scope . done ( )
241
+ } )
242
+
225
243
it ( 'should pass through the result if a not supported encoding was used' , async ( ) => {
226
244
const message = 'Lorem ipsum dolor sit amet'
227
245
const compressed = Buffer . from ( message )
@@ -240,9 +258,33 @@ describe('Native Fetch', () => {
240
258
241
259
it ( 'should throw error if wrong encoding is used' , async ( ) => {
242
260
const message = 'Lorem ipsum dolor sit amet'
261
+ const compressed = zlib . gzipSync ( message )
262
+
263
+ const scope = nock ( 'http://example.test' )
264
+ . get ( '/foo' )
265
+ . reply ( 200 , compressed , {
266
+ 'X-Transfer-Length' : String ( message . length ) ,
267
+ 'Content-Length' : undefined ,
268
+ 'Content-Encoding' : 'br' ,
269
+ } )
270
+ const response = await fetch ( 'http://example.test/foo' )
271
+ await response
272
+ . text ( )
273
+ . then ( ( ) => {
274
+ throw new Error ( 'Should have thrown' )
275
+ } )
276
+ . catch ( e => {
277
+ expect ( e . message ) . to . contain ( 'Decompression failed' )
278
+ scope . done ( )
279
+ } )
280
+ } )
281
+
282
+ it . skip ( 'should throw error if encoding is used with uncompressed body' , async ( ) => {
283
+ const message = 'Lorem ipsum dolor sit amet'
284
+
243
285
const scope = nock ( 'http://example.test' )
244
286
. get ( '/foo' )
245
- . reply ( 200 , message , {
287
+ . reply ( 200 , Buffer . from ( message ) , {
246
288
'X-Transfer-Length' : String ( message . length ) ,
247
289
'Content-Length' : undefined ,
248
290
'Content-Encoding' : 'br' ,
@@ -254,7 +296,7 @@ describe('Native Fetch', () => {
254
296
throw new Error ( 'Should have thrown' )
255
297
} )
256
298
. catch ( e => {
257
- expect ( e . message ) . to . contain ( 'unexpected end of file ' )
299
+ expect ( e . message ) . to . contain ( 'Decompression failed ' )
258
300
scope . done ( )
259
301
} )
260
302
} )
0 commit comments