Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent b9db5e1 · Browse files · 2 files+38 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+38 added−8 removed

  • tests:+34 added−7 removed
  • other:+4 added−1 removed

2 files+38 added−8 removed

modified:httpx/_models.py+4 added-1 removed
1194 unmodified lines
1195 self.response = response11961197 def info(self) -> email.message.Message:1198 info = email.message.Message()1199 for key, value in self.response.headers.items():1199 for key, value in self.response.headers.multi_items():1200 #  Note that setting `info[key]` here is an "append" operation,1201 # not a "replace" operation.1202 # https://docs.python.org/3/library/email.compat32-message.html#email.message.Message.__setitem__1203 info[key] = value1204 return info
modified:tests/models/test_cookies.py+34 added-7 removed
1import http23import pytest43from httpx import CookieConflict, Cookies5import httpx678def test_cookies():7 cookies = Cookies({"name": "value"})9 cookies = httpx.Cookies({"name": "value"})10 assert cookies["name"] == "value"11 assert "name" in cookies12 assert len(cookies) == 113 assert dict(cookies) == {"name": "value"}
6 unmodified lines
20 assert bool(cookies) is False212223def test_cookies_update():22 cookies = Cookies()23 more_cookies = Cookies()24 cookies = httpx.Cookies()25 more_cookies = httpx.Cookies()26 more_cookies.set("name", "value", domain="example.com")2728 cookies.update(more_cookies)29 assert dict(cookies) == {"name": "value"}30 assert cookies.get("name", domain="example.com") == "value"313233def test_cookies_with_domain():32 cookies = Cookies()34 cookies = httpx.Cookies()35 cookies.set("name", "value", domain="example.com")36 cookies.set("name", "value", domain="example.org")3736 with pytest.raises(CookieConflict):38 with pytest.raises(httpx.CookieConflict):39 cookies["name"]4041 cookies.clear(domain="example.com")42 assert len(cookies) == 1434445def test_cookies_with_domain_and_path():44 cookies = Cookies()46 cookies = httpx.Cookies()47 cookies.set("name", "value", domain="example.com", path="/subpath/1")48 cookies.set("name", "value", domain="example.com", path="/subpath/2")49 cookies.clear(domain="example.com", path="/subpath/1")50 assert len(cookies) == 151 cookies.delete("name", domain="example.com", path="/subpath/2")52 assert len(cookies) == 0535455def test_multiple_set_cookie():56 jar = http.cookiejar.CookieJar()57 headers = [58 (59 b"Set-Cookie",60 b"1P_JAR=2020-08-09-18; expires=Tue, 08-Sep-2020 18:33:35 GMT; "61 b"path=/; domain=.example.org; Secure",62 ),63 (64 b"Set-Cookie",65 b"NID=204=KWdXOuypc86YvRfBSiWoW1dEXfSl_5qI7sxZY4umlk4J35yNTeNEkw15"66 b"MRaujK6uYCwkrtjihTTXZPp285z_xDOUzrdHt4dj0Z5C0VOpbvdLwRdHatHAzQs7"67 b"7TsaiWY78a3qU9r7KP_RbSLvLl2hlhnWFR2Hp5nWKPsAcOhQgSg; expires=Mon, "68 b"08-Feb-2021 18:33:35 GMT; path=/; domain=.example.org; HttpOnly",69 ),70 ]71 request = httpx.Request("GET", "https://www.example.org")72 response = httpx.Response(200, request=request, headers=headers)7374 cookies = httpx.Cookies(jar)75 cookies.extract_cookies(response)7677 assert len(cookies) == 2