test_proxy_http.py
This file
Path
tests/dispatch/test_proxy_http.py
206 lines · 6.9 KiB · Python
Last changed by
Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
History of this fileTake it
The walk stopped after 200 commits: lines older than 8155352 point at it.
Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 1–7why, one step back1import pytest23import httpx45from .utils import MockRawSocketBackend67Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 8–9why, one step back8@pytest.mark.asyncio9async def test_proxy_tunnel_success():Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 10–24why, one step back10 raw_io = MockRawSocketBackend(11 data_to_send=(12 [13 b"HTTP/1.1 200 OK\r\n"14 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"15 b"Server: proxy-server\r\n"16 b"\r\n",17 b"HTTP/1.1 404 Not Found\r\n"18 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"19 b"Server: origin-server\r\n"20 b"\r\n",21 ]22 ),23 )24 async with httpx.HTTPProxy(Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 25why, one step back25 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 26why, one step back26 ) as proxy:Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 27why, one step back27 response = await proxy.request("GET", "http://example.com")Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 28–44why, one step back2829 assert response.status_code == 40430 assert response.headers["Server"] == "origin-server"3132 assert response.request.method == "GET"33 assert response.request.url == "http://example.com"34 assert response.request.headers["Host"] == "example.com"3536 recv = raw_io.received_data37 assert len(recv) == 338 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"39 assert recv[1].startswith(40 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"41 )42 assert recv[2].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")4344Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 45why, one step back45@pytest.mark.asyncioDocument digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 46why, one step back46@pytest.mark.parametrize("status_code", [300, 304, 308, 401, 500])Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 47why, one step back47async def test_proxy_tunnel_non_2xx_response(status_code):Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 48–60why, one step back48 raw_io = MockRawSocketBackend(49 data_to_send=(50 [51 b"HTTP/1.1 %d Not Good\r\n" % status_code,52 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"53 b"Server: proxy-server\r\n"54 b"\r\n",55 ]56 ),57 )5859 with pytest.raises(httpx.ProxyError) as e:60 async with httpx.HTTPProxy(Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 61why, one step back61 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 62why, one step back62 ) as proxy:Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 63why, one step back63 await proxy.request("GET", "http://example.com")Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 64–82why, one step back6465 # ProxyError.request should be the CONNECT request not the original request66 assert e.value.request.method == "CONNECT"67 assert e.value.request.headers["Host"] == "127.0.0.1:8000"68 assert e.value.request.url.full_path == "example.com:80"6970 # ProxyError.response should be the CONNECT response71 assert e.value.response.status_code == status_code72 assert e.value.response.headers["Server"] == "proxy-server"7374 # Verify that the request wasn't sent after receiving an error from CONNECT75 recv = raw_io.received_data76 assert len(recv) == 277 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"78 assert recv[1].startswith(79 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"80 )8182Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
lines 83–84why, one step back83@pytest.mark.asyncio84async def test_proxy_tunnel_start_tls():Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 85–87why, one step back85 raw_io = MockRawSocketBackend(86 data_to_send=(87 [Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 88why, one step back88 # Tunnel ResponseDocument digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 89–92why, one step back89 b"HTTP/1.1 200 OK\r\n"90 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"91 b"Server: proxy-server\r\n"92 b"\r\n",Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 93why, one step back93 # Response 1Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 94–96why, one step back94 b"HTTP/1.1 404 Not Found\r\n"95 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"96 b"Server: origin-server\r\n"Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 97–98why, one step back97 b"Connection: keep-alive\r\n"98 b"Content-Length: 0\r\n"Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 99why, one step back99 b"\r\n",Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 100–106why, one step back100 # Response 2101 b"HTTP/1.1 200 OK\r\n"102 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"103 b"Server: origin-server\r\n"104 b"Connection: keep-alive\r\n"105 b"Content-Length: 0\r\n"106 b"\r\n",Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 107–110why, one step back107 ]108 ),109 )110 async with httpx.HTTPProxy(Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 111why, one step back111 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 112why, one step back112 ) as proxy:Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 113why, one step back113 resp = await proxy.request("GET", "https://example.com")Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 114why, one step back114Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 115–116why, one step back115 assert resp.status_code == 404116 assert resp.headers["Server"] == "origin-server"Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 117why, one step back117Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 118–120why, one step back118 assert resp.request.method == "GET"119 assert resp.request.url == "https://example.com"120 assert resp.request.headers["Host"] == "example.com"Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 121why, one step back121Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 122why, one step back122 await resp.aread()Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 123–124why, one step back123124 # Make another request to see that the tunnel is re-used.Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 125why, one step back125 resp = await proxy.request("GET", "https://example.com/target")Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 126–133why, one step back126127 assert resp.status_code == 200128 assert resp.headers["Server"] == "origin-server"129130 assert resp.request.method == "GET"131 assert resp.request.url == "https://example.com/target"132 assert resp.request.headers["Host"] == "example.com"133Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 134why, one step back134 await resp.aread()Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 135why, one step back135Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 136why, one step back136 recv = raw_io.received_dataConfigure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 137why, one step back137 assert len(recv) == 5Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 138–143why, one step back138 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"139 assert recv[1].startswith(140 b"CONNECT example.com:443 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"141 )142 assert recv[2] == b"--- START_TLS(example.com) ---"143 assert recv[3].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 144why, one step back144 assert recv[4].startswith(b"GET /target HTTP/1.1\r\nhost: example.com\r\n")Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 145–146why, one step back145146Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 147why, one step back147@pytest.mark.asyncioTighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 148why, one step back148@pytest.mark.parametrize("proxy_mode", ["FORWARD_ONLY", "DEFAULT"])Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be
line 149why, one step back149async def test_proxy_forwarding(proxy_mode):Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 150–160why, one step back150 raw_io = MockRawSocketBackend(151 data_to_send=(152 [153 b"HTTP/1.1 200 OK\r\n"154 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"155 b"Server: origin-server\r\n"156 b"\r\n"157 ]158 ),159 )160 async with httpx.HTTPProxy(Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 161–164why, one step back161 proxy_url="http://127.0.0.1:8000",162 backend=raw_io,163 proxy_mode=proxy_mode,164 proxy_headers={"Proxy-Authorization": "test", "Override": "2"},Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 165why, one step back165 ) as proxy:Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
line 166why, one step back166 response = await proxy.request(Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 167why, one step back167 "GET", "http://example.com", headers={"override": "1"}Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
line 168why, one step back168 )Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 169–183why, one step back169170 assert response.status_code == 200171 assert response.headers["Server"] == "origin-server"172173 assert response.request.method == "GET"174 assert response.request.url == "http://127.0.0.1:8000"175 assert response.request.url.full_path == "http://example.com"176 assert response.request.headers["Host"] == "example.com"177178 recv = raw_io.received_data179 assert len(recv) == 2180 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"181 assert recv[1].startswith(182 b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"183 )Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 184–185why, one step back184 assert b"proxy-authorization: test" in recv[1]185 assert b"override: 1" in recv[1]Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 186–198why, one step back186187188def test_proxy_url_with_username_and_password():189 proxy = httpx.HTTPProxy("http://user:password@example.com:1080")190191 assert proxy.proxy_url == "http://example.com:1080"192 assert proxy.proxy_headers["Proxy-Authorization"] == "Basic dXNlcjpwYXNzd29yZA=="193194195def test_proxy_repr():196 proxy = httpx.HTTPProxy(197 "http://127.0.0.1:1080",198 proxy_headers={"Custom": "Header"},Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 199why, one step back199 proxy_mode="DEFAULT",Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
lines 200–204why, one step back200 )201202 assert repr(proxy) == (203 "HTTPProxy(proxy_url=URL('http://127.0.0.1:1080') "204 "proxy_headers=Headers({'custom': 'Header'}) "Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 205why, one step back205 "proxy_mode='DEFAULT')"Document digest auth (#348)Yeray Diaz Diazbefore adoption8155352
line 206why, one step back206 )