Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fetch): support Content-Encoding response header #604

Merged
merged 14 commits into from
Oct 25, 2024
Next Next commit
failing test
Michael Solomon committed Jul 12, 2024

Unverified

No user is associated with the committer email.
commit 65ca83a1887773ead537f8e3dde39f492282f917
20 changes: 20 additions & 0 deletions test/modules/fetch/intercept/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { vi, it, expect, beforeAll, afterEach, afterAll } from 'vitest'
import zlib from "zlib";
import fetch from 'node-fetch'
import { RequestHandler } from 'express'
import { HttpServer } from '@open-draft/test-server/http'
@@ -329,3 +330,22 @@ it('intercepts an HTTPS PATCH request', async () => {

expect(requestId).toMatch(REQUEST_ID_REGEXP)
})

it.only('support content-encoding: gzip, br', async () => {
kettanaito marked this conversation as resolved.
Show resolved Hide resolved
const message = 'Lorem ipsum dolor sit amet'
const compressed = zlib.brotliCompressSync(zlib.gzipSync(message))

interceptor.once('request', ({ controller }) => {
controller.respondWith(new Response(compressed, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For mocked responses, you most definitely need a third-party. Interceptors mustn't meddle with your input based on any headers.

I'm mostly curious about receiving a compressed request in Node.js.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mostly curious about receiving a compressed request in Node.js.

I hope I understand your question correctly but:
Fetch: decompress the response according to the content encoding header.
IncomingMessage: return the body as-is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! That seems to be the case, although we may want to add integration tests for the ClientRequest interceptor to verify it. I do believe it's going to expose the response body as-is and it's up to the developer to decompress it.

The fetch client has decompression built-in, I can confirm it by looking at the source of Undici.

headers: {
'Content-Length': String(compressed.length),
'Content-Encoding': 'gzip, br',
}
}))
})

const response = await fetch('http://localhost')

expect(response.status).toBe(200)
expect(await response.text()).toBe(message)
})