Skip to content
Myr

test_proxy_http.py

This file

Path

tests/dispatch/test_proxy_http.py

207 lines · 6.9 KiB · Python

Last changed by

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

History of this file

Take it

The walk stopped after 200 commits: lines older than 5e4f54d point at it.

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 1–3why, one step back
1import pytest
23import httpx

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

line 4why, one step back
4from httpx._dispatch.proxy_http import HTTPProxy

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 5–8why, one step back
5
6from .utils import MockRawSocketBackend78

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 9–10why, one step back
9@pytest.mark.asyncio
10async def test_proxy_tunnel_success():

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 11–24why, one step back
11 raw_io = MockRawSocketBackend(
12 data_to_send=(13 [14 b"HTTP/1.1 200 OK\r\n"15 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"16 b"Server: proxy-server\r\n"17 b"\r\n",18 b"HTTP/1.1 404 Not Found\r\n"19 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"20 b"Server: origin-server\r\n"21 b"\r\n",22 ]23 ),24 )

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 25why, one step back
25 async with HTTPProxy(

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

line 26why, one step back
26 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 27–45why, one step back
27 ) as proxy:
28 response = await proxy.request("GET", "http://example.com")2930 assert response.status_code == 40431 assert response.headers["Server"] == "origin-server"3233 assert response.request.method == "GET"34 assert response.request.url == "http://example.com"35 assert response.request.headers["Host"] == "example.com"3637 recv = raw_io.received_data38 assert len(recv) == 339 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"40 assert recv[1].startswith(41 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"42 )43 assert recv[2].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")4445

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

line 46why, one step back
46@pytest.mark.asyncio

Add test script (#451)Florimond Mancabefore adoption5e4f54d

line 47why, one step back
47@pytest.mark.parametrize("status_code", [300, 304, 308, 401, 500])

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

line 48why, one step back
48async def test_proxy_tunnel_non_2xx_response(status_code):

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 49–60why, one step back
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 )5960 with pytest.raises(httpx.ProxyError) as e:

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 61why, one step back
61 async with HTTPProxy(

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

line 62why, one step back
62 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 63–83why, one step back
63 ) as proxy:
64 await proxy.request("GET", "http://example.com")6566 # ProxyError.request should be the CONNECT request not the original request67 assert e.value.request.method == "CONNECT"68 assert e.value.request.headers["Host"] == "127.0.0.1:8000"69 assert e.value.request.url.full_path == "example.com:80"7071 # ProxyError.response should be the CONNECT response72 assert e.value.response.status_code == status_code73 assert e.value.response.headers["Server"] == "proxy-server"7475 # Verify that the request wasn't sent after receiving an error from CONNECT76 recv = raw_io.received_data77 assert len(recv) == 278 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"79 assert recv[1].startswith(80 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"81 )8283

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

lines 84–85why, one step back
84@pytest.mark.asyncio
85async def test_proxy_tunnel_start_tls():

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 86–110why, one step back
86 raw_io = MockRawSocketBackend(
87 data_to_send=(88 [89 # Tunnel Response90 b"HTTP/1.1 200 OK\r\n"91 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"92 b"Server: proxy-server\r\n"93 b"\r\n",94 # Response 195 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"Connection: keep-alive\r\n"99 b"Content-Length: 0\r\n"100 b"\r\n",101 # Response 2102 b"HTTP/1.1 200 OK\r\n"103 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"104 b"Server: origin-server\r\n"105 b"Connection: keep-alive\r\n"106 b"Content-Length: 0\r\n"107 b"\r\n",108 ]109 ),110 )

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 111why, one step back
111 async with HTTPProxy(

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

line 112why, one step back
112 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 113–122why, one step back
113 ) as proxy:
114 resp = await proxy.request("GET", "https://example.com")115116 assert resp.status_code == 404117 assert resp.headers["Server"] == "origin-server"118119 assert resp.request.method == "GET"120 assert resp.request.url == "https://example.com"121 assert resp.request.headers["Host"] == "example.com"122

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 123why, one step back
123 await resp.aread()

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 124–134why, one step back
124
125 # Make another request to see that the tunnel is re-used.126 resp = await proxy.request("GET", "https://example.com/target")127128 assert resp.status_code == 200129 assert resp.headers["Server"] == "origin-server"130131 assert resp.request.method == "GET"132 assert resp.request.url == "https://example.com/target"133 assert resp.request.headers["Host"] == "example.com"134

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 135why, one step back
135 await resp.aread()

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 136–147why, one step back
136
137 recv = raw_io.received_data138 assert len(recv) == 5139 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"140 assert recv[1].startswith(141 b"CONNECT example.com:443 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"142 )143 assert recv[2] == b"--- START_TLS(example.com) ---"144 assert recv[3].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")145 assert recv[4].startswith(b"GET /target HTTP/1.1\r\nhost: example.com\r\n")146147

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

line 148why, one step back
148@pytest.mark.asyncio

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

line 149why, one step back
149@pytest.mark.parametrize("proxy_mode", ["FORWARD_ONLY", "DEFAULT"])

Clean up 'backend' fixture (#664)Florimond Mancabefore adoptiond0427be

line 150why, one step back
150async def test_proxy_forwarding(proxy_mode):

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 151–160why, one step back
151 raw_io = MockRawSocketBackend(
152 data_to_send=(153 [154 b"HTTP/1.1 200 OK\r\n"155 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"156 b"Server: origin-server\r\n"157 b"\r\n"158 ]159 ),160 )

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 161why, one step back
161 async with HTTPProxy(

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 162–189why, one step back
162 proxy_url="http://127.0.0.1:8000",
163 backend=raw_io,164 proxy_mode=proxy_mode,165 proxy_headers={"Proxy-Authorization": "test", "Override": "2"},166 ) as proxy:167 response = await proxy.request(168 "GET", "http://example.com", headers={"override": "1"}169 )170171 assert response.status_code == 200172 assert response.headers["Server"] == "origin-server"173174 assert response.request.method == "GET"175 assert response.request.url == "http://127.0.0.1:8000"176 assert response.request.url.full_path == "http://example.com"177 assert response.request.headers["Host"] == "example.com"178179 recv = raw_io.received_data180 assert len(recv) == 2181 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"182 assert recv[1].startswith(183 b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"184 )185 assert b"proxy-authorization: test" in recv[1]186 assert b"override: 1" in recv[1]187188189def test_proxy_url_with_username_and_password():

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 190why, one step back
190 proxy = HTTPProxy("http://user:password@example.com:1080")

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 191–196why, one step back
191
192 assert proxy.proxy_url == "http://example.com:1080"193 assert proxy.proxy_headers["Proxy-Authorization"] == "Basic dXNlcjpwYXNzd29yZA=="194195196def test_proxy_repr():

Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76

line 197why, one step back
197 proxy = HTTPProxy(

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 198–199why, one step back
198 "http://127.0.0.1:1080",
199 proxy_headers={"Custom": "Header"},

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

line 200why, one step back
200 proxy_mode="DEFAULT",

Add test script (#451)Florimond Mancabefore adoption5e4f54d

lines 201–205why, one step back
201 )
202203 assert repr(proxy) == (204 "HTTPProxy(proxy_url=URL('http://127.0.0.1:1080') "205 "proxy_headers=Headers({'custom': 'Header'}) "

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

line 206why, one step back
206 "proxy_mode='DEFAULT')"

Add test script (#451)Florimond Mancabefore adoption5e4f54d

line 207why, one step back
207 )