Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

190 lines · 5.5 KiB · Python

Last changed by

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

History of this file

Take it

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 6–7why, one step back
6
7

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

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 11–12why, one step back
11
12

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 32–35why, one step back
32 }
33 }3435

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

lines 40–42why, one step back
40 client = httpx.Client(
41 transport=httpx.MockTransport(echo_headers), headers=client_headers42 )

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

lines 62–64why, one step back
62 client = httpx.Client(
63 transport=httpx.MockTransport(echo_headers), headers=client_headers64 )

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 75–79why, one step back
75 "x-auth-token": "BazToken",
76 }77 }7879

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 97–112why, one step back
97 }
98 }99100 assert second_response.status_code == 200101 assert second_response.json() == {102 "headers": {103 "accept": "*/*",104 "accept-encoding": "gzip, deflate, br",105 "another-header": "AThing",106 "connection": "keep-alive",107 "host": "example.org",108 "user-agent": "python-myclient/0.2.1",109 }110 }111112

Header refinements (#1248)Tom Christiebefore adoption19b863a

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

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

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

Header refinements (#1248)Tom Christiebefore adoption19b863a

lines 120–134why, one step back
120 del client.headers["User-Agent"]
121122 response = client.get(url)123124 assert response.status_code == 200125 assert response.json() == {126 "headers": {127 "accept": "*/*",128 "accept-encoding": "gzip, deflate, br",129 "connection": "keep-alive",130 "host": "example.org",131 }132 }133134

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 137–140why, one step back
137 with pytest.raises(KeyError):
138 del headers["baz"]139140

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 142–148why, one step back
142 """
143 The Host header should only include the hostname, or hostname:port144 (for non-default ports only). Any userinfo or default port should not145 be present.146 """147 url = "http://username:password@example.org:80/echo_headers"148

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 160–164why, one step back
160 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
161 }162 }163164

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 166–171why, one step back
166 """
167 If the URL includes a non-default port, then it should be included in168 the Host header.169 """170 url = "http://username:password@example.org:123/echo_headers"171

Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac

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

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 174–181why, one step back
174
175 assert response.status_code == 200176 assert response.json() == {177 "headers": {178 "accept": "*/*",179 "accept-encoding": "gzip, deflate, br",180 "connection": "keep-alive",181 "host": "example.org:123",

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

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

Pin isort version (#1053)Josep Cugatbefore adoption330a825

lines 183–185why, one step back
183 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
184 }185 }

Fix request auto headers (#1205)Tom Christiebefore adoption19515e8

lines 186–188why, one step back
186
187188def test_request_auto_headers():

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

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

Fix request auto headers (#1205)Tom Christiebefore adoption19515e8

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