Skip to content

Commit

Permalink
Changed one of cookies types from List to Sequence (encode#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdeler committed Aug 21, 2020
1 parent e4de55b commit 749bcb6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
10 changes: 5 additions & 5 deletions httpx/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,16 +1047,16 @@ 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, CookieJar):
self.jar = cookies
elif isinstance(cookies, Cookies):
self.jar = CookieJar()
for cookie in cookies.jar:
self.jar.set_cookie(cookie)
else:
self.jar = cookies
self.jar = CookieJar()
for key, value in cookies:
self.set(key, value)

def extract_cookies(self, response: Response) -> None:
"""
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], List[Tuple[str, str]]]
CookieTypes = Union["Cookies", CookieJar, Dict[str, str], Sequence[Tuple[str, str]]]

CertTypes = Union[str, Tuple[str, str], Tuple[str, str, str]]
VerifyTypes = Union[str, bool, ssl.SSLContext]
Expand Down
24 changes: 17 additions & 7 deletions tests/models/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,21 @@ def test_multiple_set_cookie():
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:
@pytest.mark.parametrize(
"cookies_arg",
[
{"name1": "val1", "name2": "val2"},
[("name1", "val1"), ("name2", "val2")],
(("name1", "val1"), ("name2", "val2")),
{("name1", "val1"), ("name2", "val2")},
httpx.Cookies([("name1", "val1"), ("name2", "val2")]),
],
)
@pytest.mark.parametrize("expected_cookies", ([("name1", "val1"), ("name2", "val2")],))
def test_cookies_can_be_a_list_of_tuples(cookies_arg, expected_cookies):
cookies = httpx.Cookies(cookies_arg)

assert len(cookies.items()) == len(expected_cookies)

for k, v in expected_cookies:
assert cookies[k] == v

0 comments on commit 749bcb6

Please sign in to comment.