test(prompt_templates): make test_convert_url hermetic instead of hitting picsum.photos

test_convert_url called convert_url_to_base64 against a live picsum.photos
URL and asserted nothing, so it added no real signal and broke CI whenever
the host was unreachable (it was returning 522 and blocking this branch).
Replace the live call with a mocked HTTP client and assert the produced
base64 data URL, so the conversion path is exercised deterministically with
no network dependency. This suite runs under VCR, which is why a transport
level mock (respx) does not reliably intercept; mocking the client object
itself is robust regardless.
This commit is contained in:
mateo-berri 2026-06-22 16:16:50 +00:00
parent db2bc491d2
commit 0a82e138a9
No known key found for this signature in database

View file

@ -605,8 +605,33 @@ def test_no_messages_yields_user_text():
assert contents == expected_output
def test_convert_url():
convert_url_to_base64("https://picsum.photos/id/237/200/300")
def test_convert_url(monkeypatch):
import base64
from unittest.mock import MagicMock
import httpx
from litellm.litellm_core_utils.prompt_templates.image_handling import (
in_memory_cache,
)
url = "https://picsum.photos/id/237/200/300"
image_bytes = b"\x89PNG\r\n\x1a\nfake-png-bytes"
mock_client = MagicMock()
mock_client.get.return_value = httpx.Response(
200, content=image_bytes, headers={"Content-Type": "image/png"}
)
monkeypatch.setattr(litellm, "user_url_validation", False, raising=False)
monkeypatch.setattr(litellm, "module_level_client", mock_client, raising=False)
in_memory_cache.flush_cache()
result = convert_url_to_base64(url)
expected = "data:image/png;base64," + base64.b64encode(image_bytes).decode("utf-8")
assert result == expected
mock_client.get.assert_called_once()
def test_azure_tool_call_invoke_helper():