Skip to content

Commit e7b7edf

Browse files
tirkarthiasvetlov
authored andcommitted
[3.7] bpo-12144: Handle cookies with expires attribute in CookieJar.make_cookies (GH-13921) (GH-16092)
* [3.7] bpo-12144: Handle cookies with expires attribute in CookieJar.make_cookies (GH-13921) Handle time comparison for cookies with `expires` attribute when `CookieJar.make_cookies` is called. Co-authored-by: Demian Brecht <[email protected]> https://bugs.python.org/issue12144 Automerge-Triggered-By: @asvetlov (cherry picked from commit bb41147) Co-authored-by: Xtreak <[email protected]> * Use warnings module instead of test.support.check_no_warnings * [3.7] bpo-12144: Handle cookies with expires attribute in CookieJar.make_cookies (GH-13921) Handle time comparison for cookies with `expires` attribute when `CookieJar.make_cookies` is called. Co-authored-by: Demian Brecht <[email protected]> https://bugs.python.org/issue12144 Automerge-Triggered-By: @asvetlov. (cherry picked from commit bb41147) Co-authored-by: Xtreak <[email protected]>
1 parent d3f8a11 commit e7b7edf

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Lib/http/cookiejar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,7 @@ def make_cookies(self, response, request):
15901590
headers = response.info()
15911591
rfc2965_hdrs = headers.get_all("Set-Cookie2", [])
15921592
ns_hdrs = headers.get_all("Set-Cookie", [])
1593+
self._policy._now = self._now = int(time.time())
15931594

15941595
rfc2965 = self._policy.rfc2965
15951596
netscape = self._policy.netscape
@@ -1669,8 +1670,6 @@ def extract_cookies(self, response, request):
16691670
_debug("extract_cookies: %s", response.info())
16701671
self._cookies_lock.acquire()
16711672
try:
1672-
self._policy._now = self._now = int(time.time())
1673-
16741673
for cookie in self.make_cookies(response, request):
16751674
if self._policy.set_ok(cookie, request):
16761675
_debug(" setting cookie: %s", cookie)

Lib/test/test_http_cookiejar.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import time
77
import unittest
88
import urllib.request
9+
import warnings
910

1011
from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape,
1112
parse_ns_headers, join_header_words, split_header_words, Cookie,
@@ -560,6 +561,16 @@ def test_expires(self):
560561
# if expires is in future, keep cookie...
561562
c = CookieJar()
562563
future = time2netscape(time.time()+3600)
564+
565+
with warnings.catch_warnings(record=True) as warns:
566+
headers = [f"Set-Cookie: FOO=BAR; path=/; expires={future}"]
567+
req = urllib.request.Request("http://www.coyote.com/")
568+
res = FakeResponse(headers, "http://www.coyote.com/")
569+
cookies = c.make_cookies(res, req)
570+
self.assertEqual(len(cookies), 1)
571+
self.assertEqual(time2netscape(cookies[0].expires), future)
572+
self.assertEqual(len(warns), 0)
573+
563574
interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
564575
future)
565576
self.assertEqual(len(c), 1)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Ensure cookies with ``expires`` attribute are handled in
2+
:meth:`CookieJar.make_cookies`.

0 commit comments

Comments
 (0)