Skip to content

The agent that grows with you

test(api-server): trim SSE CORS tests to one positive invariant per stream

kshitijk4poorbf88a5955570
diff --git a/tests/gateway/test_api_server.py b/tests/gateway/test_api_server.py
index ec007ce6967..9031c4c215b 100644
--- a/tests/gateway/test_api_server.py
+++ b/tests/gateway/test_api_server.py
@@ -17,6 +17,7 @@ import json
import time
import types
import uuid
+from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -2264,20 +2265,22 @@ class TestCORS:
@pytest.mark.asyncio
- async def test_cors_headers_present_on_session_chat_stream(self):
- """SSE streaming endpoint must include CORS headers on the response.
-
- The CORS middleware cannot inject headers into a ``StreamResponse``
- after ``prepare()`` flushes them, so the handler must resolve CORS
- headers up front — same pattern as ``/v1/chat/completions`` and
- ``/v1/responses`` streaming paths (#72892).
+ @pytest.mark.parametrize("live_bot_chat", [False, True], ids=["agent_turn", "live_bot_chat"])
+ async def test_cors_headers_present_on_session_chat_stream(self, live_bot_chat):
+ """Both session SSE writers (agent turn and live Bot Chat hand-off) must
+ resolve CORS up front: the middleware can't touch headers after
+ ``prepare()`` flushes them (#72892).
"""
adapter = _make_adapter(cors_origins=["http://localhost:3000"])
app = _create_app(adapter)
+ admitted = (Path("unused"), {"delivery_id": "d1", "status": "queued"}) if live_bot_chat else None
+ settled = {"delivery_id": "d1", "status": "settled", "reply": "ok"}
async with TestClient(TestServer(app)) as cli:
with (
patch.object(adapter, "_get_existing_session_or_404", return_value=({"id": "s1"}, None)),
patch.object(adapter, "_conversation_history_for_session", return_value=[]),
+ patch.object(adapter, "_admit_to_live_bot_chat", new_callable=AsyncMock, return_value=admitted),
+ patch.object(adapter, "_await_live_bot_chat_receipt", new_callable=AsyncMock, return_value=settled),
patch.object(adapter, "_run_agent", new_callable=AsyncMock) as mock_run,
):
mock_run.return_value = (
@@ -2291,32 +2294,10 @@ class TestCORS:
)
assert resp.status == 200
await resp.text() # consume SSE stream fully
+ assert mock_run.called is not live_bot_chat
assert resp.headers.get("Access-Control-Allow-Origin") == "http://localhost:3000"
assert "POST" in resp.headers.get("Access-Control-Allow-Methods", "")
- @pytest.mark.asyncio
- async def test_cors_headers_absent_on_session_chat_stream_no_origin(self):
- """No CORS headers on the streaming endpoint when no Origin is sent."""
- adapter = _make_adapter()
- app = _create_app(adapter)
- async with TestClient(TestServer(app)) as cli:
- with (
- patch.object(adapter, "_get_existing_session_or_404", return_value=({"id": "s1"}, None)),
- patch.object(adapter, "_conversation_history_for_session", return_value=[]),
- patch.object(adapter, "_run_agent", new_callable=AsyncMock) as mock_run,
- ):
- mock_run.return_value = (
- {"final_response": "ok", "messages": [], "api_calls": 1},
- {"input_tokens": 1, "output_tokens": 1, "total_tokens": 2},
- )
- resp = await cli.post(
- "/api/sessions/s1/chat/stream",
- json={"message": "hi"},
- )
- assert resp.status == 200
- await resp.text()
- assert resp.headers.get("Access-Control-Allow-Origin") is None
-
# ---------------------------------------------------------------------------
# Conversation parameter
diff --git a/tests/gateway/test_api_server_runs.py b/tests/gateway/test_api_server_runs.py
index f81bd265b96..e8c670048e6 100644
--- a/tests/gateway/test_api_server_runs.py
+++ b/tests/gateway/test_api_server_runs.py
@@ -896,37 +896,6 @@ class TestRunEventsCORS:
assert "Authorization" in resp.headers.get("Access-Control-Allow-Headers", "")
await resp.text()
- @pytest.mark.asyncio
- async def test_events_cors_headers_absent_without_origin(self):
- adapter = self._primed_adapter()
- self._prime_closed_stream(adapter, "cors_run_2")
- app = _create_runs_app(adapter)
- async with TestClient(TestServer(app)) as cli:
- resp = await cli.get(
- "/v1/runs/cors_run_2/events",
- headers={"Authorization": "Bearer sk-secret"},
- )
- assert resp.status == 200
- assert resp.headers.get("Access-Control-Allow-Origin") is None
- await resp.text()
-
- @pytest.mark.asyncio
- async def test_events_rejects_disallowed_origin(self):
- adapter = self._primed_adapter()
- self._prime_closed_stream(adapter, "cors_run_3")
- app = _create_runs_app(adapter)
- async with TestClient(TestServer(app)) as cli:
- resp = await cli.get(
- "/v1/runs/cors_run_3/events",
- headers={
- "Authorization": "Bearer sk-secret",
- "Origin": "http://evil.example",
- },
- )
- assert resp.status == 403
- assert resp.headers.get("Access-Control-Allow-Origin") is None
- await resp.text()
-
# ---------------------------------------------------------------------------
# POST /v1/runs/{run_id}/steer — steer a running agent