Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made cookies construct-able from a list of tuples #1211

Merged
merged 2 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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