Skip to content
Closed
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
4 changes: 2 additions & 2 deletions src/interceptors/ClientRequest/utils/recordRawHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ type HeaderTuple = [string, string]
type RawHeaders = Array<HeaderTuple>
type SetHeaderBehavior = 'set' | 'append'

const kRawHeaders = Symbol('kRawHeaders')
export const kRawHeaders = Symbol('kRawHeaders')
const kRestorePatches = Symbol('kRestorePatches')

function recordRawHeader(
Expand All @@ -29,7 +29,7 @@ function recordRawHeader(
* Define the raw headers symbol on the given `Headers` instance.
* If the symbol already exists, this function does nothing.
*/
function ensureRawHeadersSymbol(
export function ensureRawHeadersSymbol(
headers: Headers,
rawHeaders: RawHeaders
): void {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/fetchUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ensureRawHeadersSymbol, kRawHeaders } from "../interceptors/ClientRequest/utils/recordRawHeaders"

export interface FetchResponseInit extends ResponseInit {
url?: string
}
Expand Down Expand Up @@ -89,5 +91,6 @@ export class FetchResponse extends Response {
}

FetchResponse.setUrl(init.url, this)
ensureRawHeadersSymbol(this.headers, Reflect.get(init.headers || {}, kRawHeaders) || [])
}
}
2 changes: 2 additions & 0 deletions test/features/events/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { HttpRequestEventMap } from '../../../src'
import { XMLHttpRequestInterceptor } from '../../../src/interceptors/XMLHttpRequest'
import { BatchInterceptor } from '../../../src/BatchInterceptor'
import { ClientRequestInterceptor } from '../../../src/interceptors/ClientRequest'
import { kRawHeaders } from '../../../src/interceptors/ClientRequest/utils/recordRawHeaders'
import { FetchInterceptor } from '../../../src/interceptors/fetch'
import {
useCors,
Expand Down Expand Up @@ -148,6 +149,7 @@ it('ClientRequest: emits the "response" event upon the original response', async
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.headers.get('x-response-type')).toBe('original')
expect(Reflect.get(response.headers, kRawHeaders)).toContainEqual(['x-response-type', 'original'])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The only thing I don't quite like about this change is this assertion. We shouldn't tap into internal symbols anywhere.

Is there any other way to check that the fix is working?

Copy link
Copy Markdown
Member Author

@mikicho mikicho Apr 7, 2025

Choose a reason for hiding this comment

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

I can't think of another way. This is what I plan to do in nock as well, to extract the rawHeaders from the response instance in the response listener.

Do you have another direction, even a general one?

await expect(response.text()).resolves.toBe('original-response-text')

expect(isMockedResponse).toBe(false)
Expand Down