test_proxy_http.py
This file
Path
tests/dispatch/test_proxy_http.py
207 lines · 6.9 KiB · Python
Last changed by
Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3
History of this fileTake it
The walk stopped after 200 commits: lines older than 7361d60 point at it.
Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 1–3why, one step back1import pytest23import httpxSwitch to private module names (#785)Florimond Mancabefore adoption82dc6f3
line 4why, one step back4from httpx._dispatch.proxy_http import HTTPProxyMake nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 5–8why, one step back56from .utils import MockRawSocketBackend78Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 9–10why, one step back9@pytest.mark.asyncio10async def test_proxy_tunnel_success():Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 11–24why, one step back11 raw_io = MockRawSocketBackend(12 data_to_send=(13 [14 b"HTTP/1.1 200 OK\r\n"15 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"16 b"Server: proxy-server\r\n"17 b"\r\n",18 b"HTTP/1.1 404 Not Found\r\n"19 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"20 b"Server: origin-server\r\n"21 b"\r\n",22 ]23 ),24 )Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 25why, one step back25 async with HTTPProxy(Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 26why, one step back26 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 27–45why, one step back27 ) as proxy:28 response = await proxy.request("GET", "http://example.com")2930 assert response.status_code == 40431 assert response.headers["Server"] == "origin-server"3233 assert response.request.method == "GET"34 assert response.request.url == "http://example.com"35 assert response.request.headers["Host"] == "example.com"3637 recv = raw_io.received_data38 assert len(recv) == 339 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"40 assert recv[1].startswith(41 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"42 )43 assert recv[2].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")4445Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 46why, one step back46@pytest.mark.asyncioMake nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
line 47why, one step back47@pytest.mark.parametrize("status_code", [300, 304, 308, 401, 500])Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 48why, one step back48async def test_proxy_tunnel_non_2xx_response(status_code):Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 49–60why, one step back49 raw_io = MockRawSocketBackend(50 data_to_send=(51 [52 b"HTTP/1.1 %d Not Good\r\n" % status_code,53 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"54 b"Server: proxy-server\r\n"55 b"\r\n",56 ]57 ),58 )5960 with pytest.raises(httpx.ProxyError) as e:Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 61why, one step back61 async with HTTPProxy(Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 62why, one step back62 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 63–83why, one step back63 ) as proxy:64 await proxy.request("GET", "http://example.com")6566 # ProxyError.request should be the CONNECT request not the original request67 assert e.value.request.method == "CONNECT"68 assert e.value.request.headers["Host"] == "127.0.0.1:8000"69 assert e.value.request.url.full_path == "example.com:80"7071 # ProxyError.response should be the CONNECT response72 assert e.value.response.status_code == status_code73 assert e.value.response.headers["Server"] == "proxy-server"7475 # Verify that the request wasn't sent after receiving an error from CONNECT76 recv = raw_io.received_data77 assert len(recv) == 278 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"79 assert recv[1].startswith(80 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"81 )8283Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 84–85why, one step back84@pytest.mark.asyncio85async def test_proxy_tunnel_start_tls():Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 86–110why, one step back86 raw_io = MockRawSocketBackend(87 data_to_send=(88 [89 # Tunnel Response90 b"HTTP/1.1 200 OK\r\n"91 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"92 b"Server: proxy-server\r\n"93 b"\r\n",94 # Response 195 b"HTTP/1.1 404 Not Found\r\n"96 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"97 b"Server: origin-server\r\n"98 b"Connection: keep-alive\r\n"99 b"Content-Length: 0\r\n"100 b"\r\n",101 # Response 2102 b"HTTP/1.1 200 OK\r\n"103 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"104 b"Server: origin-server\r\n"105 b"Connection: keep-alive\r\n"106 b"Content-Length: 0\r\n"107 b"\r\n",108 ]109 ),110 )Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 111why, one step back111 async with HTTPProxy(Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 112why, one step back112 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 113–122why, one step back113 ) as proxy:114 resp = await proxy.request("GET", "https://example.com")115116 assert resp.status_code == 404117 assert resp.headers["Server"] == "origin-server"118119 assert resp.request.method == "GET"120 assert resp.request.url == "https://example.com"121 assert resp.request.headers["Host"] == "example.com"122Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 123why, one step back123 await resp.aread()Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 124–134why, one step back124125 # Make another request to see that the tunnel is re-used.126 resp = await proxy.request("GET", "https://example.com/target")127128 assert resp.status_code == 200129 assert resp.headers["Server"] == "origin-server"130131 assert resp.request.method == "GET"132 assert resp.request.url == "https://example.com/target"133 assert resp.request.headers["Host"] == "example.com"134Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 135why, one step back135 await resp.aread()Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 136–147why, one step back136137 recv = raw_io.received_data138 assert len(recv) == 5139 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"140 assert recv[1].startswith(141 b"CONNECT example.com:443 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"142 )143 assert recv[2] == b"--- START_TLS(example.com) ---"144 assert recv[3].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")145 assert recv[4].startswith(b"GET /target HTTP/1.1\r\nhost: example.com\r\n")146147Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 148why, one step back148@pytest.mark.asyncioTighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 149why, one step back149@pytest.mark.parametrize("proxy_mode", ["FORWARD_ONLY", "DEFAULT"])Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 150why, one step back150async def test_proxy_forwarding(proxy_mode):Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 151–160why, one step back151 raw_io = MockRawSocketBackend(152 data_to_send=(153 [154 b"HTTP/1.1 200 OK\r\n"155 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"156 b"Server: origin-server\r\n"157 b"\r\n"158 ]159 ),160 )Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 161why, one step back161 async with HTTPProxy(Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 162–189why, one step back162 proxy_url="http://127.0.0.1:8000",163 backend=raw_io,164 proxy_mode=proxy_mode,165 proxy_headers={"Proxy-Authorization": "test", "Override": "2"},166 ) as proxy:167 response = await proxy.request(168 "GET", "http://example.com", headers={"override": "1"}169 )170171 assert response.status_code == 200172 assert response.headers["Server"] == "origin-server"173174 assert response.request.method == "GET"175 assert response.request.url == "http://127.0.0.1:8000"176 assert response.request.url.full_path == "http://example.com"177 assert response.request.headers["Host"] == "example.com"178179 recv = raw_io.received_data180 assert len(recv) == 2181 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"182 assert recv[1].startswith(183 b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"184 )185 assert b"proxy-authorization: test" in recv[1]186 assert b"override: 1" in recv[1]187188189def test_proxy_url_with_username_and_password():Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 190why, one step back190 proxy = HTTPProxy("http://user:password@example.com:1080")Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 191–196why, one step back191192 assert proxy.proxy_url == "http://example.com:1080"193 assert proxy.proxy_headers["Proxy-Authorization"] == "Basic dXNlcjpwYXNzd29yZA=="194195196def test_proxy_repr():Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 197why, one step back197 proxy = HTTPProxy(Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 198–199why, one step back198 "http://127.0.0.1:1080",199 proxy_headers={"Custom": "Header"},Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 200why, one step back200 proxy_mode="DEFAULT",Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
lines 201–205why, one step back201 )202203 assert repr(proxy) == (204 "HTTPProxy(proxy_url=URL('http://127.0.0.1:1080') "205 "proxy_headers=Headers({'custom': 'Header'}) "Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 206why, one step back206 "proxy_mode='DEFAULT')"Make nox always reuse virtualenvs by default (#460)Florimond Mancabefore adoption7361d60
line 207why, one step back207 )