@@ -193,13 +193,25 @@ def make_rpc_request(
193193 client : TestClient ,
194194 method : str ,
195195 params : Optional [Dict [str , Any ]] = None ,
196- req_id : Union [str , int ] = 1
196+ req_id : Union [str , int ] = 1 ,
197+ # --- ADDED: Allow sending raw content ---
198+ raw_content : Optional [Union [str , bytes ]] = None ,
199+ json_payload : Optional [Dict [str , Any ]] = None ,
200+ # --- END ADDED ---
197201) -> Any :
198202 """Helper to make JSON-RPC POST requests."""
199- payload = {"jsonrpc" : "2.0" , "method" : method , "id" : req_id }
200- if params is not None :
201- payload ["params" ] = params
202- response = client .post ("/a2a/" , json = payload )
203+ # --- MODIFIED: Handle raw_content ---
204+ if raw_content is not None :
205+ headers = {"Content-Type" : "application/json" } # Still set header
206+ response = client .post ("/a2a/" , content = raw_content , headers = headers )
207+ elif json_payload is not None :
208+ response = client .post ("/a2a/" , json = json_payload )
209+ # --- END MODIFIED ---
210+ else :
211+ payload = {"jsonrpc" : "2.0" , "method" : method , "id" : req_id }
212+ if params is not None :
213+ payload ["params" ] = params
214+ response = client .post ("/a2a/" , json = payload )
203215 return response
204216
205217# --- Test Cases ---
@@ -379,3 +391,80 @@ async def test_route_tasks_sendSubscribe_generator_error(test_app_with_agent):
379391 assert error_data .get ("error" ) == "stream_error"
380392 assert f"RuntimeError: { error_message } " in error_data .get ("message" , "" )
381393 # --- END MODIFIED ---
394+
395+ # --- ADDED: Tests for Invalid JSON-RPC Requests ---
396+
397+ def test_invalid_json_request (test_app_with_agent ):
398+ """Test sending invalid JSON."""
399+ _ , client , _ = test_app_with_agent
400+ response = make_rpc_request (client , method = "" , raw_content = b"{invalid json" )
401+ assert response .status_code == status .HTTP_200_OK # JSON-RPC error
402+ resp_data = response .json ()
403+ assert resp_data ["id" ] is None # No ID could be parsed
404+ assert resp_data ["error" ]["code" ] == JSONRPC_PARSE_ERROR
405+
406+ def test_non_dict_request (test_app_with_agent ):
407+ """Test sending a JSON array instead of an object."""
408+ _ , client , _ = test_app_with_agent
409+ response = make_rpc_request (client , method = "" , json_payload = [1 , 2 , 3 ])
410+ assert response .status_code == status .HTTP_200_OK # JSON-RPC error
411+ resp_data = response .json ()
412+ assert resp_data ["id" ] is None # No ID could be parsed
413+ assert resp_data ["error" ]["code" ] == JSONRPC_INVALID_REQUEST
414+ assert "Payload must be a JSON object" in resp_data ["error" ]["message" ]
415+
416+ def test_missing_method_request (test_app_with_agent ):
417+ """Test sending a request missing the 'method' field."""
418+ _ , client , _ = test_app_with_agent
419+ payload = {"jsonrpc" : "2.0" , "id" : "m-err" }
420+ response = make_rpc_request (client , method = "" , json_payload = payload )
421+ assert response .status_code == status .HTTP_200_OK # JSON-RPC error
422+ resp_data = response .json ()
423+ assert resp_data ["id" ] == "m-err"
424+ assert resp_data ["error" ]["code" ] == JSONRPC_INVALID_REQUEST
425+ assert "'method' is required" in resp_data ["error" ]["message" ]
426+
427+ def test_invalid_jsonrpc_version (test_app_with_agent ):
428+ """Test sending a request with the wrong 'jsonrpc' version."""
429+ _ , client , _ = test_app_with_agent
430+ payload = {"jsonrpc" : "1.0" , "method" : "test" , "id" : "v-err" }
431+ response = make_rpc_request (client , method = "" , json_payload = payload )
432+ assert response .status_code == status .HTTP_200_OK # JSON-RPC error
433+ resp_data = response .json ()
434+ assert resp_data ["id" ] == "v-err"
435+ assert resp_data ["error" ]["code" ] == JSONRPC_INVALID_REQUEST
436+ assert "'jsonrpc' must be '2.0'" in resp_data ["error" ]["message" ]
437+
438+ # --- ADDED: Tests for tasks/sendSubscribe Parameter Validation ---
439+
440+ def test_route_tasks_sendSubscribe_missing_params (test_app_with_agent ):
441+ """Test tasks/sendSubscribe with missing 'params' field."""
442+ mock_agent , client , store = test_app_with_agent
443+ response = make_rpc_request (client , "tasks/sendSubscribe" , params = None , req_id = "sub-bad1" )
444+ assert response .status_code == status .HTTP_200_OK # JSON-RPC error
445+ resp_data = response .json ()
446+ assert resp_data ["id" ] == "sub-bad1"
447+ assert resp_data ["error" ]["code" ] == JSONRPC_INVALID_PARAMS
448+ assert "Params must be a dictionary" in resp_data ["error" ]["message" ]
449+
450+ def test_route_tasks_sendSubscribe_missing_id_in_params (test_app_with_agent ):
451+ """Test tasks/sendSubscribe with 'params' missing the 'id' key."""
452+ mock_agent , client , store = test_app_with_agent
453+ response = make_rpc_request (client , "tasks/sendSubscribe" , params = {"other" : "value" }, req_id = "sub-bad2" )
454+ assert response .status_code == status .HTTP_200_OK # JSON-RPC error
455+ resp_data = response .json ()
456+ assert resp_data ["id" ] == "sub-bad2"
457+ assert resp_data ["error" ]["code" ] == JSONRPC_INVALID_PARAMS
458+ assert "'id' parameter is required" in resp_data ["error" ]["message" ]
459+
460+ def test_route_tasks_sendSubscribe_invalid_id_type (test_app_with_agent ):
461+ """Test tasks/sendSubscribe with 'id' parameter of wrong type."""
462+ mock_agent , client , store = test_app_with_agent
463+ response = make_rpc_request (client , "tasks/sendSubscribe" , params = {"id" : 12345 }, req_id = "sub-bad3" )
464+ assert response .status_code == status .HTTP_200_OK # JSON-RPC error
465+ resp_data = response .json ()
466+ assert resp_data ["id" ] == "sub-bad3"
467+ assert resp_data ["error" ]["code" ] == JSONRPC_INVALID_PARAMS
468+ assert "'id' parameter is required" in resp_data ["error" ]["message" ] # Error message might be generic
469+
470+ # --- END ADDED ---
0 commit comments