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): improve rawHeaders recording #602

Merged
merged 8 commits into from
Jul 12, 2024

Conversation

mikicho
Copy link
Contributor

@mikicho mikicho commented Jul 11, 2024

@kettanaito The problem was that we didn't unset the kRestorePatches symbol on dispose

I think my solution may not be "private" enough because I set it to be configurable. Alternatively, we can conditionally return undefined by toggling a variable, but it's smelly and error-prone.
What is the best solution here?

@mikicho mikicho changed the title fix headers reapply fix(ClientRequest): unset kRestorePatches on dispose Jul 11, 2024
@mikicho mikicho requested a review from kettanaito July 12, 2024 13:31
@kettanaito
Copy link
Member

Some tests are failing 🤦 Forgot to test on latest v18/v20.

@kettanaito
Copy link
Member

There's a breaking change in Headers behavior between the minor versions of v18 of Node. The latest v18 works as expected. I will suggest people to update to the latest if they encounter any issues.

expect(getRawFetchHeaders(response.headers)).toEqual([['X-My-Header', '1']])
})

it('stops recording once the patches are restored', () => {
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 wondering if we need the test I wrote if we have this test

Copy link
Member

Choose a reason for hiding this comment

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

Unit vs integration. Let's keep yours. It tests that ClientRequest interceptors applies/restores the headers patching. The unit tests don't test that.

@kettanaito
Copy link
Member

Fixed the compatibility with the latest v18/v20. The tests are finally green again.

@mikicho, could you please give these changes another look once you have a moment? Thank you.

@kettanaito kettanaito changed the title fix(ClientRequest): unset kRestorePatches on dispose fix(ClientRequest): improve rawHeaders recording Jul 12, 2024
const request = Reflect.construct(target, args, newTarget)

if (typeof args[1] === 'object' && args[1].headers != null) {
defineRawHeaders(request.headers, inferRawHeaders(args[1].headers))
Copy link
Member

Choose a reason for hiding this comment

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

Latest Node.js v18 and v20 does not copy Headers symbols (previous versions did!). We need to manually copy the kRawHeaders symbol (or set it by creating a new Headers instance) when constructing Request/Response instances.

@mikicho
Copy link
Contributor Author

mikicho commented Jul 12, 2024

@kettanaito LGTM

@kettanaito
Copy link
Member

Forgot to add unit tests for .append() and .set(). Will add and merge today/tomorrow /cc @mikicho

@kettanaito kettanaito merged commit 5135505 into main Jul 12, 2024
2 checks passed
@kettanaito kettanaito deleted the Michael/fix-headers-apply-dispose branch July 12, 2024 15:48
@kettanaito
Copy link
Member

Released: v0.33.1 🎉

This has been released in v0.33.1!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants