test_proxy_http.py
This file
Path
tests/dispatch/test_proxy_http.py
202 lines · 6.9 KiB · Python
Last changed by
Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
History of this fileTake it
1import 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 )23 async with httpx.HTTPProxy(24 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",25 ) as proxy:26 response = await proxy.request("GET", "http://example.com")2728 assert response.status_code == 40429 assert response.headers["Server"] == "origin-server"3031 assert response.request.method == "GET"32 assert response.request.url == "http://example.com"33 assert response.request.headers["Host"] == "example.com"3435 recv = raw_io.received_data36 assert len(recv) == 337 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"38 assert recv[1].startswith(39 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"40 )41 assert recv[2].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")424344@pytest.mark.parametrize("status_code", [300, 304, 308, 401, 500])45async def test_proxy_tunnel_non_2xx_response(backend, status_code):46 raw_io = MockRawSocketBackend(47 data_to_send=(48 [49 b"HTTP/1.1 %d Not Good\r\n" % status_code,50 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"51 b"Server: proxy-server\r\n"52 b"\r\n",53 ]54 ),55 )5657 with pytest.raises(httpx.ProxyError) as e:58 async with httpx.HTTPProxy(59 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",60 ) as proxy:61 await proxy.request("GET", "http://example.com")6263 # ProxyError.request should be the CONNECT request not the original request64 assert e.value.request.method == "CONNECT"65 assert e.value.request.headers["Host"] == "127.0.0.1:8000"66 assert e.value.request.url.full_path == "example.com:80"6768 # ProxyError.response should be the CONNECT response69 assert e.value.response.status_code == status_code70 assert e.value.response.headers["Server"] == "proxy-server"7172 # Verify that the request wasn't sent after receiving an error from CONNECT73 recv = raw_io.received_data74 assert len(recv) == 275 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"76 assert recv[1].startswith(77 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"78 )798081async def test_proxy_tunnel_start_tls(backend):82 raw_io = MockRawSocketBackend(83 data_to_send=(84 [85 # Tunnel Response86 b"HTTP/1.1 200 OK\r\n"87 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"88 b"Server: proxy-server\r\n"89 b"\r\n",90 # Response 191 b"HTTP/1.1 404 Not Found\r\n"92 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"93 b"Server: origin-server\r\n"94 b"Connection: keep-alive\r\n"95 b"Content-Length: 0\r\n"96 b"\r\n",97 # Response 298 b"HTTP/1.1 200 OK\r\n"99 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"100 b"Server: origin-server\r\n"101 b"Connection: keep-alive\r\n"102 b"Content-Length: 0\r\n"103 b"\r\n",104 ]105 ),106 )107 async with httpx.HTTPProxy(108 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",109 ) as proxy:110 resp = await proxy.request("GET", "https://example.com")111112 assert resp.status_code == 404113 assert resp.headers["Server"] == "origin-server"114115 assert resp.request.method == "GET"116 assert resp.request.url == "https://example.com"117 assert resp.request.headers["Host"] == "example.com"118119 await resp.read()120121 # Make another request to see that the tunnel is re-used.122 resp = await proxy.request("GET", "https://example.com/target")123124 assert resp.status_code == 200125 assert resp.headers["Server"] == "origin-server"126127 assert resp.request.method == "GET"128 assert resp.request.url == "https://example.com/target"129 assert resp.request.headers["Host"] == "example.com"130131 await resp.read()132133 recv = raw_io.received_data134 assert len(recv) == 5135 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"136 assert recv[1].startswith(137 b"CONNECT example.com:443 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"138 )139 assert recv[2] == b"--- START_TLS(example.com) ---"140 assert recv[3].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")141 assert recv[4].startswith(b"GET /target HTTP/1.1\r\nhost: example.com\r\n")142143144@pytest.mark.parametrize("proxy_mode", ["FORWARD_ONLY", "DEFAULT"])145async def test_proxy_forwarding(backend, proxy_mode):146 raw_io = MockRawSocketBackend(147 data_to_send=(148 [149 b"HTTP/1.1 200 OK\r\n"150 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"151 b"Server: origin-server\r\n"152 b"\r\n"153 ]154 ),155 )156 async with httpx.HTTPProxy(157 proxy_url="http://127.0.0.1:8000",158 backend=raw_io,159 proxy_mode=proxy_mode,160 proxy_headers={"Proxy-Authorization": "test", "Override": "2"},161 ) as proxy:162 response = await proxy.request(163 "GET", "http://example.com", headers={"override": "1"}164 )165166 assert response.status_code == 200167 assert response.headers["Server"] == "origin-server"168169 assert response.request.method == "GET"170 assert response.request.url == "http://127.0.0.1:8000"171 assert response.request.url.full_path == "http://example.com"172 assert response.request.headers["Host"] == "example.com"173174 recv = raw_io.received_data175 assert len(recv) == 2176 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"177 assert recv[1].startswith(178 b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"179 )180 assert b"proxy-authorization: test" in recv[1]181 assert b"override: 1" in recv[1]182183184def test_proxy_url_with_username_and_password():185 proxy = httpx.HTTPProxy("http://user:password@example.com:1080")186187 assert proxy.proxy_url == "http://example.com:1080"188 assert proxy.proxy_headers["Proxy-Authorization"] == "Basic dXNlcjpwYXNzd29yZA=="189190191def test_proxy_repr():192 proxy = httpx.HTTPProxy(193 "http://127.0.0.1:1080",194 proxy_headers={"Custom": "Header"},195 proxy_mode="DEFAULT",196 )197198 assert repr(proxy) == (199 "HTTPProxy(proxy_url=URL('http://127.0.0.1:1080') "200 "proxy_headers=Headers({'custom': 'Header'}) "201 "proxy_mode='DEFAULT')"202 )