litellm-mirror/tests/litellm/llms/custom_httpx/test_http_handler.py
Adrian Lyjak be3ddac8b8 fix for #7605, lazy load httpx client
There's a few of these clients instantiated at startup, and they start adding up.
They were around 10ms to 30ms each to create, so this shaves off ~50ms of startup time
2025-04-10 00:09:01 -04:00

56 lines
1.8 KiB
Python

import io
import os
import pathlib
import respx
import ssl
import sys
from unittest.mock import MagicMock
import pytest
sys.path.insert(
0, os.path.abspath("../../../..")
) # Adds the parent directory to the system path
import litellm
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
@pytest.mark.asyncio
async def test_ssl_security_level(monkeypatch):
# Set environment variable for SSL security level
monkeypatch.setenv("SSL_SECURITY_LEVEL", "DEFAULT@SECLEVEL=1")
# Create async client with SSL verification disabled to isolate SSL context testing
client = AsyncHTTPHandler(ssl_verify=False)
# Get the SSL context from the client
ssl_context = client.client._transport._pool._ssl_context
# Verify that the SSL context exists and has the correct cipher string
assert isinstance(ssl_context, ssl.SSLContext)
@pytest.mark.asyncio
async def test_basic_async_request(respx_mock: respx.MockRouter):
respx_mock.get("https://api.example.com").respond(
json={"message": "Hello, world!"}
)
# Create async client with SSL verification disabled to isolate SSL context testing
client = AsyncHTTPHandler(ssl_verify=False)
response = await client.get("https://api.example.com")
assert response.status_code == 200
assert response.json() == {"message": "Hello, world!"}
def test_basic_sync_request(respx_mock: respx.MockRouter):
respx_mock.get("https://api.example.com").respond(
json={"message": "Hello, world!"}
)
# Create async client with SSL verification disabled to isolate SSL context testing
client = HTTPHandler(ssl_verify=False)
response = client.get("https://api.example.com")
assert response.status_code == 200
assert response.json() == {"message": "Hello, world!"}