Merge #16611: BaseCookie now parses 'secure' and 'httponly' flags. · pythoncapi/cpython@0cb8e51 · GitHub
Skip to content

Commit 0cb8e51

Browse files
committed
Merge python#16611: BaseCookie now parses 'secure' and 'httponly' flags.
2 parents 9dd279a + cd0f74b commit 0cb8e51

3 files changed

Lines changed: 61 additions & 11 deletions

File tree

Lib/http/cookies.py

Lines changed: 19 additions & 10 deletions

Lib/test/test_http_cookies.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,51 @@ def test_special_attrs(self):
109109
self.assertEqual(C.output(),
110110
'Set-Cookie: Customer="WILE_E_COYOTE"; Max-Age=10')
111111

112-
# others
112+
def test_set_secure_httponly_attrs(self):
113113
C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"')
114114
C['Customer']['secure'] = True
115115
C['Customer']['httponly'] = True
116116
self.assertEqual(C.output(),
117117
'Set-Cookie: Customer="WILE_E_COYOTE"; httponly; secure')
118118

119+
def test_secure_httponly_false_if_not_present(self):
120+
C = cookies.SimpleCookie()
121+
C.load('eggs=scrambled; Path=/bacon')
122+
self.assertFalse(C['eggs']['httponly'])
123+
self.assertFalse(C['eggs']['secure'])
124+
125+
def test_secure_httponly_true_if_present(self):
126+
# Issue 16611
127+
C = cookies.SimpleCookie()
128+
C.load('eggs=scrambled; httponly; secure; Path=/bacon')
129+
self.assertTrue(C['eggs']['httponly'])
130+
self.assertTrue(C['eggs']['secure'])
131+
132+
def test_secure_httponly_true_if_have_value(self):
133+
# This isn't really valid, but demonstrates what the current code
134+
# is expected to do in this case.
135+
C = cookies.SimpleCookie()
136+
C.load('eggs=scrambled; httponly=foo; secure=bar; Path=/bacon')
137+
self.assertTrue(C['eggs']['httponly'])
138+
self.assertTrue(C['eggs']['secure'])
139+
# Here is what it actually does; don't depend on this behavior. These
140+
# checks are testing backward compatibility for issue 16611.
141+
self.assertEqual(C['eggs']['httponly'], 'foo')
142+
self.assertEqual(C['eggs']['secure'], 'bar')
143+
144+
def test_bad_attrs(self):
145+
# issue 16611: make sure we don't break backward compatibility.
146+
C = cookies.SimpleCookie()
147+
C.load('cookie=with; invalid; version; second=cookie;')
148+
self.assertEqual(C.output(),
149+
'Set-Cookie: cookie=with\r\nSet-Cookie: second=cookie')
150+
151+
def test_extra_spaces(self):
152+
C = cookies.SimpleCookie()
153+
C.load('eggs = scrambled ; secure ; path = bar ; foo=foo ')
154+
self.assertEqual(C.output(),
155+
'Set-Cookie: eggs=scrambled; Path=bar; secure\r\nSet-Cookie: foo=foo')
156+
119157
def test_quoted_meta(self):
120158
# Try cookie with quoted meta-data
121159
C = cookies.SimpleCookie()

Misc/NEWS

Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)