Skip to content

Commit 7f25bd4

Browse files
author
Jianke LIN
committed
test: adapt SSE cleanup tests to httpx2 client
1 parent 7ecc23d commit 7f25bd4

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

tests/client/test_streamable_http_response_cleanup.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import contextlib
22

3-
import httpx
3+
import httpx2
44
import pytest
5-
from httpx_sse import ServerSentEvent
5+
from httpx2 import ServerSentEvent
66
from mcp_types import JSONRPCRequest
77

88
from mcp.client.streamable_http import RequestContext, StreamableHTTPTransport
@@ -11,10 +11,10 @@
1111

1212

1313
class _RaiseEventSource:
14-
def __init__(self, response: httpx.Response) -> None:
14+
def __init__(self, response: httpx2.Response) -> None:
1515
self.response = response
1616

17-
async def aiter_sse(self):
17+
async def __aiter__(self):
1818
yield ServerSentEvent(event="message", data="", id=None, retry=None)
1919
raise RuntimeError("boom")
2020

@@ -27,15 +27,15 @@ async def spy_aclose() -> None:
2727
nonlocal closed
2828
closed = True
2929

30-
response = httpx.Response(200, headers={"content-type": "text/event-stream"})
30+
response = httpx2.Response(200, headers={"content-type": "text/event-stream"})
3131
response.aclose = spy_aclose # type: ignore[method-assign]
3232

3333
monkeypatch.setattr("mcp.client.streamable_http.EventSource", _RaiseEventSource)
3434

3535
send_stream, receive_stream = create_context_streams[SessionMessage | Exception](1)
3636
async with send_stream, receive_stream:
3737
transport = StreamableHTTPTransport("http://example.invalid/mcp")
38-
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200))) as client:
38+
async with httpx2.AsyncClient(transport=httpx2.MockTransport(lambda _: httpx2.Response(200))) as client:
3939
ctx = RequestContext(
4040
client=client,
4141
session_id=None,
@@ -49,21 +49,20 @@ async def spy_aclose() -> None:
4949

5050

5151
@pytest.mark.anyio
52-
async def test_handle_resumption_request_closes_response_when_aconnect_sse_raises(
52+
async def test_handle_resumption_request_closes_response_when_sse_connect_raises(
5353
monkeypatch: pytest.MonkeyPatch,
5454
) -> None:
5555
@contextlib.asynccontextmanager
56-
async def fake_aconnect_sse(*_args: object, **_kwargs: object):
56+
async def fake_sse(*_args: object, **_kwargs: object):
5757
raise RuntimeError("connect failed")
5858
yield
5959

60-
monkeypatch.setattr("mcp.client.streamable_http.aconnect_sse", fake_aconnect_sse)
61-
6260
send_stream, receive_stream = create_context_streams[SessionMessage | Exception](1)
6361
async with send_stream, receive_stream:
6462
transport = StreamableHTTPTransport("http://example.invalid/mcp")
6563
metadata = ClientMessageMetadata(resumption_token="1")
66-
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200))) as client:
64+
async with httpx2.AsyncClient(transport=httpx2.MockTransport(lambda _: httpx2.Response(200))) as client:
65+
monkeypatch.setattr(client, "sse", fake_sse)
6766
ctx = RequestContext(
6867
client=client,
6968
session_id=None,
@@ -90,24 +89,23 @@ async def spy_aclose() -> None:
9089
nonlocal closed
9190
closed = True
9291

93-
response = httpx.Response(
92+
response = httpx2.Response(
9493
200,
9594
headers={"content-type": "text/event-stream"},
96-
request=httpx.Request("GET", "http://example.invalid/mcp"),
95+
request=httpx2.Request("GET", "http://example.invalid/mcp"),
9796
)
9897
response.aclose = spy_aclose # type: ignore[method-assign]
9998

10099
@contextlib.asynccontextmanager
101-
async def fake_aconnect_sse(*_args: object, **_kwargs: object):
100+
async def fake_sse(*_args: object, **_kwargs: object):
102101
yield _RaiseEventSource(response)
103102

104-
monkeypatch.setattr("mcp.client.streamable_http.aconnect_sse", fake_aconnect_sse)
105-
106103
send_stream, receive_stream = create_context_streams[SessionMessage | Exception](1)
107104
async with send_stream, receive_stream:
108105
transport = StreamableHTTPTransport("http://example.invalid/mcp")
109106
metadata = ClientMessageMetadata(resumption_token="1")
110-
async with httpx.AsyncClient(transport=httpx.MockTransport(lambda _: httpx.Response(200))) as client:
107+
async with httpx2.AsyncClient(transport=httpx2.MockTransport(lambda _: httpx2.Response(200))) as client:
108+
monkeypatch.setattr(client, "sse", fake_sse)
111109
ctx = RequestContext(
112110
client=client,
113111
session_id=None,

0 commit comments

Comments
 (0)