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 fileTake it
The walk stopped after 200 commits: lines older than 09c3e90 point at it.
Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
lines 1–4why, one step back1#!/usr/bin/env python323import pytest4Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 5why, one step back5import httpxAdd test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
lines 6–7why, one step back67Refactor tests to use `MockTransport(<handler_function>)` (#1281)Tom Christiebefore adoptionc2afd2d
lines 8–9why, one step back8def 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 back10 return httpx.Response(200, json=data)Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
lines 11–12why, one step back1112Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 13why, one step back13def test_client_header():Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
lines 14–19why, one step back14 """15 Set a header in the Client.16 """17 url = "http://example.org/echo_headers"18 headers = {"Example-Header": "example-value"}19Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
line 20why, one step back20 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 back21 response = client.get(url)Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
lines 22–30why, one step back2223 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 back31 "user-agent": f"python-httpx/{httpx.__version__}",Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
lines 32–35why, one step back32 }33 }3435Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 36why, one step back36def test_header_merge():Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
lines 37–39why, one step back37 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 back40 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 back43 response = client.get(url, headers=request_headers)Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
lines 44–57why, one step back4445 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 }5657Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 58why, one step back58def test_header_merge_conflicting_headers():Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
lines 59–61why, one step back59 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 back62 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 back65 response = client.get(url, headers=request_headers)Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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__}",Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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():Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
line 81why, one step back81 url = "http://example.org/echo_headers"Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
line 82why, one step back82 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 back83 first_response = client.get(url)Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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)Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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__}",Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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"118Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
line 119why, one step back119 client = httpx.Client(transport=httpx.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 }133134Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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"})Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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():Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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"148Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
line 149why, one step back149 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 back150 response = client.get(url)Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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__}",Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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():Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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"171Add `httpx.MockTransport()` (#1401)Tom Christiebefore adoption9c7c2ac
line 172why, one step back172 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 back173 response = client.get(url)Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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__}",Add test for HEAD redirect behavior (#1184)Florimond Mancabefore adoption09c3e90
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