test_headers.py
This file
Path
tests/client/test_headers.py
182 lines · 5.5 KiB · Python
Last changed by
Httpcore interface (#804)Tom Christiebefore adoption3046e92
History of this fileTake it
The walk stopped after 200 commits: lines older than 07586f9 point at it.
Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 1–2why, one step back1#!/usr/bin/env python32Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 3why, one step back3import typingConvert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
line 4why, one step back4Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 5why, one step back5import httpcoreConvert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 6–7why, one step back6import pytest7Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 8–9why, one step back8from httpx import AsyncClient, Headers, __version__9from httpx._content_streams import ContentStream, JSONStreamConvert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 10–11why, one step back1011Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 12–13why, one step back12class MockDispatch(httpcore.AsyncHTTPTransport):13 async def request(Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
line 14why, one step back14 self,Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 15–27why, one step back15 method: bytes,16 url: typing.Tuple[bytes, bytes, int, bytes],17 headers: typing.List[typing.Tuple[bytes, bytes]],18 stream: ContentStream,19 timeout: typing.Dict[str, typing.Optional[float]] = None,20 ) -> typing.Tuple[21 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream22 ]:23 headers_dict = dict(24 [(key.decode("ascii"), value.decode("ascii")) for key, value in headers]25 )26 body = JSONStream({"headers": headers_dict})27 return b"HTTP/1.1", 200, b"OK", [], bodyConvert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 28–29why, one step back2829Drop sync (#544)Tom Christiebefore adoption206c537
lines 30–31why, one step back30@pytest.mark.asyncio31async def test_client_header():Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 32–37why, one step back32 """33 Set a header in the Client.34 """35 url = "http://example.org/echo_headers"36 headers = {"Example-Header": "example-value"}37Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 38why, one step back38 client = AsyncClient(dispatch=MockDispatch(), headers=headers)Drop sync (#544)Tom Christiebefore adoption206c537
line 39why, one step back39 response = await client.get(url)Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 40–53why, one step back4041 assert response.status_code == 20042 assert response.json() == {43 "headers": {44 "accept": "*/*",45 "accept-encoding": "gzip, deflate, br",46 "connection": "keep-alive",47 "example-header": "example-value",48 "host": "example.org",49 "user-agent": f"python-httpx/{__version__}",50 }51 }5253Drop sync (#544)Tom Christiebefore adoption206c537
lines 54–55why, one step back54@pytest.mark.asyncio55async def test_header_merge():Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 56–58why, one step back56 url = "http://example.org/echo_headers"57 client_headers = {"User-Agent": "python-myclient/0.2.1"}58 request_headers = {"X-Auth-Token": "FooBarBazToken"}Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 59why, one step back59 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)Drop sync (#544)Tom Christiebefore adoption206c537
line 60why, one step back60 response = await client.get(url, headers=request_headers)Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 61–74why, one step back6162 assert response.status_code == 20063 assert response.json() == {64 "headers": {65 "accept": "*/*",66 "accept-encoding": "gzip, deflate, br",67 "connection": "keep-alive",68 "host": "example.org",69 "user-agent": "python-myclient/0.2.1",70 "x-auth-token": "FooBarBazToken",71 }72 }7374Drop sync (#544)Tom Christiebefore adoption206c537
lines 75–76why, one step back75@pytest.mark.asyncio76async def test_header_merge_conflicting_headers():Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 77–79why, one step back77 url = "http://example.org/echo_headers"78 client_headers = {"X-Auth-Token": "FooBar"}79 request_headers = {"X-Auth-Token": "BazToken"}Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 80why, one step back80 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)Drop sync (#544)Tom Christiebefore adoption206c537
line 81why, one step back81 response = await client.get(url, headers=request_headers)Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 82–95why, one step back8283 assert response.status_code == 20084 assert response.json() == {85 "headers": {86 "accept": "*/*",87 "accept-encoding": "gzip, deflate, br",88 "connection": "keep-alive",89 "host": "example.org",90 "user-agent": f"python-httpx/{__version__}",91 "x-auth-token": "BazToken",92 }93 }9495Drop sync (#544)Tom Christiebefore adoption206c537
lines 96–97why, one step back96@pytest.mark.asyncio97async def test_header_update():Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
line 98why, one step back98 url = "http://example.org/echo_headers"Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 99why, one step back99 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
lines 100–104why, one step back100 first_response = await client.get(url)101 client.headers.update(102 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}103 )104 second_response = await client.get(url)Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 105–130why, one step back105106 assert first_response.status_code == 200107 assert first_response.json() == {108 "headers": {109 "accept": "*/*",110 "accept-encoding": "gzip, deflate, br",111 "connection": "keep-alive",112 "host": "example.org",113 "user-agent": f"python-httpx/{__version__}",114 }115 }116117 assert second_response.status_code == 200118 assert second_response.json() == {119 "headers": {120 "accept": "*/*",121 "accept-encoding": "gzip, deflate, br",122 "another-header": "AThing",123 "connection": "keep-alive",124 "host": "example.org",125 "user-agent": "python-myclient/0.2.1",126 }127 }128129130def test_header_does_not_exist():Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 131why, one step back131 headers = Headers({"foo": "bar"})Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 132–135why, one step back132 with pytest.raises(KeyError):133 del headers["baz"]134135Drop sync (#544)Tom Christiebefore adoption206c537
line 136why, one step back136@pytest.mark.asyncioFix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d
lines 137–142why, one step back137async def test_host_with_auth_and_port_in_url():138 """139 The Host header should only include the hostname, or hostname:port140 (for non-default ports only). Any userinfo or default port should not141 be present.142 """Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 143–144why, one step back143 url = "http://username:password@example.org:80/echo_headers"144Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 145why, one step back145 client = AsyncClient(dispatch=MockDispatch())Drop sync (#544)Tom Christiebefore adoption206c537
line 146why, one step back146 response = await client.get(url)Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 147–153why, one step back147148 assert response.status_code == 200149 assert response.json() == {150 "headers": {151 "accept": "*/*",152 "accept-encoding": "gzip, deflate, br",153 "connection": "keep-alive",Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d
lines 154–168why, one step back154 "host": "example.org",155 "user-agent": f"python-httpx/{__version__}",156 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",157 }158 }159160161@pytest.mark.asyncio162async def test_host_with_non_default_port_in_url():163 """164 If the URL includes a non-default port, then it should be included in165 the Host header.166 """167 url = "http://username:password@example.org:123/echo_headers"168Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84
line 169why, one step back169 client = AsyncClient(dispatch=MockDispatch())Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d
lines 170–178why, one step back170 response = await client.get(url)171172 assert response.status_code == 200173 assert response.json() == {174 "headers": {175 "accept": "*/*",176 "accept-encoding": "gzip, deflate, br",177 "connection": "keep-alive",178 "host": "example.org:123",Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9
lines 179–182why, one step back179 "user-agent": f"python-httpx/{__version__}",180 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",181 }182 }