Skip to content

Commit fefbebc

Browse files
committed
wip: add cookie test files
1 parent e860dd8 commit fefbebc

File tree

3 files changed

+546
-0
lines changed

3 files changed

+546
-0
lines changed
Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,329 @@
1+
# /**
2+
# * Copyright 2018 Google Inc. All rights reserved.
3+
# * Modifications copyright (c) Microsoft Corporation.
4+
# *
5+
# * Licensed under the Apache License, Version 2.0 (the "License");
6+
# * you may not use this file except in compliance with the License.
7+
# * You may obtain a copy of the License at
8+
# *
9+
# * http://www.apache.org/licenses/LICENSE-2.0
10+
# *
11+
# * Unless required by applicable law or agreed to in writing, software
12+
# * distributed under the License is distributed on an "AS IS" BASIS,
13+
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# * See the License for the specific language governing permissions and
15+
# * limitations under the License.
16+
# */
17+
18+
# const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions;
19+
20+
# it('should work', async({context, page, server}) => {
21+
# await page.goto(server.EMPTY_PAGE);
22+
# await context.addCookies([{
23+
# url: server.EMPTY_PAGE,
24+
# name: 'password',
25+
# value: '123456'
26+
# }]);
27+
# expect(await page.evaluate(() => document.cookie)).toEqual('password=123456');
28+
# });
29+
# it('should roundtrip cookie', async({context, page, server}) => {
30+
# await page.goto(server.EMPTY_PAGE);
31+
# // @see https://en.wikipedia.org/wiki/Year_2038_problem
32+
# const date = +(new Date('1/1/2038'));
33+
# const documentCookie = await page.evaluate(timestamp => {
34+
# const date = new Date(timestamp);
35+
# document.cookie = `username=John Doe;expires=${date.toUTCString()}`;
36+
# return document.cookie;
37+
# }, date);
38+
# expect(documentCookie).toBe('username=John Doe');
39+
# const cookies = await context.cookies();
40+
# await context.clearCookies();
41+
# expect(await context.cookies()).toEqual([]);
42+
# await context.addCookies(cookies);
43+
# expect(await context.cookies()).toEqual(cookies);
44+
# });
45+
# it('should send cookie header', async({server, context}) => {
46+
# let cookie = '';
47+
# server.setRoute('/empty.html', (req, res) => {
48+
# cookie = req.headers.cookie;
49+
# res.end();
50+
# });
51+
# await context.addCookies([{url: server.EMPTY_PAGE, name: 'cookie', value: 'value'}]);
52+
# const page = await context.newPage();
53+
# await page.goto(server.EMPTY_PAGE);
54+
# expect(cookie).toBe('cookie=value');
55+
# });
56+
# it('should isolate cookies in browser contexts', async({context, server, browser}) => {
57+
# const anotherContext = await browser.newContext();
58+
# await context.addCookies([{url: server.EMPTY_PAGE, name: 'isolatecookie', value: 'page1value'}]);
59+
# await anotherContext.addCookies([{url: server.EMPTY_PAGE, name: 'isolatecookie', value: 'page2value'}]);
60+
61+
# const cookies1 = await context.cookies();
62+
# const cookies2 = await anotherContext.cookies();
63+
# expect(cookies1.length).toBe(1);
64+
# expect(cookies2.length).toBe(1);
65+
# expect(cookies1[0].name).toBe('isolatecookie');
66+
# expect(cookies1[0].value).toBe('page1value');
67+
# expect(cookies2[0].name).toBe('isolatecookie');
68+
# expect(cookies2[0].value).toBe('page2value');
69+
# await anotherContext.close();
70+
# });
71+
# it('should isolate session cookies', async({context, server, browser}) => {
72+
# server.setRoute('/setcookie.html', (req, res) => {
73+
# res.setHeader('Set-Cookie', 'session=value');
74+
# res.end();
75+
# });
76+
# {
77+
# const page = await context.newPage();
78+
# await page.goto(server.PREFIX + '/setcookie.html');
79+
# }
80+
# {
81+
# const page = await context.newPage();
82+
# await page.goto(server.EMPTY_PAGE);
83+
# const cookies = await context.cookies();
84+
# expect(cookies.length).toBe(1);
85+
# expect(cookies.map(c => c.value).join(',')).toBe('value');
86+
# }
87+
# {
88+
# const context2 = await browser.newContext();
89+
# const page = await context2.newPage();
90+
# await page.goto(server.EMPTY_PAGE);
91+
# const cookies = await context2.cookies();
92+
# expect(cookies[0] && cookies[0].name).toBe(undefined);
93+
# await context2.close();
94+
# }
95+
# });
96+
# it('should isolate persistent cookies', async({context, server, browser}) => {
97+
# server.setRoute('/setcookie.html', (req, res) => {
98+
# res.setHeader('Set-Cookie', 'persistent=persistent-value; max-age=3600');
99+
# res.end();
100+
# });
101+
# const page = await context.newPage();
102+
# await page.goto(server.PREFIX + '/setcookie.html');
103+
104+
# const context1 = context;
105+
# const context2 = await browser.newContext();
106+
# const [page1, page2] = await Promise.all([context1.newPage(), context2.newPage()]);
107+
# await Promise.all([page1.goto(server.EMPTY_PAGE), page2.goto(server.EMPTY_PAGE)]);
108+
# const [cookies1, cookies2] = await Promise.all([context1.cookies(), context2.cookies()]);
109+
# expect(cookies1.length).toBe(1);
110+
# expect(cookies1[0].name).toBe('persistent');
111+
# expect(cookies1[0].value).toBe('persistent-value');
112+
# expect(cookies2.length).toBe(0);
113+
# await context2.close();
114+
# });
115+
# it('should isolate send cookie header', async({server, context, browser}) => {
116+
# let cookie = [];
117+
# server.setRoute('/empty.html', (req, res) => {
118+
# cookie = req.headers.cookie || '';
119+
# res.end();
120+
# });
121+
# await context.addCookies([{url: server.EMPTY_PAGE, name: 'sendcookie', value: 'value'}]);
122+
# {
123+
# const page = await context.newPage();
124+
# await page.goto(server.EMPTY_PAGE);
125+
# expect(cookie).toBe('sendcookie=value');
126+
# }
127+
# {
128+
# const context = await browser.newContext();
129+
# const page = await context.newPage();
130+
# await page.goto(server.EMPTY_PAGE);
131+
# expect(cookie).toBe('');
132+
# await context.close();
133+
# }
134+
# });
135+
# it.slow()('should isolate cookies between launches', async({browserType, server, defaultBrowserOptions}) => {
136+
# const browser1 = await browserType.launch(defaultBrowserOptions);
137+
# const context1 = await browser1.newContext();
138+
# await context1.addCookies([{url: server.EMPTY_PAGE, name: 'cookie-in-context-1', value: 'value', expires: Date.now() / 1000 + 10000}]);
139+
# await browser1.close();
140+
141+
# const browser2 = await browserType.launch(defaultBrowserOptions);
142+
# const context2 = await browser2.newContext();
143+
# const cookies = await context2.cookies();
144+
# expect(cookies.length).toBe(0);
145+
# await browser2.close();
146+
# });
147+
# it('should set multiple cookies', async({context, page, server}) => {
148+
# await page.goto(server.EMPTY_PAGE);
149+
# await context.addCookies([{
150+
# url: server.EMPTY_PAGE,
151+
# name: 'multiple-1',
152+
# value: '123456'
153+
# }, {
154+
# url: server.EMPTY_PAGE,
155+
# name: 'multiple-2',
156+
# value: 'bar'
157+
# }]);
158+
# expect(await page.evaluate(() => {
159+
# const cookies = document.cookie.split(';');
160+
# return cookies.map(cookie => cookie.trim()).sort();
161+
# })).toEqual([
162+
# 'multiple-1=123456',
163+
# 'multiple-2=bar',
164+
# ]);
165+
# });
166+
# it('should have |expires| set to |-1| for session cookies', async({context, server}) => {
167+
# await context.addCookies([{
168+
# url: server.EMPTY_PAGE,
169+
# name: 'expires',
170+
# value: '123456'
171+
# }]);
172+
# const cookies = await context.cookies();
173+
# expect(cookies[0].expires).toBe(-1);
174+
# });
175+
# it('should set cookie with reasonable defaults', async({context, server}) => {
176+
# await context.addCookies([{
177+
# url: server.EMPTY_PAGE,
178+
# name: 'defaults',
179+
# value: '123456'
180+
# }]);
181+
# const cookies = await context.cookies();
182+
# expect(cookies.sort((a, b) => a.name.localeCompare(b.name))).toEqual([{
183+
# name: 'defaults',
184+
# value: '123456',
185+
# domain: 'localhost',
186+
# path: '/',
187+
# expires: -1,
188+
# httpOnly: false,
189+
# secure: false,
190+
# sameSite: 'None',
191+
# }]);
192+
# });
193+
# it('should set a cookie with a path', async({context, page, server}) => {
194+
# await page.goto(server.PREFIX + '/grid.html');
195+
# await context.addCookies([{
196+
# domain: 'localhost',
197+
# path: '/grid.html',
198+
# name: 'gridcookie',
199+
# value: 'GRID',
200+
# }]);
201+
# expect(await context.cookies()).toEqual([{
202+
# name: 'gridcookie',
203+
# value: 'GRID',
204+
# domain: 'localhost',
205+
# path: '/grid.html',
206+
# expires: -1,
207+
# httpOnly: false,
208+
# secure: false,
209+
# sameSite: 'None',
210+
# }]);
211+
# expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID');
212+
# await page.goto(server.EMPTY_PAGE);
213+
# expect(await page.evaluate('document.cookie')).toBe('');
214+
# await page.goto(server.PREFIX + '/grid.html');
215+
# expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID');
216+
# });
217+
# it('should not set a cookie with blank page URL', async function({context, server}) {
218+
# let error = null;
219+
# try {
220+
# await context.addCookies([
221+
# {url: server.EMPTY_PAGE, name: 'example-cookie', value: 'best'},
222+
# {url: 'about:blank', name: 'example-cookie-blank', value: 'best'}
223+
# ]);
224+
# } catch (e) {
225+
# error = e;
226+
# }
227+
# expect(error.message).toContain(
228+
# `Blank page can not have cookie "example-cookie-blank"`
229+
# );
230+
# });
231+
# it('should not set a cookie on a data URL page', async function({context}) {
232+
# let error = null;
233+
# try {
234+
# await context.addCookies([{url: 'data:,Hello%2C%20World!', name: 'example-cookie', value: 'best'}]);
235+
# } catch (e) {
236+
# error = e;
237+
# }
238+
# expect(error.message).toContain('Data URL page can not have cookie "example-cookie"');
239+
# });
240+
# it('should default to setting secure cookie for HTTPS websites', async({context, page, server}) => {
241+
# await page.goto(server.EMPTY_PAGE);
242+
# const SECURE_URL = 'https://example.com';
243+
# await context.addCookies([{
244+
# url: SECURE_URL,
245+
# name: 'foo',
246+
# value: 'bar',
247+
# }]);
248+
# const [cookie] = await context.cookies(SECURE_URL);
249+
# expect(cookie.secure).toBe(true);
250+
# });
251+
# it('should be able to set unsecure cookie for HTTP website', async({context, page, server}) => {
252+
# await page.goto(server.EMPTY_PAGE);
253+
# const HTTP_URL = 'http://example.com';
254+
# await context.addCookies([{
255+
# url: HTTP_URL,
256+
# name: 'foo',
257+
# value: 'bar',
258+
# }]);
259+
# const [cookie] = await context.cookies(HTTP_URL);
260+
# expect(cookie.secure).toBe(false);
261+
# });
262+
# it('should set a cookie on a different domain', async({context, page, server}) => {
263+
# await page.goto(server.EMPTY_PAGE);
264+
# await context.addCookies([{
265+
# url: 'https://www.example.com',
266+
# name: 'example-cookie',
267+
# value: 'best',
268+
# }]);
269+
# expect(await page.evaluate('document.cookie')).toBe('');
270+
# expect(await context.cookies('https://www.example.com')).toEqual([{
271+
# name: 'example-cookie',
272+
# value: 'best',
273+
# domain: 'www.example.com',
274+
# path: '/',
275+
# expires: -1,
276+
# httpOnly: false,
277+
# secure: true,
278+
# sameSite: 'None',
279+
# }]);
280+
# });
281+
# it('should set cookies for a frame', async({context, page, server}) => {
282+
# await page.goto(server.EMPTY_PAGE);
283+
# await context.addCookies([
284+
# {url: server.PREFIX, name: 'frame-cookie', value: 'value'}
285+
# ]);
286+
# await page.evaluate(src => {
287+
# let fulfill;
288+
# const promise = new Promise(x => fulfill = x);
289+
# const iframe = document.createElement('iframe');
290+
# document.body.appendChild(iframe);
291+
# iframe.onload = fulfill;
292+
# iframe.src = src;
293+
# return promise;
294+
# }, server.PREFIX + '/grid.html');
295+
296+
# expect(await page.frames()[1].evaluate('document.cookie')).toBe('frame-cookie=value');
297+
# });
298+
# it('should(not) block third party cookies', async({context, page, server}) => {
299+
# await page.goto(server.EMPTY_PAGE);
300+
# await page.evaluate(src => {
301+
# let fulfill;
302+
# const promise = new Promise(x => fulfill = x);
303+
# const iframe = document.createElement('iframe');
304+
# document.body.appendChild(iframe);
305+
# iframe.onload = fulfill;
306+
# iframe.src = src;
307+
# return promise;
308+
# }, server.CROSS_PROCESS_PREFIX + '/grid.html');
309+
# await page.frames()[1].evaluate(`document.cookie = 'username=John Doe'`);
310+
# await page.waitForTimeout(2000);
311+
# const allowsThirdParty = CHROMIUM || FFOX;
312+
# const cookies = await context.cookies(server.CROSS_PROCESS_PREFIX + '/grid.html');
313+
# if (allowsThirdParty) {
314+
# expect(cookies).toEqual([
315+
# {
316+
# "domain": "127.0.0.1",
317+
# "expires": -1,
318+
# "httpOnly": false,
319+
# "name": "username",
320+
# "path": "/",
321+
# "sameSite": "None",
322+
# "secure": false,
323+
# "value": "John Doe"
324+
# }
325+
# ]);
326+
# } else {
327+
# expect(cookies).toEqual([]);
328+
# }
329+
# });
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# /**
2+
# * Copyright 2018 Google Inc. All rights reserved.
3+
# * Modifications copyright (c) Microsoft Corporation.
4+
# *
5+
# * Licensed under the Apache License, Version 2.0 (the "License");
6+
# * you may not use this file except in compliance with the License.
7+
# * You may obtain a copy of the License at
8+
# *
9+
# * http://www.apache.org/licenses/LICENSE-2.0
10+
# *
11+
# * Unless required by applicable law or agreed to in writing, software
12+
# * distributed under the License is distributed on an "AS IS" BASIS,
13+
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# * See the License for the specific language governing permissions and
15+
# * limitations under the License.
16+
# */
17+
18+
# const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions;
19+
20+
# it('should clear cookies', async({context, page, server}) => {
21+
# await page.goto(server.EMPTY_PAGE);
22+
# await context.addCookies([{
23+
# url: server.EMPTY_PAGE,
24+
# name: 'cookie1',
25+
# value: '1'
26+
# }]);
27+
# expect(await page.evaluate('document.cookie')).toBe('cookie1=1');
28+
# await context.clearCookies();
29+
# expect(await context.cookies()).toEqual([]);
30+
# await page.reload();
31+
# expect(await page.evaluate('document.cookie')).toBe('');
32+
# });
33+
34+
# it('should isolate cookies when clearing', async({context, server, browser}) => {
35+
# const anotherContext = await browser.newContext();
36+
# await context.addCookies([{url: server.EMPTY_PAGE, name: 'page1cookie', value: 'page1value'}]);
37+
# await anotherContext.addCookies([{url: server.EMPTY_PAGE, name: 'page2cookie', value: 'page2value'}]);
38+
39+
# expect((await context.cookies()).length).toBe(1);
40+
# expect((await anotherContext.cookies()).length).toBe(1);
41+
42+
# await context.clearCookies();
43+
# expect((await context.cookies()).length).toBe(0);
44+
# expect((await anotherContext.cookies()).length).toBe(1);
45+
46+
# await anotherContext.clearCookies();
47+
# expect((await context.cookies()).length).toBe(0);
48+
# expect((await anotherContext.cookies()).length).toBe(0);
49+
# await anotherContext.close();
50+
# });

0 commit comments

Comments
 (0)