|
1 | 1 | import logging |
2 | 2 |
|
| 3 | +from typing import Any |
| 4 | + |
3 | 5 | from unittest.mock import MagicMock |
4 | 6 |
|
5 | 7 | import pytest |
|
9 | 11 | from httpx import ASGITransport, AsyncClient |
10 | 12 |
|
11 | 13 | from a2a.grpc import a2a_pb2 |
| 14 | +from a2a.server.apps.rest import fastapi_app |
| 15 | +from a2a.server.apps.rest import rest_adapter |
| 16 | +from a2a.server.apps.rest.rest_adapter import RESTAdapter |
12 | 17 | from a2a.server.apps.rest.fastapi_app import A2ARESTFastAPIApplication |
13 | 18 | from a2a.server.request_handlers.request_handler import RequestHandler |
14 | 19 | from a2a.types import ( |
@@ -57,6 +62,84 @@ async def client(app: FastAPI) -> AsyncClient: |
57 | 62 | ) |
58 | 63 |
|
59 | 64 |
|
| 65 | +@pytest.fixture |
| 66 | +def mark_pkg_starlette_not_installed(): |
| 67 | + pkg_starlette_installed_flag = rest_adapter._package_starlette_installed |
| 68 | + rest_adapter._package_starlette_installed = False |
| 69 | + yield |
| 70 | + rest_adapter._package_starlette_installed = pkg_starlette_installed_flag |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture |
| 74 | +def mark_pkg_fastapi_not_installed(): |
| 75 | + pkg_fastapi_installed_flag = fastapi_app._package_fastapi_installed |
| 76 | + fastapi_app._package_fastapi_installed = False |
| 77 | + yield |
| 78 | + fastapi_app._package_fastapi_installed = pkg_fastapi_installed_flag |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.anyio |
| 82 | +async def test_create_rest_adapter_with_present_deps_succeeds( |
| 83 | + agent_card: AgentCard, request_handler: RequestHandler |
| 84 | +): |
| 85 | + try: |
| 86 | + _app = RESTAdapter(agent_card, request_handler) |
| 87 | + except ImportError: |
| 88 | + pytest.fail( |
| 89 | + 'With packages starlette and see-starlette present, creating an' |
| 90 | + ' RESTAdapter instance should not raise ImportError' |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.anyio |
| 95 | +async def test_create_rest_adapter_with_missing_deps_raises_importerror( |
| 96 | + agent_card: AgentCard, |
| 97 | + request_handler: RequestHandler, |
| 98 | + mark_pkg_starlette_not_installed: Any, |
| 99 | +): |
| 100 | + with pytest.raises( |
| 101 | + ImportError, |
| 102 | + match=( |
| 103 | + 'Packages `starlette` and `sse-starlette` are required to use' |
| 104 | + ' the `RESTAdapter`.' |
| 105 | + ), |
| 106 | + ): |
| 107 | + _app = RESTAdapter(agent_card, request_handler) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.anyio |
| 111 | +async def test_create_a2a_rest_fastapi_app_with_present_deps_succeeds( |
| 112 | + agent_card: AgentCard, request_handler: RequestHandler |
| 113 | +): |
| 114 | + try: |
| 115 | + _app = A2ARESTFastAPIApplication(agent_card, request_handler).build( |
| 116 | + agent_card_url='/well-known/agent.json', rpc_url='' |
| 117 | + ) |
| 118 | + except ImportError: |
| 119 | + pytest.fail( |
| 120 | + 'With the fastapi package present, creating a' |
| 121 | + ' A2ARESTFastAPIApplication instance should not raise ImportError' |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.anyio |
| 126 | +async def test_create_a2a_rest_fastapi_app_with_missing_deps_raises_importerror( |
| 127 | + agent_card: AgentCard, |
| 128 | + request_handler: RequestHandler, |
| 129 | + mark_pkg_fastapi_not_installed: Any, |
| 130 | +): |
| 131 | + with pytest.raises( |
| 132 | + ImportError, |
| 133 | + match=( |
| 134 | + 'The `fastapi` package is required to use the' |
| 135 | + ' `A2ARESTFastAPIApplication`' |
| 136 | + ), |
| 137 | + ): |
| 138 | + _app = A2ARESTFastAPIApplication(agent_card, request_handler).build( |
| 139 | + agent_card_url='/well-known/agent.json', rpc_url='' |
| 140 | + ) |
| 141 | + |
| 142 | + |
60 | 143 | @pytest.mark.anyio |
61 | 144 | async def test_send_message_success_message( |
62 | 145 | client: AsyncClient, request_handler: MagicMock |
|
0 commit comments