|
1 | 1 | import asyncio |
2 | 2 | from collections.abc import AsyncGenerator |
3 | 3 | from typing import NamedTuple |
4 | | -from unittest.mock import ANY, AsyncMock |
| 4 | +from unittest.mock import ANY, AsyncMock, patch |
5 | 5 |
|
6 | 6 | import grpc |
7 | 7 | import httpx |
8 | 8 | import pytest |
9 | 9 | import pytest_asyncio |
10 | 10 | from grpc.aio import Channel |
11 | 11 |
|
| 12 | +from a2a.client import ClientConfig |
| 13 | +from a2a.client.base_client import BaseClient |
12 | 14 | from a2a.client.transports import JsonRpcTransport, RestTransport |
13 | 15 | from a2a.client.transports.base import ClientTransport |
14 | 16 | from a2a.client.transports.grpc import GrpcTransport |
@@ -767,3 +769,58 @@ def channel_factory(address: str) -> Channel: |
767 | 769 | assert transport._needs_extended_card is False |
768 | 770 |
|
769 | 771 | await transport.close() |
| 772 | + |
| 773 | + |
| 774 | +@pytest.mark.asyncio |
| 775 | +async def test_base_client_sends_message_with_extensions( |
| 776 | + jsonrpc_setup: TransportSetup, agent_card: AgentCard |
| 777 | +) -> None: |
| 778 | + """ |
| 779 | + Integration test for BaseClient with JSON-RPC transport to ensure extensions are included in headers. |
| 780 | + """ |
| 781 | + transport = jsonrpc_setup.transport |
| 782 | + agent_card.capabilities.streaming = False |
| 783 | + |
| 784 | + # Create a BaseClient instance |
| 785 | + client = BaseClient( |
| 786 | + card=agent_card, |
| 787 | + config=ClientConfig(streaming=False), |
| 788 | + transport=transport, |
| 789 | + consumers=[], |
| 790 | + middleware=[], |
| 791 | + ) |
| 792 | + |
| 793 | + message_to_send = Message( |
| 794 | + role=Role.user, |
| 795 | + message_id='msg-integration-test-extensions', |
| 796 | + parts=[Part(root=TextPart(text='Hello, extensions test!'))], |
| 797 | + ) |
| 798 | + extensions = [ |
| 799 | + 'https://example.com/test-ext/v1', |
| 800 | + 'https://example.com/test-ext/v2', |
| 801 | + ] |
| 802 | + |
| 803 | + with patch.object( |
| 804 | + transport, '_send_request', new_callable=AsyncMock |
| 805 | + ) as mock_send_request: |
| 806 | + mock_send_request.return_value = { |
| 807 | + 'id': '123', |
| 808 | + 'jsonrpc': '2.0', |
| 809 | + 'result': TASK_FROM_BLOCKING.model_dump(mode='json'), |
| 810 | + } |
| 811 | + |
| 812 | + # Call send_message on the BaseClient |
| 813 | + async for _ in client.send_message( |
| 814 | + request=message_to_send, extensions=extensions |
| 815 | + ): |
| 816 | + pass |
| 817 | + |
| 818 | + mock_send_request.assert_called_once() |
| 819 | + call_args, _ = mock_send_request.call_args |
| 820 | + kwargs = call_args[1] |
| 821 | + headers = kwargs.get('headers', {}) |
| 822 | + assert 'X-A2A-Extensions' in headers |
| 823 | + assert headers['X-A2A-Extensions'] == ','.join(extensions) |
| 824 | + |
| 825 | + if hasattr(transport, 'close'): |
| 826 | + await transport.close() |
0 commit comments