Skip to content
Myr

test_proxy_http.py

This file

Path

tests/dispatch/test_proxy_http.py

206 lines · 7.0 KiB · Python

Last changed by

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

History of this file

Take 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", backend=raw_io, proxy_mode="TUNNEL_ONLY",26    ) as proxy:27        response = await proxy.request("GET", "http://example.com")2829        assert response.status_code == 40430        assert response.headers["Server"] == "origin-server"3132        assert response.request.method == "GET"33        assert response.request.url == "http://example.com"34        assert response.request.headers["Host"] == "example.com"3536    recv = raw_io.received_data37    assert len(recv) == 338    assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"39    assert recv[1].startswith(40        b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"41    )42    assert recv[2].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")434445@pytest.mark.parametrize("status_code", [300, 304, 308, 401, 500])46async def test_proxy_tunnel_non_2xx_response(backend, status_code):47    raw_io = MockRawSocketBackend(48        data_to_send=(49            [50                b"HTTP/1.1 %d Not Good\r\n" % status_code,51                b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"52                b"Server: proxy-server\r\n"53                b"\r\n",54            ]55        ),56        backend=backend,57    )5859    with pytest.raises(httpx.ProxyError) as e:60        async with httpx.HTTPProxy(61            proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",62        ) as proxy:63            await proxy.request("GET", "http://example.com")6465    # ProxyError.request should be the CONNECT request not the original request66    assert e.value.request.method == "CONNECT"67    assert e.value.request.headers["Host"] == "127.0.0.1:8000"68    assert e.value.request.url.full_path == "example.com:80"6970    # ProxyError.response should be the CONNECT response71    assert e.value.response.status_code == status_code72    assert e.value.response.headers["Server"] == "proxy-server"7374    # Verify that the request wasn't sent after receiving an error from CONNECT75    recv = raw_io.received_data76    assert len(recv) == 277    assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"78    assert recv[1].startswith(79        b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"80    )818283async def test_proxy_tunnel_start_tls(backend):84    raw_io = MockRawSocketBackend(85        data_to_send=(86            [87                # Tunnel Response88                b"HTTP/1.1 200 OK\r\n"89                b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"90                b"Server: proxy-server\r\n"91                b"\r\n",92                # Response 193                b"HTTP/1.1 404 Not Found\r\n"94                b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"95                b"Server: origin-server\r\n"96                b"Connection: keep-alive\r\n"97                b"Content-Length: 0\r\n"98                b"\r\n",99                # Response 2100                b"HTTP/1.1 200 OK\r\n"101                b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"102                b"Server: origin-server\r\n"103                b"Connection: keep-alive\r\n"104                b"Content-Length: 0\r\n"105                b"\r\n",106            ]107        ),108        backend=backend,109    )110    async with httpx.HTTPProxy(111        proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",112    ) as proxy:113        resp = await proxy.request("GET", "https://example.com")114115        assert resp.status_code == 404116        assert resp.headers["Server"] == "origin-server"117118        assert resp.request.method == "GET"119        assert resp.request.url == "https://example.com"120        assert resp.request.headers["Host"] == "example.com"121122        await resp.read()123124        # Make another request to see that the tunnel is re-used.125        resp = await proxy.request("GET", "https://example.com/target")126127        assert resp.status_code == 200128        assert resp.headers["Server"] == "origin-server"129130        assert resp.request.method == "GET"131        assert resp.request.url == "https://example.com/target"132        assert resp.request.headers["Host"] == "example.com"133134        await resp.read()135136    recv = raw_io.received_data137    assert len(recv) == 5138    assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"139    assert recv[1].startswith(140        b"CONNECT example.com:443 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"141    )142    assert recv[2] == b"--- START_TLS(example.com) ---"143    assert recv[3].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")144    assert recv[4].startswith(b"GET /target HTTP/1.1\r\nhost: example.com\r\n")145146147@pytest.mark.parametrize("proxy_mode", ["FORWARD_ONLY", "DEFAULT"])148async def test_proxy_forwarding(backend, proxy_mode):149    raw_io = MockRawSocketBackend(150        data_to_send=(151            [152                b"HTTP/1.1 200 OK\r\n"153                b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"154                b"Server: origin-server\r\n"155                b"\r\n"156            ]157        ),158        backend=backend,159    )160    async with httpx.HTTPProxy(161        proxy_url="http://127.0.0.1:8000",162        backend=raw_io,163        proxy_mode=proxy_mode,164        proxy_headers={"Proxy-Authorization": "test", "Override": "2"},165    ) as proxy:166        response = await proxy.request(167            "GET", "http://example.com", headers={"override": "1"}168        )169170        assert response.status_code == 200171        assert response.headers["Server"] == "origin-server"172173        assert response.request.method == "GET"174        assert response.request.url == "http://127.0.0.1:8000"175        assert response.request.url.full_path == "http://example.com"176        assert response.request.headers["Host"] == "example.com"177178    recv = raw_io.received_data179    assert len(recv) == 2180    assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"181    assert recv[1].startswith(182        b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"183    )184    assert b"proxy-authorization: test" in recv[1]185    assert b"override: 1" in recv[1]186187188def test_proxy_url_with_username_and_password():189    proxy = httpx.HTTPProxy("http://user:password@example.com:1080")190191    assert proxy.proxy_url == "http://example.com:1080"192    assert proxy.proxy_headers["Proxy-Authorization"] == "Basic dXNlcjpwYXNzd29yZA=="193194195def test_proxy_repr():196    proxy = httpx.HTTPProxy(197        "http://127.0.0.1:1080",198        proxy_headers={"Custom": "Header"},199        proxy_mode="DEFAULT",200    )201202    assert repr(proxy) == (203        "HTTPProxy(proxy_url=URL('http://127.0.0.1:1080') "204        "proxy_headers=Headers({'custom': 'Header'}) "205        "proxy_mode='DEFAULT')"206    )