11"""Tests for network utility helpers."""
22
3+ import builtins
4+
35from astrbot .core .utils import network_utils
46
57
@@ -18,6 +20,39 @@ def test_sanitize_proxy_url_masks_empty_password_credentials():
1820 assert network_utils ._sanitize_proxy_url (proxy ) == "http://****@127.0.0.1:1080"
1921
2022
23+ def test_sanitize_proxy_url_returns_original_when_no_credentials ():
24+ proxy = "http://127.0.0.1:1080"
25+ assert network_utils ._sanitize_proxy_url (proxy ) == proxy
26+
27+
28+ def test_sanitize_proxy_url_returns_original_for_non_url_text ():
29+ proxy = "not a url"
30+ assert network_utils ._sanitize_proxy_url (proxy ) == proxy
31+
32+
33+ def test_sanitize_proxy_url_returns_original_for_empty_string ():
34+ assert network_utils ._sanitize_proxy_url ("" ) == ""
35+
36+
37+ def test_sanitize_proxy_url_masks_credentials_for_ipv6_host ():
38+ proxy = "http://user:secret@[::1]:1080"
39+ assert network_utils ._sanitize_proxy_url (proxy ) == "http://****@[::1]:1080"
40+
41+
42+ def test_sanitize_proxy_url_falls_back_to_placeholder_on_parse_error (monkeypatch ):
43+ proxy = "http://user:secret@127.0.0.1:1080"
44+ original_import = builtins .__import__
45+
46+ def guarded_import (name , globals_ = None , locals_ = None , fromlist = (), level = 0 ):
47+ if name == "urllib.parse" :
48+ raise ImportError ("boom" )
49+ return original_import (name , globals_ , locals_ , fromlist , level )
50+
51+ monkeypatch .setattr (builtins , "__import__" , guarded_import )
52+
53+ assert network_utils ._sanitize_proxy_url (proxy ) == "****"
54+
55+
2156def test_is_socks_proxy_detects_supported_schemes ():
2257 assert network_utils ._is_socks_proxy ("socks5://127.0.0.1:1080" )
2358 assert network_utils ._is_socks_proxy ("socks4://127.0.0.1:1080" )
@@ -42,3 +77,42 @@ def fake_error(message: str):
4277
4378 assert "http://token@127.0.0.1:1080" not in captured ["message" ]
4479 assert "http://****@127.0.0.1:1080" in captured ["message" ]
80+
81+
82+ def test_log_connection_failure_without_proxy_does_not_log_proxy_label (monkeypatch ):
83+ captured = {}
84+
85+ def fake_error (message : str ):
86+ captured ["message" ] = message
87+
88+ monkeypatch .setattr (network_utils .logger , "error" , fake_error )
89+ monkeypatch .delenv ("http_proxy" , raising = False )
90+ monkeypatch .delenv ("https_proxy" , raising = False )
91+
92+ network_utils .log_connection_failure (
93+ provider_label = "OpenAI" ,
94+ error = RuntimeError ("connection failed" ),
95+ proxy = None ,
96+ )
97+
98+ assert "代理地址" not in captured ["message" ]
99+ assert "connection failed" in captured ["message" ]
100+
101+
102+ def test_log_connection_failure_keeps_error_text_when_no_proxy_text (monkeypatch ):
103+ proxy = "http://token@127.0.0.1:1080"
104+ captured = {}
105+
106+ def fake_error (message : str ):
107+ captured ["message" ] = message
108+
109+ monkeypatch .setattr (network_utils .logger , "error" , fake_error )
110+
111+ network_utils .log_connection_failure (
112+ provider_label = "OpenAI" ,
113+ error = RuntimeError ("connect timeout" ),
114+ proxy = proxy ,
115+ )
116+
117+ assert "http://****@127.0.0.1:1080" in captured ["message" ]
118+ assert "connect timeout" in captured ["message" ]
0 commit comments