Keep PR 900 overrides bounded and opt-in

The PR adds API limit overrides and static proxy support, but the review found that the default proxy provider changed to an invalid static placeholder and the new API fields accepted unbounded values. This keeps the existing proxy default intact, makes static proxy explicit via config or CLI, validates API limit ranges, and adds focused regression coverage for both paths.

Constraint: PR branch must remain contributor-branch compatible and avoid adding dependencies

Rejected: Keep static as the default provider | breaks existing --enable_ip_proxy defaults with an invalid placeholder URL

Rejected: Accept arbitrary integer limits | lets API callers request negative or excessive crawl sizes

Confidence: high

Scope-risk: narrow

Directive: Do not change proxy provider defaults when adding new providers; new providers should be opt-in and covered by provider-specific tests

Tested: uv run pytest tests/test_api_limits.py tests/test_static_proxy_provider.py

Tested: uv run pytest tests

Tested: uv run pytest test/test_utils.py

Tested: uv run python -m compileall api cmd_arg config proxy tests

Tested: git diff --cached --check

Not-tested: Live crawler run against external platforms or real proxy vendor endpoints
This commit is contained in:
程序员阿江(Relakkes)
2026-05-29 21:27:52 +08:00
parent f997befce9
commit 8e93438fe5
7 changed files with 126 additions and 34 deletions

View File

@@ -13,7 +13,7 @@ async def test_cmd_arg_crawler_max_notes_count():
# Store original values
orig_notes = config.CRAWLER_MAX_NOTES_COUNT
orig_comments = config.CRAWLER_MAX_COMMENTS_COUNT_SINGLENOTES
try:
await parse_cmd([
"--platform", "xhs",
@@ -28,7 +28,7 @@ async def test_cmd_arg_crawler_max_notes_count():
def test_crawler_manager_build_command():
cm = CrawlerManager()
# 1. No max limits passed in API request
req1 = CrawlerStartRequest(
platform=PlatformEnum.XHS,
@@ -64,10 +64,10 @@ def test_crawler_manager_build_command():
def test_api_start_crawler_with_limits():
client = TestClient(app)
with patch("api.routers.crawler.crawler_manager.start", new_callable=AsyncMock) as mock_start:
mock_start.return_value = True
# Test case 1: with limits
response = client.post("/api/crawler/start", json={
"platform": "xhs",
@@ -77,10 +77,10 @@ def test_api_start_crawler_with_limits():
"max_notes_count": 50,
"max_comments_count": 5
})
assert response.status_code == 200
assert response.json() == {"status": "ok", "message": "Crawler started successfully"}
mock_start.assert_called_once()
called_request = mock_start.call_args[0][0]
assert called_request.platform == PlatformEnum.XHS
@@ -89,10 +89,10 @@ def test_api_start_crawler_with_limits():
def test_api_start_crawler_without_limits():
client = TestClient(app)
with patch("api.routers.crawler.crawler_manager.start", new_callable=AsyncMock) as mock_start:
mock_start.return_value = True
# Test case 2: without limits
response = client.post("/api/crawler/start", json={
"platform": "xhs",
@@ -100,10 +100,38 @@ def test_api_start_crawler_without_limits():
"crawler_type": "search",
"keywords": "test"
})
assert response.status_code == 200
mock_start.assert_called_once()
called_request = mock_start.call_args[0][0]
assert called_request.platform == PlatformEnum.XHS
assert called_request.max_notes_count is None
assert called_request.max_comments_count is None
@pytest.mark.parametrize(
("field_name", "value"),
[
("max_notes_count", 0),
("max_notes_count", -1),
("max_notes_count", 10001),
("max_comments_count", 0),
("max_comments_count", -1),
("max_comments_count", 10001),
],
)
def test_api_rejects_invalid_limits(field_name, value):
client = TestClient(app)
payload = {
"platform": "xhs",
"login_type": "qrcode",
"crawler_type": "search",
"keywords": "test",
field_name: value,
}
with patch("api.routers.crawler.crawler_manager.start", new_callable=AsyncMock) as mock_start:
response = client.post("/api/crawler/start", json=payload)
assert response.status_code == 422
mock_start.assert_not_called()

View File

@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
import pytest
import config
from proxy.proxy_ip_pool import StaticProxyProvider, create_ip_pool
from proxy.types import ProviderNameEnum
def test_default_proxy_provider_remains_existing_provider():
assert config.IP_PROXY_PROVIDER_NAME == ProviderNameEnum.KUAI_DAILI_PROVIDER.value
assert config.IP_PROXY_POOL_COUNT == 2
assert config.STATIC_PROXY_URL == ""
@pytest.mark.asyncio
async def test_static_proxy_provider_parses_proxy_url(monkeypatch):
monkeypatch.setattr(config, "STATIC_PROXY_URL", "http://user:p%40ss@example.com:8080")
proxies = await StaticProxyProvider().get_proxy(1)
assert len(proxies) == 1
proxy = proxies[0]
assert proxy.ip == "example.com"
assert proxy.port == 8080
assert proxy.user == "user"
assert proxy.password == "p@ss"
assert proxy.protocol == "http://"
assert proxy.expired_time_ts is not None
@pytest.mark.asyncio
async def test_static_proxy_provider_rejects_invalid_url(monkeypatch):
monkeypatch.setattr(config, "STATIC_PROXY_URL", "http://your_home_domain:port")
proxies = await StaticProxyProvider().get_proxy(1)
assert proxies == []
@pytest.mark.asyncio
async def test_static_proxy_pool_disables_validation(monkeypatch):
monkeypatch.setattr(config, "IP_PROXY_PROVIDER_NAME", ProviderNameEnum.STATIC_PROVIDER.value)
monkeypatch.setattr(config, "STATIC_PROXY_URL", "https://example.com:8443")
pool = await create_ip_pool(ip_pool_count=2, enable_validate_ip=True)
assert pool.enable_validate_ip is False
assert len(pool.proxy_list) == 1
assert pool.proxy_list[0].protocol == "https://"