test: deduplicate proxy value and construct tool via constructor

- Use a local variable for the proxy URL instead of hardcoding it twice
- Pass proxy through the WebSearchTool constructor instead of mutating
  after instantiation (matches real usage path)
- Add assertion that timeout is still forwarded correctly
- Use generic mock data instead of test-specific strings
This commit is contained in:
hyoukadev
2026-06-24 15:45:44 +08:00
committed by Xubin Ren
parent c66a0217d2
commit 9c6eaf0bed
+10 -6
View File
@@ -369,21 +369,25 @@ async def test_duckduckgo_search(monkeypatch):
async def test_duckduckgo_search_passes_proxy(monkeypatch):
"""DDGS client must receive the configured proxy so search works behind a proxy."""
captured: dict = {}
proxy_url = "http://proxy.example:8080"
class ProxyCaptorDDGS:
def __init__(self, **kw):
captured.update(kw)
def text(self, query, max_results=5):
return [{"title": "Proxied", "href": "https://ddg.example", "body": "OK"}]
return [{"title": "Result", "href": "https://example.com", "body": "OK"}]
monkeypatch.setattr("ddgs.DDGS", ProxyCaptorDDGS)
tool = _tool(provider="duckduckgo")
tool.proxy = "http://192.168.1.1:8080"
result = await tool.execute(query="hello")
assert captured.get("proxy") == "http://192.168.1.1:8080"
assert "Proxied" in result
tool = WebSearchTool(
config=WebSearchConfig(provider="duckduckgo"),
proxy=proxy_url,
)
result = await tool.execute(query="test")
assert captured["proxy"] == proxy_url
assert captured["timeout"] == 10
assert "Result" in result
@pytest.mark.asyncio