Skip to content
Myr

test_headers.py

This file

Path

tests/client/test_headers.py

190 lines · 5.6 KiB · Python

Last changed by

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

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

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

line 3why, one step back
3import json

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

lines 4–6why, one step back
4
5import pytest6

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

line 7why, one step back
7import httpx

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

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

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

lines 9–10why, one step back
9
10

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

lines 11–14why, one step back
11def echo_headers(request: httpx.Request) -> httpx.Response:
12 data = {"headers": dict(request.headers)}13 content = json.dumps(data).encode("utf-8")14 return httpx.Response(200, content=content)

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

lines 15–16why, one step back
15
16

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

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

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

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

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

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

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

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

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

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

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

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

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

lines 36–39why, one step back
36 }
37 }3839

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

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

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

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

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

line 44why, one step back
44 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 45why, one step back
45 response = client.get(url, headers=request_headers)

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

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

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

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

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

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

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

line 64why, one step back
64 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 65why, one step back
65 response = client.get(url, headers=request_headers)

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

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

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

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

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

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

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

line 82why, one step back
82 client = httpx.Client(transport=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)

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

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)

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

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

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

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

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

line 119why, one step back
119 client = httpx.Client(transport=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

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

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"})

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

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

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

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

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

line 149why, one step back
149 client = httpx.Client(transport=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)

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

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

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

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

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

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

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

line 172why, one step back
172 client = httpx.Client(transport=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)

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

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

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

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