test_proxy_http.py
This file
Path
tests/dispatch/test_proxy_http.py
207 lines · 6.9 KiB · Python
Last changed by
Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
History of this fileTake it
Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 1–90why, 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(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", f"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", f"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 [Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 91why, one step back91 # Tunnel ResponseImplement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 92–95why, one step back92 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",Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 96why, one step back96 # Response 1Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 97–99why, one step back97 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"Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 100–101why, one step back100 b"Connection: keep-alive\r\n"101 b"Content-Length: 0\r\n"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 102why, one step back102 b"\r\n",Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 103–109why, one step back103 # 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",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 110–118why, one step back110 ]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:Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 119why, one step back119 resp = await proxy.request("GET", f"https://example.com")Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 120why, one step back120Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 121–122why, one step back121 assert resp.status_code == 404122 assert resp.headers["Server"] == "origin-server"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 123why, one step back123Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 124–126why, one step back124 assert resp.request.method == "GET"125 assert resp.request.url == "https://example.com"126 assert resp.request.headers["Host"] == "example.com"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 127why, one step back127Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 128–141why, one step back128 await resp.read()129130 # Make another request to see that the tunnel is re-used.131 resp = await proxy.request("GET", f"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()141Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 142why, one step back142 recv = raw_io.received_dataConfigure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 143why, one step back143 assert len(recv) == 5Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 144–149why, one step back144 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")Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 150why, one step back150 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 151–207why, one step back151152153@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", backend=raw_io, proxy_mode=proxy_mode170 ) as proxy:171 response = await proxy.request("GET", f"http://example.com")172173 assert response.status_code == 200174 assert response.headers["Server"] == "origin-server"175176 assert response.request.method == "GET"177 assert response.request.url == "http://127.0.0.1:8000"178 assert response.request.url.full_path == "http://example.com"179 assert response.request.headers["Host"] == "example.com"180181 recv = raw_io.received_data182 assert len(recv) == 2183 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"184 assert recv[1].startswith(185 b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"186 )187188189def test_proxy_url_with_username_and_password():190 proxy = httpx.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 = httpx.HTTPProxy(198 "http://127.0.0.1:1080",199 proxy_headers={"Custom": "Header"},200 proxy_mode=httpx.HTTPProxyMode.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=<HTTPProxyMode.DEFAULT: 'DEFAULT'>)"207 )