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 fileTake 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 back1#!/usr/bin/env python32Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 3why, one step back3import jsonRemove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 4–6why, one step back45import pytest6Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 7why, one step back7import httpxRefactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 8why, one step back8from tests.utils import MockTransportRemove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 9–10why, one step back910Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 11–14why, one step back11def 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 back1516Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 17why, one step back17def test_client_header():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 18–23why, one step back18 """19 Set a header in the Client.20 """21 url = "http://example.org/echo_headers"22 headers = {"Example-Header": "example-value"}23Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 24why, one step back24 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 back25 response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 26–34why, one step back2627 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 back35 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 36–39why, one step back36 }37 }3839Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 40why, one step back40def test_header_merge():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 41–43why, one step back41 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 back44 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 back45 response = client.get(url, headers=request_headers)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 46–59why, one step back4647 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 }5859Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 60why, one step back60def test_header_merge_conflicting_headers():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 61–63why, one step back61 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 back64 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 back65 response = client.get(url, headers=request_headers)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 66–73why, one step back6667 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 back74 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 75–79why, one step back75 "x-auth-token": "BazToken",76 }77 }7879Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 80why, one step back80def test_header_update():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
line 81why, one step back81 url = "http://example.org/echo_headers"Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 82why, one step back82 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 back83 first_response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 84–86why, one step back84 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 back87 second_response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 88–95why, one step back8889 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 back96 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 97–112why, one step back97 }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 }111112Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 113–118why, one step back113def test_remove_default_header():114 """115 Remove a default header from the Client.116 """117 url = "http://example.org/echo_headers"118Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 119why, one step back119 client = httpx.Client(transport=MockTransport(echo_headers))Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 120–134why, one step back120 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 }133134Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
line 135why, one step back135def test_header_does_not_exist():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 136why, one step back136 headers = httpx.Headers({"foo": "bar"})Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 137–140why, one step back137 with pytest.raises(KeyError):138 del headers["baz"]139140Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 141why, one step back141def test_host_with_auth_and_port_in_url():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 142–148why, one step back142 """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"148Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 149why, one step back149 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 back150 response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 151–158why, one step back151152 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 back159 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 160–164why, one step back160 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",161 }162 }163164Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 165why, one step back165def test_host_with_non_default_port_in_url():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 166–171why, one step back166 """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"171Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
line 172why, one step back172 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 back173 response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 174–181why, one step back174175 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 back182 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 183–185why, one step back183 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",184 }185 }Fix request auto headers (#1205)Tom Christiebefore adoption19515e8
lines 186–188why, one step back186187188def test_request_auto_headers():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 189why, one step back189 request = httpx.Request("GET", "https://www.example.org/")Fix request auto headers (#1205)Tom Christiebefore adoption19515e8
line 190why, one step back190 assert "host" in request.headers