|
8 | 8 |
|
9 | 9 | with try_import() as imports_successful: |
10 | 10 | from fastapi import FastAPI |
| 11 | + from fastapi.routing import APIRoute |
11 | 12 | from httpx import ASGITransport, AsyncClient |
12 | 13 |
|
13 | 14 | from pydantic_ai.fastapi.agent_router import AgentAPIRouter |
@@ -66,6 +67,38 @@ async def test_models_list_and_get( |
66 | 67 | assert detail['error']['type'] == 'not_found_error' |
67 | 68 |
|
68 | 69 |
|
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_routers_disabled( |
| 72 | + registry_with_openai_clients: AgentRegistry, |
| 73 | +) -> None: |
| 74 | + """Verify whether disabling apis actually effectively not adds APIRoutes to the app.""" |
| 75 | + registry = registry_with_openai_clients |
| 76 | + |
| 77 | + router = AgentAPIRouter(agent_registry=registry, disable_completions_api=True, disable_response_api=True) |
| 78 | + |
| 79 | + app = FastAPI() |
| 80 | + app.include_router(router) |
| 81 | + |
| 82 | + transport = ASGITransport(app=app) |
| 83 | + |
| 84 | + api_routes: list[APIRoute] = list(filter(lambda x: isinstance(x, APIRoute), app.routes)) # type: ignore |
| 85 | + assert {item.path for item in api_routes} == {'/v1/models', '/v1/models/{model_id}'} |
| 86 | + |
| 87 | + async with AsyncClient(transport=transport, base_url='http://testserver') as client: |
| 88 | + payload = { |
| 89 | + 'model': 'test-model', |
| 90 | + 'messages': [{'role': 'user', 'content': 'hello'}], |
| 91 | + } |
| 92 | + |
| 93 | + response = await client.post('/v1/chat/completions', json=payload) |
| 94 | + assert response.is_error |
| 95 | + assert response.status_code == 404 |
| 96 | + |
| 97 | + response = await client.post('/v1/responses', json=payload) |
| 98 | + assert response.is_error |
| 99 | + assert response.status_code == 404 |
| 100 | + |
| 101 | + |
69 | 102 | @pytest.mark.asyncio |
70 | 103 | async def test_route_not_implemented(registry_with_openai_clients: AgentRegistry) -> None: |
71 | 104 | """Isolated test to assert registry raises KeyError for models that only implement the other route.""" |
|
0 commit comments