Skip to content

Commit

Permalink
Refactor cookie handling in CookiesMiddleware and add test case for o…
Browse files Browse the repository at this point in the history
…ff-domain jar storage
  • Loading branch information
Emmanuel Rondan committed Jun 22, 2023
1 parent 4dacad0 commit be0a1d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 2 additions & 4 deletions scrapy/downloadermiddlewares/cookies.py
Expand Up @@ -40,13 +40,11 @@ def _process_cookies(self, cookies, *, jar, request):
cookie_domain = cookie_domain[1:]

request_domain = urlparse_cached(request).hostname.lower()

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

jar.set_cookie_if_ok(cookie, request)
jar.set_cookie(cookie)

def process_request(self, request, spider):
if request.meta.get("dont_merge_cookies", False):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_downloadermiddleware_cookies.py
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 be0a1d2

Please sign in to comment.