Files
crawl4ai/test_scripts/test_proxy_deprecation.py
AHMET YILMAZ f7a3366f72 #1375 : refactor(proxy) Deprecate 'proxy' parameter in BrowserConfig and enhance proxy string parsing
- Updated ProxyConfig.from_string to support multiple proxy formats, including URLs with credentials.
- Deprecated the 'proxy' parameter in BrowserConfig, replacing it with 'proxy_config' for better flexibility.
- Added warnings for deprecated usage and clarified behavior when both parameters are provided.
- Updated documentation and tests to reflect changes in proxy configuration handling.
2025-08-28 17:21:49 +08:00

71 lines
2.4 KiB
Python

import sys
import warnings
from crawl4ai.async_configs import BrowserConfig, ProxyConfig
def main() -> int:
warnings.simplefilter("always", DeprecationWarning)
# Case 1: Using deprecated proxy string should emit DeprecationWarning and auto-convert
captured = []
proxy_str = "23.95.150.145:6114:username:password"
with warnings.catch_warnings(record=True) as w:
cfg = BrowserConfig(proxy=proxy_str, headless=True)
captured = [m for m in w if issubclass(m.category, DeprecationWarning)]
if not captured:
print("[FAIL] No DeprecationWarning emitted for BrowserConfig(proxy=...) usage.")
return 1
if cfg.proxy is not None:
print("[FAIL] cfg.proxy should be None after auto-conversion.")
return 1
if not isinstance(cfg.proxy_config, ProxyConfig):
print("[FAIL] cfg.proxy_config should be a ProxyConfig instance after auto-conversion.")
return 1
# Basic sanity checks on auto-parsed proxy_config
if not cfg.proxy_config.server or ":" not in (cfg.proxy_config.server or ""):
print("[FAIL] proxy_config.server appears invalid after conversion:", cfg.proxy_config.server)
return 1
if not cfg.proxy_config.username or not cfg.proxy_config.password:
print("[FAIL] proxy_config credentials missing after conversion.")
return 1
print("[OK] DeprecationWarning captured and proxy auto-converted to proxy_config.")
# Case 2: Using proxy_config directly should not emit DeprecationWarning
with warnings.catch_warnings(record=True) as w2:
cfg2 = BrowserConfig(
proxy_config={
"server": "http://127.0.0.1:8080",
"username": "u",
"password": "p",
},
headless=True,
)
if any(issubclass(m.category, DeprecationWarning) for m in w2):
print("[FAIL] Unexpected DeprecationWarning when using proxy_config.")
return 1
if cfg2.proxy is not None:
print("[FAIL] cfg2.proxy should be None (only proxy_config should be used).")
return 1
if not isinstance(cfg2.proxy_config, ProxyConfig):
print("[FAIL] cfg2.proxy_config should be a ProxyConfig instance.")
return 1
print("[OK] proxy_config path works without deprecation warnings.")
print("All checks passed.")
return 0
if __name__ == "__main__":
raise SystemExit(main())