Skip to content

Commit 451e69c

Browse files
author
Michael Solomon
authored
fix(fetch): support Content-Encoding response header (#2797)
1 parent 4213d99 commit 451e69c

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"main": "./index.js",
2323
"types": "types",
2424
"dependencies": {
25-
"@mswjs/interceptors": "^0.36.4",
25+
"@mswjs/interceptors": "^0.36.6",
2626
"json-stringify-safe": "^5.0.1",
2727
"propagate": "^2.0.0"
2828
},

tests/test_fetch.js

+45-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe('Native Fetch', () => {
149149
await fetch('https://api.test.com/data', { headers })
150150
})
151151

152-
describe.skip('content-encoding', () => {
152+
describe('content-encoding', () => {
153153
it('should accept gzipped content', async () => {
154154
const message = 'Lorem ipsum dolor sit amet'
155155
const compressed = zlib.gzipSync(message)
@@ -222,6 +222,24 @@ describe('Native Fetch', () => {
222222
scope.done()
223223
})
224224

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+
225243
it('should pass through the result if a not supported encoding was used', async () => {
226244
const message = 'Lorem ipsum dolor sit amet'
227245
const compressed = Buffer.from(message)
@@ -240,9 +258,33 @@ describe('Native Fetch', () => {
240258

241259
it('should throw error if wrong encoding is used', async () => {
242260
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+
243285
const scope = nock('http://example.test')
244286
.get('/foo')
245-
.reply(200, message, {
287+
.reply(200, Buffer.from(message), {
246288
'X-Transfer-Length': String(message.length),
247289
'Content-Length': undefined,
248290
'Content-Encoding': 'br',
@@ -254,7 +296,7 @@ describe('Native Fetch', () => {
254296
throw new Error('Should have thrown')
255297
})
256298
.catch(e => {
257-
expect(e.message).to.contain('unexpected end of file')
299+
expect(e.message).to.contain('Decompression failed')
258300
scope.done()
259301
})
260302
})

0 commit comments

Comments
 (0)