-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix(routeFromHAR): hack harRouter to merge set-cookie headers #38255
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
base: main
Are you sure you want to change the base?
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.
| // 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) |
There was a problem hiding this comment.
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 }) => { |
There was a problem hiding this comment.
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();
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.