Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

204 lines · 6.0 KiB · Python

Last changed by

Header refinements (#1248)Tom Christiebefore adoption19b863a

History of this file

Take it

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

line 4why, one step back
4

Httpcore interface (#804)Tom Christiebefore adoption3046e92

line 5why, one step back
5import httpcore

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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(

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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():

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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)

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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__}",

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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():

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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)

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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():

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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)

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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__}",

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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():

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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)

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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)

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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__}",

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

lines 111–126why, 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 }125126

Header refinements (#1248)Tom Christiebefore adoption19b863a

lines 127–148why, one step back
127def test_remove_default_header():
128 """129 Remove a default header from the Client.130 """131 url = "http://example.org/echo_headers"132133 client = httpx.Client(transport=MockTransport())134 del client.headers["User-Agent"]135136 response = client.get(url)137138 assert response.status_code == 200139 assert response.json() == {140 "headers": {141 "accept": "*/*",142 "accept-encoding": "gzip, deflate, br",143 "connection": "keep-alive",144 "host": "example.org",145 }146 }147148

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

line 149why, one step back
149def test_header_does_not_exist():

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

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

lines 151–154why, one step back
151 with pytest.raises(KeyError):
152 del headers["baz"]153154

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

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

lines 156–162why, one step back
156 """
157 The Host header should only include the hostname, or hostname:port158 (for non-default ports only). Any userinfo or default port should not159 be present.160 """161 url = "http://username:password@example.org:80/echo_headers"162

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

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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

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

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

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

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

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

lines 180–185why, one step back
180 """
181 If the URL includes a non-default port, then it should be included in182 the Host header.183 """184 url = "http://username:password@example.org:123/echo_headers"185

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

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

lines 188–195why, one step back
188
189 assert response.status_code == 200190 assert response.json() == {191 "headers": {192 "accept": "*/*",193 "accept-encoding": "gzip, deflate, br",194 "connection": "keep-alive",195 "host": "example.org:123",

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

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

Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a

lines 197–199why, one step back
197 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
198 }199 }

Fix request auto headers (#1205)Tom Christiebefore adoption19515e8

lines 200–202why, one step back
200
201202def test_request_auto_headers():

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

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

Fix request auto headers (#1205)Tom Christiebefore adoption19515e8

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