fix(routeFromHAR): hack harRouter to merge set-cookie headers#38255
Closed
yepitschunked wants to merge 1 commit intomicrosoft:mainfrom
Closed
fix(routeFromHAR): hack harRouter to merge set-cookie headers#38255yepitschunked wants to merge 1 commit intomicrosoft:mainfrom
yepitschunked wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Route.fulfill currently does not support multiple headers with the same name (microsoft#37342). There are workarounds when using this API directly (merging headers, tweaking the header name casing, etc.), but this is problematic for routeFromHAR, which depends on this API internally. This patch adds special handling for set-cookie headers within harRouter to merge them into one header. There's some precedent for treating set-cookie specially at various places in the codebase (ex: https://github.com/microsoft/playwright/blob/f54478a23e0daa450fe524905eabc8aabf6efb07/packages/playwright-core/src/utils/isomorphic/headers.ts#L29, https://github.com/microsoft/playwright/blob/baeb065e9ea84502f347129a0b896a85d2a8dada/packages/playwright-core/src/server/chromium/crNetworkManager.ts#L675), so I think this is okay.
yury-s
reviewed
Nov 20, 2025
| // route.fulfill does not support multiple set-cookie headers. We need to merge them into one. | ||
| const setCookieHeaders = response.headers!.filter(h => h.name.toLowerCase() === 'set-cookie'); | ||
| const transformedHeaders = response.headers!.filter(h => h.name.toLowerCase() !== 'set-cookie'); | ||
| if (setCookieHeaders.length > 1) |
Member
There was a problem hiding this comment.
This will delete set-cookie header if there is only one entry. Let's add a test for that case. Also let's not take original headers array if there is no set-cookie header.
| expect(await page2.evaluate(fetchFunction, { path: '/echo', body: '12' })).toBe('12'); | ||
| }); | ||
|
|
||
| it('should replay requests with multiple set-cookie headers properly', async ({ context, asset }) => { |
Member
There was a problem hiding this comment.
Let's also add a test for recording and replaying a multicookie:
diff --git i/tests/library/browsercontext-har.spec.ts w/tests/library/browsercontext-har.spec.ts
index 6e6404590..4f5c39581 100644
--- i/tests/library/browsercontext-har.spec.ts
+++ w/tests/library/browsercontext-har.spec.ts
@@ -452,6 +452,33 @@ it('should ignore boundary when matching multipart/form-data body', {
await expect(page2.locator('div')).toHaveText('done');
});
+it('should record multiple set-cookie headers', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/31495' }
+}, async ({ contextFactory, server }, testInfo) => {
+ server.setRoute('/empty.html', (req, res) => {
+ res.setHeader('Content-Type', 'text/html');
+ res.setHeader('set-cookie', ['first=foo', 'second=bar']);
+ res.end();
+ });
+
+ const harPath = testInfo.outputPath('har.zip');
+ console.log('HAR path:', harPath);
+ const context1 = await contextFactory();
+ await context1.routeFromHAR(harPath, { update: true });
+ const page1 = await context1.newPage();
+ await page1.goto(server.EMPTY_PAGE);
+ const cookie1 = await page1.evaluate(() => document.cookie);
+ expect(cookie1.split('; ').sort().join('; ')).toBe('first=foo; second=bar');
+ await context1.close();
+
+ const context2 = await contextFactory();
+ await context2.routeFromHAR(harPath, { notFound: 'abort' });
+ const page2 = await context2.newPage();
+ await page2.goto(server.EMPTY_PAGE);
+ const cookie2 = await page2.evaluate(() => document.cookie);
+ expect(cookie2.split('; ').sort().join('; ')).toBe('first=foo; second=bar');
+});
+
it('should update har.zip for page', async ({ contextFactory, server }, testInfo) => {
const harPath = testInfo.outputPath('har.zip');
const context1 = await contextFactory();
Member
|
Closing as abandoned |
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.
Route.fulfill currently does not support multiple headers with the same name (#37342). There are workarounds when using this API directly (merging headers, tweaking the header name casing, etc.), but this is problematic for routeFromHAR, which depends on
this API internally. This patch adds special handling for set-cookie headers within harRouter to merge them into one header. There's some precedent for treating set-cookie specially at various places in the codebase:
playwright/packages/playwright-core/src/utils/isomorphic/headers.ts
Line 29 in f54478a
playwright/packages/playwright-core/src/server/chromium/crNetworkManager.ts
Line 675 in baeb065
so I think this is okay.