Allow cookie to contains several '='

close #1500
This commit is contained in:
Mathieu Clabaut
2016-10-08 16:42:22 +02:00
parent e6a3081b42
commit 3f96c521f3
2 changed files with 30 additions and 6 deletions

View File

@@ -46,6 +46,17 @@ class WebClient(object):
self.default_headers = default_headers
self.sessions = {}
self.session_regex = session_regex and re.compile(session_regex)
self.headers = {}
def _parse_headers_in_cookies(self):
self.cookies = {}
if 'set-cookie' in self.headers:
for item in self.headers['set-cookie'].split(','):
cookie = item[:item.find(';')]
pos = cookie.find('=')
key = cookie[:pos]
value = cookie[pos+1:]
self.cookies[key.strip()] = value.strip()
def get(self, url, cookies=None, headers=None, auth=None):
return self.post(url, data=None, cookies=cookies,
@@ -149,12 +160,7 @@ class WebClient(object):
else:
raise error
# parse headers into cookies
self.cookies = {}
if 'set-cookie' in self.headers:
for item in self.headers['set-cookie'].split(','):
key, value = item[:item.find(';')].split('=')
self.cookies[key.strip()] = value.strip()
self._parse_headers_in_cookies()
# check is a new session id has been issued, symptom of broken session
if self.session_regex is not None:

View File

@@ -49,6 +49,24 @@ def stopwebserver():
webserverprocess.terminate()
class Cookie(unittest.TestCase):
def testParseMultipleEquals(self):
""" Test for issue #1500.
Ensure that a cookie containing one or more '=' is correctly parsed
"""
client = WebClient()
client.headers['set-cookie'] = "key = value with one =;"
client._parse_headers_in_cookies()
self.assertIn("key", client.cookies)
self.assertEqual(client.cookies['key'], "value with one =")
client.headers['set-cookie'] = "key = value with one = and another one =;"
client._parse_headers_in_cookies()
client._parse_headers_in_cookies()
self.assertIn("key", client.cookies)
self.assertEqual(client.cookies['key'], "value with one = and another one =")
class LiveTest(unittest.TestCase):
@classmethod