Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

182 lines · 5.5 KiB · Python

Last changed by

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

History of this file

Take it

The walk stopped after 200 commits: lines older than b234203 point at it.

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

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

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

line 4why, one step back
4

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 5why, one step back
5import httpcore

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 8why, one step back
8import httpx

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 9why, one step back
9from httpx._content_streams import ContentStream, JSONStream

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 10–11why, one step back
10
11

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 12–13why, one step back
12class MockTransport(httpcore.SyncHTTPTransport):
13 def request(

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

line 14why, one step back
14 self,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 15why, one step back
15 method: bytes,

Type check tests (#1054)Florimond Mancabefore adoption0296c2b

lines 16–17why, one step back
16 url: typing.Tuple[bytes, bytes, typing.Optional[int], bytes],
17 headers: typing.List[typing.Tuple[bytes, bytes]] = None,

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 18why, one step back
18 stream: httpcore.SyncByteStream = None,

Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722

line 19why, one step back
19 timeout: typing.Mapping[str, typing.Optional[float]] = None,

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 20–22why, one step back
20 ) -> typing.Tuple[
21 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream22 ]:

Type check tests (#1054)Florimond Mancabefore adoption0296c2b

line 23why, one step back
23 assert headers is not None

Improve list/dict comprehensions (#1036)Taneli Hukkinenbefore adoption6cf9039

lines 24–26why, one step back
24 headers_dict = {
25 key.decode("ascii"): value.decode("ascii") for key, value in headers26 }

Httpcore interface (#804)Tom Christiebefore adoption3046e92

lines 27–28why, one step back
27 body = JSONStream({"headers": headers_dict})
28 return b"HTTP/1.1", 200, b"OK", [], body

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 29–30why, one step back
29
30

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 31why, one step back
31def test_client_header():

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 38–39why, one step back
38 client = httpx.Client(transport=MockTransport(), headers=headers)
39 response = client.get(url)

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 40–48why, 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",

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 49why, one step back
49 "user-agent": f"python-httpx/{httpx.__version__}",

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 50–53why, one step back
50 }
51 }5253

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 54why, one step back
54def test_header_merge():

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 55–57why, one step back
55 url = "http://example.org/echo_headers"
56 client_headers = {"User-Agent": "python-myclient/0.2.1"}57 request_headers = {"X-Auth-Token": "FooBarBazToken"}

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 58–59why, one step back
58 client = httpx.Client(transport=MockTransport(), headers=client_headers)
59 response = client.get(url, headers=request_headers)

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 60–73why, one step back
60
61 assert response.status_code == 20062 assert response.json() == {63 "headers": {64 "accept": "*/*",65 "accept-encoding": "gzip, deflate, br",66 "connection": "keep-alive",67 "host": "example.org",68 "user-agent": "python-myclient/0.2.1",69 "x-auth-token": "FooBarBazToken",70 }71 }7273

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 74why, one step back
74def test_header_merge_conflicting_headers():

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 75–77why, one step back
75 url = "http://example.org/echo_headers"
76 client_headers = {"X-Auth-Token": "FooBar"}77 request_headers = {"X-Auth-Token": "BazToken"}

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 78–79why, one step back
78 client = httpx.Client(transport=MockTransport(), headers=client_headers)
79 response = client.get(url, headers=request_headers)

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 80–87why, one step back
80
81 assert response.status_code == 20082 assert response.json() == {83 "headers": {84 "accept": "*/*",85 "accept-encoding": "gzip, deflate, br",86 "connection": "keep-alive",87 "host": "example.org",

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 88why, one step back
88 "user-agent": f"python-httpx/{httpx.__version__}",

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 89–93why, one step back
89 "x-auth-token": "BazToken",
90 }91 }9293

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 94why, one step back
94def test_header_update():

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

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

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 96–97why, one step back
96 client = httpx.Client(transport=MockTransport())
97 first_response = client.get(url)

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 98–100why, one step back
98 client.headers.update(
99 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}100 )

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 101why, one step back
101 second_response = client.get(url)

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 102–109why, one step back
102
103 assert first_response.status_code == 200104 assert first_response.json() == {105 "headers": {106 "accept": "*/*",107 "accept-encoding": "gzip, deflate, br",108 "connection": "keep-alive",109 "host": "example.org",

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 110why, one step back
110 "user-agent": f"python-httpx/{httpx.__version__}",

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 111–127why, one step back
111 }
112 }113114 assert second_response.status_code == 200115 assert second_response.json() == {116 "headers": {117 "accept": "*/*",118 "accept-encoding": "gzip, deflate, br",119 "another-header": "AThing",120 "connection": "keep-alive",121 "host": "example.org",122 "user-agent": "python-myclient/0.2.1",123 }124 }125126127def test_header_does_not_exist():

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 128why, one step back
128 headers = httpx.Headers({"foo": "bar"})

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 129–132why, one step back
129 with pytest.raises(KeyError):
130 del headers["baz"]131132

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 133why, one step back
133def test_host_with_auth_and_port_in_url():

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 134–140why, one step back
134 """
135 The Host header should only include the hostname, or hostname:port136 (for non-default ports only). Any userinfo or default port should not137 be present.138 """139 url = "http://username:password@example.org:80/echo_headers"140

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 141–142why, one step back
141 client = httpx.Client(transport=MockTransport())
142 response = client.get(url)

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 143–150why, one step back
143
144 assert response.status_code == 200145 assert response.json() == {146 "headers": {147 "accept": "*/*",148 "accept-encoding": "gzip, deflate, br",149 "connection": "keep-alive",150 "host": "example.org",

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 151why, one step back
151 "user-agent": f"python-httpx/{httpx.__version__}",

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 152–156why, one step back
152 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
153 }154 }155156

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 157why, one step back
157def test_host_with_non_default_port_in_url():

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 158–163why, one step back
158 """
159 If the URL includes a non-default port, then it should be included in160 the Host header.161 """162 url = "http://username:password@example.org:123/echo_headers"163

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

lines 164–165why, one step back
164 client = httpx.Client(transport=MockTransport())
165 response = client.get(url)

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 166–173why, one step back
166
167 assert response.status_code == 200168 assert response.json() == {169 "headers": {170 "accept": "*/*",171 "accept-encoding": "gzip, deflate, br",172 "connection": "keep-alive",173 "host": "example.org:123",

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 174why, one step back
174 "user-agent": f"python-httpx/{httpx.__version__}",

Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203

lines 175–177why, one step back
175 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
176 }177 }

Fix request auto headers (#1205)Tom Christiebefore adoption19515e8

lines 178–180why, one step back
178
179180def test_request_auto_headers():

Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205

line 181why, one step back
181 request = httpx.Request("GET", "https://www.example.org/")

Fix request auto headers (#1205)Tom Christiebefore adoption19515e8

line 182why, one step back
182 assert "host" in request.headers