|
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 | | -# */ |
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
17 | 14 |
|
18 | | -# const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions; |
| 15 | +import datetime |
19 | 16 |
|
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 | 17 |
|
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}) => { |
| 18 | +async def test_should_work(context, page, server): |
| 19 | + await page.goto(server.EMPTY_PAGE) |
| 20 | + await context.addCookies( |
| 21 | + [{"url": server.EMPTY_PAGE, "name": "password", "value": "123456"}] |
| 22 | + ) |
| 23 | + assert await page.evaluate("() => document.cookie") == "password=123456" |
| 24 | + |
| 25 | + |
| 26 | +async def test_should_roundtrip_cookie(context, page, server): |
| 27 | + await page.goto(server.EMPTY_PAGE) |
| 28 | + # @see https://en.wikipedia.org/wiki/Year_2038_problem |
| 29 | + date = int(datetime.datetime(2038, 1, 1).timestamp() * 1000) |
| 30 | + document_cookie = await page.evaluate( |
| 31 | + """timestamp => { |
| 32 | + const date = new Date(timestamp); |
| 33 | + document.cookie = `username=John Doe;expires=${date.toUTCString()}`; |
| 34 | + return document.cookie; |
| 35 | + }""", |
| 36 | + date, |
| 37 | + ) |
| 38 | + assert document_cookie == "username=John Doe" |
| 39 | + cookies = await context.cookies() |
| 40 | + await context.clearCookies() |
| 41 | + assert await context.cookies() == [] |
| 42 | + await context.addCookies(cookies) |
| 43 | + assert await context.cookies() == cookies |
| 44 | + |
| 45 | + |
| 46 | +async def test_should_send_cookie_header(server, context): |
| 47 | + cookie = [] |
| 48 | + |
| 49 | + def handler(request): |
| 50 | + cookie.extend(request.requestHeaders.getRawHeaders("cookie")) |
| 51 | + request.finish() |
| 52 | + |
| 53 | + server.set_route("/empty.html", handler) |
| 54 | + await context.addCookies( |
| 55 | + [{"url": server.EMPTY_PAGE, "name": "cookie", "value": "value"}] |
| 56 | + ) |
| 57 | + page = await context.newPage() |
| 58 | + await page.goto(server.EMPTY_PAGE) |
| 59 | + assert cookie == ["cookie=value"] |
| 60 | + |
| 61 | + |
| 62 | +async def test_should_isolate_cookies_in_browser_contexts(context, server, browser): |
| 63 | + another_context = await browser.newContext() |
| 64 | + await context.addCookies( |
| 65 | + [{"url": server.EMPTY_PAGE, "name": "isolatecookie", "value": "page1value"}] |
| 66 | + ) |
| 67 | + await another_context.addCookies( |
| 68 | + [{"url": server.EMPTY_PAGE, "name": "isolatecookie", "value": "page2value"}] |
| 69 | + ) |
| 70 | + |
| 71 | + cookies_1 = await context.cookies() |
| 72 | + cookies_2 = await another_context.cookies() |
| 73 | + assert len(cookies_1) == 1 |
| 74 | + assert len(cookies_2) == 1 |
| 75 | + assert cookies_1[0]["name"] == "isolatecookie" |
| 76 | + assert cookies_1[0]["value"] == "page1value" |
| 77 | + assert cookies_2[0]["name"] == "isolatecookie" |
| 78 | + assert cookies_2[0]["value"] == "page2value" |
| 79 | + await another_context.close() |
| 80 | + |
| 81 | + |
| 82 | +# async def test_should isolate session cookies', async({context, server, browser}) => { |
72 | 83 | # server.setRoute('/setcookie.html', (req, res) => { |
73 | 84 | # res.setHeader('Set-Cookie', 'session=value'); |
74 | 85 | # res.end(); |
75 | 86 | # }); |
76 | 87 | # { |
77 | | -# const page = await context.newPage(); |
| 88 | +# page = await context.newPage(); |
78 | 89 | # await page.goto(server.PREFIX + '/setcookie.html'); |
79 | 90 | # } |
80 | 91 | # { |
|
93 | 104 | # await context2.close(); |
94 | 105 | # } |
95 | 106 | # }); |
96 | | -# it('should isolate persistent cookies', async({context, server, browser}) => { |
| 107 | +# async def test_should isolate persistent cookies', async({context, server, browser}) => { |
97 | 108 | # server.setRoute('/setcookie.html', (req, res) => { |
98 | 109 | # res.setHeader('Set-Cookie', 'persistent=persistent-value; max-age=3600'); |
99 | 110 | # res.end(); |
|
112 | 123 | # expect(cookies2.length).toBe(0); |
113 | 124 | # await context2.close(); |
114 | 125 | # }); |
115 | | -# it('should isolate send cookie header', async({server, context, browser}) => { |
| 126 | +# async def test_should isolate send cookie header', async({server, context, browser}) => { |
116 | 127 | # let cookie = []; |
117 | 128 | # server.setRoute('/empty.html', (req, res) => { |
118 | 129 | # cookie = req.headers.cookie || ''; |
|
144 | 155 | # expect(cookies.length).toBe(0); |
145 | 156 | # await browser2.close(); |
146 | 157 | # }); |
147 | | -# it('should set multiple cookies', async({context, page, server}) => { |
| 158 | +# async def test_should set multiple cookies', async({context, page, server}) => { |
148 | 159 | # await page.goto(server.EMPTY_PAGE); |
149 | 160 | # await context.addCookies([{ |
150 | 161 | # url: server.EMPTY_PAGE, |
|
163 | 174 | # 'multiple-2=bar', |
164 | 175 | # ]); |
165 | 176 | # }); |
166 | | -# it('should have |expires| set to |-1| for session cookies', async({context, server}) => { |
| 177 | +# async def test_should have |expires| set to |-1| for session cookies', async({context, server}) => { |
167 | 178 | # await context.addCookies([{ |
168 | 179 | # url: server.EMPTY_PAGE, |
169 | 180 | # name: 'expires', |
|
172 | 183 | # const cookies = await context.cookies(); |
173 | 184 | # expect(cookies[0].expires).toBe(-1); |
174 | 185 | # }); |
175 | | -# it('should set cookie with reasonable defaults', async({context, server}) => { |
| 186 | +# async def test_should set cookie with reasonable defaults', async({context, server}) => { |
176 | 187 | # await context.addCookies([{ |
177 | 188 | # url: server.EMPTY_PAGE, |
178 | 189 | # name: 'defaults', |
|
190 | 201 | # sameSite: 'None', |
191 | 202 | # }]); |
192 | 203 | # }); |
193 | | -# it('should set a cookie with a path', async({context, page, server}) => { |
| 204 | +# async def test_should set a cookie with a path', async({context, page, server}) => { |
194 | 205 | # await page.goto(server.PREFIX + '/grid.html'); |
195 | 206 | # await context.addCookies([{ |
196 | 207 | # domain: 'localhost', |
|
214 | 225 | # await page.goto(server.PREFIX + '/grid.html'); |
215 | 226 | # expect(await page.evaluate('document.cookie')).toBe('gridcookie=GRID'); |
216 | 227 | # }); |
217 | | -# it('should not set a cookie with blank page URL', async function({context, server}) { |
| 228 | +# async def test_should not set a cookie with blank page URL', async function({context, server}) { |
218 | 229 | # let error = null; |
219 | 230 | # try { |
220 | 231 | # await context.addCookies([ |
|
228 | 239 | # `Blank page can not have cookie "example-cookie-blank"` |
229 | 240 | # ); |
230 | 241 | # }); |
231 | | -# it('should not set a cookie on a data URL page', async function({context}) { |
| 242 | +# async def test_should not set a cookie on a data URL page', async function({context}) { |
232 | 243 | # let error = null; |
233 | 244 | # try { |
234 | 245 | # await context.addCookies([{url: 'data:,Hello%2C%20World!', name: 'example-cookie', value: 'best'}]); |
|
237 | 248 | # } |
238 | 249 | # expect(error.message).toContain('Data URL page can not have cookie "example-cookie"'); |
239 | 250 | # }); |
240 | | -# it('should default to setting secure cookie for HTTPS websites', async({context, page, server}) => { |
| 251 | +# async def test_should default to setting secure cookie for HTTPS websites', async({context, page, server}) => { |
241 | 252 | # await page.goto(server.EMPTY_PAGE); |
242 | 253 | # const SECURE_URL = 'https://example.com'; |
243 | 254 | # await context.addCookies([{ |
|
248 | 259 | # const [cookie] = await context.cookies(SECURE_URL); |
249 | 260 | # expect(cookie.secure).toBe(true); |
250 | 261 | # }); |
251 | | -# it('should be able to set unsecure cookie for HTTP website', async({context, page, server}) => { |
| 262 | +# async def test_should be able to set unsecure cookie for HTTP website', async({context, page, server}) => { |
252 | 263 | # await page.goto(server.EMPTY_PAGE); |
253 | 264 | # const HTTP_URL = 'http://example.com'; |
254 | 265 | # await context.addCookies([{ |
|
259 | 270 | # const [cookie] = await context.cookies(HTTP_URL); |
260 | 271 | # expect(cookie.secure).toBe(false); |
261 | 272 | # }); |
262 | | -# it('should set a cookie on a different domain', async({context, page, server}) => { |
| 273 | +# async def test_should set a cookie on a different domain', async({context, page, server}) => { |
263 | 274 | # await page.goto(server.EMPTY_PAGE); |
264 | 275 | # await context.addCookies([{ |
265 | 276 | # url: 'https://www.example.com', |
|
278 | 289 | # sameSite: 'None', |
279 | 290 | # }]); |
280 | 291 | # }); |
281 | | -# it('should set cookies for a frame', async({context, page, server}) => { |
| 292 | +# async def test_should set cookies for a frame', async({context, page, server}) => { |
282 | 293 | # await page.goto(server.EMPTY_PAGE); |
283 | 294 | # await context.addCookies([ |
284 | 295 | # {url: server.PREFIX, name: 'frame-cookie', value: 'value'} |
|
295 | 306 |
|
296 | 307 | # expect(await page.frames()[1].evaluate('document.cookie')).toBe('frame-cookie=value'); |
297 | 308 | # }); |
298 | | -# it('should(not) block third party cookies', async({context, page, server}) => { |
| 309 | +# async def test_should(not) block third party cookies', async({context, page, server}) => { |
299 | 310 | # await page.goto(server.EMPTY_PAGE); |
300 | 311 | # await page.evaluate(src => { |
301 | 312 | # let fulfill; |
|
0 commit comments