Skip to content
Myr

test_proxy_http.py

This file

Path

tests/dispatch/test_proxy_http.py

181 lines · 6.0 KiB · Python

Last changed by

Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca

History of this file

Take it

Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca

lines 1–181why, one step back
1import pytest
23import 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 [91 b"HTTP/1.1 200 OK\r\n"92 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"93 b"Server: proxy-server\r\n"94 b"\r\n",95 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"\r\n",99 ]100 ),101 backend=backend,102 )103 async with httpx.HTTPProxy(104 proxy_url="http://127.0.0.1:8000",105 backend=raw_io,106 proxy_mode=httpx.HTTPProxyMode.TUNNEL_ONLY,107 ) as proxy:108 response = await proxy.request("GET", f"https://example.com")109110 assert response.status_code == 404111 assert response.headers["Server"] == "origin-server"112113 assert response.request.method == "GET"114 assert response.request.url == "https://example.com"115 assert response.request.headers["Host"] == "example.com"116117 recv = raw_io.received_data118 assert len(recv) == 4119 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"120 assert recv[1].startswith(121 b"CONNECT example.com:443 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"122 )123 assert recv[2] == b"--- START_TLS(example.com) ---"124 assert recv[3].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")125126127@pytest.mark.parametrize(128 "proxy_mode", [httpx.HTTPProxyMode.FORWARD_ONLY, httpx.HTTPProxyMode.DEFAULT]129)130async def test_proxy_forwarding(backend, proxy_mode):131 raw_io = MockRawSocketBackend(132 data_to_send=(133 [134 b"HTTP/1.1 200 OK\r\n"135 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"136 b"Server: origin-server\r\n"137 b"\r\n"138 ]139 ),140 backend=backend,141 )142 async with httpx.HTTPProxy(143 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode=proxy_mode144 ) as proxy:145 response = await proxy.request("GET", f"http://example.com")146147 assert response.status_code == 200148 assert response.headers["Server"] == "origin-server"149150 assert response.request.method == "GET"151 assert response.request.url == "http://127.0.0.1:8000"152 assert response.request.url.full_path == "http://example.com"153 assert response.request.headers["Host"] == "example.com"154155 recv = raw_io.received_data156 assert len(recv) == 2157 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"158 assert recv[1].startswith(159 b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"160 )161162163def test_proxy_url_with_username_and_password():164 proxy = httpx.HTTPProxy("http://user:password@example.com:1080")165166 assert proxy.proxy_url == "http://example.com:1080"167 assert proxy.proxy_headers["Proxy-Authorization"] == "Basic dXNlcjpwYXNzd29yZA=="168169170def test_proxy_repr():171 proxy = httpx.HTTPProxy(172 "http://127.0.0.1:1080",173 proxy_headers={"Custom": "Header"},174 proxy_mode=httpx.HTTPProxyMode.DEFAULT,175 )176177 assert repr(proxy) == (178 "HTTPProxy(proxy_url=URL('http://127.0.0.1:1080') "179 "proxy_headers=Headers({'custom': 'Header'}) "180 "proxy_mode=<HTTPProxyMode.DEFAULT: 'DEFAULT'>)"181 )