Spaces:
Configuration error
Configuration error
File size: 4,404 Bytes
447ebeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
"""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(json.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"
|