test_proxy_http.py
This file
Path
tests/dispatch/test_proxy_http.py
202 lines · 6.9 KiB · Python
Last changed by
Refactor tests in the light of backend auto-detection (#615)Florimond Mancabefore adoptionab41a5d
History of this fileTake it
Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 1–23why, 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 )23 async with httpx.HTTPProxy(Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 24why, one step back24 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 25why, one step back25 ) as proxy:Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 26why, one step back26 response = await proxy.request("GET", "http://example.com")Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 27–58why, one step back2728 assert response.status_code == 40429 assert response.headers["Server"] == "origin-server"3031 assert response.request.method == "GET"32 assert response.request.url == "http://example.com"33 assert response.request.headers["Host"] == "example.com"3435 recv = raw_io.received_data36 assert len(recv) == 337 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"38 assert recv[1].startswith(39 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"40 )41 assert recv[2].startswith(b"GET / HTTP/1.1\r\nhost: example.com\r\n")424344@pytest.mark.parametrize("status_code", [300, 304, 308, 401, 500])45async def test_proxy_tunnel_non_2xx_response(backend, status_code):46 raw_io = MockRawSocketBackend(47 data_to_send=(48 [49 b"HTTP/1.1 %d Not Good\r\n" % status_code,50 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"51 b"Server: proxy-server\r\n"52 b"\r\n",53 ]54 ),55 )5657 with pytest.raises(httpx.ProxyError) as e:58 async with httpx.HTTPProxy(Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 59why, one step back59 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 60why, one step back60 ) as proxy:Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 61why, one step back61 await proxy.request("GET", "http://example.com")Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 62–84why, one step back6263 # ProxyError.request should be the CONNECT request not the original request64 assert e.value.request.method == "CONNECT"65 assert e.value.request.headers["Host"] == "127.0.0.1:8000"66 assert e.value.request.url.full_path == "example.com:80"6768 # ProxyError.response should be the CONNECT response69 assert e.value.response.status_code == status_code70 assert e.value.response.headers["Server"] == "proxy-server"7172 # Verify that the request wasn't sent after receiving an error from CONNECT73 recv = raw_io.received_data74 assert len(recv) == 275 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"76 assert recv[1].startswith(77 b"CONNECT example.com:80 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"78 )798081async def test_proxy_tunnel_start_tls(backend):82 raw_io = MockRawSocketBackend(83 data_to_send=(84 [Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 85why, one step back85 # Tunnel ResponseImplement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 86–89why, one step back86 b"HTTP/1.1 200 OK\r\n"87 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"88 b"Server: proxy-server\r\n"89 b"\r\n",Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 90why, one step back90 # Response 1Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 91–93why, one step back91 b"HTTP/1.1 404 Not Found\r\n"92 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"93 b"Server: origin-server\r\n"Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 94–95why, one step back94 b"Connection: keep-alive\r\n"95 b"Content-Length: 0\r\n"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 96why, one step back96 b"\r\n",Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 97–103why, one step back97 # Response 298 b"HTTP/1.1 200 OK\r\n"99 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"100 b"Server: origin-server\r\n"101 b"Connection: keep-alive\r\n"102 b"Content-Length: 0\r\n"103 b"\r\n",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 104–107why, one step back104 ]105 ),106 )107 async with httpx.HTTPProxy(Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 108why, one step back108 proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode="TUNNEL_ONLY",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 109why, one step back109 ) as proxy:Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 110why, one step back110 resp = await proxy.request("GET", "https://example.com")Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 111why, one step back111Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 112–113why, one step back112 assert resp.status_code == 404113 assert resp.headers["Server"] == "origin-server"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 114why, one step back114Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 115–117why, one step back115 assert resp.request.method == "GET"116 assert resp.request.url == "https://example.com"117 assert resp.request.headers["Host"] == "example.com"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 118why, one step back118Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 119–121why, one step back119 await resp.read()120121 # Make another request to see that the tunnel is re-used.Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 122why, one step back122 resp = await proxy.request("GET", "https://example.com/target")Configure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
lines 123–132why, one step back123124 assert resp.status_code == 200125 assert resp.headers["Server"] == "origin-server"126127 assert resp.request.method == "GET"128 assert resp.request.url == "https://example.com/target"129 assert resp.request.headers["Host"] == "example.com"130131 await resp.read()132Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 133why, one step back133 recv = raw_io.received_dataConfigure Proxy from Client (#353)Seth Michael Larsonbefore adoptionb0fa085
line 134why, one step back134 assert len(recv) == 5Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 135–140why, one step back135 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"136 assert recv[1].startswith(137 b"CONNECT example.com:443 HTTP/1.1\r\nhost: 127.0.0.1:8000\r\n"138 )139 assert recv[2] == b"--- START_TLS(example.com) ---"140 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 141why, one step back141 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 142–143why, one step back142143Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 144why, one step back144@pytest.mark.parametrize("proxy_mode", ["FORWARD_ONLY", "DEFAULT"])Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 145–156why, one step back145async def test_proxy_forwarding(backend, proxy_mode):146 raw_io = MockRawSocketBackend(147 data_to_send=(148 [149 b"HTTP/1.1 200 OK\r\n"150 b"Date: Sun, 10 Oct 2010 23:26:07 GMT\r\n"151 b"Server: origin-server\r\n"152 b"\r\n"153 ]154 ),155 )156 async with httpx.HTTPProxy(Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 157–160why, one step back157 proxy_url="http://127.0.0.1:8000",158 backend=raw_io,159 proxy_mode=proxy_mode,160 proxy_headers={"Proxy-Authorization": "test", "Override": "2"},Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 161why, one step back161 ) as proxy:Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
line 162why, one step back162 response = await proxy.request(Add flake8-pie plugin (#419)Stephen Brown IIbefore adoptiondb3e3a0
line 163why, one step back163 "GET", "http://example.com", headers={"override": "1"}Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
line 164why, one step back164 )Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 165–179why, one step back165166 assert response.status_code == 200167 assert response.headers["Server"] == "origin-server"168169 assert response.request.method == "GET"170 assert response.request.url == "http://127.0.0.1:8000"171 assert response.request.url.full_path == "http://example.com"172 assert response.request.headers["Host"] == "example.com"173174 recv = raw_io.received_data175 assert len(recv) == 2176 assert recv[0] == b"--- CONNECT(127.0.0.1, 8000) ---"177 assert recv[1].startswith(178 b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"179 )Get test suite back to ~100% line coverage (#406)Seth Michael Larsonbefore adoption05f5dc2
lines 180–181why, one step back180 assert b"proxy-authorization: test" in recv[1]181 assert b"override: 1" in recv[1]Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 182–194why, one step back182183184def test_proxy_url_with_username_and_password():185 proxy = httpx.HTTPProxy("http://user:password@example.com:1080")186187 assert proxy.proxy_url == "http://example.com:1080"188 assert proxy.proxy_headers["Proxy-Authorization"] == "Basic dXNlcjpwYXNzd29yZA=="189190191def test_proxy_repr():192 proxy = httpx.HTTPProxy(193 "http://127.0.0.1:1080",194 proxy_headers={"Custom": "Header"},Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 195why, one step back195 proxy_mode="DEFAULT",Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
lines 196–200why, one step back196 )197198 assert repr(proxy) == (199 "HTTPProxy(proxy_url=URL('http://127.0.0.1:1080') "200 "proxy_headers=Headers({'custom': 'Header'}) "Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 201why, one step back201 "proxy_mode='DEFAULT')"Implement HTTPProxy dispatcher (#259)Seth Michael Larsonbefore adoption57c27ca
line 202why, one step back202 )