Skip to content

Commit

Permalink
fix(fetch): support Content-Encoding response header (#2797)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Solomon authored Nov 14, 2024
1 parent 4213d99 commit 451e69c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"main": "./index.js",
"types": "types",
"dependencies": {
"@mswjs/interceptors": "^0.36.4",
"@mswjs/interceptors": "^0.36.6",
"json-stringify-safe": "^5.0.1",
"propagate": "^2.0.0"
},
Expand Down
48 changes: 45 additions & 3 deletions tests/test_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('Native Fetch', () => {
await fetch('https://api.test.com/data', { headers })
})

describe.skip('content-encoding', () => {
describe('content-encoding', () => {
it('should accept gzipped content', async () => {
const message = 'Lorem ipsum dolor sit amet'
const compressed = zlib.gzipSync(message)
Expand Down Expand Up @@ -222,6 +222,24 @@ describe('Native Fetch', () => {
scope.done()
})

it('should accept gzip and deflate content', async () => {
const message = 'Lorem ipsum dolor sit amet'
const compressed = zlib.deflateSync(zlib.gzipSync(message))

const scope = nock('http://example.test')
.get('/foo')
.reply(200, compressed, {
'X-Transfer-Length': String(compressed.length),
'Content-Length': undefined,
'Content-Encoding': 'gzip, deflate',
})
const response = await fetch('http://example.test/foo')

expect(response.status).to.equal(200)
expect(await response.text()).to.equal(message)
scope.done()
})

it('should pass through the result if a not supported encoding was used', async () => {
const message = 'Lorem ipsum dolor sit amet'
const compressed = Buffer.from(message)
Expand All @@ -240,9 +258,33 @@ describe('Native Fetch', () => {

it('should throw error if wrong encoding is used', async () => {
const message = 'Lorem ipsum dolor sit amet'
const compressed = zlib.gzipSync(message)

const scope = nock('http://example.test')
.get('/foo')
.reply(200, compressed, {
'X-Transfer-Length': String(message.length),
'Content-Length': undefined,
'Content-Encoding': 'br',
})
const response = await fetch('http://example.test/foo')
await response
.text()
.then(() => {
throw new Error('Should have thrown')
})
.catch(e => {
expect(e.message).to.contain('Decompression failed')
scope.done()
})
})

it.skip('should throw error if encoding is used with uncompressed body', async () => {
const message = 'Lorem ipsum dolor sit amet'

const scope = nock('http://example.test')
.get('/foo')
.reply(200, message, {
.reply(200, Buffer.from(message), {
'X-Transfer-Length': String(message.length),
'Content-Length': undefined,
'Content-Encoding': 'br',
Expand All @@ -254,7 +296,7 @@ describe('Native Fetch', () => {
throw new Error('Should have thrown')
})
.catch(e => {
expect(e.message).to.contain('unexpected end of file')
expect(e.message).to.contain('Decompression failed')
scope.done()
})
})
Expand Down

0 comments on commit 451e69c

Please sign in to comment.