mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-07 08:26:10 +00:00
* test: drop the cwd-relative sys.path.insert calls from the test suite
TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.
Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.
Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.
* test: drop the duplicate imports the sys.path sweep exposed to F811
* test(pre-call-utils): restore the os import the new bedrock tests need
169 lines
4.3 KiB
Python
169 lines
4.3 KiB
Python
"""Tests for the HTTP client."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
import requests
|
|
|
|
|
|
|
|
import responses
|
|
|
|
from litellm.proxy.client.http_client import HTTPClient
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
"""Create a test HTTP client."""
|
|
return HTTPClient(
|
|
base_url="http://localhost:4000",
|
|
api_key="test-key",
|
|
)
|
|
|
|
|
|
@responses.activate
|
|
def test_request_get(client):
|
|
"""Test making a GET request."""
|
|
# Mock response
|
|
responses.add(
|
|
responses.GET,
|
|
"http://localhost:4000/models",
|
|
json={"models": []},
|
|
status=200,
|
|
)
|
|
|
|
# Make request
|
|
response = client.request("GET", "/models")
|
|
|
|
# Check response
|
|
assert response == {"models": []}
|
|
|
|
# Check request
|
|
assert len(responses.calls) == 1
|
|
assert responses.calls[0].request.url == "http://localhost:4000/models"
|
|
assert responses.calls[0].request.headers["Authorization"] == "Bearer test-key"
|
|
|
|
|
|
@responses.activate
|
|
def test_request_post_with_json(client):
|
|
"""Test making a POST request with JSON data."""
|
|
# Mock response
|
|
responses.add(
|
|
responses.POST,
|
|
"http://localhost:4000/models",
|
|
json={"id": "model-123"},
|
|
status=200,
|
|
)
|
|
|
|
# Test data
|
|
json_data = {"model": "gpt-4", "params": {"temperature": 0.7}}
|
|
|
|
# Make request
|
|
response = client.request(
|
|
"POST",
|
|
"/models",
|
|
json=json_data,
|
|
)
|
|
|
|
# Check response
|
|
assert response == {"id": "model-123"}
|
|
|
|
# Check request
|
|
assert len(responses.calls) == 1
|
|
assert responses.calls[0].request.url == "http://localhost:4000/models"
|
|
assert json.loads(responses.calls[0].request.body) == json_data
|
|
|
|
|
|
@responses.activate
|
|
def test_request_with_custom_headers(client):
|
|
"""Test making a request with custom headers."""
|
|
# Mock response
|
|
responses.add(
|
|
responses.GET,
|
|
"http://localhost:4000/models",
|
|
json={"models": []},
|
|
status=200,
|
|
)
|
|
|
|
# Make request with custom headers
|
|
custom_headers = {
|
|
"X-Custom-Header": "test-value",
|
|
"Accept": "application/json",
|
|
}
|
|
response = client.request(
|
|
"GET",
|
|
"/models",
|
|
headers=custom_headers,
|
|
)
|
|
|
|
# Check request headers
|
|
assert len(responses.calls) == 1
|
|
request_headers = responses.calls[0].request.headers
|
|
assert request_headers["X-Custom-Header"] == "test-value"
|
|
assert request_headers["Accept"] == "application/json"
|
|
assert request_headers["Authorization"] == "Bearer test-key"
|
|
|
|
|
|
@responses.activate
|
|
def test_request_http_error(client):
|
|
"""Test handling of HTTP errors."""
|
|
# Mock error response
|
|
responses.add(
|
|
responses.GET,
|
|
"http://localhost:4000/models",
|
|
json={"error": "Not authorized"},
|
|
status=401,
|
|
)
|
|
|
|
# Check that request raises exception
|
|
with pytest.raises(requests.exceptions.HTTPError) as exc_info:
|
|
client.request("GET", "/models")
|
|
|
|
assert exc_info.value.response.status_code == 401
|
|
|
|
|
|
@responses.activate
|
|
def test_request_invalid_json(client):
|
|
"""Test handling of invalid JSON responses."""
|
|
# Mock invalid JSON response
|
|
responses.add(
|
|
responses.GET,
|
|
"http://localhost:4000/models",
|
|
body="not json",
|
|
status=200,
|
|
)
|
|
|
|
# Check that request raises exception
|
|
with pytest.raises(requests.exceptions.JSONDecodeError) as exc_info:
|
|
client.request("GET", "/models")
|
|
|
|
|
|
def test_base_url_trailing_slash():
|
|
"""Test that trailing slashes in base_url are handled correctly."""
|
|
client = HTTPClient(
|
|
base_url="http://localhost:4000/",
|
|
api_key="test-key",
|
|
)
|
|
assert client._base_url == "http://localhost:4000"
|
|
|
|
|
|
def test_uri_leading_slash():
|
|
"""Test that URIs with and without leading slashes work."""
|
|
client = HTTPClient(base_url="http://localhost:4000")
|
|
|
|
with responses.RequestsMock() as rsps:
|
|
# Mock endpoint
|
|
rsps.add(
|
|
responses.GET,
|
|
"http://localhost:4000/models",
|
|
json={"models": []},
|
|
)
|
|
|
|
# Both of these should work and hit the same endpoint
|
|
client.request("GET", "/models")
|
|
client.request("GET", "models")
|
|
|
|
# Check that both requests went to the same URL
|
|
assert len(rsps.calls) == 2
|
|
assert rsps.calls[0].request.url == "http://localhost:4000/models"
|
|
assert rsps.calls[1].request.url == "http://localhost:4000/models"
|