The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
add tests
Colin Line420b633a1ac
diff --git a/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py b/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.pyindex 26fc18de16d..17b4243da1d 100644--- a/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py+++ b/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py@@ -1,17 +1,14 @@-import ioimport os-import pathlib-import sslimport sys-from unittest.mock import MagicMock+from unittest.mock import AsyncMock, Mock, patchimport pytestsys.path.insert(0, os.path.abspath("../../../..")) # Adds the parent directory to the system path-import litellmfrom litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler+from litellm.types.router import GenericLiteLLMParamsdef test_prepare_fake_stream_request():@@ -75,3 +72,146 @@ def test_prepare_fake_stream_request():assert "stream" not in result_dataassert result_data["model"] == "gpt-4"assert result_data["messages"] == [{"role": "user", "content": "Hello"}]+++async def test_async_anthropic_messages_handler_extra_headers():+ """+ Test that async_anthropic_messages_handler correctly extracts and merges+ extra_headers from kwargs with proper priority.+ """+ handler = BaseLLMHTTPHandler()++ # Mock the config+ mock_config = Mock()+ mock_config.validate_anthropic_messages_environment = Mock(+ return_value=({"x-api-key": "test-key"}, "https://api.anthropic.com")+ )+ mock_config.transform_anthropic_messages_request = Mock(+ return_value={"model": "claude-3-opus-20240229", "messages": []}+ )++ # Mock the client+ mock_client = AsyncMock()+ mock_response = Mock()+ mock_response.status_code = 200+ mock_response.json.return_value = {+ "id": "msg_123",+ "type": "message",+ "role": "assistant",+ "content": [{"type": "text", "text": "Hello!"}],+ "model": "claude-3-opus-20240229",+ "stop_reason": "end_turn",+ }+ mock_client.post = AsyncMock(return_value=mock_response)++ # Mock logging object+ mock_logging_obj = Mock()+ mock_logging_obj.update_environment_variables = Mock()+ mock_logging_obj.model_call_details = {}+ mock_logging_obj.stream = False++ # Test case 1: Only extra_headers in kwargs+ kwargs = {+ "extra_headers": {+ "X-Custom-Header": "from-kwargs",+ "X-Auth-Token": "token123",+ }+ }++ with patch(+ "litellm.litellm_core_utils.get_provider_specific_headers.ProviderSpecificHeaderUtils.get_provider_specific_headers"+ ) as mock_provider_headers:+ mock_provider_headers.return_value = None++ # Capture what headers are passed to validate_anthropic_messages_environment+ captured_headers = {}+ def capture_validate(*args, **kwargs):+ captured_headers.update(kwargs.get("headers", {}))+ return ({"x-api-key": "test-key"}, "https://api.anthropic.com")++ mock_config.validate_anthropic_messages_environment = capture_validate++ try:+ await handler.async_anthropic_messages_handler(+ model="claude-3-opus-20240229",+ messages=[{"role": "user", "content": "Hello"}],+ anthropic_messages_provider_config=mock_config,+ anthropic_messages_optional_request_params={},+ custom_llm_provider="anthropic",+ litellm_params=GenericLiteLLMParams(),+ logging_obj=mock_logging_obj,+ client=mock_client,+ kwargs=kwargs,+ )+ except Exception:+ pass # We're testing header extraction, not the full flow++ # Verify extra_headers were extracted and merged+ assert "X-Custom-Header" in captured_headers+ assert captured_headers["X-Custom-Header"] == "from-kwargs"+ assert "X-Auth-Token" in captured_headers+ assert captured_headers["X-Auth-Token"] == "token123"+++async def test_async_anthropic_messages_handler_header_priority():+ """+ Test that async_anthropic_messages_handler respects header priority:+ forwarded < extra_headers < provider_specific+ """+ handler = BaseLLMHTTPHandler()++ # Mock the config+ mock_config = Mock()+ mock_client = AsyncMock()+ mock_logging_obj = Mock()+ mock_logging_obj.update_environment_variables = Mock()+ mock_logging_obj.model_call_details = {}+ mock_logging_obj.stream = False++ # Test with all three header sources+ kwargs = {+ "headers": {"X-Priority": "forwarded", "X-Forwarded-Only": "keep"},+ "extra_headers": {"X-Priority": "extra", "X-Extra-Only": "also-keep"},+ }++ with patch(+ "litellm.litellm_core_utils.get_provider_specific_headers.ProviderSpecificHeaderUtils.get_provider_specific_headers"+ ) as mock_provider_headers:+ mock_provider_headers.return_value = {+ "X-Priority": "provider",+ "X-Provider-Only": "keep-this-too"+ }++ captured_headers = {}+ def capture_validate(*args, **kwargs):+ captured_headers.update(kwargs.get("headers", {}))+ return ({"x-api-key": "test-key"}, "https://api.anthropic.com")++ mock_config.validate_anthropic_messages_environment = capture_validate+ mock_config.transform_anthropic_messages_request = Mock(+ return_value={"model": "claude-3-opus-20240229", "messages": []}+ )++ try:+ await handler.async_anthropic_messages_handler(+ model="claude-3-opus-20240229",+ messages=[{"role": "user", "content": "Hello"}],+ anthropic_messages_provider_config=mock_config,+ anthropic_messages_optional_request_params={},+ custom_llm_provider="anthropic",+ litellm_params=GenericLiteLLMParams(),+ logging_obj=mock_logging_obj,+ client=mock_client,+ kwargs=kwargs,+ )+ except Exception:+ pass++ # Verify priority: provider_specific should win+ assert captured_headers["X-Priority"] == "provider"+ # Verify all unique headers from different sources are present+ assert captured_headers["X-Forwarded-Only"] == "keep"+ assert captured_headers["X-Extra-Only"] == "also-keep"+ assert captured_headers["X-Provider-Only"] == "keep-this-too"