|
27 | 27 | ) |
28 | 28 | from a2a.utils.constants import AGENT_CARD_WELL_KNOWN_PATH |
29 | 29 |
|
| 30 | + |
30 | 31 | logger = logging.getLogger(__name__) |
31 | 32 |
|
32 | 33 |
|
@@ -224,60 +225,67 @@ async def test_send_message_success_task( |
224 | 225 |
|
225 | 226 |
|
226 | 227 | class TestAgentCardHandler: |
227 | | - @pytest.fixture |
228 | | - def agent_card(self): |
229 | | - return AgentCard( |
230 | | - name='APIKeyAgent', |
231 | | - description='An agent that uses API Key auth.', |
232 | | - url='http://localhost:8000', |
233 | | - version='1.0.0', |
234 | | - capabilities=AgentCapabilities(), |
235 | | - default_input_modes=['text/plain'], |
236 | | - default_output_modes=['text/plain'], |
237 | | - skills=[], |
238 | | - ) |
239 | | - |
240 | | - @pytest.mark.anyio |
241 | | - async def test_agent_card_url_rewriting( |
242 | | - self, agent_card: AgentCard, |
243 | | - ): |
244 | | - """ |
245 | | - Tests that the REST endpoint correctly handles Agent URL rewriting. |
246 | | - """ |
247 | | - app_instance = A2ARESTFastAPIApplication(agent_card, AsyncMock()) |
248 | | - app = app_instance.build( |
249 | | - agent_card_url=AGENT_CARD_WELL_KNOWN_PATH |
250 | | - ) |
251 | | - client = AsyncClient( |
252 | | - transport=ASGITransport(app=app), |
253 | | - base_url="https://my-agents.com:5000" |
254 | | - ) |
255 | | - |
256 | | - response = await client.get(AGENT_CARD_WELL_KNOWN_PATH) |
257 | | - response.raise_for_status() |
258 | | - assert response.json()["url"] == "https://my-agents.com:5000" |
259 | | - |
260 | | - response = await client.get( |
261 | | - AGENT_CARD_WELL_KNOWN_PATH, |
262 | | - headers={ |
263 | | - "X-Forwarded-Host": "my-great-agents.com:5678", |
264 | | - "X-Forwarded-Proto": "http", |
265 | | - "X-Forwarded-Path": |
266 | | - "/agents/my-agent" + AGENT_CARD_WELL_KNOWN_PATH |
267 | | - } |
268 | | - ) |
269 | | - assert response.json()["url"] == "http://my-great-agents.com:5678/agents/my-agent" |
270 | | - |
271 | | - app = app_instance.build( |
272 | | - agent_card_url="/agents/my-agent" + AGENT_CARD_WELL_KNOWN_PATH |
273 | | - ) |
274 | | - client = AsyncClient( |
275 | | - transport=ASGITransport(app=app), |
276 | | - base_url="https://my-mighty-agents.com" |
277 | | - ) |
278 | | - |
279 | | - response = await client.get("/agents/my-agent" + AGENT_CARD_WELL_KNOWN_PATH) |
280 | | - assert response.json()["url"] == "https://my-mighty-agents.com/agents/my-agent" |
| 228 | + @pytest.fixture |
| 229 | + def agent_card(self): |
| 230 | + return AgentCard( |
| 231 | + name='APIKeyAgent', |
| 232 | + description='An agent that uses API Key auth.', |
| 233 | + url='http://localhost:8000', |
| 234 | + version='1.0.0', |
| 235 | + capabilities=AgentCapabilities(), |
| 236 | + default_input_modes=['text/plain'], |
| 237 | + default_output_modes=['text/plain'], |
| 238 | + skills=[], |
| 239 | + ) |
| 240 | + |
| 241 | + @pytest.mark.anyio |
| 242 | + async def test_agent_card_url_rewriting( |
| 243 | + self, |
| 244 | + agent_card: AgentCard, |
| 245 | + ): |
| 246 | + """ |
| 247 | + Tests that the REST endpoint correctly handles Agent URL rewriting. |
| 248 | + """ |
| 249 | + app_instance = A2ARESTFastAPIApplication(agent_card, AsyncMock()) |
| 250 | + app = app_instance.build(agent_card_url=AGENT_CARD_WELL_KNOWN_PATH) |
| 251 | + client = AsyncClient( |
| 252 | + transport=ASGITransport(app=app), |
| 253 | + base_url='https://my-agents.com:5000', |
| 254 | + ) |
| 255 | + |
| 256 | + response = await client.get(AGENT_CARD_WELL_KNOWN_PATH) |
| 257 | + response.raise_for_status() |
| 258 | + assert response.json()['url'] == 'https://my-agents.com:5000' |
| 259 | + |
| 260 | + response = await client.get( |
| 261 | + AGENT_CARD_WELL_KNOWN_PATH, |
| 262 | + headers={ |
| 263 | + 'X-Forwarded-Host': 'my-great-agents.com:5678', |
| 264 | + 'X-Forwarded-Proto': 'http', |
| 265 | + 'X-Forwarded-Path': '/agents/my-agent' |
| 266 | + + AGENT_CARD_WELL_KNOWN_PATH, |
| 267 | + }, |
| 268 | + ) |
| 269 | + assert ( |
| 270 | + response.json()['url'] |
| 271 | + == 'http://my-great-agents.com:5678/agents/my-agent' |
| 272 | + ) |
| 273 | + |
| 274 | + app = app_instance.build( |
| 275 | + agent_card_url='/agents/my-agent' + AGENT_CARD_WELL_KNOWN_PATH |
| 276 | + ) |
| 277 | + client = AsyncClient( |
| 278 | + transport=ASGITransport(app=app), |
| 279 | + base_url='https://my-mighty-agents.com', |
| 280 | + ) |
| 281 | + |
| 282 | + response = await client.get( |
| 283 | + '/agents/my-agent' + AGENT_CARD_WELL_KNOWN_PATH |
| 284 | + ) |
| 285 | + assert ( |
| 286 | + response.json()['url'] |
| 287 | + == 'https://my-mighty-agents.com/agents/my-agent' |
| 288 | + ) |
281 | 289 |
|
282 | 290 |
|
283 | 291 | if __name__ == '__main__': |
|
0 commit comments