|
34 | 34 | ) |
35 | 35 | from a2a.server.tasks.task_manager import TaskManager |
36 | 36 | from a2a.types import ( |
| 37 | + ContentTypeNotSupportedError, |
37 | 38 | InternalError, |
38 | 39 | InvalidAgentResponseError, |
39 | 40 | InvalidParamsError, |
@@ -987,6 +988,69 @@ async def test_delete_task_push_notification_config_info_with_config_and_no_id() |
987 | 988 | } |
988 | 989 |
|
989 | 990 |
|
| 991 | +def create_input_mode_handler(*, validate_input_modes: bool): |
| 992 | + """A handler whose card declares text/plain as its only input mode.""" |
| 993 | + return DefaultRequestHandlerV2( |
| 994 | + # HelloAgentExecutor reaches a terminal state; MockAgentExecutor |
| 995 | + # streams forever, so a blocking send against it never returns. |
| 996 | + agent_executor=HelloAgentExecutor(), |
| 997 | + task_store=InMemoryTaskStore(), |
| 998 | + agent_card=AgentCard( |
| 999 | + name='test_agent', |
| 1000 | + version='1.0', |
| 1001 | + capabilities=AgentCapabilities(streaming=True), |
| 1002 | + default_input_modes=['text/plain'], |
| 1003 | + ), |
| 1004 | + validate_input_modes=validate_input_modes, |
| 1005 | + ) |
| 1006 | + |
| 1007 | + |
| 1008 | +def create_undeclared_media_type_request(): |
| 1009 | + return SendMessageRequest( |
| 1010 | + message=Message( |
| 1011 | + role=Role.ROLE_USER, |
| 1012 | + message_id='msg_bad_media_type', |
| 1013 | + parts=[Part(text='hello', media_type='application/x-nope')], |
| 1014 | + ) |
| 1015 | + ) |
| 1016 | + |
| 1017 | + |
| 1018 | +@pytest.mark.asyncio |
| 1019 | +async def test_on_message_send_rejects_undeclared_media_type_when_enabled(): |
| 1020 | + handler = create_input_mode_handler(validate_input_modes=True) |
| 1021 | + |
| 1022 | + with pytest.raises(ContentTypeNotSupportedError): |
| 1023 | + await handler.on_message_send( |
| 1024 | + create_undeclared_media_type_request(), |
| 1025 | + create_server_call_context(), |
| 1026 | + ) |
| 1027 | + |
| 1028 | + |
| 1029 | +@pytest.mark.asyncio |
| 1030 | +async def test_on_message_send_allows_undeclared_media_type_by_default(): |
| 1031 | + """The check is opt-in, so an unflagged handler keeps accepting the part.""" |
| 1032 | + handler = create_input_mode_handler(validate_input_modes=False) |
| 1033 | + |
| 1034 | + result = await handler.on_message_send( |
| 1035 | + create_undeclared_media_type_request(), create_server_call_context() |
| 1036 | + ) |
| 1037 | + |
| 1038 | + assert result is not None |
| 1039 | + |
| 1040 | + |
| 1041 | +@pytest.mark.asyncio |
| 1042 | +async def test_on_message_send_stream_rejects_undeclared_media_type(): |
| 1043 | + """Streaming shares _setup_active_task, so it is gated by the same flag.""" |
| 1044 | + handler = create_input_mode_handler(validate_input_modes=True) |
| 1045 | + |
| 1046 | + with pytest.raises(ContentTypeNotSupportedError): |
| 1047 | + async for _ in handler.on_message_send_stream( |
| 1048 | + create_undeclared_media_type_request(), |
| 1049 | + create_server_call_context(), |
| 1050 | + ): |
| 1051 | + pass |
| 1052 | + |
| 1053 | + |
990 | 1054 | @pytest.mark.asyncio |
991 | 1055 | @pytest.mark.parametrize('terminal_state', TERMINAL_TASK_STATES) |
992 | 1056 | async def test_on_message_send_task_in_terminal_state(terminal_state): |
|
0 commit comments