test_headers.py
This file
Path
tests/client/test_headers.py
204 lines · 6.0 KiB · Python
Last changed by
Header refinements (#1248)Tom Christiebefore adoption19b863a
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 python32Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 3why, one step back3import typingRemove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
line 4why, one step back4Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 5why, one step back5import httpcoreRemove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 6–7why, one step back6import pytest7Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 8why, one step back8import httpxHttpcore interface (#804)Tom Christiebefore adoption3046e92
line 9why, one step back9from httpx._content_streams import ContentStream, JSONStreamRemove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 10–11why, one step back1011Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 12–13why, one step back12class MockTransport(httpcore.SyncHTTPTransport):13 def request(Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
line 14why, one step back14 self,Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 15why, one step back15 method: bytes,Type check tests (#1054)Florimond Mancabefore adoption0296c2b
lines 16–17why, one step back16 url: typing.Tuple[bytes, bytes, typing.Optional[int], bytes],17 headers: typing.List[typing.Tuple[bytes, bytes]] = None,Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 18why, one step back18 stream: httpcore.SyncByteStream = None,Update to httpcore 0.10 (#1126)Tom Christiebefore adoption876e722
line 19why, one step back19 timeout: typing.Mapping[str, typing.Optional[float]] = None,Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 20–22why, one step back20 ) -> typing.Tuple[21 bytes, int, bytes, typing.List[typing.Tuple[bytes, bytes]], ContentStream22 ]:Type check tests (#1054)Florimond Mancabefore adoption0296c2b
line 23why, one step back23 assert headers is not NoneImprove list/dict comprehensions (#1036)Taneli Hukkinenbefore adoption6cf9039
lines 24–26why, one step back24 headers_dict = {25 key.decode("ascii"): value.decode("ascii") for key, value in headers26 }Httpcore interface (#804)Tom Christiebefore adoption3046e92
lines 27–28why, one step back27 body = JSONStream({"headers": headers_dict})28 return b"HTTP/1.1", 200, b"OK", [], bodyRemove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 29–30why, one step back2930Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 31why, one step back31def test_client_header():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 32–37why, one step back32 """33 Set a header in the Client.34 """35 url = "http://example.org/echo_headers"36 headers = {"Example-Header": "example-value"}37Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 38–39why, one step back38 client = httpx.Client(transport=MockTransport(), headers=headers)39 response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 40–48why, one step back4041 assert response.status_code == 20042 assert response.json() == {43 "headers": {44 "accept": "*/*",45 "accept-encoding": "gzip, deflate, br",46 "connection": "keep-alive",47 "example-header": "example-value",48 "host": "example.org",Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 49why, one step back49 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 50–53why, one step back50 }51 }5253Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 54why, one step back54def test_header_merge():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 55–57why, one step back55 url = "http://example.org/echo_headers"56 client_headers = {"User-Agent": "python-myclient/0.2.1"}57 request_headers = {"X-Auth-Token": "FooBarBazToken"}Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 58–59why, one step back58 client = httpx.Client(transport=MockTransport(), headers=client_headers)59 response = client.get(url, headers=request_headers)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 60–73why, one step back6061 assert response.status_code == 20062 assert response.json() == {63 "headers": {64 "accept": "*/*",65 "accept-encoding": "gzip, deflate, br",66 "connection": "keep-alive",67 "host": "example.org",68 "user-agent": "python-myclient/0.2.1",69 "x-auth-token": "FooBarBazToken",70 }71 }7273Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 74why, one step back74def test_header_merge_conflicting_headers():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 75–77why, one step back75 url = "http://example.org/echo_headers"76 client_headers = {"X-Auth-Token": "FooBar"}77 request_headers = {"X-Auth-Token": "BazToken"}Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 78–79why, one step back78 client = httpx.Client(transport=MockTransport(), headers=client_headers)79 response = client.get(url, headers=request_headers)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 80–87why, one step back8081 assert response.status_code == 20082 assert response.json() == {83 "headers": {84 "accept": "*/*",85 "accept-encoding": "gzip, deflate, br",86 "connection": "keep-alive",87 "host": "example.org",Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 88why, one step back88 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 89–93why, one step back89 "x-auth-token": "BazToken",90 }91 }9293Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 94why, one step back94def test_header_update():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
line 95why, one step back95 url = "http://example.org/echo_headers"Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 96–97why, one step back96 client = httpx.Client(transport=MockTransport())97 first_response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 98–100why, one step back98 client.headers.update(99 {"User-Agent": "python-myclient/0.2.1", "Another-Header": "AThing"}100 )Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 101why, one step back101 second_response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 102–109why, one step back102103 assert first_response.status_code == 200104 assert first_response.json() == {105 "headers": {106 "accept": "*/*",107 "accept-encoding": "gzip, deflate, br",108 "connection": "keep-alive",109 "host": "example.org",Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 110why, one step back110 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 111–126why, one step back111 }112 }113114 assert second_response.status_code == 200115 assert second_response.json() == {116 "headers": {117 "accept": "*/*",118 "accept-encoding": "gzip, deflate, br",119 "another-header": "AThing",120 "connection": "keep-alive",121 "host": "example.org",122 "user-agent": "python-myclient/0.2.1",123 }124 }125126Header refinements (#1248)Tom Christiebefore adoption19b863a
lines 127–148why, one step back127def test_remove_default_header():128 """129 Remove a default header from the Client.130 """131 url = "http://example.org/echo_headers"132133 client = httpx.Client(transport=MockTransport())134 del client.headers["User-Agent"]135136 response = client.get(url)137138 assert response.status_code == 200139 assert response.json() == {140 "headers": {141 "accept": "*/*",142 "accept-encoding": "gzip, deflate, br",143 "connection": "keep-alive",144 "host": "example.org",145 }146 }147148Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
line 149why, one step back149def test_header_does_not_exist():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 150why, one step back150 headers = httpx.Headers({"foo": "bar"})Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 151–154why, one step back151 with pytest.raises(KeyError):152 del headers["baz"]153154Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 155why, one step back155def test_host_with_auth_and_port_in_url():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 156–162why, one step back156 """157 The Host header should only include the hostname, or hostname:port158 (for non-default ports only). Any userinfo or default port should not159 be present.160 """161 url = "http://username:password@example.org:80/echo_headers"162Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 163–164why, one step back163 client = httpx.Client(transport=MockTransport())164 response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 165–172why, one step back165166 assert response.status_code == 200167 assert response.json() == {168 "headers": {169 "accept": "*/*",170 "accept-encoding": "gzip, deflate, br",171 "connection": "keep-alive",172 "host": "example.org",Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 173why, one step back173 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 174–178why, one step back174 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",175 }176 }177178Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 179why, one step back179def test_host_with_non_default_port_in_url():Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 180–185why, one step back180 """181 If the URL includes a non-default port, then it should be included in182 the Host header.183 """184 url = "http://username:password@example.org:123/echo_headers"185Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 186–187why, one step back186 client = httpx.Client(transport=MockTransport())187 response = client.get(url)Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 188–195why, one step back188189 assert response.status_code == 200190 assert response.json() == {191 "headers": {192 "accept": "*/*",193 "accept-encoding": "gzip, deflate, br",194 "connection": "keep-alive",195 "host": "example.org:123",Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 196why, one step back196 "user-agent": f"python-httpx/{httpx.__version__}",Remove stitches in `requirements.txt` (#888)Florimond Mancabefore adoption3acd72a
lines 197–199why, one step back197 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",198 }199 }Fix request auto headers (#1205)Tom Christiebefore adoption19515e8
lines 200–202why, one step back200201202def test_request_auto_headers():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 203why, one step back203 request = httpx.Request("GET", "https://www.example.org/")Fix request auto headers (#1205)Tom Christiebefore adoption19515e8
line 204why, one step back204 assert "host" in request.headers