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
21 changes: 20 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,28 @@ 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 transformedHeaders = response.headers!.reduce((headersMap, { name, value }) => {
if (name.toLowerCase() !== 'set-cookie') {
// non-set-cookie header gets set as-is
headersMap[name] = value;
} else {
// first set-cookie header gets included as-is
if (!headersMap['set-cookie'])
headersMap['set-cookie'] = value;
else
// subsequent set-cookie headers get appended to existing header
headersMap['set-cookie'] += `\n${value}`;

}
return headersMap;
}, {} as Record<string, string>);

await route.fulfill({
status: response.status,
headers: Object.fromEntries(response.headers!.map(h => [h.name, h.value])),
headers: transformedHeaders,
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
63 changes: 63 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 }) => {
Copy link
Author

Choose a reason for hiding this comment

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

Not sure this test is still necessary with the multi-cookie test below (seems like that one both records and replays a HAR)

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 remove it and revert the manual changes to har-fulfill.har

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 Expand Up @@ -452,6 +460,61 @@ it('should ignore boundary when matching multipart/form-data body', {
await expect(page2.locator('div')).toHaveText('done');
});

it('should record single 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']);
res.end();
});

const harPath = testInfo.outputPath('har.zip');
console.log('HAR path:', harPath);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
expect(cookie1.split('; ').sort().join('; ')).toBe('first=foo');
expect(cookie1).toBe('first=foo');

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');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
expect(cookie2.split('; ').sort().join('; ')).toBe('first=foo');
expect(cookie2).toBe('first=foo');

});

it('should record multiple set-cookie headers', {
Copy link
Author

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

Suggested change
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();
Expand Down
Loading