81 lines
3.3 KiB
Python
81 lines
3.3 KiB
Python
import unittest
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import urllib3
|
|
|
|
from domainCheck.detect import register
|
|
|
|
|
|
class RegisterTimeoutConfigTests(unittest.TestCase):
|
|
def tearDown(self):
|
|
register._PROXY_MANAGERS.clear()
|
|
|
|
def test_proxy_timeout_uses_bounded_total(self):
|
|
with patch.dict("os.environ", {}, clear=False):
|
|
timeout = register._resolve_register_timeout({"http": "http://127.0.0.1:8080"})
|
|
self.assertIsInstance(timeout, urllib3.Timeout)
|
|
self.assertEqual(timeout.connect_timeout, 1.0)
|
|
self.assertEqual(timeout.read_timeout, 1.5)
|
|
self.assertEqual(timeout.total, 2.2)
|
|
|
|
def test_direct_timeout_respects_override(self):
|
|
with patch.dict(
|
|
"os.environ",
|
|
{
|
|
"DOMAINCHECK_REGISTER_DIRECT_CONNECT_TIMEOUT": "0.9",
|
|
"DOMAINCHECK_REGISTER_DIRECT_READ_TIMEOUT": "1.4",
|
|
"DOMAINCHECK_REGISTER_DIRECT_TOTAL_TIMEOUT": "1.7",
|
|
},
|
|
clear=False,
|
|
):
|
|
timeout = register._resolve_register_timeout(None)
|
|
self.assertEqual(timeout.connect_timeout, 0.9)
|
|
self.assertEqual(timeout.read_timeout, 1.4)
|
|
self.assertEqual(timeout.total, 1.7)
|
|
|
|
def test_budget_clamps_proxy_timeout_into_remaining_window(self):
|
|
with patch.dict("os.environ", {}, clear=False):
|
|
timeout = register._resolve_register_timeout(
|
|
{"http": "http://127.0.0.1:8080"},
|
|
budget_seconds=0.8,
|
|
)
|
|
self.assertEqual(timeout.total, 0.8)
|
|
self.assertLessEqual(timeout.connect_timeout, 0.35)
|
|
self.assertLessEqual(timeout.read_timeout, 0.61)
|
|
|
|
def test_request_register_converts_urllib3_timeout_for_requests(self):
|
|
session = MagicMock()
|
|
response = MagicMock()
|
|
session.get.return_value = response
|
|
timeout = urllib3.Timeout(connect=1.1, read=1.7, total=2.4)
|
|
with patch("domainCheck.detect.register._get_http_manager", return_value=session):
|
|
result = register._request_register(
|
|
"https://rdap.verisign.com/com/v1/domain/example",
|
|
proxies={"http": "http://127.0.0.1:8080", "https": "http://127.0.0.1:8080"},
|
|
timeout=timeout,
|
|
)
|
|
self.assertIs(result, response)
|
|
session.get.assert_called_once_with(
|
|
"https://rdap.verisign.com/com/v1/domain/example",
|
|
timeout=(1.1, 1.7),
|
|
proxies={"http": "http://127.0.0.1:8080", "https": "http://127.0.0.1:8080"},
|
|
allow_redirects=True,
|
|
)
|
|
|
|
def test_proxy_manager_cache_evicts_oldest_entry(self):
|
|
with patch.dict("os.environ", {"DOMAINCHECK_REGISTER_PROXY_SESSION_CACHE_SIZE": "2"}, clear=False):
|
|
first = register._get_http_manager({"http": "http://127.0.0.1:8080"})
|
|
second = register._get_http_manager({"http": "http://127.0.0.1:8081"})
|
|
third = register._get_http_manager({"http": "http://127.0.0.1:8082"})
|
|
|
|
self.assertEqual(2, len(register._PROXY_MANAGERS))
|
|
self.assertIsNotNone(second)
|
|
self.assertIsNotNone(third)
|
|
|
|
recreated_first = register._get_http_manager({"http": "http://127.0.0.1:8080"})
|
|
self.assertIsNot(first, recreated_first)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|