test_proxy_manager.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import sys
  3. import unittest
  4. project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
  5. if project_root not in sys.path:
  6. sys.path.insert(0, project_root)
  7. from toolkit.proxy_manager import ProxyManager
  8. from core_types import Proxy, BizLogicError
  9. class TestProxyManager(unittest.TestCase):
  10. def setUp(self):
  11. self.pm = ProxyManager.Instance()
  12. def test_local_proxy_single(self):
  13. # 测试 local 池 (单端口)
  14. res = self.pm.get_next_proxy(["local:local"])
  15. self.assertIsInstance(res, dict)
  16. self.assertEqual(res["ip"], "127.0.0.1")
  17. self.assertEqual(res["port"], 7897)
  18. # 验证能够无缝构建 Proxy
  19. proxy_obj = Proxy(**res)
  20. self.assertEqual(proxy_obj.port, 7897)
  21. def test_local_proxy_multi_pool(self):
  22. # 测试 local_multi_pool (多端口 10001 ~ 10306)
  23. res = self.pm.get_next_proxy(["local:local_multi_pool"])
  24. self.assertIsInstance(res, dict)
  25. self.assertEqual(res["ip"], "127.0.0.1")
  26. self.assertTrue(10001 <= res["port"] <= 10400)
  27. proxy_obj = Proxy(**res)
  28. self.assertTrue(10001 <= proxy_obj.port <= 10400)
  29. def test_invalid_prefix(self):
  30. # 测试未带前缀的非法格式,必须抛出 BizLogicError
  31. with self.assertRaises(BizLogicError) as ctx:
  32. self.pm.get_next_proxy(["proxy-cheap"])
  33. self.assertIn("Must start with 'local:' or 'cloud:'", str(ctx.exception))
  34. def test_nonexistent_local_pool(self):
  35. # 测试不存在的本地池
  36. with self.assertRaises(BizLogicError) as ctx:
  37. self.pm.get_next_proxy(["local:non_existent_pool_12345"])
  38. self.assertIn("No proxies found in local pools", str(ctx.exception))
  39. if __name__ == "__main__":
  40. unittest.main()