test_headers.py
This file
Path
tests/client/test_headers.py
182 lines · 5.5 KiB · Python
Last changed by
Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
History of this fileTake it
The walk stopped after 200 commits: lines older than b234203 point at it.
Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
lines 1–2why, one step back1#!/usr/bin/env python32Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 3why, one step back3import typingDetect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
line 4why, one step back4Httpcore interface (#804)Tom Christiebefore adoption3046e92
line 5why, one step back5import httpcoreDetect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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, JSONStreamDetect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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(Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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", [], bodyDetect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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():Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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)Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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__}",Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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():Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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)Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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():Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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)Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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__}",Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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():Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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)Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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)Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
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__}",Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
lines 111–127why, 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 }125126127def test_header_does_not_exist():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 128why, one step back128 headers = httpx.Headers({"foo": "bar"})Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
lines 129–132why, one step back129 with pytest.raises(KeyError):130 del headers["baz"]131132Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 133why, one step back133def test_host_with_auth_and_port_in_url():Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
lines 134–140why, one step back134 """135 The Host header should only include the hostname, or hostname:port136 (for non-default ports only). Any userinfo or default port should not137 be present.138 """139 url = "http://username:password@example.org:80/echo_headers"140Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 141–142why, one step back141 client = httpx.Client(transport=MockTransport())142 response = client.get(url)Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
lines 143–150why, one step back143144 assert response.status_code == 200145 assert response.json() == {146 "headers": {147 "accept": "*/*",148 "accept-encoding": "gzip, deflate, br",149 "connection": "keep-alive",150 "host": "example.org",Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 151why, one step back151 "user-agent": f"python-httpx/{httpx.__version__}",Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
lines 152–156why, one step back152 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",153 }154 }155156Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 157why, one step back157def test_host_with_non_default_port_in_url():Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
lines 158–163why, one step back158 """159 If the URL includes a non-default port, then it should be included in160 the Host header.161 """162 url = "http://username:password@example.org:123/echo_headers"163Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
lines 164–165why, one step back164 client = httpx.Client(transport=MockTransport())165 response = client.get(url)Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
lines 166–173why, one step back166167 assert response.status_code == 200168 assert response.json() == {169 "headers": {170 "accept": "*/*",171 "accept-encoding": "gzip, deflate, br",172 "connection": "keep-alive",173 "host": "example.org:123",Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 174why, one step back174 "user-agent": f"python-httpx/{httpx.__version__}",Detect credentials in proxy URLs and create Proxy-authorization header (#780)Yeray Diaz Diazbefore adoptionb234203
lines 175–177why, one step back175 "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",176 }177 }Fix request auto headers (#1205)Tom Christiebefore adoption19515e8
lines 178–180why, one step back178179180def test_request_auto_headers():Better test case consistency. Prefer `import httpx` and `httpx.Client`. (#1222)Tom Christiebefore adoption28c7205
line 181why, one step back181 request = httpx.Request("GET", "https://www.example.org/")Fix request auto headers (#1205)Tom Christiebefore adoption19515e8
line 182why, one step back182 assert "host" in request.headers