]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fix behaviour with multiple Set-Cookie headers (#1156)
authorTom Christie <tom@tomchristie.com>
Mon, 10 Aug 2020 13:53:51 +0000 (14:53 +0100)
committerGitHub <noreply@github.com>
Mon, 10 Aug 2020 13:53:51 +0000 (14:53 +0100)
httpx/_models.py
tests/models/test_cookies.py

index 2d75e8f542e4da0fcab7c8060d34aa0ec4c1fd72..d388ea9b7864b574251b7d9cb5d35808d801eb8c 100644 (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
index 554681e1ec0a5b3fd5b98d3177c18ea1311d642a..bd485b349f1b6aa7dedf781134742355ab5f45fb 100644 (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