Support header comparisons with dict or list. (#1326)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* 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
authored by Musale Martin · committed by GitHub · parent 666cbbd · Browse files · 4 files·+31 added−45 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Tom Christie <tom@tomchristie.com>
Diff stats
4 files+31 added−45 removed
- tests:+27 added−43 removed
- other:+4 added−2 removed
Provenance
- Commit
815ef94- Tree
71d0aaa- Author
Musale Martin <martinmshale@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
666cbbd
4 files changed+31 added−45 removed
modified:httpx/_models.py+4 added-2 removed
722 unmodified lines723 def __len__(self) -> int:724 return len(self._list)725 726 def __eq__(self, other: typing.Any) -> bool:727- if not isinstance(other, Headers):727+ try:728+ other_headers = Headers(other)729+ except ValueError:730 return False729- return sorted(self._list) == sorted(other._list)731+ return sorted(self._list) == sorted(other_headers._list)732 733 def __repr__(self) -> str:734 class_name = self.__class__.__name__735 733 unmodified linesmodified:tests/models/test_headers.py+4 added-2 removed
20 unmodified lines21 assert h.multi_items() == [("a", "123"), ("a", "456"), ("b", "789")]22 assert list(h) == ["a", "b"]23 assert dict(h) == {"a": "123, 456", "b": "789"}24 assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"25- assert h == httpx.Headers([("a", "123"), ("b", "789"), ("a", "456")])26- assert h != [("a", "123"), ("A", "456"), ("b", "789")]25+ assert h == [("a", "123"), ("b", "789"), ("a", "456")]26+ assert h == [("a", "123"), ("A", "456"), ("b", "789")]27+ assert h == {"a": "123", "A": "456", "b": "789"}28+ assert h != "a: 123\nA: 456\nb: 789"29 30 h = httpx.Headers({"a": "123", "b": "789"})31 assert h["A"] == "123"32 assert h["B"] == "789"144 unmodified linesmodified:tests/models/test_requests.py+8 added-16 removed
24 unmodified lines25 def __iter__(self):26 yield b"test 123" # pragma: nocover27 28 request = httpx.Request("POST", "http://example.org", content=Content())29- assert request.headers == httpx.Headers(30- {"Host": "example.org", "Transfer-Encoding": "chunked"}31- )29+ assert request.headers == {"Host": "example.org", "Transfer-Encoding": "chunked"}30 31 32 def test_generator_with_transfer_encoding_header():33 def content():34 yield b"test 123" # pragma: nocover35 36 request = httpx.Request("POST", "http://example.org", content=content())39- assert request.headers == httpx.Headers(40- {"Host": "example.org", "Transfer-Encoding": "chunked"}41- )37+ assert request.headers == {"Host": "example.org", "Transfer-Encoding": "chunked"}38 39 40 def test_generator_with_content_length_header():41 def content():2 unmodified lines44 headers = {"Content-Length": "8"}45 request = httpx.Request(46 "POST", "http://example.org", content=content(), headers=headers47 )52- assert request.headers == httpx.Headers(53- {"Host": "example.org", "Content-Length": "8"}54- )48+ assert request.headers == {"Host": "example.org", "Content-Length": "8"}49 50 51 def test_url_encoded_data():52 request = httpx.Request("POST", "http://example.org", data={"test": "123"})13 unmodified lines66 67 def test_headers():68 request = httpx.Request("POST", "http://example.org", json={"test": 123})69 76- assert request.headers == httpx.Headers(77- {78- "Host": "example.org",79- "Content-Type": "application/json",80- "Content-Length": "13",81- }70+ assert request.headers == {71+ "Host": "example.org",72+ "Content-Type": "application/json",73+ "Content-Length": "13",74+ }82- )75 76 77 def test_read_and_stream_data():78 # Ensure a request may still be streamed if it has been read.97 unmodified linesmodified:tests/models/test_responses.py+15 added-25 removed
42 unmodified lines43 44 assert response.status_code == 20045 assert response.reason_phrase == "OK"46 assert response.text == "Hello, world!"47- assert response.headers == httpx.Headers(48- {49- "Content-Length": "13",50- }51- )47+ assert response.headers == {"Content-Length": "13"}48 49 50 def test_response_text():51 response = httpx.Response(200, text="Hello, world!")52 53 assert response.status_code == 20054 assert response.reason_phrase == "OK"55 assert response.text == "Hello, world!"60- assert response.headers == httpx.Headers(61- {62- "Content-Length": "13",63- "Content-Type": "text/plain; charset=utf-8",64- }56+ assert response.headers == {57+ "Content-Length": "13",58+ "Content-Type": "text/plain; charset=utf-8",59+ }65- )60 61 62 def test_response_html():63 response = httpx.Response(200, html="<html><body>Hello, world!</html></body>")64 65 assert response.status_code == 20066 assert response.reason_phrase == "OK"67 assert response.text == "<html><body>Hello, world!</html></body>"74- assert response.headers == httpx.Headers(75- {76- "Content-Length": "39",77- "Content-Type": "text/html; charset=utf-8",78- }68+ assert response.headers == {69+ "Content-Length": "39",70+ "Content-Type": "text/html; charset=utf-8",71+ }79- )72 73 74 def test_response_json():75 response = httpx.Response(200, json={"hello": "world"})76 77 assert response.status_code == 20078 assert response.reason_phrase == "OK"79 assert response.json() == {"hello": "world"}88- assert response.headers == httpx.Headers(89- {90- "Content-Length": "18",91- "Content-Type": "application/json",92- }80+ assert response.headers == {81+ "Content-Length": "18",82+ "Content-Type": "application/json",83+ }93- )84 85 86 def test_raise_for_status():87 request = httpx.Request("GET", "https://example.org")643 unmodified lines731 def content():732 yield b"test 123" # pragma: nocover733 734 response = httpx.Response(200, content=content())745- assert response.headers == httpx.Headers({"Transfer-Encoding": "chunked"})735+ assert response.headers == {"Transfer-Encoding": "chunked"}736 737 738 def test_generator_with_content_length_header():739 def content():740 yield b"test 123" # pragma: nocover741 742 headers = {"Content-Length": "8"}743 response = httpx.Response(200, content=content(), headers=headers)754- assert response.headers == httpx.Headers({"Content-Length": "8"})744+ assert response.headers == {"Content-Length": "8"}