Add repr to Cookies for displaying available cookies (#1411)

* Add repr to Cookies for displaying available cookies

* Add unit test

* Simplify repr

* Remove file
This commit is contained in:
Andrés Álvarez 2020-12-03 15:06:42 -06:00 committed by GitHub
parent 9005bd5df6
commit 28cbe77676
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -1491,6 +1491,16 @@ class Cookies(MutableMapping):
return True
return False
def __repr__(self) -> str:
cookies_repr = ", ".join(
[
f"<Cookie {cookie.name}={cookie.value} for {cookie.domain} />"
for cookie in self.jar
]
)
return f"<Cookies[{cookies_repr}]>"
class _CookieCompatRequest(urllib.request.Request):
"""
Wraps a `Request` instance up in a compatibility interface suitable

View File

@ -85,3 +85,14 @@ def test_cookies_can_be_a_list_of_tuples():
assert len(cookies.items()) == 2
for k, v in cookies_val:
assert cookies[k] == v
def test_cookies_repr():
cookies = httpx.Cookies()
cookies.set(name="foo", value="bar", domain="http://blah.com")
cookies.set(name="fizz", value="buzz", domain="http://hello.com")
assert (
repr(cookies)
== "<Cookies[<Cookie foo=bar for http://blah.com />, <Cookie fizz=buzz for http://hello.com />]>"
)