bpo-991266: Fix quoting of Comment attribute of SimpleCookie (GH-6555)

This commit is contained in:
Berker Peksag 2018-04-23 02:48:11 +03:00 committed by GitHub
parent b81ca28b37
commit d5a2377c3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 0 deletions

View file

@ -408,6 +408,8 @@ def OutputString(self, attrs=None):
append("%s=%s" % (self._reserved[key], _getdate(value)))
elif key == "max-age" and isinstance(value, int):
append("%s=%d" % (self._reserved[key], value))
elif key == "comment" and isinstance(value, str):
append("%s=%s" % (self._reserved[key], _quote(value)))
elif key in self._flags:
if value:
append(str(self._reserved[key]))

View file

@ -220,6 +220,16 @@ def test_illegal_chars(self):
with self.assertRaises(cookies.CookieError):
C.load(rawdata)
def test_comment_quoting(self):
c = cookies.SimpleCookie()
c['foo'] = '\N{COPYRIGHT SIGN}'
self.assertEqual(str(c['foo']), 'Set-Cookie: foo="\\251"')
c['foo']['comment'] = 'comment \N{COPYRIGHT SIGN}'
self.assertEqual(
str(c['foo']),
'Set-Cookie: foo="\\251"; Comment="comment \\251"'
)
class MorselTests(unittest.TestCase):
"""Tests for the Morsel object."""

View file

@ -0,0 +1 @@
Fix quoting of the ``Comment`` attribute of :class:`http.cookies.SimpleCookie`.