test_proxy_http.py
This file
Path
tests/dispatch/test_proxy_http.py
206 lines · 7.0 KiB · Python
Last changed by
Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
History of this fileTake it
Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 1–24why, one step back1import pytest23import httpx45from .utils import MockRawSocketBackend678async def test_proxy_tunnel_success(backend):9 raw_io = MockRawSocketBackend(10 data_to_send=(11 [12 b"HTTP/1.1 200 OK\r\n"13 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"14 b"Server: proxy-server\r\n"15 b"\r\n",16 b"HTTP/1.1 404 Not Found\r\n"17 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"18 b"Server: origin-server\r\n"19 b"\r\n",20 ]21 ),22 backend=backend,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",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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")Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 28–60why, 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")434445@pytest.mark.parametrize("status_code", [300, 304, 308, 401, 500])46async def test_proxy_tunnel_non_2xx_response(backend, status_code):47 raw_io = MockRawSocketBackend(48 data_to_send=(49 [50 b"HTTP/1.1 %d Not Good\r\n" % status_code,51 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"52 b"Server: proxy-server\r\n"53 b"\r\n",54 ]55 ),56 backend=backend,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",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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")Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 64–86why, 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 )818283async def test_proxy_tunnel_start_tls(backend):84 raw_io = MockRawSocketBackend(85 data_to_send=(86 [Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 87why, one step back87 # Tunnel ResponseImplement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 88–91why, one step back88 b"HTTP/1.1 200 OK\r\n"89 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"90 b"Server: proxy-server\r\n"91 b"\r\n",Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 92why, one step back92 # Response 1Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 93–95why, one step back93 b"HTTP/1.1 404 Not Found\r\n"94 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"95 b"Server: origin-server\r\n"Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 96–97why, one step back96 b"Connection: keep-alive\r\n"97 b"Content-Length: 0\r\n"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 98why, one step back98 b"\r\n",Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 99–105why, one step back99 # Response 2100 b"HTTP/1.1 200 OK\r\n"101 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"102 b"Server: origin-server\r\n"103 b"Connection: keep-alive\r\n"104 b"Content-Length: 0\r\n"105 b"\r\n",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 106–110why, one step back106 ]107 ),108 backend=backend,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",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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")Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 121why, one step back121Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 122–124why, one step back122 await resp.read()123124 # 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–135why, 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"133134 await resp.read()135Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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) == 5Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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")Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 145–146why, one step back145146Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 147why, one step back147@pytest.mark.parametrize("proxy_mode", ["FORWARD_ONLY", "DEFAULT"])Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 148–160why, one step back148async def test_proxy_forwarding(backend, proxy_mode):149 raw_io = MockRawSocketBackend(150 data_to_send=(151 [152 b"HTTP/1.1 200 OK\r\n"153 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"154 b"Server: origin-server\r\n"155 b"\r\n"156 ]157 ),158 backend=backend,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"},Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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 )Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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]Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
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')"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 206why, one step back206 )