feat: handle multiple Set-Cookie headers#108
Open
uncrft wants to merge 1 commit intovalendres:mainfrom
Open
Conversation
Set-Cookie headersSet-Cookie headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
mswallows setting cookies in mock handlers by setting theSet-Cookieheader.As long as a single
Set-Cookieheader is set,playwright-mswhandles it perfectly. However,mswalso accepts aHeadersobject. The spec allows storing multiple entries of the same header stored as comma-separated strings. These multi-entries headers can be constructed usingappend. It's such a common use-case that the spec includes agetSetCookieutility function to retrieve the array ofSet-Cookieheaders.And this is where the issue arises: Playwright's
route.fulfillfunction expects aRecord<string, string>for the headers, but it does not handle comma-separated values. The current implementation iterates over the header entries from the mocked response to create an object compliant withroute.fulfill, as a consequence only the last entry of each header is accounted for.Proposed solution
Playwright exposes the
BrowserContext.addCookiesfunction which could be used to set cookies instead of relying onroute.fulfill. However the function does not handle cookie strings, but rather a parsed version. I implemented aparseSetCookieHeadesfunction converting the mocked response headers into aBrowserContext.addCookiescompliant object, and updated theonMockedResponsecallback ofmsw'shandleRequestfunction to handleSet-Cookieheaders separately from the other headers.