]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add repr to Cookies for displaying available cookies (#1411)
authorAndrés Álvarez <andresalvarez353@gmail.com>
Thu, 3 Dec 2020 21:06:42 +0000 (15:06 -0600)
committerGitHub <noreply@github.com>
Thu, 3 Dec 2020 21:06:42 +0000 (22:06 +0100)
* Add repr to Cookies for displaying available cookies

* Add unit test

* Simplify repr

* Remove file

httpx/_models.py
tests/models/test_cookies.py

index 3310ff51976df093b144008cc6a0eb7e7c94d1cc..a77f7a57904d354957bd1ae5ec2a11acecaa2ca0 100644 (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
index 980278122c1ae725f42be06ab206a7c212faee70..dbe1bfb99e5ec0b7b0277e42ae419badb1a1ce3b 100644 (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 />]>"
+    )