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