1- import json
21from unittest import mock
32
43import pytest
4+
5+ from pydantic import ValidationError
56from starlette .testclient import TestClient
67
78from a2a .server .apps import A2AFastAPIApplication , A2AStarletteApplication
1011 AgentCapabilities ,
1112 AgentCard ,
1213 In ,
14+ InvalidRequestError ,
15+ JSONParseError ,
1316 Message ,
1417 Part ,
1518 Role ,
1619 SecurityScheme ,
1720 TextPart ,
18- JSONParseError ,
19- InvalidRequestError ,
2021)
21- from pydantic import ValidationError
2222
2323
2424@pytest .fixture
@@ -107,7 +107,10 @@ def test_handle_invalid_json(agent_card_with_api_key: AgentCard):
107107 app_instance = A2AStarletteApplication (agent_card_with_api_key , handler )
108108 client = TestClient (app_instance .build ())
109109
110- response = client .post ('/' , content = '{ "jsonrpc": "2.0", "method": "test", "id": 1, "params": { "key": "value" }' )
110+ response = client .post (
111+ '/' ,
112+ content = '{ "jsonrpc": "2.0", "method": "test", "id": 1, "params": { "key": "value" }' ,
113+ )
111114 assert response .status_code == 200
112115 data = response .json ()
113116 assert data ['error' ]['code' ] == JSONParseError ().code
@@ -119,12 +122,12 @@ def test_handle_oversized_payload(agent_card_with_api_key: AgentCard):
119122 app_instance = A2AStarletteApplication (agent_card_with_api_key , handler )
120123 client = TestClient (app_instance .build ())
121124
122- large_string = "a" * 2_000_000 # 2MB string
125+ large_string = 'a' * 2_000_000 # 2MB string
123126 payload = {
124- " jsonrpc" : " 2.0" ,
125- " method" : " test" ,
126- "id" : 1 ,
127- " params" : {" data" : large_string },
127+ ' jsonrpc' : ' 2.0' ,
128+ ' method' : ' test' ,
129+ 'id' : 1 ,
130+ ' params' : {' data' : large_string },
128131 }
129132
130133 # Starlette/FastAPI's default max request size is around 1MB.
@@ -139,7 +142,7 @@ def test_handle_oversized_payload(agent_card_with_api_key: AgentCard):
139142 data = response .json ()
140143 assert data ['error' ]['code' ] == InvalidRequestError ().code
141144 else :
142- assert response .status_code == 413
145+ assert response .status_code == 413
143146 except Exception as e :
144147 # Depending on server setup, it might just drop the connection for very large payloads
145148 assert isinstance (e , (ConnectionResetError , RuntimeError ))
@@ -151,25 +154,25 @@ def test_handle_unicode_characters(agent_card_with_api_key: AgentCard):
151154 app_instance = A2AStarletteApplication (agent_card_with_api_key , handler )
152155 client = TestClient (app_instance .build ())
153156
154- unicode_text = " こんにちは世界" # "Hello world" in Japanese
157+ unicode_text = ' こんにちは世界' # "Hello world" in Japanese
155158 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"
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' ,
164167 }
165- }
168+ },
166169 }
167170
168171 # Mock a handler for this method
169172 handler .on_message_send .return_value = Message (
170173 role = Role .agent ,
171- parts = [Part (root = TextPart (text = f" Received: { unicode_text } " ))],
172- messageId = " response-unicode"
174+ parts = [Part (root = TextPart (text = f' Received: { unicode_text } ' ))],
175+ messageId = ' response-unicode' ,
173176 )
174177
175178 response = client .post ('/' , json = unicode_payload )
@@ -180,4 +183,4 @@ def test_handle_unicode_characters(agent_card_with_api_key: AgentCard):
180183 assert response .status_code == 200
181184 data = response .json ()
182185 assert 'error' not in data or data ['error' ] is None
183- assert data ['result' ]['parts' ][0 ]['text' ] == f" Received: { unicode_text } "
186+ assert data ['result' ]['parts' ][0 ]['text' ] == f' Received: { unicode_text } '
0 commit comments