2021-09-30 03:15:32 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation. 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.
|
|
|
|
*/
|
|
|
|
|
2023-01-06 01:39:49 +03:00
|
|
|
import type { LookupAddress } from 'dns';
|
2021-10-01 00:14:29 +03:00
|
|
|
import fs from 'fs';
|
2021-10-19 17:38:27 +03:00
|
|
|
import type { APIRequestContext } from 'playwright-core';
|
2022-03-26 02:05:50 +03:00
|
|
|
import { expect, playwrightTest } from '../config/browserTest';
|
2021-09-30 03:15:32 +03:00
|
|
|
|
|
|
|
export type GlobalFetchFixtures = {
|
2022-10-12 09:06:16 +03:00
|
|
|
request: APIRequestContext;
|
|
|
|
};
|
2021-09-30 03:15:32 +03:00
|
|
|
|
|
|
|
const it = playwrightTest.extend<GlobalFetchFixtures>({
|
|
|
|
request: async ({ playwright }, use) => {
|
2021-10-06 04:53:19 +03:00
|
|
|
const request = await playwright.request.newContext({ ignoreHTTPSErrors: true });
|
2021-09-30 03:15:32 +03:00
|
|
|
await use(request);
|
|
|
|
await request.dispose();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-10-01 00:14:29 +03:00
|
|
|
type PromiseArg<T> = T extends Promise<infer R> ? R : never;
|
2021-10-19 17:38:27 +03:00
|
|
|
type StorageStateType = PromiseArg<ReturnType<APIRequestContext['storageState']>>;
|
2021-10-01 00:14:29 +03:00
|
|
|
|
2021-09-30 03:15:32 +03:00
|
|
|
it.skip(({ mode }) => mode !== 'default');
|
|
|
|
|
2023-01-06 01:39:49 +03:00
|
|
|
const __testHookLookup = (hostname: string): LookupAddress[] => {
|
|
|
|
if (hostname === 'localhost' || hostname.endsWith('one.com') || hostname.endsWith('two.com'))
|
|
|
|
return [{ address: '127.0.0.1', family: 4 }];
|
|
|
|
else
|
|
|
|
throw new Error(`Failed to resolve hostname: ${hostname}`);
|
|
|
|
};
|
2021-09-30 03:15:32 +03:00
|
|
|
|
|
|
|
it('should store cookie from Set-Cookie header', async ({ request, server }) => {
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['a=b', 'c=d; max-age=3600; domain=b.one.com; path=/input', 'e=f; domain=b.one.com; path=/input/subfolder']);
|
|
|
|
res.end();
|
|
|
|
});
|
2023-01-06 01:39:49 +03:00
|
|
|
await request.get(`http://a.b.one.com:${server.PORT}/setcookie.html`, { __testHookLookup } as any);
|
2021-09-30 03:15:32 +03:00
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/input/button.html'),
|
2023-01-06 01:39:49 +03:00
|
|
|
request.get(`http://b.one.com:${server.PORT}/input/button.html`, { __testHookLookup } as any)
|
2021-09-30 03:15:32 +03:00
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('c=d');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should filter outgoing cookies by path', async ({ request, server }) => {
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['a=v; path=/input/subfolder', 'b=v; path=/input', 'c=v;']);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
await request.get(`${server.PREFIX}/setcookie.html`);
|
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/input/button.html'),
|
|
|
|
request.get(`${server.PREFIX}/input/button.html`)
|
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('b=v; c=v');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should filter outgoing cookies by domain', async ({ request, server }) => {
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['a=v; domain=one.com', 'b=v; domain=.b.one.com', 'c=v; domain=other.com']);
|
|
|
|
res.end();
|
|
|
|
});
|
2023-01-06 01:39:49 +03:00
|
|
|
await request.get(`http://a.b.one.com:${server.PORT}/setcookie.html`, { __testHookLookup } as any);
|
2021-09-30 03:15:32 +03:00
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
2023-01-06 01:39:49 +03:00
|
|
|
request.get(`http://www.b.one.com:${server.PORT}/empty.html`, { __testHookLookup } as any)
|
2021-09-30 03:15:32 +03:00
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('a=v; b=v');
|
|
|
|
|
|
|
|
const [serverRequest2] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
2023-01-06 01:39:49 +03:00
|
|
|
request.get(`http://two.com:${server.PORT}/empty.html`, { __testHookLookup } as any)
|
2021-09-30 03:15:32 +03:00
|
|
|
]);
|
|
|
|
expect(serverRequest2.headers.cookie).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do case-insensitive match of cookie domain', async ({ request, server }) => {
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['a=v; domain=One.com', 'b=v; domain=.B.oNe.com']);
|
|
|
|
res.end();
|
|
|
|
});
|
2023-01-06 01:39:49 +03:00
|
|
|
await request.get(`http://a.b.one.com:${server.PORT}/setcookie.html`, { __testHookLookup } as any);
|
2021-09-30 03:15:32 +03:00
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
2023-01-06 01:39:49 +03:00
|
|
|
request.get(`http://www.b.one.com:${server.PORT}/empty.html`, { __testHookLookup } as any)
|
2021-09-30 03:15:32 +03:00
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('a=v; b=v');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should do case-insensitive match of request domain', async ({ request, server }) => {
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['a=v; domain=one.com', 'b=v; domain=.b.one.com']);
|
|
|
|
res.end();
|
|
|
|
});
|
2023-01-06 01:39:49 +03:00
|
|
|
await request.get(`http://a.b.one.com:${server.PORT}/setcookie.html`, { __testHookLookup } as any);
|
2021-09-30 03:15:32 +03:00
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
2023-01-06 01:39:49 +03:00
|
|
|
request.get(`http://WWW.B.ONE.COM:${server.PORT}/empty.html`, { __testHookLookup } as any)
|
2021-09-30 03:15:32 +03:00
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('a=v; b=v');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should send secure cookie over https', async ({ request, server, httpsServer }) => {
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['a=v; secure', 'b=v']);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
await request.get(`${server.PREFIX}/setcookie.html`);
|
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
httpsServer.waitForRequest('/empty.html'),
|
|
|
|
request.get(httpsServer.EMPTY_PAGE)
|
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('a=v; b=v');
|
|
|
|
});
|
|
|
|
|
2022-03-02 20:33:30 +03:00
|
|
|
it('should send secure cookie over http for localhost', async ({ request, server }) => {
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['a=v; secure', 'b=v']);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
await request.get(`${server.PREFIX}/setcookie.html`);
|
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
|
|
|
request.get(server.EMPTY_PAGE)
|
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('a=v; b=v');
|
|
|
|
});
|
|
|
|
|
2021-09-30 03:15:32 +03:00
|
|
|
it('should send not expired cookies', async ({ request, server }) => {
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
const tomorrow = new Date();
|
|
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
res.setHeader('Set-Cookie', ['a=v', `b=v; expires=${tomorrow.toUTCString()}`]);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
await request.get(`${server.PREFIX}/setcookie.html`);
|
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
|
|
|
request.get(server.EMPTY_PAGE)
|
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('a=v; b=v');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove expired cookies', async ({ request, server }) => {
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['a=v', `b=v; expires=${new Date().toUTCString()}`]);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
await request.get(`${server.PREFIX}/setcookie.html`);
|
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
|
|
|
request.get(server.EMPTY_PAGE)
|
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('a=v');
|
|
|
|
});
|
|
|
|
|
2022-01-26 22:27:43 +03:00
|
|
|
it('should store cookie from Set-Cookie header even if it contains equal signs', async ({ request, server }) => {
|
|
|
|
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/11612' });
|
|
|
|
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['f=value == value=; secure; httpOnly; path=/some=value']);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
2023-01-06 01:39:49 +03:00
|
|
|
await request.get(`http://a.b.one.com:${server.PORT}/setcookie.html`, { __testHookLookup } as any);
|
2022-01-26 22:27:43 +03:00
|
|
|
const state = await request.storageState();
|
|
|
|
expect(state).toEqual({
|
|
|
|
'cookies': [
|
|
|
|
{
|
|
|
|
domain: 'a.b.one.com',
|
|
|
|
expires: -1,
|
|
|
|
name: 'f',
|
|
|
|
path: '/some=value',
|
|
|
|
sameSite: 'Lax',
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
value: 'value == value=',
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'origins': []
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-12 09:06:16 +03:00
|
|
|
it('should override cookie from Set-Cookie header', async ({ request, server }) => {
|
|
|
|
const tomorrow = new Date();
|
|
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', [`a=old; expires=${tomorrow.toUTCString()}`]);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
const dayAfterTomorrow = new Date(tomorrow);
|
|
|
|
dayAfterTomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
const dayAfterTomorrowInSeconds = Math.floor(dayAfterTomorrow.valueOf() / 1000);
|
|
|
|
server.setRoute('/updatecookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', [`a=new; expires=${dayAfterTomorrow.toUTCString()}`]);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
await request.get(`${server.PREFIX}/setcookie.html`);
|
|
|
|
await request.get(`${server.PREFIX}/updatecookie.html`);
|
|
|
|
|
|
|
|
const state = await request.storageState();
|
|
|
|
|
|
|
|
expect(state.cookies).toHaveLength(1);
|
|
|
|
expect(state.cookies[0].name).toBe(`a`);
|
|
|
|
expect(state.cookies[0].value).toBe(`new`);
|
|
|
|
expect(state.cookies[0].expires).toBe(dayAfterTomorrowInSeconds);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should override cookie from Set-Cookie header even if it expired', async ({ request, server }) => {
|
|
|
|
const tomorrow = new Date();
|
|
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', [`a=ok`, `b=ok; expires=${tomorrow.toUTCString()}`]);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
server.setRoute('/unsetsetcookie.html', (req, res) => {
|
|
|
|
const pastDateString = new Date(1970, 0, 1, 0, 0, 0, 0).toUTCString();
|
|
|
|
res.setHeader('Set-Cookie', [`a=; expires=${pastDateString}`, `b=; expires=${pastDateString}`]);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
await request.get(`${server.PREFIX}/setcookie.html`);
|
|
|
|
await request.get(`${server.PREFIX}/unsetsetcookie.html`);
|
|
|
|
|
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/empty.html'),
|
|
|
|
request.get(server.EMPTY_PAGE)
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(serverRequest.headers.cookie).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2021-10-01 00:14:29 +03:00
|
|
|
it('should export cookies to storage state', async ({ request, server }) => {
|
|
|
|
const expires = new Date('12/31/2100 PST');
|
|
|
|
server.setRoute('/setcookie.html', (req, res) => {
|
|
|
|
res.setHeader('Set-Cookie', ['a=b', `c=d; expires=${expires.toUTCString()}; domain=b.one.com; path=/input`, 'e=f; domain=b.one.com; path=/input/subfolder']);
|
|
|
|
res.end();
|
|
|
|
});
|
2023-01-06 01:39:49 +03:00
|
|
|
await request.get(`http://a.b.one.com:${server.PORT}/setcookie.html`, { __testHookLookup } as any);
|
2021-10-01 00:14:29 +03:00
|
|
|
const state = await request.storageState();
|
|
|
|
expect(state).toEqual({
|
|
|
|
'cookies': [
|
|
|
|
{
|
|
|
|
'name': 'a',
|
|
|
|
'value': 'b',
|
|
|
|
'domain': 'a.b.one.com',
|
|
|
|
'path': '/',
|
|
|
|
'expires': -1,
|
|
|
|
'httpOnly': false,
|
|
|
|
'secure': false,
|
|
|
|
'sameSite': 'Lax'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'c',
|
|
|
|
'value': 'd',
|
|
|
|
'domain': '.b.one.com',
|
|
|
|
'path': '/input',
|
|
|
|
'expires': +expires / 1000,
|
|
|
|
'httpOnly': false,
|
|
|
|
'secure': false,
|
|
|
|
'sameSite': 'Lax'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'e',
|
|
|
|
'value': 'f',
|
|
|
|
'domain': '.b.one.com',
|
|
|
|
'path': '/input/subfolder',
|
|
|
|
'expires': -1,
|
|
|
|
'httpOnly': false,
|
|
|
|
'secure': false,
|
|
|
|
'sameSite': 'Lax'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'origins': []
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should preserve local storage on import/export of storage state', async ({ playwright, server }) => {
|
|
|
|
const storageState: StorageStateType = {
|
|
|
|
cookies: [
|
|
|
|
{
|
|
|
|
'name': 'a',
|
|
|
|
'value': 'b',
|
|
|
|
'domain': 'a.b.one.com',
|
|
|
|
'path': '/',
|
|
|
|
'expires': -1,
|
|
|
|
'httpOnly': false,
|
|
|
|
'secure': false,
|
|
|
|
'sameSite': 'Lax'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
origins: [
|
|
|
|
{
|
|
|
|
origin: 'https://www.example.com',
|
|
|
|
localStorage: [{
|
|
|
|
name: 'name1',
|
|
|
|
value: 'value1'
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
]
|
|
|
|
};
|
2021-10-06 04:53:19 +03:00
|
|
|
const request = await playwright.request.newContext({ storageState });
|
2021-10-01 00:14:29 +03:00
|
|
|
await request.get(server.EMPTY_PAGE);
|
|
|
|
const exportedState = await request.storageState();
|
|
|
|
expect(exportedState).toEqual(storageState);
|
|
|
|
await request.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should send cookies from storage state', async ({ playwright, server }) => {
|
|
|
|
const expires = new Date('12/31/2099 PST');
|
|
|
|
const storageState: StorageStateType = {
|
|
|
|
'cookies': [
|
|
|
|
{
|
|
|
|
'name': 'a',
|
|
|
|
'value': 'b',
|
|
|
|
'domain': 'a.b.one.com',
|
|
|
|
'path': '/',
|
|
|
|
'expires': -1,
|
|
|
|
'httpOnly': false,
|
|
|
|
'secure': false,
|
|
|
|
'sameSite': 'Lax'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'c',
|
|
|
|
'value': 'd',
|
|
|
|
'domain': '.b.one.com',
|
|
|
|
'path': '/first/',
|
|
|
|
'expires': +expires / 1000,
|
|
|
|
'httpOnly': false,
|
|
|
|
'secure': false,
|
|
|
|
'sameSite': 'Lax'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'e',
|
|
|
|
'value': 'f',
|
|
|
|
'domain': '.b.one.com',
|
|
|
|
'path': '/first/second',
|
|
|
|
'expires': -1,
|
|
|
|
'httpOnly': false,
|
|
|
|
'secure': false,
|
|
|
|
'sameSite': 'Lax'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'origins': []
|
|
|
|
};
|
2021-10-06 04:53:19 +03:00
|
|
|
const request = await playwright.request.newContext({ storageState });
|
2021-10-01 00:14:29 +03:00
|
|
|
const [serverRequest] = await Promise.all([
|
|
|
|
server.waitForRequest('/first/second/third/not_found.html'),
|
2023-01-06 01:39:49 +03:00
|
|
|
request.get(`http://www.a.b.one.com:${server.PORT}/first/second/third/not_found.html`, { __testHookLookup } as any)
|
2021-10-01 00:14:29 +03:00
|
|
|
]);
|
|
|
|
expect(serverRequest.headers.cookie).toBe('c=d; e=f');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('storage state should round-trip through file', async ({ playwright, server }, testInfo) => {
|
|
|
|
const storageState: StorageStateType = {
|
|
|
|
'cookies': [
|
|
|
|
{
|
|
|
|
'name': 'a',
|
|
|
|
'value': 'b',
|
|
|
|
'domain': 'a.b.one.com',
|
|
|
|
'path': '/',
|
|
|
|
'expires': -1,
|
|
|
|
'httpOnly': false,
|
|
|
|
'secure': false,
|
|
|
|
'sameSite': 'Lax'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
'origins': []
|
|
|
|
};
|
|
|
|
|
2021-10-06 04:53:19 +03:00
|
|
|
const request1 = await playwright.request.newContext({ storageState });
|
2021-10-01 00:14:29 +03:00
|
|
|
const path = testInfo.outputPath('storage-state.json');
|
|
|
|
const state1 = await request1.storageState({ path });
|
|
|
|
expect(state1).toEqual(storageState);
|
|
|
|
|
|
|
|
const written = await fs.promises.readFile(path, 'utf8');
|
|
|
|
expect(JSON.stringify(state1, undefined, 2)).toBe(written);
|
|
|
|
|
2021-10-06 04:53:19 +03:00
|
|
|
const request2 = await playwright.request.newContext({ storageState: path });
|
2021-10-01 00:14:29 +03:00
|
|
|
const state2 = await request2.storageState();
|
|
|
|
expect(state2).toEqual(storageState);
|
|
|
|
});
|