|
| 1 | +import pytest |
| 2 | + |
| 3 | +from apimatic_core.http.configurations.proxy_settings import ProxySettings |
| 4 | + |
| 5 | + |
| 6 | +class TestProxySettings: |
| 7 | + def test_has_expected_keys(self): |
| 8 | + ps = ProxySettings(address="proxy.local") |
| 9 | + proxies = ps.to_proxies() |
| 10 | + assert set(proxies.keys()) == {"http", "https"} |
| 11 | + |
| 12 | + @pytest.mark.parametrize( |
| 13 | + "address, port, username, password, exp_http, exp_https", |
| 14 | + [ |
| 15 | + pytest.param( |
| 16 | + "proxy.local", None, None, None, |
| 17 | + "http://proxy.local", |
| 18 | + "https://proxy.local", |
| 19 | + id="no-auth-no-port", |
| 20 | + ), |
| 21 | + pytest.param( |
| 22 | + "proxy.local", 8080, None, None, |
| 23 | + "http://proxy.local:8080", |
| 24 | + "https://proxy.local:8080", |
| 25 | + id="no-auth-with-port", |
| 26 | + ), |
| 27 | + pytest.param( |
| 28 | + "proxy.local", 8080, "user", "pass", |
| 29 | + "http://user:pass@proxy.local:8080", |
| 30 | + "https://user:pass@proxy.local:8080", |
| 31 | + id="auth-with-port", |
| 32 | + ), |
| 33 | + pytest.param( |
| 34 | + "proxy.local", None, "user", None, |
| 35 | + # password None -> empty string: "user:@" |
| 36 | + "http://user:@proxy.local", |
| 37 | + "https://user:@proxy.local", |
| 38 | + id="auth-username-only-password-none", |
| 39 | + ), |
| 40 | + pytest.param( |
| 41 | + "proxy.local", None, "a b", "p@ss#", |
| 42 | + # URL-encoding of space/@/# |
| 43 | + "http://a%20b:p%40ss%23@proxy.local", |
| 44 | + "https://a%20b:p%40ss%23@proxy.local", |
| 45 | + id="auth-with-url-encoding", |
| 46 | + ), |
| 47 | + pytest.param( |
| 48 | + "localhost", None, "", "", |
| 49 | + # empty username triggers auth block (since not None) -> ':@' |
| 50 | + "http://:@localhost", |
| 51 | + "https://:@localhost", |
| 52 | + id="empty-username-and-password", |
| 53 | + ), |
| 54 | + ], |
| 55 | + ) |
| 56 | + def test_formats(self, address, port, username, password, exp_http, exp_https): |
| 57 | + ps = ProxySettings(address, port, username, password) |
| 58 | + proxies = ps.to_proxies() |
| 59 | + assert proxies["http"] == exp_http |
| 60 | + assert proxies["https"] == exp_https |
| 61 | + |
| 62 | + @pytest.mark.parametrize("address", ["proxy.local", "localhost"]) |
| 63 | + def test_no_trailing_colon_when_no_port(self, address): |
| 64 | + ps = ProxySettings(address) |
| 65 | + proxies = ps.to_proxies() |
| 66 | + assert not proxies["http"].endswith(":") |
| 67 | + assert not proxies["https"].endswith(":") |
| 68 | + assert "::" not in proxies["http"] |
| 69 | + assert "::" not in proxies["https"] |
| 70 | + |
| 71 | + def test_single_colon_before_port(self): |
| 72 | + ps = ProxySettings(address="proxy.local", port=3128) |
| 73 | + proxies = ps.to_proxies() |
| 74 | + assert proxies["http"].endswith(":3128") |
| 75 | + assert proxies["https"].endswith(":3128") |
| 76 | + assert "proxy.local::3128" not in proxies["http"] |
| 77 | + assert "proxy.local::3128" not in proxies["https"] |
| 78 | + |
| 79 | + # --- NEW: scheme trimming cases (reflecting the reverted, simpler behavior) --- |
| 80 | + |
| 81 | + def test_trims_http_scheme_no_port(self): |
| 82 | + ps = ProxySettings(address="http://proxy.local") |
| 83 | + proxies = ps.to_proxies() |
| 84 | + assert proxies["http"] == "http://proxy.local" |
| 85 | + assert proxies["https"] == "https://proxy.local" |
| 86 | + |
| 87 | + def test_trims_https_scheme_trailing_slash_with_port_and_auth(self): |
| 88 | + ps = ProxySettings(address="https://proxy.local/", port=8080, username="user", password="secret") |
| 89 | + proxies = ps.to_proxies() |
| 90 | + assert proxies["http"] == "http://user:secret@proxy.local:8080" |
| 91 | + assert proxies["https"] == "https://user:secret@proxy.local:8080" |
| 92 | + assert not proxies["http"].endswith(":") |
| 93 | + assert not proxies["https"].endswith(":") |
| 94 | + |
0 commit comments