Skip to content

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]

test fix

Ishaan Jaff9c8619ed1071
diff --git a/tests/test_litellm/proxy/client/test_http_client.py b/tests/test_litellm/proxy/client/test_http_client.py
index 3bb6c4cef84..b85724139be 100644
--- a/tests/test_litellm/proxy/client/test_http_client.py
+++ b/tests/test_litellm/proxy/client/test_http_client.py
@@ -138,7 +138,7 @@ def test_request_invalid_json(client):
)
# Check that request raises exception
- with pytest.raises(json.JSONDecodeError) as exc_info:
+ with pytest.raises(requests.exceptions.JSONDecodeError) as exc_info:
client.request("GET", "/models")
diff --git a/tests/test_litellm/proxy/client/test_keys.py b/tests/test_litellm/proxy/client/test_keys.py
index 4e341e8ea7d..bc6114ab350 100644
--- a/tests/test_litellm/proxy/client/test_keys.py
+++ b/tests/test_litellm/proxy/client/test_keys.py
@@ -3,6 +3,7 @@ import sys
import pytest
import requests
+import responses
sys.path.insert(
0, os.path.abspath("../../..")
@@ -129,7 +130,8 @@ def test_list_request_all_parameters(client):
}
-def test_list_mock_response_pagination(client, requests_mock):
+def test_list_mock_response_pagination(client):
"""Test list with a mocked paginated response"""
mock_response = {
"data": {
@@ -155,17 +157,19 @@ def test_list_mock_response_pagination(client, requests_mock):
}
}
- requests_mock.get(
- f"{client._base_url}/key/list",
+ responses.add(
+ responses.GET,
+ f"{client._base_url}/key/list?page=1&size=2",
json=mock_response,
- additional_matcher=lambda r: r.qs == {"page": ["1"], "size": ["2"]},
+ status=200,
)
response = client.list(page=1, size=2)
assert response == mock_response
-def test_list_mock_response_filtered(client, requests_mock):
+def test_list_mock_response_filtered(client):
"""Test list with a mocked filtered response"""
mock_response = {
"keys": [
@@ -181,22 +185,25 @@ def test_list_mock_response_filtered(client, requests_mock):
]
}
- requests_mock.get(
- f"{client._base_url}/key/list",
+ responses.add(
+ responses.GET,
+ f"{client._base_url}/key/list?user_id=user123&team_id=team456",
json=mock_response,
- additional_matcher=lambda r: (
- r.qs.get("user_id") == ["user123"] and r.qs.get("team_id") == ["team456"]
- ),
+ status=200,
)
response = client.list(user_id="user123", team_id="team456")
assert response == mock_response
-def test_list_unauthorized_error(client, requests_mock):
+def test_list_unauthorized_error(client):
"""Test that list raises UnauthorizedError for 401 responses"""
- requests_mock.get(
- f"{client._base_url}/key/list", status_code=401, json={"error": "Unauthorized"}
+ responses.add(
+ responses.GET,
+ f"{client._base_url}/key/list",
+ status=401,
+ json={"error": "Unauthorized"},
)
with pytest.raises(UnauthorizedError):
@@ -241,7 +248,8 @@ def test_generate_request_full(client):
}
-def test_generate_mock_response(client, requests_mock):
+def test_generate_mock_response(client):
"""Test generate with a mocked successful response"""
mock_response = {
"key": "new-test-key",
@@ -256,7 +264,12 @@ def test_generate_mock_response(client, requests_mock):
"config": {"max_parallel_requests": 5},
}
- requests_mock.post(f"{client._base_url}/key/generate", json=mock_response)
+ responses.add(
+ responses.POST,
+ f"{client._base_url}/key/generate",
+ json=mock_response,
+ status=200,
+ )
response = client.generate(
key_alias="test-key-alias",
@@ -268,11 +281,13 @@ def test_generate_mock_response(client, requests_mock):
assert response == mock_response
-def test_generate_unauthorized_error(client, requests_mock):
+def test_generate_unauthorized_error(client):
"""Test that generate raises UnauthorizedError for 401 responses"""
- requests_mock.post(
+ responses.add(
+ responses.POST,
f"{client._base_url}/key/generate",
- status_code=401,
+ status=401,
json={"error": "Unauthorized"},
)
@@ -318,24 +333,32 @@ def test_delete_request_with_keys_and_aliases(client):
assert request.json == {"keys": keys_to_delete, "key_aliases": aliases_to_delete}
-def test_delete_mock_response(client, requests_mock):
+def test_delete_mock_response(client):
"""Test delete with a mocked successful response"""
mock_response = {
"status": "success",
"deleted_keys": ["key1", "key2"],
"deleted_aliases": ["alias1"],
}
- requests_mock.post(f"{client._base_url}/key/delete", json=mock_response)
+ responses.add(
+ responses.POST,
+ f"{client._base_url}/key/delete",
+ json=mock_response,
+ status=200,
+ )
response = client.delete(keys=["key1", "key2"], key_aliases=["alias1"])
assert response == mock_response
-def test_delete_unauthorized_error(client, requests_mock):
+def test_delete_unauthorized_error(client):
"""Test that delete raises UnauthorizedError for 401 responses"""
- requests_mock.post(
+ responses.add(
+ responses.POST,
f"{client._base_url}/key/delete",
- status_code=401,
+ status=401,
json={"error": "Unauthorized"},
)
@@ -352,7 +375,8 @@ def test_info_request_minimal(client, base_url, api_key):
assert request.headers["Authorization"] == f"Bearer {api_key}"
-def test_info_mock_response(client, requests_mock):
+def test_info_mock_response(client):
"""Test info with a mocked successful response"""
mock_response = {
"key": "test-key",
@@ -361,27 +385,36 @@ def test_info_mock_response(client, requests_mock):
"models": ["gpt-4"],
"spend": 100.0,
}
- requests_mock.get(f"{client._base_url}/keys/info?key=test-key", json=mock_response)
+ responses.add(
+ responses.GET,
+ f"{client._base_url}/keys/info?key=test-key",
+ json=mock_response,
+ status=200,
+ )
response = client.info(key="test-key")
assert response == mock_response
-def test_info_unauthorized_error(client, requests_mock):
+def test_info_unauthorized_error(client):
"""Test that info raises UnauthorizedError for 401 responses"""
- requests_mock.get(
+ responses.add(
+ responses.GET,
f"{client._base_url}/keys/info?key=test-key",
- status_code=401,
+ status=401,
json={"error": "Unauthorized"},
)
with pytest.raises(UnauthorizedError):
client.info(key="test-key")
-def test_info_server_error(client, requests_mock):
+def test_info_server_error(client):
"""Test that info raises HTTPError for server errors"""
- requests_mock.get(
+ responses.add(
+ responses.GET,
f"{client._base_url}/keys/info?key=test-key",
- status_code=500,
+ status=500,
json={"error": "Internal Server Error"},
)
with pytest.raises(requests.exceptions.HTTPError):
diff --git a/tests/test_litellm/proxy/client/test_model_groups.py b/tests/test_litellm/proxy/client/test_model_groups.py
index 4642d96170f..3a4f1b157c3 100644
--- a/tests/test_litellm/proxy/client/test_model_groups.py
+++ b/tests/test_litellm/proxy/client/test_model_groups.py
@@ -3,6 +3,7 @@ import sys
import pytest
import requests
+import responses
sys.path.insert(
0, os.path.abspath("../../..")
@@ -75,7 +76,8 @@ def test_info_url_variants(base_url, expected):
assert request.url == expected
-def test_info_with_mock_response(client, requests_mock):
+def test_info_with_mock_response(client):
"""Test the full info execution with a mocked response"""
mock_data = {
"data": [
@@ -94,7 +96,12 @@ def test_info_with_mock_response(client, requests_mock):
},
]
}
- requests_mock.get(f"{client._base_url}/model_group/info", json=mock_data)
+ responses.add(
+ responses.GET,
+ f"{client._base_url}/model_group/info",
+ json=mock_data,
+ status=200,
+ )
response = client.info()
assert response == mock_data["data"]
@@ -103,11 +110,13 @@ def test_info_with_mock_response(client, requests_mock):
assert response[1]["model_group_name"] == "azure-group"
-def test_info_unauthorized_error(client, requests_mock):
+def test_info_unauthorized_error(client):
"""Test that info raises UnauthorizedError for 401 responses"""
- requests_mock.get(
+ responses.add(
+ responses.GET,
f"{client._base_url}/model_group/info",
- status_code=401,
+ status=401,
json={"error": "Invalid API key"},
)
@@ -116,11 +125,13 @@ def test_info_unauthorized_error(client, requests_mock):
assert exc_info.value.orig_exception.response.status_code == 401
-def test_info_other_errors(client, requests_mock):
+def test_info_other_errors(client):
"""Test that info raises normal HTTPError for non-401 errors"""
- requests_mock.get(
+ responses.add(
+ responses.GET,
f"{client._base_url}/model_group/info",
- status_code=500,
+ status=500,
json={"error": "Internal Server Error"},
)