Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/playwright-core/src/client/harRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ export class HarRouter {
// test when HAR was recorded but we'd abort it immediately.
if (response.status === -1)
return;

// 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)
transformedHeaders.push({ name: 'set-cookie', value: setCookieHeaders.map(h => h.value).join('\n') });

await route.fulfill({
status: response.status,
headers: Object.fromEntries(response.headers!.map(h => [h.name, h.value])),
headers: Object.fromEntries(transformedHeaders.map(h => [h.name, h.value])),
body: response.body!
});
return;
Expand Down
8 changes: 8 additions & 0 deletions tests/assets/har-fulfill.har
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
{
"name": "content-type",
"value": "text/html"
},
{
"name": "Set-Cookie",
"value": "playwright=works;"
},
{
"name": "Set-Cookie",
"value": "with=multiple-set-cookie-headers;"
}
],
"content": {
Expand Down
8 changes: 8 additions & 0 deletions tests/library/browsercontext-har.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ it('should record overridden requests to har', async ({ contextFactory, server }
expect(await page2.evaluate(fetchFunction, { path: '/echo', body: '12' })).toBe('12');
});

it('should replay requests with multiple set-cookie headers properly', async ({ context, asset }) => {
const path = asset('har-fulfill.har');
await context.routeFromHAR(path);
const page = await context.newPage();
await page.goto('http://no.playwright/');
expect(await page.context().cookies()).toEqual([expect.objectContaining({ name: 'playwright', value: 'works' }), expect.objectContaining({ name: 'with', value: 'multiple-set-cookie-headers' })]);
});

it('should disambiguate by header', async ({ contextFactory, server }, testInfo) => {
server.setRoute('/echo', async (req, res) => {
res.end(req.headers['baz']);
Expand Down