test_proxy_http.py
This file
Path
tests/dispatch/test_proxy_http.py
214 lines · 7.2 KiB · Python
Last changed by
Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
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 backend=backend,23 )24 async with httpx.HTTPProxy(25 proxy_url="http://127.0.0.1:8000",26 backend=raw_io,27 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY,28 ) as proxy:29 response = await proxy.request("GET", "http://example.com")3031 assert response.status_code == 40432 assert response.headers["Server"] == "origin-server"3334 assert response.request.method == "GET"35 assert response.request.url == "http://example.com"36 assert response.request.headers["Host"] == "example.com"3738 recv = raw_io.received_data39 assert len(recv) == 340 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"41 assert recv[1].startswith(42 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"43 )44 assert recv[2].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")454647@pytest.mark.parametrize("status_code", [300, 304, 308, 401, 500])48async def test_proxy_tunnel_non_2xx_response(backend, 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 backend=backend,59 )6061 with pytest.raises(httpx.ProxyError) as e:62 async with httpx.HTTPProxy(63 proxy_url="http://127.0.0.1:8000",64 backend=raw_io,65 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY,66 ) as proxy:67 await proxy.request("GET", "http://example.com")6869 # ProxyError.request should be the CONNECT request not the original request70 assert e.value.request.method == "CONNECT"71 assert e.value.request.headers["Host"] == "127.0.0.1:8000"72 assert e.value.request.url.full_path == "example.com:80"7374 # ProxyError.response should be the CONNECT response75 assert e.value.response.status_code == status_code76 assert e.value.response.headers["Server"] == "proxy-server"7778 # Verify that the request wasn't sent after receiving an error from CONNECT79 recv = raw_io.received_data80 assert len(recv) == 281 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"82 assert recv[1].startswith(83 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"84 )858687async def test_proxy_tunnel_start_tls(backend):88 raw_io = MockRawSocketBackend(89 data_to_send=(90 [91 # Tunnel Response92 b"HTTP/1.1 200 OK\r\n"93 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"94 b"Server: proxy-server\r\n"95 b"\r\n",96 # Response 197 b"HTTP/1.1 404 Not Found\r\n"98 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"99 b"Server: origin-server\r\n"100 b"Connection: keep-alive\r\n"101 b"Content-Length: 0\r\n"102 b"\r\n",103 # Response 2104 b"HTTP/1.1 200 OK\r\n"105 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"106 b"Server: origin-server\r\n"107 b"Connection: keep-alive\r\n"108 b"Content-Length: 0\r\n"109 b"\r\n",110 ]111 ),112 backend=backend,113 )114 async with httpx.HTTPProxy(115 proxy_url="http://127.0.0.1:8000",116 backend=raw_io,117 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY,118 ) as proxy:119 resp = await proxy.request("GET", "https://example.com")120121 assert resp.status_code == 404122 assert resp.headers["Server"] == "origin-server"123124 assert resp.request.method == "GET"125 assert resp.request.url == "https://example.com"126 assert resp.request.headers["Host"] == "example.com"127128 await resp.read()129130 # Make another request to see that the tunnel is re-used.131 resp = await proxy.request("GET", "https://example.com/target")132133 assert resp.status_code == 200134 assert resp.headers["Server"] == "origin-server"135136 assert resp.request.method == "GET"137 assert resp.request.url == "https://example.com/target"138 assert resp.request.headers["Host"] == "example.com"139140 await resp.read()141142 recv = raw_io.received_data143 assert len(recv) == 5144 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"145 assert recv[1].startswith(146 b"CONNECT example.com:443 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"147 )148 assert recv[2] == b"--- START_TLS(example.com) ---"149 assert recv[3].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")150 assert recv[4].startswith(b"GET /target HTTP/1.1\r\nhost: example.com\r\n")151152153@pytest.mark.parametrize(154 "proxy_mode", [httpx.HTTPProxyMode.FORWARD_ONLY, httpx.HTTPProxyMode.DEFAULT]155)156async def test_proxy_forwarding(backend, proxy_mode):157 raw_io = MockRawSocketBackend(158 data_to_send=(159 [160 b"HTTP/1.1 200 OK\r\n"161 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"162 b"Server: origin-server\r\n"163 b"\r\n"164 ]165 ),166 backend=backend,167 )168 async with httpx.HTTPProxy(169 proxy_url="http://127.0.0.1:8000",170 backend=raw_io,171 proxy_mode=proxy_mode,172 proxy_headers={"Proxy-Authorization": "test", "Override": "2"},173 ) as proxy:174 response = await proxy.request(175 "GET", "http://example.com", headers={"override": "1"}176 )177178 assert response.status_code == 200179 assert response.headers["Server"] == "origin-server"180181 assert response.request.method == "GET"182 assert response.request.url == "http://127.0.0.1:8000"183 assert response.request.url.full_path == "http://example.com"184 assert response.request.headers["Host"] == "example.com"185186 recv = raw_io.received_data187 assert len(recv) == 2188 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"189 assert recv[1].startswith(190 b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"191 )192 assert b"proxy-authorization: test" in recv[1]193 assert b"override: 1" in recv[1]194195196def test_proxy_url_with_username_and_password():197 proxy = httpx.HTTPProxy("http://user:password@example.com:1080")198199 assert proxy.proxy_url == "http://example.com:1080"200 assert proxy.proxy_headers["Proxy-Authorization"] == "Basic dXNlcjpwYXNzd29yZA=="201202203def test_proxy_repr():204 proxy = httpx.HTTPProxy(205 "http://127.0.0.1:1080",206 proxy_headers={"Custom": "Header"},207 proxy_mode=httpx.HTTPProxyMode.DEFAULT,208 )209210 assert repr(proxy) == (211 "HTTPProxy(proxy_url=URL('http://127.0.0.1:1080') "212 "proxy_headers=Headers({'custom': 'Header'}) "213 "proxy_mode=<HTTPProxyMode.DEFAULT: 'DEFAULT'>)"214 )