Skip to content

Commit

Permalink
Made cookies construct-able from a list of tuples (#1211)
Browse files Browse the repository at this point in the history
* Added test which checks that cookie might be built from a list of tuples (#1209)

* Made cookies constructable from a list of tuples (#1209)
  • Loading branch information
cdeler authored Aug 24, 2020
1 parent 15c1e42 commit 6e6ece6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,10 @@ def __init__(self, cookies: CookieTypes = None) -> None:
if isinstance(cookies, dict):
for key, value in cookies.items():
self.set(key, value)
elif isinstance(cookies, list):
self.jar = CookieJar()
for key, value in cookies:
self.set(key, value)
elif isinstance(cookies, Cookies):
self.jar = CookieJar()
for cookie in cookies.jar:
Expand Down
2 changes: 1 addition & 1 deletion httpx/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
Sequence[Tuple[bytes, bytes]],
]

CookieTypes = Union["Cookies", CookieJar, Dict[str, str]]
CookieTypes = Union["Cookies", CookieJar, Dict[str, str], List[Tuple[str, str]]]

CertTypes = Union[str, Tuple[str, str], Tuple[str, str, str]]
VerifyTypes = Union[str, bool, ssl.SSLContext]
Expand Down
10 changes: 10 additions & 0 deletions tests/models/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,13 @@ def test_multiple_set_cookie():
cookies.extract_cookies(response)

assert len(cookies) == 2


def test_cookies_can_be_a_list_of_tuples():
cookies_val = [("name1", "val1"), ("name2", "val2")]

cookies = httpx.Cookies(cookies_val)

assert len(cookies.items()) == 2
for k, v in cookies_val:
assert cookies[k] == v

0 comments on commit 6e6ece6

Please sign in to comment.