Skip to content

Commit 50d3ef7

Browse files
authored
fix: Ignore unknown parameters passed in cookies (#1336)
### Description - Ignore unknown parameters passed in cookies ### Issues - Closes: #1333 ### Testing - Added a test that sets `Partitioned` cookies. To reproduce the behaviour that causes Playwright to throw an error
1 parent 861dc6a commit 50d3ef7

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/crawlee/sessions/_cookies.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def set(
9494
http_only: bool = False,
9595
secure: bool = False,
9696
same_site: Literal['Lax', 'None', 'Strict'] | None = None,
97+
**_kwargs: Any, # Unknown parameters will be ignored.
9798
) -> None:
9899
"""Create and store a cookie with modern browser attributes.
99100

tests/unit/crawlers/_playwright/test_playwright_crawler.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,32 @@ async def request_handler(context: PlaywrightCrawlingContext) -> None:
429429
assert session_cookies == {'check': 'test'}
430430

431431

432+
async def test_read_write_cookies(server_url: URL) -> None:
433+
"""Test that cookies are reloaded correctly."""
434+
async with SessionPool(max_pool_size=1) as session_pool:
435+
crawler = PlaywrightCrawler(session_pool=session_pool)
436+
437+
playwright_cookies = []
438+
session_cookies = []
439+
440+
# Check that no errors occur when reading and writing cookies.
441+
@crawler.router.default_handler
442+
async def request_handler(context: PlaywrightCrawlingContext) -> None:
443+
cookies = await context.page.context.cookies()
444+
playwright_cookies.extend(cookies)
445+
446+
if context.session:
447+
context.session.cookies.set_cookies_from_playwright_format(cookies)
448+
session_cookies.extend(context.session.cookies.get_cookies_as_dicts())
449+
450+
await crawler.run([str(server_url / 'set_complex_cookies')])
451+
452+
# Check that the cookie was received with `partitionKey`
453+
assert any('partitionKey' in cookie for cookie in playwright_cookies)
454+
455+
assert len(playwright_cookies) == len(session_cookies)
456+
457+
432458
async def test_custom_fingerprint_uses_generator_options(server_url: URL) -> None:
433459
min_width = 300
434460
max_width = 600

tests/unit/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ async def set_complex_cookies(_scope: dict[str, Any], _receive: Receive, send: S
347347
[b'set-cookie', b'basic=1; Path=/; HttpOnly; SameSite=Lax'],
348348
[b'set-cookie', b'withpath=2; Path=/html; SameSite=None'],
349349
[b'set-cookie', b'strict=3; Path=/; SameSite=Strict'],
350-
[b'set-cookie', b'secure=4; Path=/; HttpOnly; Secure; SameSite=Strict'],
350+
[b'set-cookie', b'secure=4; Path=/; HttpOnly; Secure; SameSite=Strict; Partitioned'],
351351
[b'set-cookie', b'short=5; Path=/;'],
352352
[b'set-cookie', b'domain=6; Path=/; Domain=.127.0.0.1;'],
353353
]

0 commit comments

Comments
 (0)