The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(mcp): fold the SSE boundary regressions into two invariant tests
teknium13367adfa214a
diff --git a/tests/tools/test_mcp_http_body_cap.py b/tests/tools/test_mcp_http_body_cap.pyindex 7d1f709fa84..8846366365f 100644--- a/tests/tools/test_mcp_http_body_cap.py+++ b/tests/tools/test_mcp_http_body_cap.py@@ -146,78 +146,42 @@ def _sse_client(chunks, limit=64):return _client_for(handler, limit=limit)+_EVENT1 = b"data: " + b"a" * 40+_EVENT2 = b"data: " + b"b" * 40+# SSE allows CR, LF or CRLF line terminators (mixed too), so a blank line -- the event boundary --+# is any terminator pair. Every shape, cut at every seam between two chunks.+_SPLIT_BOUNDARY_CASES = [+ pytest.param([_EVENT1 + sep[:cut], sep[cut:] + _EVENT2 + sep], id=f"{sep!r}@{cut}")+ for sep in (b"\n\n", b"\r\r", b"\n\r", b"\r\n\r\n", b"\r\n\n", b"\r\n\r", b"\n\r\n", b"\r\r\n")+ for cut in range(1, len(sep))+] + [+ # A CRLF CRLF boundary dribbled across three chunks.+ pytest.param([_EVENT1 + b"\r", b"\n\r", b"\n" + _EVENT2 + b"\r\n\r\n"], id="three-way"),+ # Two completed events in ONE chunk are two budgets, not one lumped charge.+ pytest.param([_EVENT1 + b"\n\n" + _EVENT2 + b"\n\n"], id="two-events-one-chunk"),+]++@pytest.mark.asyncio[email protected]("sep,cut", [- (b"\n\n", 1), # ...\n | \n...- (b"\r\n\r\n", 1), # ...\r | \n\r\n...- (b"\r\n\r\n", 2), # ...\r\n | \r\n...- (b"\r\n\r\n", 3), # ...\r\n\r | \n...- # The spec also allows CR terminators and mixed styles; every pairing is a boundary.- (b"\r\r", 1), # ...\r | \r...- (b"\n\r", 1), # ...\n | \r...- (b"\n\r\n", 1), # ...\n | \r\n...- (b"\n\r\n", 2), # ...\n\r | \n...- (b"\r\n\n", 1), # ...\r\n | \n...- (b"\r\n\n", 2), # ...\r\n\n | ...- (b"\r\n\r", 1), # ...\r\n | \r...- (b"\r\n\r", 2), # ...\r\n\r | ...- (b"\r\r\n", 1), # ...\r | \r\n...- (b"\r\r\n", 2), # ...\r\r | \n...-])-async def test_sse_boundary_split_across_chunks_still_resets(sep, cut):- # A completed event boundary straddling two chunks must still reset the- # per-event counter; otherwise the finished event's bytes are charged to- # the next event and the cap trips early.- event1 = b"data: " + b"a" * 40 + sep[:cut]- event2 = sep[cut:] + b"data: " + b"b" * 40 + sep- chunks = [event1, event2][email protected]("chunks", _SPLIT_BOUNDARY_CASES)+async def test_sse_boundary_split_across_chunks_still_resets(chunks):+ # Each event is under the cap while the two together exceed it: the per-event counter must+ # reset at every completed boundary regardless of where the network chunked the bytes.+ # Otherwise the finished event's bytes are charged to the next one and the cap trips early.async with _sse_client(chunks) as client:async with client.stream("GET", "http://mcp.test/sse") as resp:async for _ in resp.aiter_bytes():pass-async def test_sse_crlf_inside_event_is_not_a_boundary():- # A lone CRLF ends a line, not the event. If \r\n could split the count, a- # multi-line event over the cap would evade it.- chunks = [b"data: " + b"a" * 40 + b"\r\ndata: " + b"b" * 40 + b"\r\n\r\n"]- async with _sse_client(chunks) as client:- with pytest.raises(httpx.ReadError, match=r"SSE event exceeds"):- async with client.stream("GET", "http://mcp.test/sse") as resp:- async for _ in resp.aiter_bytes():- pass--@pytest.mark.asyncioasync def test_sse_crlf_split_across_chunks_inside_event_is_not_a_boundary():- # Same check with the \r and \n straddling the chunk seam: the carry must not- # turn a mid-event line ending into a boundary.- chunks = [b"data: " + b"a" * 40 + b"\r", b"\ndata: " + b"b" * 40 + b"\r\n\r\n"]+ # A lone CRLF ends a line, not the event, even with the \r and \n straddling the chunk seam:+ # the carry must not turn a mid-event line ending into a boundary, or a multi-line event over+ # the cap would evade it.+ chunks = [_EVENT1 + b"\r", b"\n" + _EVENT2 + b"\r\n\r\n"]async with _sse_client(chunks) as client:with pytest.raises(httpx.ReadError, match=r"SSE event exceeds"):async with client.stream("GET", "http://mcp.test/sse") as resp:async for _ in resp.aiter_bytes():pass---async def test_sse_split_boundary_over_cap_event_still_rejected():- # The carry must not weaken the cap: an event genuinely over the limit is- # still rejected even when its preceding boundary straddled chunks.- chunks = [b"data: ok\n", b"\ndata: " + b"z" * 80 + b"\n\n"]- async with _sse_client(chunks) as client:- with pytest.raises(httpx.ReadError, match=r"SSE event exceeds"):- async with client.stream("GET", "http://mcp.test/sse") as resp:- async for _ in resp.aiter_bytes():- pass---async def test_sse_boundary_split_three_ways():- # A \r\n\r\n boundary dribbled across three chunks still resets the count.- chunks = [b"data: " + b"a" * 40 + b"\r", b"\n\r", b"\ndata: " + b"b" * 40 + b"\r\n\r\n"]- async with _sse_client(chunks) as client:- async with client.stream("GET", "http://mcp.test/sse") as resp:- async for _ in resp.aiter_bytes():- pass