Skip to content

Conversation

@yepitschunked
Copy link

@yepitschunked yepitschunked commented Nov 17, 2025

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:

const sep = name.toLowerCase() === 'set-cookie' ? setCookieSeparator : separator;

function splitSetCookieHeader(headers: types.HeadersArray): types.HeadersArray {

so I think this is okay.

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.
// 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)
Copy link
Member

Choose a reason for hiding this comment

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

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 }) => {
Copy link
Member

Choose a reason for hiding this comment

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

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();

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