diff --git a/src/langbot/pkg/utils/image.py b/src/langbot/pkg/utils/image.py index 5716b07d..0296ba05 100644 --- a/src/langbot/pkg/utils/image.py +++ b/src/langbot/pkg/utils/image.py @@ -145,7 +145,8 @@ def get_qq_image_downloadable_url(image_url: str) -> tuple[str, dict]: """获取QQ图片的下载链接""" parsed = urlparse(image_url) query = parse_qs(parsed.query) - return f'http://{parsed.netloc}{parsed.path}', query + scheme = parsed.scheme or 'http' + return f'{scheme}://{parsed.netloc}{parsed.path}', query async def get_qq_image_bytes(image_url: str, query: dict = {}) -> tuple[bytes, str]: diff --git a/tests/unit_tests/utils/__init__.py b/tests/unit_tests/utils/__init__.py index e69de29b..8b137891 100644 --- a/tests/unit_tests/utils/__init__.py +++ b/tests/unit_tests/utils/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/unit_tests/utils/test_image.py b/tests/unit_tests/utils/test_image.py index 0c752a9b..90ab1caf 100644 --- a/tests/unit_tests/utils/test_image.py +++ b/tests/unit_tests/utils/test_image.py @@ -56,13 +56,21 @@ class TestGetQQImageDownloadableUrl: # Fragment is not included in query string parsing assert "http://example.com/image.jpg" in result_url - def test_https_url(self): - """Parse HTTPS URL - note: function returns http:// regardless of input scheme.""" + def test_https_url_preserves_scheme(self): + """Parse HTTPS URL without downgrading the scheme.""" url = "https://example.com/image.jpg" result_url, query = get_qq_image_downloadable_url(url) - # The function constructs URL with http:// scheme - assert "example.com/image.jpg" in result_url + assert result_url == "https://example.com/image.jpg" + assert query == {} + + def test_missing_scheme_defaults_to_http(self): + """Parse scheme-less URL with the existing HTTP default.""" + url = "example.com/image.jpg?param=value" + result_url, query = get_qq_image_downloadable_url(url) + + assert result_url == "http://example.com/image.jpg" + assert query == {"param": ["value"]} class TestExtractB64AndFormat: