Fix behaviour with multiple Set-Cookie headers (#1156)

This commit is contained in:
Tom Christie 2020-08-10 14:53:51 +01:00 committed by GitHub
parent b9db5e149e
commit 4cf74bc405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 8 deletions

View File

@ -1196,6 +1196,9 @@ class Cookies(MutableMapping):
def info(self) -> email.message.Message:
info = email.message.Message()
for key, value in self.response.headers.items():
for key, value in self.response.headers.multi_items():
#  Note that setting `info[key]` here is an "append" operation,
# not a "replace" operation.
# https://docs.python.org/3/library/email.compat32-message.html#email.message.Message.__setitem__
info[key] = value
return info

View File

@ -1,10 +1,12 @@
import http
import pytest
from httpx import CookieConflict, Cookies
import httpx
def test_cookies():
cookies = Cookies({"name": "value"})
cookies = httpx.Cookies({"name": "value"})
assert cookies["name"] == "value"
assert "name" in cookies
assert len(cookies) == 1
@ -19,8 +21,8 @@ def test_cookies():
def test_cookies_update():
cookies = Cookies()
more_cookies = Cookies()
cookies = httpx.Cookies()
more_cookies = httpx.Cookies()
more_cookies.set("name", "value", domain="example.com")
cookies.update(more_cookies)
@ -29,11 +31,11 @@ def test_cookies_update():
def test_cookies_with_domain():
cookies = Cookies()
cookies = httpx.Cookies()
cookies.set("name", "value", domain="example.com")
cookies.set("name", "value", domain="example.org")
with pytest.raises(CookieConflict):
with pytest.raises(httpx.CookieConflict):
cookies["name"]
cookies.clear(domain="example.com")
@ -41,10 +43,35 @@ def test_cookies_with_domain():
def test_cookies_with_domain_and_path():
cookies = Cookies()
cookies = httpx.Cookies()
cookies.set("name", "value", domain="example.com", path="/subpath/1")
cookies.set("name", "value", domain="example.com", path="/subpath/2")
cookies.clear(domain="example.com", path="/subpath/1")
assert len(cookies) == 1
cookies.delete("name", domain="example.com", path="/subpath/2")
assert len(cookies) == 0
def test_multiple_set_cookie():
jar = http.cookiejar.CookieJar()
headers = [
(
b"Set-Cookie",
b"1P_JAR=2020-08-09-18; expires=Tue, 08-Sep-2020 18:33:35 GMT; "
b"path=/; domain=.example.org; Secure",
),
(
b"Set-Cookie",
b"NID=204=KWdXOuypc86YvRfBSiWoW1dEXfSl_5qI7sxZY4umlk4J35yNTeNEkw15"
b"MRaujK6uYCwkrtjihTTXZPp285z_xDOUzrdHt4dj0Z5C0VOpbvdLwRdHatHAzQs7"
b"7TsaiWY78a3qU9r7KP_RbSLvLl2hlhnWFR2Hp5nWKPsAcOhQgSg; expires=Mon, "
b"08-Feb-2021 18:33:35 GMT; path=/; domain=.example.org; HttpOnly",
),
]
request = httpx.Request("GET", "https://www.example.org")
response = httpx.Response(200, request=request, headers=headers)
cookies = httpx.Cookies(jar)
cookies.extract_cookies(response)
assert len(cookies) == 2