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
1import pytest23import httpx4from httpx._dispatch.proxy_http import HTTPProxy56from .utils import MockRawSocketBackend789@pytest.mark.asyncio10async def test_proxy_tunnel_success():11 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 )25 async with HTTPProxy(26 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",27 ) 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")444546@pytest.mark.asyncio47@pytest.mark.parametrize("status_code", [300, 304, 308, 401, 500])48async def test_proxy_tunnel_non_2xx_response(status_code):49 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:61 async with HTTPProxy(62 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",63 ) 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 )828384@pytest.mark.asyncio85async def test_proxy_tunnel_start_tls():86 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 )111 async with HTTPProxy(112 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",113 ) 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"122123 await resp.aread()124125 # 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"134135 await resp.aread()136137 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")146147148@pytest.mark.asyncio149@pytest.mark.parametrize("proxy_mode", ["FORWARD_ONLY", "DEFAULT"])150async def test_proxy_forwarding(proxy_mode):151 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 )161 async with HTTPProxy(162 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():190 proxy = HTTPProxy("http://user:password@example.com:1080")191192 assert proxy.proxy_url == "http://example.com:1080"193 assert proxy.proxy_headers["Proxy-Authorization"] == "Basic dXNlcjpwYXNzd29yZA=="194195196def test_proxy_repr():197 proxy = HTTPProxy(198 "http://127.0.0.1:1080",199 proxy_headers={"Custom": "Header"},200 proxy_mode="DEFAULT",201 )202203 assert repr(proxy) == (204 "HTTPProxy(proxy_url=URL('http://127.0.0.1:1080') "205 "proxy_headers=Headers({'custom': 'Header'}) "206 "proxy_mode='DEFAULT')"207 )