Files
kaoyan/tests/test_network.py

131 lines
5.3 KiB
Python

import requests
import pytest
from studio.network import ProxyRouter, Transport, ProxyUnavailable
from studio.settings import SettingsStore
from studio.auth import CookieVault, AuthService
class Response:
def __init__(self, data, status=200, text=""):
self.data = data
self.status_code = status
self.text = text
self.headers = {}
self.url = "https://yz.chsi.com.cn/zsml/"
def json(self):
return self.data
def raise_for_status(self):
if self.status_code >= 400:
raise requests.HTTPError(response=self)
class Pool:
def __init__(self, proxies=None):
self.proxies = proxies if proxies is not None else [{"proxy": "1.2.3.4:8080", "https": True}, {"proxy": "5.6.7.8:9090", "https": True}]
def get(self, url, **kwargs):
if url.endswith("/all/"):
return Response(self.proxies)
if url.endswith("/count/"):
return Response({"count": len(self.proxies), "http_type": {"https": len(self.proxies)}})
return Response(self.proxies[0] if self.proxies else {"code": 0, "src": "no proxy"})
class Remote:
def __init__(self, fail_once=False):
self.fail_once = fail_once
self.cookies = requests.cookies.RequestsCookieJar()
def request(self, method, url, **kwargs):
if self.fail_once:
self.fail_once = False
raise requests.exceptions.ProxyError("failed")
return Response({"routed_through": (kwargs.get("proxies") or {}).get("https"), "url": url})
def setup_network(tmp_path, mode="proxy_only", pool=None, remote=None):
settings = SettingsStore(tmp_path)
settings.update({"network_mode": mode, "request_interval": 0.3})
router = ProxyRouter(settings, session=pool or Pool())
transport = Transport(settings, router, session=remote or Remote(), wait=lambda _: None)
return settings, router, transport
def test_catalog_and_score_requests_share_the_selected_proxy(tmp_path):
_, router, transport = setup_network(tmp_path)
first = transport.request("POST", "https://yz.chsi.com.cn/zsml/rs/dws.do").json()
second = transport.request("POST", "https://api.kaoyan.cn/pc/school/schoolScore").json()
assert first["routed_through"] == second["routed_through"]
assert first["routed_through"] in ["http://1.2.3.4:8080", "http://5.6.7.8:9090"]
def test_strict_proxy_mode_never_falls_back_to_direct_connection(tmp_path):
_, _, transport = setup_network(tmp_path, pool=Pool([]))
with pytest.raises(ProxyUnavailable):
transport.request("POST", "https://yz.chsi.com.cn/zsml/rs/dws.do")
def test_prefer_proxy_mode_exposes_direct_fallback(tmp_path):
_, router, transport = setup_network(tmp_path, mode="prefer_proxy", pool=Pool([]))
result = transport.request("GET", "https://api.kaoyan.cn/pc/school/schoolList").json()
assert result["routed_through"] is None
assert router.status()["route"] == "direct"
assert router.status()["fallback"] is True
def test_network_failure_rotates_to_another_proxy(tmp_path):
_, router, transport = setup_network(tmp_path, remote=Remote(fail_once=True))
original = router.resolve()
result = transport.request("GET", "https://yz.chsi.com.cn/zsml/").json()
assert result["routed_through"] != original
assert len(router.failed) == 1
def test_cookie_is_encrypted_and_not_returned_in_settings(tmp_path):
vault = CookieVault(tmp_path)
vault.save({"JSESSIONID": "secret-test-cookie", "CASTGC": "ticket"})
assert vault.load()["JSESSIONID"] == "secret-test-cookie"
assert b"secret-test-cookie" not in vault.path.read_bytes()
assert "cookie" not in SettingsStore(tmp_path).read()
def test_rate_limiting_does_not_quarantine_a_healthy_proxy(tmp_path):
class Limited(Remote):
def __init__(self):
super().__init__()
self.calls = 0
def request(self, *args, **kwargs):
self.calls += 1
if self.calls == 1:
return Response({}, status=429)
return super().request(*args, **kwargs)
_, router, transport = setup_network(tmp_path, remote=Limited())
original = router.resolve()
assert transport.request("GET", "https://yz.chsi.com.cn/zsml/").json()["routed_through"] == original
assert router.failed == set()
def test_browser_cookie_domains_preserve_same_name_sessions(tmp_path):
_, _, transport = setup_network(tmp_path)
vault = CookieVault(tmp_path)
vault.save([{"name": "JSESSIONID", "value": "account-cookie", "domain": "account.chsi.com.cn", "path": "/"}, {"name": "JSESSIONID", "value": "catalog-cookie", "domain": "yz.chsi.com.cn", "path": "/"}])
AuthService(transport, vault)
cookies = transport.session.cookies
assert cookies.get("JSESSIONID", domain="account.chsi.com.cn") == "account-cookie"
assert cookies.get("JSESSIONID", domain="yz.chsi.com.cn") == "catalog-cookie"
def test_auth_checks_protected_direction_api_rather_than_public_school_index(tmp_path):
class PublicIndex(Remote):
def request(self, method, url, **kwargs):
if url.endswith('/dws.do'):
return Response({'flag': True, 'msg': {'list': []}})
return Response({'flag': False, 'msg': '请登录'})
_, _, transport = setup_network(tmp_path, remote=PublicIndex())
auth = AuthService(transport, CookieVault(tmp_path))
assert auth.check()['valid'] is False