Skip to content
Myr

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 file

Take 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 back
1#!/usr/bin/env python3
2

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 3why, one step back
3import typing

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

line 4why, one step back
4

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 5why, one step back
5import httpcore

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 6–7why, one step back
6import pytest
7

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 8–9why, one step back
8from httpx import AsyncClient, Headers, __version__
9from httpx._content_streams import ContentStream, JSONStream

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 10–11why, one step back
10
11

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 12–13why, one step back
12class MockDispatch(httpcore.AsyncHTTPTransport):
13 async def request(

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

line 14why, one step back
14 self,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 15–27why, one step back
15 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", [], body

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 28–29why, one step back
28
29

Drop sync (#544)Tom Christiebefore adoption206c537

lines 30–31why, one step back
30@pytest.mark.asyncio
31async def test_client_header():

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 32–37why, one step back
32 """
33 Set a header in the Client.34 """35 url = "http://example.org/echo_headers"36 headers = {"Example-Header": "example-value"}37

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 38why, one step back
38 client = AsyncClient(dispatch=MockDispatch(), headers=headers)

Drop sync (#544)Tom Christiebefore adoption206c537

line 39why, one step back
39 response = await client.get(url)

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 40–53why, one step back
40
41 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 }5253

Drop sync (#544)Tom Christiebefore adoption206c537

lines 54–55why, one step back
54@pytest.mark.asyncio
55async def test_header_merge():

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 56–58why, one step back
56 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 back
59 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)

Drop sync (#544)Tom Christiebefore adoption206c537

line 60why, one step back
60 response = await client.get(url, headers=request_headers)

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 61–74why, one step back
61
62 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 }7374

Drop sync (#544)Tom Christiebefore adoption206c537

lines 75–76why, one step back
75@pytest.mark.asyncio
76async def test_header_merge_conflicting_headers():

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 77–79why, one step back
77 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 back
80 client = AsyncClient(dispatch=MockDispatch(), headers=client_headers)

Drop sync (#544)Tom Christiebefore adoption206c537

line 81why, one step back
81 response = await client.get(url, headers=request_headers)

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 82–95why, one step back
82
83 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 }9495

Drop sync (#544)Tom Christiebefore adoption206c537

lines 96–97why, one step back
96@pytest.mark.asyncio
97async def test_header_update():

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

line 98why, one step back
98 url = "http://example.org/echo_headers"

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 99why, one step back
99 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

lines 100–104why, one step back
100 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 back
105
106 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 back
131 headers = Headers({"foo": "bar"})

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 132–135why, one step back
132 with pytest.raises(KeyError):
133 del headers["baz"]134135

Drop sync (#544)Tom Christiebefore adoption206c537

line 136why, one step back
136@pytest.mark.asyncio

Fix Host header and HSTS when the default port is explicitly included in URL (#649)Tom Christiebefore adoption5ee512d

lines 137–142why, one step back
137async 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 back
143 url = "http://username:password@example.org:80/echo_headers"
144

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 145why, one step back
145 client = AsyncClient(dispatch=MockDispatch())

Drop sync (#544)Tom Christiebefore adoption206c537

line 146why, one step back
146 response = await client.get(url)

Convert debug logs to trace logs (#500)Florimond Mancabefore adoption07586f9

lines 147–153why, one step back
147
148 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 back
154 "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"168

Rename Client to AsyncClient (with compat synonym) (#680)Florimond Mancabefore adoptione284b84

line 169why, one step back
169 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 back
170 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 back
179 "user-agent": f"python-httpx/{__version__}",
180 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",181 }182 }