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(setupWorker): correctly delete internal accept header on passthrough #2375

Conversation

smouillour
Copy link
Contributor

@smouillour smouillour commented Nov 29, 2024

the function delete for Headers manage only one parameter.
what we try to do there it's to remove the value msw/passthrough to the header accept.

change will check content of accept and:

  • if the header contains only msw/passthrough then it will be removed
  • if the header contains multiple value than it will update the header to remove msw/passthrough inside
  • if the header not contains the value msw/passthrough then nothing is done

the function delete for Headers manage only one parameter. what we try to do there it's to remove
accept when it's value is equal to msw/passthrough
@smouillour smouillour closed this Nov 29, 2024
@smouillour smouillour reopened this Nov 29, 2024
@kettanaito
Copy link
Member

Hi, @smouillour. Thanks for spotting this. That's a good catch. In Node.js, you can use Headers.prototype.delete to delete a particular header value (and only that value). That's not the case for the Web API though.

Your proposal is okay if the accept header was used only by MSW, but it won't work if that internal header value was appended to an already existing accept header.

To properly solve this, we need to parse the accept header in case it's multi-valued, then remove the internal MSW header value, then stitch the remaining value back. If it's not an empty string after these transformations, rewrite the accept header to whichever string remains. If it is an empty string, delete the accept header altogether.

How does that sound?

@kettanaito kettanaito changed the title fix(mockserviceworker): wrong usage of headers.delete fix(worker): wrong usage of headers.delete Dec 1, 2024
@kettanaito kettanaito changed the title fix(worker): wrong usage of headers.delete fix(worker): correctly delete internal accept header on passthrough Dec 1, 2024
@smouillour
Copy link
Contributor Author

Hi @kettanaito,
It's clear. I will update my PR following this.

Previous fix remove the accept header if it was equal to msw/passthrough. Now we will check the
content of it and remove the value msw/passthrough if it inside. if there is only this value the
header accept is removed
@kettanaito
Copy link
Member

This looks great, thank you!

One thing remaining is to add a test for this since we didn't catch this before. Would you like to give it a try? If not that's okay, I can add a test when I have some time this/next week.

@smouillour
Copy link
Contributor Author

I added tests.
I had to export getResponse to do that as I used it as starting point for the test.
Hoping that everything will be okay and that will be enough

@kettanaito
Copy link
Member

Thanks, @smouillour. We usually take a different approach to testing worker-related things, that being relying on e2d tests (build -> run in the browser). I've reverted your test and added the in-browser test instead. If you are curious, take a look to see the difference (mostly that it's implementation-agnostic now).

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Looks great! 🚀

@kettanaito kettanaito added release candidate scope:browser Related to MSW running in a browser labels Dec 5, 2024
@kettanaito kettanaito changed the title fix(worker): correctly delete internal accept header on passthrough fix(setupWorker): correctly delete internal accept header on passthrough Dec 5, 2024
@kettanaito kettanaito merged commit 3f40055 into mswjs:main Dec 5, 2024
16 checks passed
@kettanaito
Copy link
Member

Welcome to contributors, @smouillour 👏

@kettanaito
Copy link
Member

Released: v2.6.7 🎉

This has been released in v2.6.7!

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


Predictable release automation by @ossjs/release.

@mckelveygreg
Copy link

Oh my goodness, thank you for finding this! I've been trying to figure this one out for a few months now. We use testcontainers, and I think this logic was messing with a bunch of stuff I have to passthrough in order to get to the system's docker process.

Our tests finally passed again! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release candidate scope:browser Related to MSW running in a browser
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants