| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from urllib3.exceptions import MaxRetryError, NewConnectionError, ProxyError |
| 6 |
from urllib3.poolmanager import ProxyManager |
| 7 |
from urllib3.util.retry import Retry |
| 8 |
from urllib3.util.url import parse_url |
| 9 |
|
| 10 |
from .port_helpers import find_unused_port |
| 11 |
|
| 12 |
|
| 13 |
class TestProxyManager: |
| 14 |
@pytest.mark.parametrize("proxy_scheme", ["http", "https"]) |
| 15 |
def test_proxy_headers(self, proxy_scheme: str) -> None: |
| 16 |
url = "http://pypi.org/project/urllib3/" |
| 17 |
proxy_url = f"{proxy_scheme}://something:1234" |
| 18 |
with ProxyManager(proxy_url) as p: |
| 19 |
# Verify default headers |
| 20 |
default_headers = {"Accept": "*/*", "Host": "pypi.org"} |
| 21 |
headers = p._set_proxy_headers(url) |
| 22 |
|
| 23 |
assert headers == default_headers |
| 24 |
|
| 25 |
# Verify default headers don't overwrite provided headers |
| 26 |
provided_headers = { |
| 27 |
"Accept": "application/json", |
| 28 |
"custom": "header", |
| 29 |
"Host": "test.python.org", |
| 30 |
} |
| 31 |
headers = p._set_proxy_headers(url, provided_headers) |
| 32 |
|
| 33 |
assert headers == provided_headers |
| 34 |
|
| 35 |
# Verify proxy with nonstandard port |
| 36 |
provided_headers = {"Accept": "application/json"} |
| 37 |
expected_headers = provided_headers.copy() |
| 38 |
expected_headers.update({"Host": "pypi.org:8080"}) |
| 39 |
url_with_port = "http://pypi.org:8080/project/urllib3/" |
| 40 |
headers = p._set_proxy_headers(url_with_port, provided_headers) |
| 41 |
|
| 42 |
assert headers == expected_headers |
| 43 |
|
| 44 |
def test_default_port(self) -> None: |
| 45 |
with ProxyManager("http://something") as p: |
| 46 |
assert p.proxy is not None |
| 47 |
assert p.proxy.port == 80 |
| 48 |
with ProxyManager("https://something") as p: |
| 49 |
assert p.proxy is not None |
| 50 |
assert p.proxy.port == 443 |
| 51 |
|
| 52 |
def test_invalid_scheme(self) -> None: |
| 53 |
with pytest.raises(AssertionError): |
| 54 |
ProxyManager("invalid://host/p") |
| 55 |
with pytest.raises(ValueError): |
| 56 |
ProxyManager("invalid://host/p") |
| 57 |
|
| 58 |
def test_proxy_tunnel(self) -> None: |
| 59 |
http_url = parse_url("http://example.com") |
| 60 |
https_url = parse_url("https://example.com") |
| 61 |
with ProxyManager("http://proxy:8080") as p: |
| 62 |
assert p._proxy_requires_url_absolute_form(http_url) |
| 63 |
assert p._proxy_requires_url_absolute_form(https_url) is False |
| 64 |
|
| 65 |
with ProxyManager("https://proxy:8080") as p: |
| 66 |
assert p._proxy_requires_url_absolute_form(http_url) |
| 67 |
assert p._proxy_requires_url_absolute_form(https_url) is False |
| 68 |
|
| 69 |
with ProxyManager("https://proxy:8080", use_forwarding_for_https=True) as p: |
| 70 |
assert p._proxy_requires_url_absolute_form(http_url) |
| 71 |
assert p._proxy_requires_url_absolute_form(https_url) |
| 72 |
|
| 73 |
def test_proxy_connect_retry(self) -> None: |
| 74 |
retry = Retry(total=None, connect=False) |
| 75 |
port = find_unused_port() |
| 76 |
with ProxyManager(f"http://localhost:{port}") as p: |
| 77 |
with pytest.raises(ProxyError) as ei: |
| 78 |
p.urlopen("HEAD", url="http://localhost/", retries=retry) |
| 79 |
assert isinstance(ei.value.original_error, NewConnectionError) |
| 80 |
|
| 81 |
retry = Retry(total=None, connect=2) |
| 82 |
with ProxyManager(f"http://localhost:{port}") as p: |
| 83 |
with pytest.raises(MaxRetryError) as ei1: |
| 84 |
p.urlopen("HEAD", url="http://localhost/", retries=retry) |
| 85 |
assert ei1.value.reason is not None |
| 86 |
assert isinstance(ei1.value.reason, ProxyError) |
| 87 |
assert isinstance(ei1.value.reason.original_error, NewConnectionError) |