test: add tests for non-2xx HTTP status handling (fixes #3091)#3093
Open
zsxh1990 wants to merge 4 commits into
Open
test: add tests for non-2xx HTTP status handling (fixes #3091)#3093zsxh1990 wants to merge 4 commits into
zsxh1990 wants to merge 4 commits into
Conversation
…rotocol#3091) Verify that StreamableHTTPTransport._handle_post_request properly handles 401/403/5xx by sending JSONRPCError to the caller instead of letting the request timeout. Tests: - 401 → JSONRPCError with INTERNAL_ERROR code - 403 → JSONRPCError with INTERNAL_ERROR code - 500 → JSONRPCError with INTERNAL_ERROR code - JSON error body → parsed and used directly Closes modelcontextprotocol#3091
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="tests/client/test_non2xx_status.py">
<violation number="1" location="tests/client/test_non2xx_status.py:27">
P1: These tests fail before exercising status handling because `AsyncMock.stream()` returns a coroutine, but `_handle_post_request` uses `client.stream()` directly as an async context manager. Making `client` a `MagicMock` lets its `stream` method return the configured `mock_stream_ctx`, matching `httpx.AsyncClient.stream()`'s interface.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| ctx.session_message.message = MagicMock(spec=JSONRPCRequest) | ||
| ctx.session_message.message.id = request_id | ||
| ctx.read_stream_writer = AsyncMock() | ||
| ctx.client = AsyncMock() |
There was a problem hiding this comment.
P1: These tests fail before exercising status handling because AsyncMock.stream() returns a coroutine, but _handle_post_request uses client.stream() directly as an async context manager. Making client a MagicMock lets its stream method return the configured mock_stream_ctx, matching httpx.AsyncClient.stream()'s interface.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/client/test_non2xx_status.py, line 27:
<comment>These tests fail before exercising status handling because `AsyncMock.stream()` returns a coroutine, but `_handle_post_request` uses `client.stream()` directly as an async context manager. Making `client` a `MagicMock` lets its `stream` method return the configured `mock_stream_ctx`, matching `httpx.AsyncClient.stream()`'s interface.</comment>
<file context>
@@ -0,0 +1,146 @@
+ ctx.session_message.message = MagicMock(spec=JSONRPCRequest)
+ ctx.session_message.message.id = request_id
+ ctx.read_stream_writer = AsyncMock()
+ ctx.client = AsyncMock()
+ ctx.metadata = None
+ return ctx
</file context>
- SessionMessage is in mcp.shared.message, not mcp_types - Format with ruff
Use StreamableHTTPTransport(url) instead of __new__() to avoid AttributeError on uninitialized attributes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add tests verifying that
StreamableHTTPTransport._handle_post_requestproperly handles non-2xx HTTP status codes.Tests Added
Verification
The current implementation already handles non-2xx correctly (lines 342-373 in
streamable_http.py). These tests document the expected behavior and prevent regressions.Key Behavior
JSONRPCErrorsent to caller with original request IDINTERNAL_ERRORCloses #3091
/claim #3091