Replace httpx and httpx-sse with httpx2 (#2972)
Co-authored-by: Max Isbey <224885523+maxisbey@users.noreply.github.com>
This commit is contained in:
parent
1216c53693
commit
2713b53b12
1 changed files with 1029 additions and 948 deletions
|
|
@ -9,7 +9,7 @@ travels on -- that the SDK client never exposes. Transport-agnostic behaviour is
|
|||
import anyio
|
||||
import pytest
|
||||
from anyio.lowlevel import checkpoint
|
||||
from httpx_sse import ServerSentEvent, aconnect_sse
|
||||
from httpx2 import ServerSentEvent
|
||||
from inline_snapshot import snapshot
|
||||
from mcp_types import (
|
||||
CLIENT_CAPABILITIES_META_KEY,
|
||||
|
|
@ -260,7 +260,7 @@ async def test_a_second_standalone_get_stream_on_the_same_session_returns_409()
|
|||
async with mounted_app(_server()) as (http, _):
|
||||
session_id = await initialize_via_http(http)
|
||||
|
||||
async with aconnect_sse(http, "GET", "/mcp", headers=base_headers(session_id=session_id)) as first:
|
||||
async with http.sse("/mcp", headers=base_headers(session_id=session_id)) as first:
|
||||
assert first.response.status_code == 200
|
||||
# The standalone-stream writer registers its key as its first action, then parks
|
||||
# awaiting messages; one yield to the loop lets that registration complete before the
|
||||
|
|
@ -296,10 +296,10 @@ async def test_messages_are_routed_to_exactly_one_stream() -> None:
|
|||
get_events: list[ServerSentEvent] = []
|
||||
|
||||
async def read_standalone_stream() -> None:
|
||||
async with aconnect_sse(http, "GET", "/mcp", headers=base_headers(session_id=session_id)) as get:
|
||||
async with http.sse("/mcp", headers=base_headers(session_id=session_id)) as get:
|
||||
assert get.response.status_code == 200
|
||||
standalone_ready.set()
|
||||
async for event in get.aiter_sse():
|
||||
async for event in get:
|
||||
get_events.append(event)
|
||||
seen_on_standalone.set()
|
||||
|
||||
|
|
@ -312,16 +312,15 @@ async def test_messages_are_routed_to_exactly_one_stream() -> None:
|
|||
|
||||
params = CallToolRequestParams(name="narrate", arguments={})
|
||||
body = JSONRPCRequest(jsonrpc="2.0", id=5, method="tools/call", params=params.model_dump())
|
||||
async with aconnect_sse(
|
||||
http,
|
||||
"POST",
|
||||
async with http.sse(
|
||||
"/mcp",
|
||||
method="POST",
|
||||
json=body.model_dump(by_alias=True, exclude_none=True),
|
||||
headers=base_headers(session_id=session_id),
|
||||
) as post:
|
||||
assert post.response.status_code == 200
|
||||
# The POST stream iterator ends when the server closes the stream after the response.
|
||||
post_events = [event async for event in post.aiter_sse()]
|
||||
post_events = [event async for event in post]
|
||||
|
||||
await seen_on_standalone.wait()
|
||||
tg.cancel_scope.cancel()
|
||||
|
|
@ -360,11 +359,14 @@ async def test_origin_validation_rejects_disallowed_origins_when_enabled() -> No
|
|||
"/mcp", json=initialize_body(), headers=base_headers() | {"origin": "http://evil.example"}
|
||||
)
|
||||
bad_host = await http.post("/mcp", json=initialize_body(), headers=base_headers() | {"host": "evil.example"})
|
||||
async with aconnect_sse(
|
||||
http, "POST", "/mcp", json=initialize_body(), headers=base_headers() | {"origin": "http://127.0.0.1:8000"}
|
||||
async with http.sse(
|
||||
"/mcp",
|
||||
method="POST",
|
||||
json=initialize_body(),
|
||||
headers=base_headers() | {"origin": "http://127.0.0.1:8000"},
|
||||
) as ok:
|
||||
assert ok.response.status_code == 200
|
||||
assert [event async for event in ok.aiter_sse()]
|
||||
assert [event async for event in ok]
|
||||
|
||||
assert (bad_origin.status_code, bad_origin.text) == snapshot((403, "Invalid Origin header"))
|
||||
assert (bad_host.status_code, bad_host.text) == snapshot((421, "Invalid Host header"))
|
||||
|
|
@ -372,10 +374,10 @@ async def test_origin_validation_rejects_disallowed_origins_when_enabled() -> No
|
|||
async with mounted_app(
|
||||
Server("unguarded"), transport_security=TransportSecuritySettings(enable_dns_rebinding_protection=False)
|
||||
) as (http, _):
|
||||
async with aconnect_sse(
|
||||
http, "POST", "/mcp", json=initialize_body(), headers=base_headers() | {"origin": "http://evil.example"}
|
||||
async with http.sse(
|
||||
"/mcp", method="POST", json=initialize_body(), headers=base_headers() | {"origin": "http://evil.example"}
|
||||
) as unguarded:
|
||||
status = unguarded.response.status_code
|
||||
assert [event async for event in unguarded.aiter_sse()]
|
||||
assert [event async for event in unguarded]
|
||||
|
||||
assert status == 200
|
||||
|
|
|
|||
Loading…
Reference in a new issue