* 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>
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__
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"
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():
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():
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():
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():
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():
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():
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():
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():
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():
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"}