]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Support header comparisons with dict or list. (#1326)
authorMusale Martin <martinmshale@gmail.com>
Fri, 25 Sep 2020 11:28:34 +0000 (14:28 +0300)
committerGitHub <noreply@github.com>
Fri, 25 Sep 2020 11:28:34 +0000 (12:28 +0100)
* Support header comparisons with dict or list.

* Add check for no headers item

* Fixup testcases affected by headers comparison using dict or list

* Update test_responses.py

Co-authored-by: Tom Christie <tom@tomchristie.com>
httpx/_models.py
tests/models/test_headers.py
tests/models/test_requests.py
tests/models/test_responses.py

index 4fe59d4c5030c70dd11f95940365867b333f8bc2..4ef35ed88af2f024da23d5eb5d26b1c88908133c 100644 (file)
@@ -724,9 +724,11 @@ class Headers(typing.MutableMapping[str, str]):
         return len(self._list)
 
     def __eq__(self, other: typing.Any) -> bool:
-        if not isinstance(other, Headers):
+        try:
+            other_headers = Headers(other)
+        except ValueError:
             return False
-        return sorted(self._list) == sorted(other._list)
+        return sorted(self._list) == sorted(other_headers._list)
 
     def __repr__(self) -> str:
         class_name = self.__class__.__name__
index 263db1192082bfbedf03c68e9347f401f6a42826..d671dc41869cec7c8b5b9edc98377b4af7dff866 100644 (file)
@@ -22,8 +22,10 @@ def test_headers():
     assert list(h) == ["a", "b"]
     assert dict(h) == {"a": "123, 456", "b": "789"}
     assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"
-    assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])
-    assert h != [("a", "123"), ("A", "456"), ("b", "789")]
+    assert h == [("a", "123"), ("b", "789"), ("a", "456")]
+    assert h == [("a", "123"), ("A", "456"), ("b", "789")]
+    assert h == {"a": "123", "A": "456", "b": "789"}
+    assert h != "a: 123\nA: 456\nb: 789"
 
     h = httpx.Headers({"a": "123", "b": "789"})
     assert h["A"] == "123"
index c20a11bc138fe6d47cc369bfa4bc6ff62f1a542a..8756a45985a4a4c9ce46a01c7e1e2fa5e027d413 100644 (file)
@@ -26,9 +26,7 @@ def test_iterable_content():
             yield b"test 123"  # pragma: nocover
 
     request = httpx.Request("POST", "http://example.org", content=Content())
-    assert request.headers == httpx.Headers(
-        {"Host": "example.org", "Transfer-Encoding": "chunked"}
-    )
+    assert request.headers == {"Host": "example.org", "Transfer-Encoding": "chunked"}
 
 
 def test_generator_with_transfer_encoding_header():
@@ -36,9 +34,7 @@ def test_generator_with_transfer_encoding_header():
         yield b"test 123"  # pragma: nocover
 
     request = httpx.Request("POST", "http://example.org", content=content())
-    assert request.headers == httpx.Headers(
-        {"Host": "example.org", "Transfer-Encoding": "chunked"}
-    )
+    assert request.headers == {"Host": "example.org", "Transfer-Encoding": "chunked"}
 
 
 def test_generator_with_content_length_header():
@@ -49,9 +45,7 @@ def test_generator_with_content_length_header():
     request = httpx.Request(
         "POST", "http://example.org", content=content(), headers=headers
     )
-    assert request.headers == httpx.Headers(
-        {"Host": "example.org", "Content-Length": "8"}
-    )
+    assert request.headers == {"Host": "example.org", "Content-Length": "8"}
 
 
 def test_url_encoded_data():
@@ -73,13 +67,11 @@ def test_json_encoded_data():
 def test_headers():
     request = httpx.Request("POST", "http://example.org", json={"test": 123})
 
-    assert request.headers == httpx.Headers(
-        {
-            "Host": "example.org",
-            "Content-Type": "application/json",
-            "Content-Length": "13",
-        }
-    )
+    assert request.headers == {
+        "Host": "example.org",
+        "Content-Type": "application/json",
+        "Content-Length": "13",
+    }
 
 
 def test_read_and_stream_data():
index 6241fd3e22ef898804cdb9ea4f6648d4894b8576..ef26beda09685b4aea2e636994a031c885a0ab29 100644 (file)
@@ -44,11 +44,7 @@ def test_response_content():
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
     assert response.text == "Hello, world!"
-    assert response.headers == httpx.Headers(
-        {
-            "Content-Length": "13",
-        }
-    )
+    assert response.headers == {"Content-Length": "13"}
 
 
 def test_response_text():
@@ -57,12 +53,10 @@ def test_response_text():
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
     assert response.text == "Hello, world!"
-    assert response.headers == httpx.Headers(
-        {
-            "Content-Length": "13",
-            "Content-Type": "text/plain; charset=utf-8",
-        }
-    )
+    assert response.headers == {
+        "Content-Length": "13",
+        "Content-Type": "text/plain; charset=utf-8",
+    }
 
 
 def test_response_html():
@@ -71,12 +65,10 @@ def test_response_html():
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
     assert response.text == "<html><body>Hello, world!</html></body>"
-    assert response.headers == httpx.Headers(
-        {
-            "Content-Length": "39",
-            "Content-Type": "text/html; charset=utf-8",
-        }
-    )
+    assert response.headers == {
+        "Content-Length": "39",
+        "Content-Type": "text/html; charset=utf-8",
+    }
 
 
 def test_response_json():
@@ -85,12 +77,10 @@ def test_response_json():
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
     assert response.json() == {"hello": "world"}
-    assert response.headers == httpx.Headers(
-        {
-            "Content-Length": "18",
-            "Content-Type": "application/json",
-        }
-    )
+    assert response.headers == {
+        "Content-Length": "18",
+        "Content-Type": "application/json",
+    }
 
 
 def test_raise_for_status():
@@ -742,7 +732,7 @@ def test_generator_with_transfer_encoding_header():
         yield b"test 123"  # pragma: nocover
 
     response = httpx.Response(200, content=content())
-    assert response.headers == httpx.Headers({"Transfer-Encoding": "chunked"})
+    assert response.headers == {"Transfer-Encoding": "chunked"}
 
 
 def test_generator_with_content_length_header():
@@ -751,4 +741,4 @@ def test_generator_with_content_length_header():
 
     headers = {"Content-Length": "8"}
     response = httpx.Response(200, content=content(), headers=headers)
-    assert response.headers == httpx.Headers({"Content-Length": "8"})
+    assert response.headers == {"Content-Length": "8"}