def test_decode_2(self): c = SimpleCookie() c['test'] = b"\xf0" c2 = SimpleCookie() c2.load(c.output()[12:]) self.assertEqual(c['test'].value, c2['test'].value) c3 = parse_cookie(c.output()[12:]) self.assertEqual(c['test'].value, c3['test'])
def test_httponly_after_load(self): """ Test that we can use httponly attribute on cookies that we load """ c = SimpleCookie() c.load("name=val") c['name']['httponly'] = True self.assertTrue(c['name']['httponly'])
def test_decode_2(self): """ Test that we haven't broken normal encoding """ c = SimpleCookie() c['test'] = b"\xf0" c2 = SimpleCookie() c2.load(c.output()) self.assertEqual(c['test'].value, c2['test'].value)
def test_decode(self): """Semicolons and commas are decoded.""" c = SimpleCookie() c['test'] = "An,awkward;value" c2 = SimpleCookie() c2.load(c.output()[12:]) self.assertEqual(c['test'].value, c2['test'].value) c3 = parse_cookie(c.output()[12:]) self.assertEqual(c['test'].value, c3['test'])
def test_decode(self): """ Test that we can still preserve semi-colons and commas """ c = SimpleCookie() c['test'] = "An,awkward;value" c2 = SimpleCookie() c2.load(c.output()) self.assertEqual(c['test'].value, c2['test'].value)
def test_decode_2(self): """ Test that we haven't broken normal encoding """ c = SimpleCookie() c["test"] = b"\xf0" c2 = SimpleCookie() c2.load(c.output()[12:]) self.assertEqual(c["test"].value, c2["test"].value) c3 = parse_cookie(c.output()[12:]) self.assertEqual(c["test"].value, c3["test"])
def test_decode(self): """ Test that we can still preserve semi-colons and commas """ c = SimpleCookie() c["test"] = "An,awkward;value" c2 = SimpleCookie() c2.load(c.output()[12:]) self.assertEqual(c["test"].value, c2["test"].value) c3 = parse_cookie(c.output()[12:]) self.assertEqual(c["test"].value, c3["test"])
def test_pickle(self): rawdata = 'Customer="WILE_E_COYOTE"; Path=/acme; Version=1' expected_output = 'Set-Cookie: %s' % rawdata C = SimpleCookie() C.load(rawdata) self.assertEqual(C.output(), expected_output) for proto in range(pickle.HIGHEST_PROTOCOL + 1): C1 = pickle.loads(pickle.dumps(C, protocol=proto)) self.assertEqual(C1.output(), expected_output)
def test_httponly_after_load(self): c = SimpleCookie() c.load("name=val") c['name']['httponly'] = True self.assertTrue(c['name']['httponly'])
def test_load_dict(self): c = SimpleCookie() c.load({"name": "val"}) self.assertEqual(c["name"].value, "val")
def translate_response(response): proxy_response = HttpResponse(response.content, status=response.status_code) if 'set-cookie' in response.headers: # Here we have to decode the Set-Cookie ourselves because # requests will pack the set-cookie headers under the same key, # comma separated, which comma SimpleCookie.load() will append # to the path in the Morsel class (ie. Path=/,). # This of course results in the browser not sending the cookie # back to us later on. #pylint: disable=protected-access if six.PY2: set_cookie_lines \ = response.raw._original_response.msg.getallmatchingheaders( 'Set-Cookie') else: # We implement our own search here because # ``getallmatchingheaders`` is broken in Python3 # (see https://bugs.python.org/issue5053) set_cookie_lines = [] for line, data in six.iteritems( response.raw._original_response.msg): if line.lower() == 'set-cookie': set_cookie_lines.append(line + ': ' + data) set_cookies_cont = '' set_cookies = [] for line in set_cookie_lines: # The parsing is complicated by the fact that # ``getallmatchingheaders`` will return continuation lines # as an entry in the list. if line.startswith('Set-Cookie'): if set_cookies_cont: set_cookies += [set_cookies_cont] set_cookies_cont = line[11:].strip() else: set_cookies_cont += line.strip() if set_cookies_cont: set_cookies += [set_cookies_cont] intercepted_cookies = SimpleCookie() for cookie_text in set_cookies: intercepted_cookies.load(cookie_text) excluded_cookies = set([ 'sessionid', # XXX hardcoded ]) for key in excluded_cookies: if key in intercepted_cookies: del intercepted_cookies[key] #pylint: disable=maybe-no-member proxy_response.cookies.update(intercepted_cookies) excluded_headers = set([ 'set-cookie', # Previously parsed. # Hop-by-hop headers # ------------------ # Certain response headers should NOT be just tunneled through. # For more info, see: # http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'te', 'trailers', 'transfer-encoding', 'upgrade', # Although content-encoding is not listed among the hop-by-hop # headers, it can cause trouble as well. Just let the server set # the value as it should be. 'content-encoding', # Since the remote server may or may not have sent the content # in the same encoding as Django will, let Django worry about # what the length should be. 'content-length', ]) for key, value in six.iteritems(response.headers): if key.lower() in excluded_headers: continue proxy_response[key] = value return proxy_response
def test_load_dict(self): c = SimpleCookie() c.load({'name': 'val'}) self.assertEqual(c['name'].value, 'val')
def test_httponly_after_load(self): c = SimpleCookie() c.load("name=val") c["name"]["httponly"] = True self.assertTrue(c["name"]["httponly"])
def translate_response(response): proxy_response = HttpResponse( response.content, status=response.status_code) if 'set-cookie' in response.headers: # Here we have to decode the Set-Cookie ourselves because # requests will pack the set-cookie headers under the same key, # comma separated, which comma SimpleCookie.load() will append # to the path in the Morsel class (ie. Path=/,). # This of course results in the browser not sending the cookie # back to us later on. #pylint: disable=protected-access if six.PY2: set_cookie_lines \ = response.raw._original_response.msg.getallmatchingheaders( 'Set-Cookie') else: # We implement our own search here because # ``getallmatchingheaders`` is broken in Python3 # (see https://bugs.python.org/issue5053) set_cookie_lines = [] for line, data in six.iteritems( response.raw._original_response.msg): if line.lower() == 'set-cookie': set_cookie_lines.append(line + ': ' + data) set_cookies_cont = '' set_cookies = [] for line in set_cookie_lines: # The parsing is complicated by the fact that # ``getallmatchingheaders`` will return continuation lines # as an entry in the list. if line.startswith('Set-Cookie'): if set_cookies_cont: set_cookies += [set_cookies_cont] set_cookies_cont = line[11:].strip() else: set_cookies_cont += line.strip() if set_cookies_cont: set_cookies += [set_cookies_cont] intercepted_cookies = SimpleCookie() for cookie_text in set_cookies: intercepted_cookies.load(cookie_text) excluded_cookies = set([ 'sessionid', # XXX hardcoded ]) for key in excluded_cookies: if key in intercepted_cookies: del intercepted_cookies[key] #pylint: disable=maybe-no-member proxy_response.cookies.update(intercepted_cookies) excluded_headers = set([ 'set-cookie', # Previously parsed. # Hop-by-hop headers # ------------------ # Certain response headers should NOT be just tunneled through. # For more info, see: # http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'te', 'trailers', 'transfer-encoding', 'upgrade', # Although content-encoding is not listed among the hop-by-hop # headers, it can cause trouble as well. Just let the server set # the value as it should be. 'content-encoding', # Since the remote server may or may not have sent the content # in the same encoding as Django will, let Django worry about # what the length should be. 'content-length', ]) for key, value in six.iteritems(response.headers): if key.lower() in excluded_headers: continue proxy_response[key] = value return proxy_response