Skip to content

Commit

Permalink
- creating custom policy to add cookies to the jar
Browse files Browse the repository at this point in the history
- adding test to make sure we don't add a cookie with an unrelated domain from a response
  • Loading branch information
Emmanuel Rondan committed Aug 28, 2023
1 parent 4dacad0 commit c8b19d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion scrapy/http/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
IPV4_RE = re.compile(r"\.\d+$", re.ASCII)


class CustomDefaultCookiePolicy(DefaultCookiePolicy):
def set_ok(self, cookie, request):
# always set cookies
return True


class CookieJar:
def __init__(self, policy=None, check_expired_frequency=10000):
self.policy = policy or DefaultCookiePolicy()
self.policy = policy or CustomDefaultCookiePolicy()
self.jar = _CookieJar(self.policy)
self.jar._cookies_lock = _DummyLock()
self.check_expired_frequency = check_expired_frequency
Expand Down
27 changes: 27 additions & 0 deletions tests/test_downloadermiddleware_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,30 @@ 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")

def test_process_response_unrelated_domain(self):
request = Request("http://a.example")
request.headers["Cookie"] = "foo=bar; domain=b.example"
response = Response("http://c.example")
response.headers["Set-Cookie"] = "asd=fgh; domain=d.example"
self.mw.process_response(request, response, spider=None)
jar = self.mw.jars[None]
assert not jar._cookies.get("c.example", {}).get("/", {}).get("asd")

0 comments on commit c8b19d8

Please sign in to comment.