|
1 | 1 | from unittest import mock
|
2 | 2 |
|
3 | 3 | import pytest
|
| 4 | + |
| 5 | +from pydantic import ValidationError |
4 | 6 | from starlette.testclient import TestClient
|
5 | 7 |
|
6 | 8 | from a2a.server.apps import A2AFastAPIApplication, A2AStarletteApplication
|
|
9 | 11 | AgentCapabilities,
|
10 | 12 | AgentCard,
|
11 | 13 | In,
|
| 14 | + InvalidRequestError, |
| 15 | + JSONParseError, |
| 16 | + Message, |
| 17 | + Part, |
| 18 | + Role, |
12 | 19 | SecurityScheme,
|
| 20 | + TextPart, |
13 | 21 | )
|
14 |
| -from pydantic import ValidationError |
15 | 22 |
|
16 | 23 |
|
17 | 24 | @pytest.fixture
|
@@ -92,3 +99,88 @@ def test_fastapi_agent_card_with_api_key_scheme_alias(
|
92 | 99 | assert 'in' in security_scheme_json
|
93 | 100 | assert 'in_' not in security_scheme_json
|
94 | 101 | assert security_scheme_json['in'] == 'header'
|
| 102 | + |
| 103 | + |
| 104 | +def test_handle_invalid_json(agent_card_with_api_key: AgentCard): |
| 105 | + """Test handling of malformed JSON.""" |
| 106 | + handler = mock.AsyncMock() |
| 107 | + app_instance = A2AStarletteApplication(agent_card_with_api_key, handler) |
| 108 | + client = TestClient(app_instance.build()) |
| 109 | + |
| 110 | + response = client.post( |
| 111 | + '/', |
| 112 | + content='{ "jsonrpc": "2.0", "method": "test", "id": 1, "params": { "key": "value" }', |
| 113 | + ) |
| 114 | + assert response.status_code == 200 |
| 115 | + data = response.json() |
| 116 | + assert data['error']['code'] == JSONParseError().code |
| 117 | + |
| 118 | + |
| 119 | +def test_handle_oversized_payload(agent_card_with_api_key: AgentCard): |
| 120 | + """Test handling of oversized JSON payloads.""" |
| 121 | + handler = mock.AsyncMock() |
| 122 | + app_instance = A2AStarletteApplication(agent_card_with_api_key, handler) |
| 123 | + client = TestClient(app_instance.build()) |
| 124 | + |
| 125 | + large_string = 'a' * 2_000_000 # 2MB string |
| 126 | + payload = { |
| 127 | + 'jsonrpc': '2.0', |
| 128 | + 'method': 'test', |
| 129 | + 'id': 1, |
| 130 | + 'params': {'data': large_string}, |
| 131 | + } |
| 132 | + |
| 133 | + # Starlette/FastAPI's default max request size is around 1MB. |
| 134 | + # This test will likely fail with a 413 Payload Too Large if the default is not increased. |
| 135 | + # If the application is expected to handle larger payloads, the server configuration needs to be adjusted. |
| 136 | + # For this test, we expect a 413 or a graceful JSON-RPC error if the app handles it. |
| 137 | + |
| 138 | + try: |
| 139 | + response = client.post('/', json=payload) |
| 140 | + # If the app handles it gracefully and returns a JSON-RPC error |
| 141 | + if response.status_code == 200: |
| 142 | + data = response.json() |
| 143 | + assert data['error']['code'] == InvalidRequestError().code |
| 144 | + else: |
| 145 | + assert response.status_code == 413 |
| 146 | + except Exception as e: |
| 147 | + # Depending on server setup, it might just drop the connection for very large payloads |
| 148 | + assert isinstance(e, (ConnectionResetError, RuntimeError)) |
| 149 | + |
| 150 | + |
| 151 | +def test_handle_unicode_characters(agent_card_with_api_key: AgentCard): |
| 152 | + """Test handling of unicode characters in JSON payload.""" |
| 153 | + handler = mock.AsyncMock() |
| 154 | + app_instance = A2AStarletteApplication(agent_card_with_api_key, handler) |
| 155 | + client = TestClient(app_instance.build()) |
| 156 | + |
| 157 | + unicode_text = 'こんにちは世界' # "Hello world" in Japanese |
| 158 | + unicode_payload = { |
| 159 | + 'jsonrpc': '2.0', |
| 160 | + 'method': 'message/send', |
| 161 | + 'id': 'unicode_test', |
| 162 | + 'params': { |
| 163 | + 'message': { |
| 164 | + 'role': 'user', |
| 165 | + 'parts': [{'kind': 'text', 'text': unicode_text}], |
| 166 | + 'messageId': 'msg-unicode', |
| 167 | + } |
| 168 | + }, |
| 169 | + } |
| 170 | + |
| 171 | + # Mock a handler for this method |
| 172 | + handler.on_message_send.return_value = Message( |
| 173 | + role=Role.agent, |
| 174 | + parts=[Part(root=TextPart(text=f'Received: {unicode_text}'))], |
| 175 | + messageId='response-unicode', |
| 176 | + ) |
| 177 | + |
| 178 | + response = client.post('/', json=unicode_payload) |
| 179 | + |
| 180 | + # We are not testing the handler logic here, just that the server can correctly |
| 181 | + # deserialize the unicode payload without errors. A 200 response with any valid |
| 182 | + # JSON-RPC response indicates success. |
| 183 | + assert response.status_code == 200 |
| 184 | + data = response.json() |
| 185 | + assert 'error' not in data or data['error'] is None |
| 186 | + assert data['result']['parts'][0]['text'] == f'Received: {unicode_text}' |
0 commit comments