test(proxy): forward each method through the typesafe route to a mocked upstream

The route test only resolved route names. It now sends every method through
the proxy with a virtual key and asserts the upstream receives that method,
the proxy's TypeSafe key and the caller's body
This commit is contained in:
Yuneng Jiang 2026-09-17 18:19:42 -07:00
parent b9e468100b
commit 349e8b9358
No known key found for this signature in database

View file

@ -6149,9 +6149,41 @@ class TestTypeSafePassthroughRoute:
request.json = AsyncMock(return_value=body)
return request
@pytest.mark.parametrize("method", ["GET", "POST", "PUT", "DELETE", "PATCH"])
def test_route_serves_every_method(self, method: str):
assert _resolve_route_name(method, "/typesafe/v1/systemone") == "typesafe_proxy_route"
@pytest.fixture
def client(self, monkeypatch: pytest.MonkeyPatch) -> Iterator[TestClient]:
from litellm.proxy.proxy_server import app
monkeypatch.setenv("TYPESAFE_API_KEY", "typesafe-test-key")
monkeypatch.setenv("TYPESAFE_API_BASE", "https://typesafe.example/base")
monkeypatch.delenv("SERVER_ROOT_PATH", raising=False)
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
litellm.in_memory_llm_clients_cache.flush_cache()
monkeypatch.setitem(app.dependency_overrides, user_api_key_auth, lambda: UserAPIKeyAuth(api_key="sk-virtual"))
yield TestClient(app)
@pytest.mark.parametrize(
"method, body",
[
("GET", None),
("POST", {"state": "x"}),
("PUT", {"state": "x"}),
("DELETE", None),
("PATCH", {"state": "x"}),
],
)
def test_forwards_every_method_and_body_upstream(
self, client: TestClient, method: str, body: dict[str, str] | None
) -> None:
with respx.mock(assert_all_called=True) as upstream:
route = upstream.request(method, "https://typesafe.example/base/v1/systemone").mock(
return_value=httpx.Response(200, json={"id": "upstream_123"})
)
response = client.request(method, "/typesafe/v1/systemone", json=body)
assert (response.status_code, response.json()) == (200, {"id": "upstream_123"})
sent: Final = route.calls.last.request
assert sent.headers["authorization"] == "Bearer typesafe-test-key"
assert json.loads(sent.content or b"{}") == (body or {})
@pytest.mark.asyncio
async def test_forwards_target_auth_headers_provider_and_query(self, monkeypatch):