Skip to content
Closed
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
43 changes: 41 additions & 2 deletions packages/playwright-core/src/utils/isomorphic/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ export function headersObjectToArray(headers: HeadersObject, separator?: string,
if (values === undefined)
continue;
if (separator) {
const sep = name.toLowerCase() === 'set-cookie' ? setCookieSeparator : separator;
for (const value of values.split(sep!))
const lowerCaseName = name.toLowerCase();
const sep = lowerCaseName === 'set-cookie' ? setCookieSeparator : separator;
const splitValues = lowerCaseName === 'set-cookie' ? splitSetCookieHeader(values, sep!) : values.split(sep!);
for (const value of splitValues)
result.push({ name, value: value.trim() });
} else {
result.push({ name, value: values });
Expand All @@ -42,3 +44,40 @@ export function headersArrayToObject(headers: HeadersArray, lowerCase: boolean):
result[lowerCase ? name.toLowerCase() : name] = value;
return result;
}

function splitSetCookieHeader(value: string, separator: string): string[] {
if (!separator || !value.includes(separator))
return [value];
if (separator !== ',')
return value.split(separator);
const result: string[] = [];
let lastIndex = 0;
let inExpires = false;
const lowerValue = value.toLowerCase();
for (let i = 0; i < value.length; ++i) {
if (!inExpires && lowerValue.startsWith('expires=', i))
inExpires = true;
if (value[i] === ';')
inExpires = false;
if (value[i] === ',' && !inExpires && looksLikeCookieStart(value, i + 1)) {
result.push(value.substring(lastIndex, i).trim());
lastIndex = i + 1;
}
}
result.push(value.substring(lastIndex).trim());
return result.filter(v => v);
}

function looksLikeCookieStart(header: string, start: number): boolean {
const rest = header.substring(start).trimStart();
const eqIndex = rest.indexOf('=');
if (eqIndex <= 0)
return false;
const semicolonIndex = rest.indexOf(';');
if (semicolonIndex !== -1 && semicolonIndex < eqIndex)
return false;
const name = rest.substring(0, eqIndex).trim();
if (!name)
return false;
return !/[\s;,]/.test(name);
}
35 changes: 35 additions & 0 deletions tests/library/unit/headers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { test, expect } from '@playwright/test';
import { headersObjectToArray } from '../../../packages/playwright-core/src/utils/isomorphic/headers';
Copy link
Member

Choose a reason for hiding this comment

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

We normally don't test internals with unit tests and prefer end-to-end tests that call public APIs.


test.describe('headersObjectToArray set-cookie splitting', () => {
test('keeps expires date comma intact for single cookie', () => {
const value = 'id=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Path=/';
const result = headersObjectToArray({ 'Set-Cookie': value }, ',', ',');
expect(result).toEqual([{ name: 'Set-Cookie', value }]);
});

test('splits multiple cookies combined by WebKit comma separator', () => {
const value = 'id=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Path=/, session=abc; Secure';
const result = headersObjectToArray({ 'Set-Cookie': value }, ',', ',');
expect(result).toEqual([
{ name: 'Set-Cookie', value: 'id=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Path=/' },
{ name: 'Set-Cookie', value: 'session=abc; Secure' },
]);
});
});