-
Notifications
You must be signed in to change notification settings - Fork 421
Expand file tree
/
Copy pathtest_client_server_integration.py
More file actions
832 lines (669 loc) · 24.8 KB
/
test_client_server_integration.py
File metadata and controls
832 lines (669 loc) · 24.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
import asyncio
from collections.abc import AsyncGenerator
from typing import NamedTuple
from unittest.mock import ANY, AsyncMock, patch
import grpc
import httpx
import pytest
import pytest_asyncio
from grpc.aio import Channel
from a2a.client import ClientConfig
from a2a.client.base_client import BaseClient
from a2a.client.transports import JsonRpcTransport, RestTransport
from a2a.client.transports.base import ClientTransport
from a2a.client.transports.grpc import GrpcTransport
from a2a.grpc import a2a_pb2_grpc
from a2a.server.apps import A2AFastAPIApplication, A2ARESTFastAPIApplication
from a2a.server.request_handlers import GrpcHandler, RequestHandler
from a2a.types import (
AgentCapabilities,
AgentCard,
AgentInterface,
GetTaskPushNotificationConfigParams,
Message,
MessageSendParams,
Part,
PushNotificationConfig,
Role,
Task,
TaskIdParams,
TaskPushNotificationConfig,
TaskQueryParams,
TaskState,
TaskStatus,
TaskStatusUpdateEvent,
TextPart,
TransportProtocol,
)
# --- Test Constants ---
TASK_FROM_STREAM = Task(
id='task-123-stream',
context_id='ctx-456-stream',
status=TaskStatus(state=TaskState.completed),
kind='task',
)
TASK_FROM_BLOCKING = Task(
id='task-789-blocking',
context_id='ctx-101-blocking',
status=TaskStatus(state=TaskState.completed),
kind='task',
)
GET_TASK_RESPONSE = Task(
id='task-get-456',
context_id='ctx-get-789',
status=TaskStatus(state=TaskState.working),
kind='task',
)
CANCEL_TASK_RESPONSE = Task(
id='task-cancel-789',
context_id='ctx-cancel-101',
status=TaskStatus(state=TaskState.canceled),
kind='task',
)
CALLBACK_CONFIG = TaskPushNotificationConfig(
task_id='task-callback-123',
push_notification_config=PushNotificationConfig(
id='pnc-abc', url='http://callback.example.com', token=''
),
)
RESUBSCRIBE_EVENT = TaskStatusUpdateEvent(
task_id='task-resub-456',
context_id='ctx-resub-789',
status=TaskStatus(state=TaskState.working),
final=False,
)
# --- Test Fixtures ---
@pytest.fixture
def mock_request_handler() -> AsyncMock:
"""Provides a mock RequestHandler for the server-side handlers."""
handler = AsyncMock(spec=RequestHandler)
# Configure on_message_send for non-streaming calls
handler.on_message_send.return_value = TASK_FROM_BLOCKING
# Configure on_message_send_stream for streaming calls
async def stream_side_effect(*args, **kwargs):
yield TASK_FROM_STREAM
handler.on_message_send_stream.side_effect = stream_side_effect
# Configure other methods
handler.on_get_task.return_value = GET_TASK_RESPONSE
handler.on_cancel_task.return_value = CANCEL_TASK_RESPONSE
handler.on_set_task_push_notification_config.side_effect = (
lambda params, context: params
)
handler.on_get_task_push_notification_config.return_value = CALLBACK_CONFIG
async def resubscribe_side_effect(*args, **kwargs):
yield RESUBSCRIBE_EVENT
handler.on_resubscribe_to_task.side_effect = resubscribe_side_effect
return handler
@pytest.fixture
def agent_card() -> AgentCard:
"""Provides a sample AgentCard for tests."""
return AgentCard(
name='Test Agent',
description='An agent for integration testing.',
url='http://testserver',
version='1.0.0',
capabilities=AgentCapabilities(streaming=True, push_notifications=True),
skills=[],
default_input_modes=['text/plain'],
default_output_modes=['text/plain'],
preferred_transport=TransportProtocol.jsonrpc,
supports_authenticated_extended_card=False,
additional_interfaces=[
AgentInterface(
transport=TransportProtocol.http_json, url='http://testserver'
),
AgentInterface(
transport=TransportProtocol.grpc, url='localhost:50051'
),
],
)
class TransportSetup(NamedTuple):
"""Holds the transport and handler for a given test."""
transport: ClientTransport
handler: AsyncMock
# --- HTTP/JSON-RPC/REST Setup ---
@pytest.fixture
def http_base_setup(mock_request_handler: AsyncMock, agent_card: AgentCard):
"""A base fixture to patch the sse-starlette event loop issue."""
from sse_starlette import sse
sse.AppStatus.should_exit_event = asyncio.Event()
yield mock_request_handler, agent_card
@pytest.fixture
def jsonrpc_setup(http_base_setup) -> TransportSetup:
"""Sets up the JsonRpcTransport and in-memory server."""
mock_request_handler, agent_card = http_base_setup
app_builder = A2AFastAPIApplication(
agent_card, mock_request_handler, extended_agent_card=agent_card
)
app = app_builder.build()
httpx_client = httpx.AsyncClient(transport=httpx.ASGITransport(app=app))
transport = JsonRpcTransport(
httpx_client=httpx_client, agent_card=agent_card
)
return TransportSetup(transport=transport, handler=mock_request_handler)
@pytest.fixture
def rest_setup(http_base_setup) -> TransportSetup:
"""Sets up the RestTransport and in-memory server."""
mock_request_handler, agent_card = http_base_setup
app_builder = A2ARESTFastAPIApplication(agent_card, mock_request_handler)
app = app_builder.build()
httpx_client = httpx.AsyncClient(transport=httpx.ASGITransport(app=app))
transport = RestTransport(httpx_client=httpx_client, agent_card=agent_card)
return TransportSetup(transport=transport, handler=mock_request_handler)
# --- gRPC Setup ---
@pytest_asyncio.fixture
async def grpc_server_and_handler(
mock_request_handler: AsyncMock, agent_card: AgentCard
) -> AsyncGenerator[tuple[str, AsyncMock], None]:
"""Creates and manages an in-process gRPC test server."""
server = grpc.aio.server()
port = server.add_insecure_port('[::]:0')
server_address = f'localhost:{port}'
servicer = GrpcHandler(agent_card, mock_request_handler)
a2a_pb2_grpc.add_A2AServiceServicer_to_server(servicer, server)
await server.start()
yield server_address, mock_request_handler
await server.stop(0)
# --- The Integration Tests ---
@pytest.mark.asyncio
@pytest.mark.parametrize(
'transport_setup_fixture',
[
pytest.param('jsonrpc_setup', id='JSON-RPC'),
pytest.param('rest_setup', id='REST'),
],
)
async def test_http_transport_sends_message_streaming(
transport_setup_fixture: str, request
) -> None:
"""
Integration test for HTTP-based transports (JSON-RPC, REST) streaming.
"""
transport_setup: TransportSetup = request.getfixturevalue(
transport_setup_fixture
)
transport = transport_setup.transport
handler = transport_setup.handler
message_to_send = Message(
role=Role.user,
message_id='msg-integration-test',
parts=[Part(root=TextPart(text='Hello, integration test!'))],
)
params = MessageSendParams(message=message_to_send)
stream = transport.send_message_streaming(request=params)
first_event = await anext(stream)
assert first_event.id == TASK_FROM_STREAM.id
assert first_event.context_id == TASK_FROM_STREAM.context_id
handler.on_message_send_stream.assert_called_once()
call_args, _ = handler.on_message_send_stream.call_args
received_params: MessageSendParams = call_args[0]
assert received_params.message.message_id == message_to_send.message_id
assert (
received_params.message.parts[0].root.text
== message_to_send.parts[0].root.text
)
if hasattr(transport, 'close'):
await transport.close()
@pytest.mark.asyncio
async def test_grpc_transport_sends_message_streaming(
grpc_server_and_handler: tuple[str, AsyncMock],
agent_card: AgentCard,
) -> None:
"""
Integration test specifically for the gRPC transport streaming.
"""
server_address, handler = grpc_server_and_handler
agent_card.url = server_address
def channel_factory(address: str) -> Channel:
return grpc.aio.insecure_channel(address)
channel = channel_factory(server_address)
transport = GrpcTransport(channel=channel, agent_card=agent_card)
message_to_send = Message(
role=Role.user,
message_id='msg-grpc-integration-test',
parts=[Part(root=TextPart(text='Hello, gRPC integration test!'))],
)
params = MessageSendParams(message=message_to_send)
stream = transport.send_message_streaming(request=params)
first_event = await anext(stream)
assert first_event.id == TASK_FROM_STREAM.id
assert first_event.context_id == TASK_FROM_STREAM.context_id
handler.on_message_send_stream.assert_called_once()
call_args, _ = handler.on_message_send_stream.call_args
received_params: MessageSendParams = call_args[0]
assert received_params.message.message_id == message_to_send.message_id
assert (
received_params.message.parts[0].root.text
== message_to_send.parts[0].root.text
)
await transport.close()
@pytest.mark.asyncio
@pytest.mark.parametrize(
'transport_setup_fixture',
[
pytest.param('jsonrpc_setup', id='JSON-RPC'),
pytest.param('rest_setup', id='REST'),
],
)
async def test_http_transport_sends_message_blocking(
transport_setup_fixture: str, request
) -> None:
"""
Integration test for HTTP-based transports (JSON-RPC, REST) blocking.
"""
transport_setup: TransportSetup = request.getfixturevalue(
transport_setup_fixture
)
transport = transport_setup.transport
handler = transport_setup.handler
message_to_send = Message(
role=Role.user,
message_id='msg-integration-test-blocking',
parts=[Part(root=TextPart(text='Hello, blocking test!'))],
)
params = MessageSendParams(message=message_to_send)
result = await transport.send_message(request=params)
assert result.id == TASK_FROM_BLOCKING.id
assert result.context_id == TASK_FROM_BLOCKING.context_id
handler.on_message_send.assert_awaited_once()
call_args, _ = handler.on_message_send.call_args
received_params: MessageSendParams = call_args[0]
assert received_params.message.message_id == message_to_send.message_id
assert (
received_params.message.parts[0].root.text
== message_to_send.parts[0].root.text
)
if hasattr(transport, 'close'):
await transport.close()
@pytest.mark.asyncio
async def test_grpc_transport_sends_message_blocking(
grpc_server_and_handler: tuple[str, AsyncMock],
agent_card: AgentCard,
) -> None:
"""
Integration test specifically for the gRPC transport blocking.
"""
server_address, handler = grpc_server_and_handler
agent_card.url = server_address
def channel_factory(address: str) -> Channel:
return grpc.aio.insecure_channel(address)
channel = channel_factory(server_address)
transport = GrpcTransport(channel=channel, agent_card=agent_card)
message_to_send = Message(
role=Role.user,
message_id='msg-grpc-integration-test-blocking',
parts=[Part(root=TextPart(text='Hello, gRPC blocking test!'))],
)
params = MessageSendParams(message=message_to_send)
result = await transport.send_message(request=params)
assert result.id == TASK_FROM_BLOCKING.id
assert result.context_id == TASK_FROM_BLOCKING.context_id
handler.on_message_send.assert_awaited_once()
call_args, _ = handler.on_message_send.call_args
received_params: MessageSendParams = call_args[0]
assert received_params.message.message_id == message_to_send.message_id
assert (
received_params.message.parts[0].root.text
== message_to_send.parts[0].root.text
)
await transport.close()
@pytest.mark.asyncio
@pytest.mark.parametrize(
'transport_setup_fixture',
[
pytest.param('jsonrpc_setup', id='JSON-RPC'),
pytest.param('rest_setup', id='REST'),
],
)
async def test_http_transport_get_task(
transport_setup_fixture: str, request
) -> None:
transport_setup: TransportSetup = request.getfixturevalue(
transport_setup_fixture
)
transport = transport_setup.transport
handler = transport_setup.handler
params = TaskQueryParams(id=GET_TASK_RESPONSE.id)
result = await transport.get_task(request=params)
assert result.id == GET_TASK_RESPONSE.id
handler.on_get_task.assert_awaited_once_with(params, ANY)
if hasattr(transport, 'close'):
await transport.close()
@pytest.mark.asyncio
async def test_grpc_transport_get_task(
grpc_server_and_handler: tuple[str, AsyncMock],
agent_card: AgentCard,
) -> None:
server_address, handler = grpc_server_and_handler
agent_card.url = server_address
def channel_factory(address: str) -> Channel:
return grpc.aio.insecure_channel(address)
channel = channel_factory(server_address)
transport = GrpcTransport(channel=channel, agent_card=agent_card)
params = TaskQueryParams(id=GET_TASK_RESPONSE.id)
result = await transport.get_task(request=params)
assert result.id == GET_TASK_RESPONSE.id
handler.on_get_task.assert_awaited_once()
assert handler.on_get_task.call_args[0][0].id == GET_TASK_RESPONSE.id
await transport.close()
@pytest.mark.asyncio
@pytest.mark.parametrize(
'transport_setup_fixture',
[
pytest.param('jsonrpc_setup', id='JSON-RPC'),
pytest.param('rest_setup', id='REST'),
],
)
async def test_http_transport_cancel_task(
transport_setup_fixture: str, request
) -> None:
transport_setup: TransportSetup = request.getfixturevalue(
transport_setup_fixture
)
transport = transport_setup.transport
handler = transport_setup.handler
params = TaskIdParams(id=CANCEL_TASK_RESPONSE.id)
result = await transport.cancel_task(request=params)
assert result.id == CANCEL_TASK_RESPONSE.id
handler.on_cancel_task.assert_awaited_once_with(params, ANY)
if hasattr(transport, 'close'):
await transport.close()
@pytest.mark.asyncio
async def test_grpc_transport_cancel_task(
grpc_server_and_handler: tuple[str, AsyncMock],
agent_card: AgentCard,
) -> None:
server_address, handler = grpc_server_and_handler
agent_card.url = server_address
def channel_factory(address: str) -> Channel:
return grpc.aio.insecure_channel(address)
channel = channel_factory(server_address)
transport = GrpcTransport(channel=channel, agent_card=agent_card)
params = TaskIdParams(id=CANCEL_TASK_RESPONSE.id)
result = await transport.cancel_task(request=params)
assert result.id == CANCEL_TASK_RESPONSE.id
handler.on_cancel_task.assert_awaited_once()
assert handler.on_cancel_task.call_args[0][0].id == CANCEL_TASK_RESPONSE.id
await transport.close()
@pytest.mark.asyncio
@pytest.mark.parametrize(
'transport_setup_fixture',
[
pytest.param('jsonrpc_setup', id='JSON-RPC'),
pytest.param('rest_setup', id='REST'),
],
)
async def test_http_transport_set_task_callback(
transport_setup_fixture: str, request
) -> None:
transport_setup: TransportSetup = request.getfixturevalue(
transport_setup_fixture
)
transport = transport_setup.transport
handler = transport_setup.handler
params = CALLBACK_CONFIG
result = await transport.set_task_callback(request=params)
assert result.task_id == CALLBACK_CONFIG.task_id
assert (
result.push_notification_config.id
== CALLBACK_CONFIG.push_notification_config.id
)
assert (
result.push_notification_config.url
== CALLBACK_CONFIG.push_notification_config.url
)
handler.on_set_task_push_notification_config.assert_awaited_once_with(
params, ANY
)
if hasattr(transport, 'close'):
await transport.close()
@pytest.mark.asyncio
async def test_grpc_transport_set_task_callback(
grpc_server_and_handler: tuple[str, AsyncMock],
agent_card: AgentCard,
) -> None:
server_address, handler = grpc_server_and_handler
agent_card.url = server_address
def channel_factory(address: str) -> Channel:
return grpc.aio.insecure_channel(address)
channel = channel_factory(server_address)
transport = GrpcTransport(channel=channel, agent_card=agent_card)
params = CALLBACK_CONFIG
result = await transport.set_task_callback(request=params)
assert result.task_id == CALLBACK_CONFIG.task_id
assert (
result.push_notification_config.id
== CALLBACK_CONFIG.push_notification_config.id
)
assert (
result.push_notification_config.url
== CALLBACK_CONFIG.push_notification_config.url
)
handler.on_set_task_push_notification_config.assert_awaited_once()
assert (
handler.on_set_task_push_notification_config.call_args[0][0].task_id
== CALLBACK_CONFIG.task_id
)
await transport.close()
@pytest.mark.asyncio
@pytest.mark.parametrize(
'transport_setup_fixture',
[
pytest.param('jsonrpc_setup', id='JSON-RPC'),
pytest.param('rest_setup', id='REST'),
],
)
async def test_http_transport_get_task_callback(
transport_setup_fixture: str, request
) -> None:
transport_setup: TransportSetup = request.getfixturevalue(
transport_setup_fixture
)
transport = transport_setup.transport
handler = transport_setup.handler
params = GetTaskPushNotificationConfigParams(
id=CALLBACK_CONFIG.task_id,
push_notification_config_id=CALLBACK_CONFIG.push_notification_config.id,
)
result = await transport.get_task_callback(request=params)
assert result.task_id == CALLBACK_CONFIG.task_id
assert (
result.push_notification_config.id
== CALLBACK_CONFIG.push_notification_config.id
)
assert (
result.push_notification_config.url
== CALLBACK_CONFIG.push_notification_config.url
)
handler.on_get_task_push_notification_config.assert_awaited_once_with(
params, ANY
)
if hasattr(transport, 'close'):
await transport.close()
@pytest.mark.asyncio
async def test_grpc_transport_get_task_callback(
grpc_server_and_handler: tuple[str, AsyncMock],
agent_card: AgentCard,
) -> None:
server_address, handler = grpc_server_and_handler
agent_card.url = server_address
def channel_factory(address: str) -> Channel:
return grpc.aio.insecure_channel(address)
channel = channel_factory(server_address)
transport = GrpcTransport(channel=channel, agent_card=agent_card)
params = GetTaskPushNotificationConfigParams(
id=CALLBACK_CONFIG.task_id,
push_notification_config_id=CALLBACK_CONFIG.push_notification_config.id,
)
result = await transport.get_task_callback(request=params)
assert result.task_id == CALLBACK_CONFIG.task_id
assert (
result.push_notification_config.id
== CALLBACK_CONFIG.push_notification_config.id
)
assert (
result.push_notification_config.url
== CALLBACK_CONFIG.push_notification_config.url
)
handler.on_get_task_push_notification_config.assert_awaited_once()
assert (
handler.on_get_task_push_notification_config.call_args[0][0].id
== CALLBACK_CONFIG.task_id
)
await transport.close()
@pytest.mark.asyncio
@pytest.mark.parametrize(
'transport_setup_fixture',
[
pytest.param('jsonrpc_setup', id='JSON-RPC'),
pytest.param('rest_setup', id='REST'),
],
)
async def test_http_transport_resubscribe(
transport_setup_fixture: str, request
) -> None:
transport_setup: TransportSetup = request.getfixturevalue(
transport_setup_fixture
)
transport = transport_setup.transport
handler = transport_setup.handler
params = TaskIdParams(id=RESUBSCRIBE_EVENT.task_id)
stream = transport.resubscribe(request=params)
first_event = await anext(stream)
assert first_event.task_id == RESUBSCRIBE_EVENT.task_id
handler.on_resubscribe_to_task.assert_called_once_with(params, ANY)
if hasattr(transport, 'close'):
await transport.close()
@pytest.mark.asyncio
async def test_grpc_transport_resubscribe(
grpc_server_and_handler: tuple[str, AsyncMock],
agent_card: AgentCard,
) -> None:
server_address, handler = grpc_server_and_handler
agent_card.url = server_address
def channel_factory(address: str) -> Channel:
return grpc.aio.insecure_channel(address)
channel = channel_factory(server_address)
transport = GrpcTransport(channel=channel, agent_card=agent_card)
params = TaskIdParams(id=RESUBSCRIBE_EVENT.task_id)
stream = transport.resubscribe(request=params)
first_event = await anext(stream)
assert first_event.task_id == RESUBSCRIBE_EVENT.task_id
handler.on_resubscribe_to_task.assert_called_once()
assert (
handler.on_resubscribe_to_task.call_args[0][0].id
== RESUBSCRIBE_EVENT.task_id
)
await transport.close()
@pytest.mark.asyncio
@pytest.mark.parametrize(
'transport_setup_fixture',
[
pytest.param('jsonrpc_setup', id='JSON-RPC'),
pytest.param('rest_setup', id='REST'),
],
)
async def test_http_transport_get_card(
transport_setup_fixture: str, request, agent_card: AgentCard
) -> None:
transport_setup: TransportSetup = request.getfixturevalue(
transport_setup_fixture
)
transport = transport_setup.transport
# Get the base card.
result = await transport.get_card()
assert result.name == agent_card.name
assert transport.agent_card.name == agent_card.name
assert transport._needs_extended_card is False
if hasattr(transport, 'close'):
await transport.close()
@pytest.mark.asyncio
async def test_http_transport_get_authenticated_card(
agent_card: AgentCard,
mock_request_handler: AsyncMock,
) -> None:
agent_card.supports_authenticated_extended_card = True
extended_agent_card = agent_card.model_copy(deep=True)
extended_agent_card.name = 'Extended Agent Card'
app_builder = A2ARESTFastAPIApplication(
agent_card,
mock_request_handler,
extended_agent_card=extended_agent_card,
)
app = app_builder.build()
httpx_client = httpx.AsyncClient(transport=httpx.ASGITransport(app=app))
transport = RestTransport(httpx_client=httpx_client, agent_card=agent_card)
result = await transport.get_card()
assert result.name == extended_agent_card.name
assert transport.agent_card.name == extended_agent_card.name
assert transport._needs_extended_card is False
if hasattr(transport, 'close'):
await transport.close()
@pytest.mark.asyncio
async def test_grpc_transport_get_card(
grpc_server_and_handler: tuple[str, AsyncMock],
agent_card: AgentCard,
) -> None:
server_address, _ = grpc_server_and_handler
agent_card.url = server_address
def channel_factory(address: str) -> Channel:
return grpc.aio.insecure_channel(address)
channel = channel_factory(server_address)
transport = GrpcTransport(channel=channel, agent_card=agent_card)
# The transport starts with a minimal card, get_card() fetches the full one
transport.agent_card.supports_authenticated_extended_card = True
result = await transport.get_card()
assert result.name == agent_card.name
assert transport.agent_card.name == agent_card.name
assert transport._needs_extended_card is False
await transport.close()
@pytest.mark.asyncio
async def test_base_client_sends_message_with_extensions(
jsonrpc_setup: TransportSetup, agent_card: AgentCard
) -> None:
"""
Integration test for BaseClient with JSON-RPC transport to ensure extensions are included in headers.
"""
transport = jsonrpc_setup.transport
agent_card.capabilities.streaming = False
# Create a BaseClient instance
client = BaseClient(
card=agent_card,
config=ClientConfig(streaming=False),
transport=transport,
consumers=[],
middleware=[],
)
message_to_send = Message(
role=Role.user,
message_id='msg-integration-test-extensions',
parts=[Part(root=TextPart(text='Hello, extensions test!'))],
)
extensions = [
'https://example.com/test-ext/v1',
'https://example.com/test-ext/v2',
]
with patch.object(
transport, '_send_request', new_callable=AsyncMock
) as mock_send_request:
mock_send_request.return_value = {
'id': '123',
'jsonrpc': '2.0',
'result': TASK_FROM_BLOCKING.model_dump(mode='json'),
}
# Call send_message on the BaseClient
async for _ in client.send_message(
request=message_to_send, extensions=extensions
):
pass
mock_send_request.assert_called_once()
call_args, _ = mock_send_request.call_args
kwargs = call_args[1]
headers = kwargs.get('headers', {})
assert 'A2A-Extensions' in headers
assert (
headers['A2A-Extensions']
== 'https://example.com/test-ext/v1,https://example.com/test-ext/v2'
)
if hasattr(transport, 'close'):
await transport.close()