Live reads go through an operator proxy pool (round-robin over the newest 100 entries, cooling on connect failures, no direct fallback); stored clearances are replayed only on the exit that earned them. Identity, clearance, solver and Turnstile rungs escalate automatically. fetch_pages reads up to 12 URLs four at a time with per-item isolation. report/reports/reports_update keep a durable queue of unreadable pages for the operator. Ships a pinned multi-stage image (non-root 10001, data in /data), an ordered CI with static gates, tests on 3.12 and 3.13 at 85% coverage, an offline image smoke, and sha/latest publishing gated on main.
162 lines
4.8 KiB
Python
162 lines
4.8 KiB
Python
import pytest
|
|
|
|
import proxy
|
|
|
|
|
|
def test_parse_default_ports_and_host_lower():
|
|
parsed = proxy.parse_one("HTTP://Example.COM")
|
|
assert parsed.scheme == "http"
|
|
assert parsed.host == "example.com"
|
|
assert parsed.port == 80
|
|
assert parsed.user == ""
|
|
|
|
|
|
def test_parse_credentials_are_decoded():
|
|
parsed = proxy.parse_one("socks5://user:p%40ss@10.0.0.1:1080")
|
|
assert (parsed.user, parsed.password) == ("user", "p@ss")
|
|
assert parsed.password not in parsed.label
|
|
assert "user:" in parsed.label
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"raw",
|
|
["", "ftp://h:1", "http://h:1/path", "http://h:1?x=1", "http://u@h:1", "http://h:99999", "http://:1"],
|
|
)
|
|
def test_parse_rejects(raw):
|
|
with pytest.raises(proxy.PoolError):
|
|
proxy.parse_one(raw)
|
|
|
|
|
|
def test_parse_list_skips_blanks_and_duplicates():
|
|
parsed = proxy.parse_list("\nhttp://a:1\n\nhttp://a:1\nhttp://b:2\n")
|
|
assert [entry.host for entry in parsed] == ["a", "b"]
|
|
|
|
|
|
def test_parse_list_rejects_invalid_whole_list():
|
|
with pytest.raises(proxy.PoolError):
|
|
proxy.parse_list("http://a:1\nftp://b:2\n")
|
|
|
|
|
|
def test_retry_after_seconds_and_date():
|
|
assert proxy.retry_after_seconds("7") == 7.0
|
|
assert proxy.retry_after_seconds(None) is None
|
|
assert proxy.retry_after_seconds("garbage") is None
|
|
assert proxy.retry_after_seconds("Wed, 21 Oct 2015 07:28:00 GMT") == 0.0
|
|
|
|
|
|
def test_pool_round_robin_and_cooldown(pool):
|
|
made = pool("http://a:1", "http://b:2")
|
|
first = made.acquire()
|
|
second = made.acquire()
|
|
made.release(first)
|
|
made.release(second)
|
|
assert first.proxy.host != second.proxy.host
|
|
made.cool(first, 30, "proxy")
|
|
assert [entry.proxy.host for entry in [made.acquire()] if entry] == [second.proxy.host]
|
|
made.release(made.acquire())
|
|
|
|
|
|
def test_pool_pin_by_url_and_label(pool):
|
|
made = pool("http://a:1", "http://b:2")
|
|
target = made.entries[1]
|
|
for pinned in (target.proxy.url, target.proxy.label):
|
|
got = made.acquire(pinned)
|
|
assert got is target
|
|
made.release(got)
|
|
|
|
|
|
def test_pool_returns_none_when_all_cooling(pool):
|
|
made = pool("http://a:1")
|
|
entry = made.acquire()
|
|
made.release(entry)
|
|
made.cool(entry, 30, "proxy")
|
|
assert made.acquire() is None
|
|
|
|
|
|
def test_refresh_keeps_last_good_and_reuses_entries(pool):
|
|
made = pool("http://a:1")
|
|
|
|
def failing(url, token):
|
|
raise proxy.PoolError("boom")
|
|
|
|
assert made.refresh(failing) is False
|
|
assert len(made) == 1
|
|
kept = made.entries[0]
|
|
|
|
def good(url, token):
|
|
return "http://a:1\nhttp://b:2\n"
|
|
|
|
assert made.refresh(good) is True
|
|
assert len(made) == 2
|
|
assert made.entries[0] is kept
|
|
|
|
|
|
def test_refresh_accepts_transport_style_errors(pool):
|
|
made = pool("http://a:1")
|
|
|
|
class Boom(ValueError):
|
|
pass
|
|
|
|
def failing(url, token):
|
|
raise Boom("network down")
|
|
|
|
assert made.refresh(failing) is False
|
|
assert len(made) == 1
|
|
|
|
|
|
def test_snapshot_reports_state(pool):
|
|
made = pool("http://a:1")
|
|
assert made.snapshot()[0]["available"] is True
|
|
assert "***" not in made.snapshot()[0]["proxy"]
|
|
|
|
|
|
def test_from_env_requires_one_source():
|
|
with pytest.raises(proxy.PoolError):
|
|
proxy.from_env({"MCP_LIVE_ROUTE": "proxy"})
|
|
with pytest.raises(proxy.PoolError):
|
|
proxy.from_env({"MCP_PROXY_URLS": "[]", "MCP_PROXY_LIST_URL": "http://x"})
|
|
|
|
|
|
def test_from_env_direct_skips_sources():
|
|
made = proxy.from_env({"MCP_LIVE_ROUTE": "direct"})
|
|
assert made.route == "direct"
|
|
assert len(made) == 0
|
|
|
|
|
|
def test_from_env_literal_and_list_validation():
|
|
made = proxy.from_env({"MCP_PROXY_URLS": '["http://a:1"]'})
|
|
assert len(made) == 1
|
|
with pytest.raises(proxy.PoolError):
|
|
proxy.from_env({"MCP_PROXY_LIST_URL": "http://x"})
|
|
with pytest.raises(proxy.PoolError):
|
|
proxy.from_env(
|
|
{"MCP_PROXY_LIST_URL": "http://x", "MCP_PROXY_LIST_TOKEN": "t", "MCP_PROXY_LIST_REFRESH_MINUTES": "abc"}
|
|
)
|
|
with pytest.raises(proxy.PoolError):
|
|
proxy.from_env({"MCP_PROXY_URLS": "{}"})
|
|
|
|
|
|
def test_max_retries_bounds():
|
|
assert proxy.max_retries({"MCP_HTTP_MAX_RETRIES": "0"}) == 0
|
|
with pytest.raises(proxy.PoolError):
|
|
proxy.max_retries({"MCP_HTTP_MAX_RETRIES": "99"})
|
|
|
|
|
|
def test_start_without_list_url_is_false():
|
|
assert proxy.Pool(_route="proxy").start(lambda url, token: "") is False
|
|
|
|
|
|
def test_limit_keeps_newest_entries(pool):
|
|
made = proxy.Pool(_route="proxy", limit=2)
|
|
made.refresh(lambda url, token: "http://a:1\nhttp://b:2\nhttp://c:3\n")
|
|
assert [entry.proxy.host for entry in made.entries] == ["b", "c"]
|
|
|
|
|
|
def test_limit_validation():
|
|
assert (
|
|
proxy.from_env({"MCP_PROXY_LIST_URL": "http://x", "MCP_PROXY_LIST_TOKEN": "t", "MCP_PROXY_LIMIT": "5"}).limit
|
|
== 5
|
|
)
|
|
with pytest.raises(proxy.PoolError):
|
|
proxy.from_env({"MCP_PROXY_LIST_URL": "http://x", "MCP_PROXY_LIST_TOKEN": "t", "MCP_PROXY_LIMIT": "0"})
|