64 lines
2.7 KiB
Python
64 lines
2.7 KiB
Python
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from domainCheck.detect import aizhan, baidu, c360, chinaz
|
|
|
|
|
|
class StepTimeoutBudgetTests(unittest.TestCase):
|
|
def tearDown(self):
|
|
c360._PROXY_SESSIONS.clear()
|
|
|
|
def test_baidu_timeout_respects_remaining_budget(self):
|
|
with patch.dict("os.environ", {}, clear=False):
|
|
timeout = baidu._resolve_baidu_timeout({"http": "http://127.0.0.1:8080"}, budget_seconds=1.1)
|
|
self.assertLessEqual(timeout, 1.1)
|
|
self.assertGreaterEqual(timeout, 0.6)
|
|
|
|
def test_360_timeout_respects_remaining_budget(self):
|
|
with patch.dict("os.environ", {}, clear=False):
|
|
timeout = c360._resolve_360_timeout({"http": "http://127.0.0.1:8080"}, budget_seconds=0.9)
|
|
self.assertLessEqual(timeout, 0.9)
|
|
self.assertGreaterEqual(timeout, 0.6)
|
|
|
|
def test_360_direct_session_disables_env_proxy_and_is_reused(self):
|
|
first = c360._get_session()
|
|
second = c360._get_session()
|
|
self.assertIs(first, second)
|
|
self.assertFalse(first.trust_env)
|
|
|
|
def test_360_proxy_session_is_reused_per_proxy_url(self):
|
|
first = c360._get_session({"http": "http://127.0.0.1:8080"})
|
|
second = c360._get_session({"https": "http://127.0.0.1:8080"})
|
|
third = c360._get_session({"http": "http://127.0.0.1:8081"})
|
|
self.assertIs(first, second)
|
|
self.assertIsNot(first, third)
|
|
self.assertFalse(first.trust_env)
|
|
|
|
def test_360_proxy_session_cache_evicts_oldest_entry(self):
|
|
with patch.dict("os.environ", {"DOMAINCHECK_360_PROXY_SESSION_CACHE_SIZE": "2"}, clear=False):
|
|
first = c360._get_session({"http": "http://127.0.0.1:8080"})
|
|
second = c360._get_session({"http": "http://127.0.0.1:8081"})
|
|
third = c360._get_session({"http": "http://127.0.0.1:8082"})
|
|
|
|
self.assertEqual(2, len(c360._PROXY_SESSIONS))
|
|
self.assertIsNotNone(second)
|
|
self.assertIsNotNone(third)
|
|
|
|
recreated_first = c360._get_session({"http": "http://127.0.0.1:8080"})
|
|
self.assertIsNot(first, recreated_first)
|
|
|
|
def test_chinaz_timeout_respects_remaining_budget(self):
|
|
timeout = chinaz._resolve_chinaz_timeout({"http": "http://127.0.0.1:8080"}, budget_seconds=1.3)
|
|
self.assertLessEqual(timeout, 1.3)
|
|
self.assertGreaterEqual(timeout, 0.6)
|
|
|
|
def test_aizhan_timeout_respects_remaining_budget(self):
|
|
with patch.dict("os.environ", {}, clear=False):
|
|
timeout = aizhan._resolve_aizhan_timeout({"http": "http://127.0.0.1:8080"}, budget_seconds=1.0)
|
|
self.assertLessEqual(timeout, 1.0)
|
|
self.assertGreaterEqual(timeout, 0.6)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|