Skip to content
Myr

test_proxies.py

This file

Path

tests/client/test_proxies.py

90 lines · 3.1 KiB · Python

Last changed by

Prep for introducing SyncClient (#713)Tom Christiebefore adoptionf17ab37

History of this file

Take it

1import pytest23import httpx456@pytest.mark.parametrize(7    ["proxies", "expected_proxies"],8    [9        ("http://127.0.0.1", [("all", "http://127.0.0.1")]),10        ({"all": "http://127.0.0.1"}, [("all", "http://127.0.0.1")]),11        (12            {"http": "http://127.0.0.1", "https": "https://127.0.0.1"},13            [("http", "http://127.0.0.1"), ("https", "https://127.0.0.1")],14        ),15        (httpx.Proxy("http://127.0.0.1"), [("all", "http://127.0.0.1")]),16        (17            {"https": httpx.Proxy("https://127.0.0.1"), "all": "http://127.0.0.1"},18            [("all", "http://127.0.0.1"), ("https", "https://127.0.0.1")],19        ),20    ],21)22def test_proxies_parameter(proxies, expected_proxies):23    client = httpx.AsyncClient(proxies=proxies)2425    for proxy_key, url in expected_proxies:26        assert proxy_key in client.proxies27        assert client.proxies[proxy_key].proxy_url == url2829    assert len(expected_proxies) == len(client.proxies)303132PROXY_URL = "http://[::1]"333435@pytest.mark.parametrize(36    ["url", "proxies", "expected"],37    [38        ("http://example.com", None, None),39        ("http://example.com", {}, None),40        ("http://example.com", {"https": PROXY_URL}, None),41        ("http://example.com", {"http://example.net": PROXY_URL}, None),42        ("http://example.com:443", {"http://example.com": PROXY_URL}, None),43        ("http://example.com", {"all": PROXY_URL}, PROXY_URL),44        ("http://example.com", {"http": PROXY_URL}, PROXY_URL),45        ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),46        ("http://example.com", {"all://example.com:80": PROXY_URL}, PROXY_URL),47        ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),48        ("http://example.com", {"http://example.com:80": PROXY_URL}, PROXY_URL),49        ("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),50        ("http://example.com:8080", {"http://example.com": PROXY_URL}, None),51        (52            "http://example.com",53            {54                "all": PROXY_URL + ":1",55                "http": PROXY_URL + ":2",56                "all://example.com": PROXY_URL + ":3",57                "http://example.com": PROXY_URL + ":4",58            },59            PROXY_URL + ":4",60        ),61        (62            "http://example.com",63            {64                "all": PROXY_URL + ":1",65                "http": PROXY_URL + ":2",66                "all://example.com": PROXY_URL + ":3",67            },68            PROXY_URL + ":3",69        ),70        (71            "http://example.com",72            {"all": PROXY_URL + ":1", "http": PROXY_URL + ":2"},73            PROXY_URL + ":2",74        ),75    ],76)77def test_dispatcher_for_request(url, proxies, expected):78    client = httpx.AsyncClient(proxies=proxies)79    dispatcher = client.dispatcher_for_url(httpx.URL(url))8081    if expected is None:82        assert dispatcher is client.dispatch83    else:84        assert isinstance(dispatcher, httpx.HTTPProxy)85        assert dispatcher.proxy_url == expected868788def test_unsupported_proxy_scheme():89    with pytest.raises(ValueError):90        httpx.AsyncClient(proxies="ftp://127.0.0.1")