2018-03-29 17:42:23 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import {
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals,
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2023-06-12 08:44:18 +00:00
|
|
|
launch,
|
2022-06-15 10:09:22 +00:00
|
|
|
} from './mocha-utils.js';
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
describe('Cookie specs', () => {
|
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.cookies', function () {
|
|
|
|
it('should return no cookies in pristine browser context', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals(await page.cookies(), []);
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
2021-03-17 16:09:10 +00:00
|
|
|
it('should get a cookie', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
document.cookie = 'username=John Doe';
|
|
|
|
});
|
2021-03-17 16:09:10 +00:00
|
|
|
|
|
|
|
expectCookieEquals(await page.cookies(), [
|
2020-05-07 10:54:55 +00:00
|
|
|
{
|
|
|
|
name: 'username',
|
|
|
|
value: 'John Doe',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/',
|
2021-02-02 07:39:29 +00:00
|
|
|
sameParty: false,
|
2020-05-07 10:54:55 +00:00
|
|
|
expires: -1,
|
|
|
|
size: 16,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true,
|
2021-02-26 08:12:43 +00:00
|
|
|
sourceScheme: 'NonSecure',
|
2020-05-07 10:54:55 +00:00
|
|
|
},
|
|
|
|
]);
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should properly report httpOnly cookie', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/empty.html', (_req, res) => {
|
2020-04-16 07:20:27 +00:00
|
|
|
res.setHeader('Set-Cookie', 'a=b; HttpOnly; Path=/');
|
2019-04-30 07:35:41 +00:00
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const cookies = await page.cookies();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(cookies).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(cookies[0]!.httpOnly).toBe(true);
|
2019-04-30 07:35:41 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should properly report "Strict" sameSite cookie', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/empty.html', (_req, res) => {
|
2020-04-16 07:20:27 +00:00
|
|
|
res.setHeader('Set-Cookie', 'a=b; SameSite=Strict');
|
2019-04-30 07:35:41 +00:00
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const cookies = await page.cookies();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(cookies).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(cookies[0]!.sameSite).toBe('Strict');
|
2019-04-30 07:35:41 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should properly report "Lax" sameSite cookie', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2022-06-15 10:09:22 +00:00
|
|
|
server.setRoute('/empty.html', (_req, res) => {
|
2020-04-16 07:20:27 +00:00
|
|
|
res.setHeader('Set-Cookie', 'a=b; SameSite=Lax');
|
2019-04-30 07:35:41 +00:00
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const cookies = await page.cookies();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(cookies).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(cookies[0]!.sameSite).toBe('Lax');
|
2019-04-30 07:35:41 +00:00
|
|
|
});
|
2021-03-17 16:09:10 +00:00
|
|
|
it('should get multiple cookies', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => {
|
|
|
|
document.cookie = 'username=John Doe';
|
|
|
|
document.cookie = 'password=1234';
|
|
|
|
});
|
|
|
|
const cookies = await page.cookies();
|
2022-06-15 10:09:22 +00:00
|
|
|
cookies.sort((a, b) => {
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
});
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals(cookies, [
|
2019-02-27 00:24:30 +00:00
|
|
|
{
|
|
|
|
name: 'password',
|
|
|
|
value: '1234',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/',
|
2021-02-02 07:39:29 +00:00
|
|
|
sameParty: false,
|
2019-02-27 00:24:30 +00:00
|
|
|
expires: -1,
|
|
|
|
size: 12,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
2020-04-16 07:20:27 +00:00
|
|
|
session: true,
|
2021-02-26 08:12:43 +00:00
|
|
|
sourceScheme: 'NonSecure',
|
2019-02-27 00:24:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'username',
|
|
|
|
value: 'John Doe',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/',
|
2021-02-02 07:39:29 +00:00
|
|
|
sameParty: false,
|
2019-02-27 00:24:30 +00:00
|
|
|
expires: -1,
|
|
|
|
size: 16,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
2020-04-16 07:20:27 +00:00
|
|
|
session: true,
|
2021-02-26 08:12:43 +00:00
|
|
|
sourceScheme: 'NonSecure',
|
2019-02-27 00:24:30 +00:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should get cookies from multiple urls', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setCookie(
|
|
|
|
{
|
|
|
|
url: 'https://foo.com',
|
|
|
|
name: 'doggo',
|
|
|
|
value: 'woofs',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: 'https://bar.com',
|
|
|
|
name: 'catto',
|
|
|
|
value: 'purrs',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
url: 'https://baz.com',
|
|
|
|
name: 'birdo',
|
|
|
|
value: 'tweets',
|
|
|
|
}
|
|
|
|
);
|
2019-02-27 00:24:30 +00:00
|
|
|
const cookies = await page.cookies('https://foo.com', 'https://baz.com');
|
2022-06-15 10:09:22 +00:00
|
|
|
cookies.sort((a, b) => {
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
});
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals(cookies, [
|
2020-05-07 10:54:55 +00:00
|
|
|
{
|
|
|
|
name: 'birdo',
|
|
|
|
value: 'tweets',
|
|
|
|
domain: 'baz.com',
|
|
|
|
path: '/',
|
2021-02-02 07:39:29 +00:00
|
|
|
sameParty: false,
|
2020-05-07 10:54:55 +00:00
|
|
|
expires: -1,
|
|
|
|
size: 11,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: true,
|
|
|
|
session: true,
|
2021-02-26 08:12:43 +00:00
|
|
|
sourcePort: 443,
|
|
|
|
sourceScheme: 'Secure',
|
2020-05-07 10:54:55 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'doggo',
|
|
|
|
value: 'woofs',
|
|
|
|
domain: 'foo.com',
|
|
|
|
path: '/',
|
2021-02-02 07:39:29 +00:00
|
|
|
sameParty: false,
|
2020-05-07 10:54:55 +00:00
|
|
|
expires: -1,
|
|
|
|
size: 10,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: true,
|
|
|
|
session: true,
|
2021-02-26 08:12:43 +00:00
|
|
|
sourcePort: 443,
|
|
|
|
sourceScheme: 'Secure',
|
2020-05-07 10:54:55 +00:00
|
|
|
},
|
|
|
|
]);
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.setCookie', function () {
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setCookie({
|
|
|
|
name: 'password',
|
2020-05-07 10:54:55 +00:00
|
|
|
value: '123456',
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await page.evaluate(() => {
|
|
|
|
return document.cookie;
|
|
|
|
})
|
|
|
|
).toEqual('password=123456');
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should isolate cookies in browser contexts', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server, browser} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-27 00:24:30 +00:00
|
|
|
const anotherContext = await browser.createIncognitoBrowserContext();
|
|
|
|
const anotherPage = await anotherContext.newPage();
|
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await anotherPage.goto(server.EMPTY_PAGE);
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setCookie({name: 'page1cookie', value: 'page1value'});
|
|
|
|
await anotherPage.setCookie({name: 'page2cookie', value: 'page2value'});
|
2019-02-27 00:24:30 +00:00
|
|
|
|
|
|
|
const cookies1 = await page.cookies();
|
|
|
|
const cookies2 = await anotherPage.cookies();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(cookies1).toHaveLength(1);
|
|
|
|
expect(cookies2).toHaveLength(1);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(cookies1[0]!.name).toBe('page1cookie');
|
|
|
|
expect(cookies1[0]!.value).toBe('page1value');
|
|
|
|
expect(cookies2[0]!.name).toBe('page2cookie');
|
|
|
|
expect(cookies2[0]!.value).toBe('page2value');
|
2019-02-27 00:24:30 +00:00
|
|
|
await anotherContext.close();
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should set multiple cookies', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setCookie(
|
|
|
|
{
|
|
|
|
name: 'password',
|
|
|
|
value: '123456',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'foo',
|
|
|
|
value: 'bar',
|
|
|
|
}
|
|
|
|
);
|
2021-05-12 16:43:05 +00:00
|
|
|
const cookieStrings = await page.evaluate(() => {
|
|
|
|
const cookies = document.cookie.split(';');
|
2022-06-15 10:09:22 +00:00
|
|
|
return cookies
|
2022-06-22 13:25:44 +00:00
|
|
|
.map(cookie => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return cookie.trim();
|
|
|
|
})
|
|
|
|
.sort();
|
2021-05-12 16:43:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(cookieStrings).toEqual(['foo=bar', 'password=123456']);
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
2020-06-12 13:55:51 +00:00
|
|
|
it('should have |expires| set to |-1| for session cookies', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setCookie({
|
|
|
|
name: 'password',
|
|
|
|
value: '123456',
|
|
|
|
});
|
|
|
|
const cookies = await page.cookies();
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(cookies[0]!.session).toBe(true);
|
|
|
|
expect(cookies[0]!.expires).toBe(-1);
|
2020-06-12 13:55:51 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should set cookie with reasonable defaults', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.setCookie({
|
|
|
|
name: 'password',
|
2020-05-07 10:54:55 +00:00
|
|
|
value: '123456',
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
|
|
|
const cookies = await page.cookies();
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals(
|
2022-06-15 10:09:22 +00:00
|
|
|
cookies.sort((a, b) => {
|
|
|
|
return a.name.localeCompare(b.name);
|
|
|
|
}),
|
2021-03-17 16:09:10 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'password',
|
|
|
|
value: '123456',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/',
|
|
|
|
sameParty: false,
|
|
|
|
expires: -1,
|
|
|
|
size: 14,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true,
|
|
|
|
sourcePort: 80,
|
|
|
|
sourceScheme: 'NonSecure',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should set a cookie with a path', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
await page.setCookie({
|
|
|
|
name: 'gridcookie',
|
|
|
|
value: 'GRID',
|
|
|
|
path: '/grid.html',
|
2020-05-07 10:54:55 +00:00
|
|
|
});
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals(await page.cookies(), [
|
2020-05-07 10:54:55 +00:00
|
|
|
{
|
|
|
|
name: 'gridcookie',
|
|
|
|
value: 'GRID',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/grid.html',
|
2021-02-02 07:39:29 +00:00
|
|
|
sameParty: false,
|
2020-05-07 10:54:55 +00:00
|
|
|
expires: -1,
|
|
|
|
size: 14,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true,
|
2021-02-26 08:12:43 +00:00
|
|
|
sourcePort: 80,
|
|
|
|
sourceScheme: 'NonSecure',
|
2020-05-07 10:54:55 +00:00
|
|
|
},
|
|
|
|
]);
|
2018-03-29 17:42:23 +00:00
|
|
|
expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID');
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals(await page.cookies(), []);
|
2018-03-29 17:42:23 +00:00
|
|
|
expect(await page.evaluate('document.cookie')).toBe('');
|
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
|
|
|
expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID');
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should not set a cookie on a blank page', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.goto('about:blank');
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2018-03-29 17:42:23 +00:00
|
|
|
try {
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setCookie({name: 'example-cookie', value: 'best'});
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
2022-06-15 10:09:22 +00:00
|
|
|
error = error_ as Error;
|
2018-03-29 17:42:23 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error.message).toContain(
|
|
|
|
'At least one of the url and domain needs to be specified'
|
|
|
|
);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should not set a cookie with blank page URL', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2018-03-29 17:42:23 +00:00
|
|
|
try {
|
|
|
|
await page.setCookie(
|
2022-06-22 13:25:44 +00:00
|
|
|
{name: 'example-cookie', value: 'best'},
|
|
|
|
{url: 'about:blank', name: 'example-cookie-blank', value: 'best'}
|
2018-03-29 17:42:23 +00:00
|
|
|
);
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
2022-06-15 10:09:22 +00:00
|
|
|
error = error_ as Error;
|
2018-03-29 17:42:23 +00:00
|
|
|
}
|
|
|
|
expect(error.message).toEqual(
|
2020-05-07 10:54:55 +00:00
|
|
|
`Blank page can not have cookie "example-cookie-blank"`
|
2018-03-29 17:42:23 +00:00
|
|
|
);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should not set a cookie on a data URL page', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.goto('data:,Hello%2C%20World!');
|
|
|
|
try {
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setCookie({name: 'example-cookie', value: 'best'});
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error_) {
|
2022-06-15 10:09:22 +00:00
|
|
|
error = error_ as Error;
|
2018-03-29 17:42:23 +00:00
|
|
|
}
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(error.message).toContain(
|
|
|
|
'At least one of the url and domain needs to be specified'
|
|
|
|
);
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should default to setting secure cookie for HTTPS websites', async () => {
|
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const SECURE_URL = 'https://example.com';
|
|
|
|
await page.setCookie({
|
|
|
|
url: SECURE_URL,
|
|
|
|
name: 'foo',
|
|
|
|
value: 'bar',
|
|
|
|
});
|
|
|
|
const [cookie] = await page.cookies(SECURE_URL);
|
|
|
|
expect(cookie!.secure).toBe(true);
|
|
|
|
});
|
2023-01-13 17:03:21 +00:00
|
|
|
it('should be able to set insecure cookie for HTTP website', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-06-12 13:55:51 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const HTTP_URL = 'http://example.com';
|
|
|
|
await page.setCookie({
|
|
|
|
url: HTTP_URL,
|
|
|
|
name: 'foo',
|
|
|
|
value: 'bar',
|
|
|
|
});
|
|
|
|
const [cookie] = await page.cookies(HTTP_URL);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(cookie!.secure).toBe(false);
|
2020-06-12 13:55:51 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should set a cookie on a different domain', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.setCookie({
|
|
|
|
url: 'https://www.example.com',
|
|
|
|
name: 'example-cookie',
|
|
|
|
value: 'best',
|
|
|
|
});
|
2018-03-29 17:42:23 +00:00
|
|
|
expect(await page.evaluate('document.cookie')).toBe('');
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals(await page.cookies(), []);
|
|
|
|
expectCookieEquals(await page.cookies('https://www.example.com'), [
|
2020-05-07 10:54:55 +00:00
|
|
|
{
|
|
|
|
name: 'example-cookie',
|
|
|
|
value: 'best',
|
|
|
|
domain: 'www.example.com',
|
|
|
|
path: '/',
|
2021-02-02 07:39:29 +00:00
|
|
|
sameParty: false,
|
2020-05-07 10:54:55 +00:00
|
|
|
expires: -1,
|
|
|
|
size: 18,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: true,
|
|
|
|
session: true,
|
2021-02-26 08:12:43 +00:00
|
|
|
sourcePort: 443,
|
|
|
|
sourceScheme: 'Secure',
|
2020-05-07 10:54:55 +00:00
|
|
|
},
|
|
|
|
]);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should set cookies from a frame', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2018-03-29 17:42:23 +00:00
|
|
|
await page.goto(server.PREFIX + '/grid.html');
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.setCookie({name: 'localhost-cookie', value: 'best'});
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
await page.evaluate(src => {
|
2022-06-15 10:09:22 +00:00
|
|
|
let fulfill!: () => void;
|
2022-06-22 13:25:44 +00:00
|
|
|
const promise = new Promise<void>(x => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (fulfill = x);
|
|
|
|
});
|
feat!: type inference for evaluation types (#8547)
This PR greatly improves the types within Puppeteer:
- **Almost everything** is auto-deduced.
- Parameters don't need to be specified in the function. They are deduced from the spread.
- Return types don't need to be specified. They are deduced from the function. (More on this below)
- Selections based on tag names correctly deduce element type, similar to TypeScript's mechanism for `getElementByTagName`.
- [**BREAKING CHANGE**] We've removed the ability to declare return types in type arguments for the following reasons:
1. Setting them will indubitably break auto-deduction.
2. You can just use `as ...` in TypeScript to coerce the correct type (given it makes sense).
- [**BREAKING CHANGE**] `waitFor` is officially gone.
To migrate to these changes, there are only four things you may need to change:
- If you set a return type using the `ReturnType` type parameter, remove it and use `as ...` and `HandleFor` (if necessary).
⛔ `evaluate<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluate(a, b) => {...}, a, b)) as ReturnType`
⛔ `evaluateHandle<ReturnType>(a: number, b: number) => {...}, a, b)`
✅ `(await evaluateHandle(a, b) => {...}, a, b)) as HandleFor<ReturnType>`
- If you set any type parameters in the *parameters* of an evaluation function, remove them.
⛔ `evaluate(a: number, b: number) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
- If you set any type parameters in the method's declaration, remove them.
⛔ `evaluate<(a: number, b: number) => void>((a, b) => {...}, a, b)`
✅ `evaluate(a, b) => {...}, a, b)`
2022-06-23 09:29:46 +00:00
|
|
|
const iframe = document.createElement('iframe');
|
2018-03-29 17:42:23 +00:00
|
|
|
document.body.appendChild(iframe);
|
|
|
|
iframe.onload = fulfill;
|
|
|
|
iframe.src = src;
|
|
|
|
return promise;
|
|
|
|
}, server.CROSS_PROCESS_PREFIX);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setCookie({
|
2018-03-29 17:42:23 +00:00
|
|
|
name: '127-cookie',
|
|
|
|
value: 'worst',
|
2020-05-07 10:54:55 +00:00
|
|
|
url: server.CROSS_PROCESS_PREFIX,
|
|
|
|
});
|
|
|
|
expect(await page.evaluate('document.cookie')).toBe(
|
|
|
|
'localhost-cookie=best'
|
|
|
|
);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(await page.frames()[1]!.evaluate('document.cookie')).toBe('');
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals(await page.cookies(), [
|
2020-05-07 10:54:55 +00:00
|
|
|
{
|
|
|
|
name: 'localhost-cookie',
|
|
|
|
value: 'best',
|
|
|
|
domain: 'localhost',
|
|
|
|
path: '/',
|
2021-02-02 07:39:29 +00:00
|
|
|
sameParty: false,
|
2020-05-07 10:54:55 +00:00
|
|
|
expires: -1,
|
|
|
|
size: 20,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true,
|
2021-02-26 08:12:43 +00:00
|
|
|
sourcePort: 80,
|
|
|
|
sourceScheme: 'NonSecure',
|
2020-05-07 10:54:55 +00:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
2021-03-17 16:09:10 +00:00
|
|
|
expectCookieEquals(await page.cookies(server.CROSS_PROCESS_PREFIX), [
|
2020-05-07 10:54:55 +00:00
|
|
|
{
|
|
|
|
name: '127-cookie',
|
|
|
|
value: 'worst',
|
|
|
|
domain: '127.0.0.1',
|
|
|
|
path: '/',
|
2021-02-02 07:39:29 +00:00
|
|
|
sameParty: false,
|
2020-05-07 10:54:55 +00:00
|
|
|
expires: -1,
|
|
|
|
size: 15,
|
|
|
|
httpOnly: false,
|
|
|
|
secure: false,
|
|
|
|
session: true,
|
2021-02-26 08:12:43 +00:00
|
|
|
sourcePort: 80,
|
|
|
|
sourceScheme: 'NonSecure',
|
2020-05-07 10:54:55 +00:00
|
|
|
},
|
|
|
|
]);
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should set secure same-site cookies from a frame', async () => {
|
2023-06-12 08:44:18 +00:00
|
|
|
const {httpsServer, defaultBrowserOptions} = getTestState();
|
2020-05-18 09:06:42 +00:00
|
|
|
|
2023-06-12 08:44:18 +00:00
|
|
|
const {browser, close} = await launch({
|
2022-09-08 10:32:39 +00:00
|
|
|
...defaultBrowserOptions,
|
|
|
|
ignoreHTTPSErrors: true,
|
|
|
|
});
|
2020-05-18 09:06:42 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
try {
|
2023-06-12 08:44:18 +00:00
|
|
|
const page = await browser.newPage();
|
2022-09-08 10:32:39 +00:00
|
|
|
await page.goto(httpsServer.PREFIX + '/grid.html');
|
|
|
|
await page.evaluate(src => {
|
|
|
|
let fulfill!: () => void;
|
|
|
|
const promise = new Promise<void>(x => {
|
|
|
|
return (fulfill = x);
|
2020-05-18 09:06:42 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
const iframe = document.createElement('iframe');
|
|
|
|
document.body.appendChild(iframe);
|
|
|
|
iframe.onload = fulfill;
|
|
|
|
iframe.src = src;
|
|
|
|
return promise;
|
|
|
|
}, httpsServer.CROSS_PROCESS_PREFIX);
|
|
|
|
await page.setCookie({
|
|
|
|
name: '127-same-site-cookie',
|
|
|
|
value: 'best',
|
|
|
|
url: httpsServer.CROSS_PROCESS_PREFIX,
|
|
|
|
sameSite: 'None',
|
|
|
|
});
|
2020-05-18 09:06:42 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
expect(await page.frames()[1]!.evaluate('document.cookie')).toBe(
|
|
|
|
'127-same-site-cookie=best'
|
|
|
|
);
|
|
|
|
expectCookieEquals(
|
|
|
|
await page.cookies(httpsServer.CROSS_PROCESS_PREFIX),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: '127-same-site-cookie',
|
|
|
|
value: 'best',
|
|
|
|
domain: '127.0.0.1',
|
|
|
|
path: '/',
|
|
|
|
sameParty: false,
|
|
|
|
expires: -1,
|
|
|
|
size: 24,
|
|
|
|
httpOnly: false,
|
|
|
|
sameSite: 'None',
|
|
|
|
secure: true,
|
|
|
|
session: true,
|
|
|
|
sourcePort: 443,
|
|
|
|
sourceScheme: 'Secure',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} finally {
|
2023-06-12 08:44:18 +00:00
|
|
|
await close();
|
2020-05-18 09:06:42 +00:00
|
|
|
}
|
2022-09-08 10:32:39 +00:00
|
|
|
});
|
2019-02-27 00:24:30 +00:00
|
|
|
});
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Page.deleteCookie', function () {
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should work', async () => {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {page, server} = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-27 00:24:30 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.setCookie(
|
|
|
|
{
|
|
|
|
name: 'cookie1',
|
|
|
|
value: '1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'cookie2',
|
|
|
|
value: '2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'cookie3',
|
|
|
|
value: '3',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
expect(await page.evaluate('document.cookie')).toBe(
|
|
|
|
'cookie1=1; cookie2=2; cookie3=3'
|
|
|
|
);
|
2022-06-22 13:25:44 +00:00
|
|
|
await page.deleteCookie({name: 'cookie2'});
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await page.evaluate('document.cookie')).toBe(
|
|
|
|
'cookie1=1; cookie3=3'
|
|
|
|
);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|