Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

187 lines · 5.5 KiB · Python

Last changed by

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

History of this file

Take it

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 1–4why, one step back
1#!/usr/bin/env python3
23import pytest4

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

line 5why, one step back
5import httpx

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 6why, one step back
6from tests.utils import MockTransport

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 7–8why, one step back
7
8

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

lines 9–10why, one step back
9def echo_headers(request: httpx.Request) -> httpx.Response:
10 data = {"headers": dict(request.headers)}

Support Response(text=...), Response(html=...), Response(json=...) (#1297)Tom Christiebefore adoptionf3c2941

line 11why, one step back
11 return httpx.Response(200, json=data)

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 12–13why, one step back
12
13

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 15–20why, one step back
15 """
16 Set a header in the Client.17 """18 url = "http://example.org/echo_headers"19 headers = {"Example-Header": "example-value"}20

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 21why, one step back
21 client = httpx.Client(transport=MockTransport(echo_headers), headers=headers)

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 23–31why, one step back
23
24 assert response.status_code == 20025 assert response.json() == {26 "headers": {27 "accept": "*/*",28 "accept-encoding": "gzip, deflate, br",29 "connection": "keep-alive",30 "example-header": "example-value",31 "host": "example.org",

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 33–36why, one step back
33 }
34 }3536

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

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

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 41why, one step back
41 client = httpx.Client(transport=MockTransport(echo_headers), headers=client_headers)

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 43–56why, one step back
43
44 assert response.status_code == 20045 assert response.json() == {46 "headers": {47 "accept": "*/*",48 "accept-encoding": "gzip, deflate, br",49 "connection": "keep-alive",50 "host": "example.org",51 "user-agent": "python-myclient/0.2.1",52 "x-auth-token": "FooBarBazToken",53 }54 }5556

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

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

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 61why, one step back
61 client = httpx.Client(transport=MockTransport(echo_headers), headers=client_headers)

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 63–70why, one step back
63
64 assert response.status_code == 20065 assert response.json() == {66 "headers": {67 "accept": "*/*",68 "accept-encoding": "gzip, deflate, br",69 "connection": "keep-alive",70 "host": "example.org",

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 72–76why, one step back
72 "x-auth-token": "BazToken",
73 }74 }7576

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

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

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 79why, one step back
79 client = httpx.Client(transport=MockTransport(echo_headers))

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

line 80why, one step back
80 first_response = client.get(url)

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

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

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 85–92why, one step back
85
86 assert first_response.status_code == 20087 assert first_response.json() == {88 "headers": {89 "accept": "*/*",90 "accept-encoding": "gzip, deflate, br",91 "connection": "keep-alive",92 "host": "example.org",

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 94–109why, one step back
94 }
95 }9697 assert second_response.status_code == 20098 assert second_response.json() == {99 "headers": {100 "accept": "*/*",101 "accept-encoding": "gzip, deflate, br",102 "another-header": "AThing",103 "connection": "keep-alive",104 "host": "example.org",105 "user-agent": "python-myclient/0.2.1",106 }107 }108109

Header refinements (#1248)Tom Christiebefore adoption19b863a

lines 110–115why, one step back
110def test_remove_default_header():
111 """112 Remove a default header from the Client.113 """114 url = "http://example.org/echo_headers"115

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 116why, one step back
116 client = httpx.Client(transport=MockTransport(echo_headers))

Header refinements (#1248)Tom Christiebefore adoption19b863a

lines 117–131why, one step back
117 del client.headers["User-Agent"]
118119 response = client.get(url)120121 assert response.status_code == 200122 assert response.json() == {123 "headers": {124 "accept": "*/*",125 "accept-encoding": "gzip, deflate, br",126 "connection": "keep-alive",127 "host": "example.org",128 }129 }130131

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

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

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 134–137why, one step back
134 with pytest.raises(KeyError):
135 del headers["baz"]136137

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

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

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 146why, one step back
146 client = httpx.Client(transport=MockTransport(echo_headers))

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 148–155why, one step back
148
149 assert response.status_code == 200150 assert response.json() == {151 "headers": {152 "accept": "*/*",153 "accept-encoding": "gzip, deflate, br",154 "connection": "keep-alive",155 "host": "example.org",

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 157–161why, one step back
157 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
158 }159 }160161

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 163–168why, one step back
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

Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d

line 169why, one step back
169 client = httpx.Client(transport=MockTransport(echo_headers))

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 171–178why, one step back
171
172 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",

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

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

Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417

lines 180–182why, one step back
180 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
181 }182 }

Fix request auto headers (#1205)Tom Christiebefore adoption19515e8

lines 183–185why, one step back
183
184185def test_request_auto_headers():

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

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

Fix request auto headers (#1205)Tom Christiebefore adoption19515e8

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