Skip to content

Commit

Permalink
Refactor cookie handling in CookiesMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Rondan committed Jun 29, 2023
1 parent 4dacad0 commit 7d32fb4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 4 additions & 4 deletions scrapy/downloadermiddlewares/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def _process_cookies(self, cookies, *, jar, request):

if cookie_domain and _is_public_domain(cookie_domain):
if cookie_domain != request_domain:
continue
cookie.domain = request_domain

jar.set_cookie_if_ok(cookie, request)
cookie.domain = request_domain
jar.set_cookie_if_ok(cookie, request)
else:
jar.set_cookie(cookie)

def process_request(self, request, spider):
if request.meta.get("dont_merge_cookies", False):
Expand Down
22 changes: 20 additions & 2 deletions tests/test_downloadermiddleware_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def test_user_set_cookie_domain_suffix_public_period(self):
"https://foo.co.uk",
"https://bar.co.uk",
"co.uk",
cookies1=False,
cookies1=True,
cookies2=False,
)

Expand All @@ -655,7 +655,7 @@ def test_user_set_cookie_domain_suffix_public_private(self):
"https://foo.blogspot.com",
"https://bar.blogspot.com",
"blogspot.com",
cookies1=False,
cookies1=True,
cookies2=False,
)

Expand Down Expand Up @@ -732,3 +732,21 @@ def test_server_set_cookie_domain_public_period(self):
"co.uk",
cookies=True,
)

def test_off_domain_jar_storage(self):
request1 = Request(
"https://a.example",
cookies=[
{
"name": "foo",
"value": "bar",
"domain": "b.example",
},
],
)
assert self.mw.process_request(request1, self.spider) is None
self.assertNotIn(b"Cookie", request1.headers)

request2 = Request("https://b.example/")
assert self.mw.process_request(request2, self.spider) is None
self.assertEqual(request2.headers.get(b"Cookie"), b"foo=bar")

0 comments on commit 7d32fb4

Please sign in to comment.