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(ClientRequest): preserve headers type when recording raw headers #660

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/interceptors/ClientRequest/utils/recordRawHeaders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ it('records raw headers (Reqest / Request as init)', () => {
expect(getRawFetchHeaders(request.headers)).toEqual([['X-My-Header', '1']])
})

it('preserves headers instanceof (Request / Request as init)', () => {
recordRawFetchHeaders()
const init = new Request(url, { headers: [['X-My-Header', '1']] })
new Request(init)
expect(init.headers).toBeInstanceOf(Headers)
})

it('preserves headers instanceof (Request / Request with Headers as init)', () => {
recordRawFetchHeaders()
const headers = new Headers([['X-My-Header', '1']])
const init = new Request(url, { headers })
new Request(init)
expect(init.headers).toBeInstanceOf(Headers)
})

it('preserves headers instanceof (Response / Response with Headers as init)', () => {
recordRawFetchHeaders()
const init = { headers: new Headers([['X-My-Header', '1']]) }
new Response(url, init)
expect(init.headers).toBeInstanceOf(Headers)
})

it('records raw headers (Request / Request+Headers as init)', () => {
recordRawFetchHeaders()
const init = new Request(url, { headers: [['X-My-Header', '1']] })
Expand Down
50 changes: 2 additions & 48 deletions src/interceptors/ClientRequest/utils/recordRawHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,44 +175,7 @@ export function recordRawFetchHeaders() {
writable: true,
value: new Proxy(Request, {
construct(target, args, newTarget) {
// Handle a `Request` instance as init.
if (
typeof args[0] === 'object' &&
args[0] instanceof Request &&
args[0].headers != null &&
args[0].headers instanceof Headers &&
Reflect.has(args[0].headers, kRawHeaders)
) {
Object.defineProperty(args[0], 'headers', {
enumerable: false,
configurable: true,
value: Reflect.get(args[0].headers, kRawHeaders),
})
}

/**
* @note If the headers init argument of Request
* is existing Headers instance, use its raw headers
* as the headers init instead.
* This is needed because the Headers constructor copies
* all normalized headers from the given Headers instance
* and uses ".append()" to add it to the new instance.
*/
if (
typeof args[1] === 'object' &&
args[1].headers != null &&
args[1].headers instanceof Headers &&
Reflect.has(args[1].headers, kRawHeaders)
) {
Object.defineProperty(args[1], 'headers', {
enumerable: false,
configurable: true,
value: Reflect.get(args[1].headers, kRawHeaders),
})
}

const request = Reflect.construct(target, args, newTarget)

const inferredRawHeaders: RawHeaders = []

// Infer raw headers from a `Request` instance used as init.
Expand All @@ -225,8 +188,8 @@ export function recordRawFetchHeaders() {
inferredRawHeaders.push(...inferRawHeaders(args[1].headers))
}

if (inferRawHeaders.length > 0) {
defineRawHeadersSymbol(request.headers, inferredRawHeaders)
if (inferredRawHeaders.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

This was a typo ✅

ensureRawHeadersSymbol(request.headers, inferredRawHeaders)
Copy link
Member

Choose a reason for hiding this comment

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

Here I'm using ensure to prevent the raw headers symbol from being re-defined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uh i don't remember changing this line...weird.

}

return request
Expand All @@ -239,15 +202,6 @@ export function recordRawFetchHeaders() {
writable: true,
value: new Proxy(Response, {
construct(target, args, newTarget) {
if (
typeof args[1] === 'object' &&
args[1].headers != null &&
args[1].headers instanceof Headers &&
Reflect.has(args[1].headers, kRawHeaders)
) {
args[1].headers = args[1].headers[kRawHeaders]
}

const response = Reflect.construct(target, args, newTarget)

if (typeof args[1] === 'object' && args[1].headers != null) {
Expand Down
Loading