Skip to content

Commit d0d2600

Browse files
magicmarkclaude
andcommitted
Skip multipart parts with empty Content-Type instead of raising
Some GraphQL servers send multipart parts without a Content-Type header as heartbeats or keep-alive padding. Previously this raised a TransportProtocolError, crashing the subscription stream. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 4300ac0 commit d0d2600

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎gql/transport/aiohttp.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,9 @@ async def _parse_multipart_part(
542542
"""
543543
# Verify the part has the correct content type
544544
content_type = part.headers.get(aiohttp.hdrs.CONTENT_TYPE, "")
545+
if not content_type:
546+
log.debug("Skipping part with no content-type (likely heartbeat)")
547+
return None
545548
if not content_type.startswith("application/json"):
546549
raise TransportProtocolError(
547550
f"Unexpected part content-type: {content_type}. "

‎tests/test_aiohttp_multipart.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,38 @@ async def test_aiohttp_multipart_wrong_part_content_type(multipart_server):
480480
assert "text/html" in str(exc_info.value)
481481

482482

483+
@pytest.mark.asyncio
484+
async def test_aiohttp_multipart_empty_content_type_skipped(multipart_server):
485+
"""Test that parts with empty/missing content-type are skipped as heartbeats."""
486+
from gql.transport.aiohttp import AIOHTTPTransport
487+
488+
book1_payload = json.dumps({"payload": {"data": {"book": book1}}})
489+
490+
parts = [
491+
("--graphql\r\n" "\r\n" "\r\n"),
492+
(
493+
"--graphql\r\n"
494+
"Content-Type: application/json\r\n"
495+
"\r\n"
496+
f"{book1_payload}\r\n"
497+
),
498+
"--graphql--\r\n",
499+
]
500+
501+
server = await multipart_server(parts)
502+
url = server.make_url("/")
503+
transport = AIOHTTPTransport(url=url)
504+
505+
async with Client(transport=transport) as session:
506+
query = gql(subscription_str)
507+
results = []
508+
async for result in session.subscribe(query):
509+
results.append(result)
510+
511+
assert len(results) == 1
512+
assert results[0]["book"]["title"] == "Book 1"
513+
514+
483515
@pytest.mark.asyncio
484516
async def test_aiohttp_multipart_response_headers(multipart_server):
485517
"""Test that response headers are captured in the transport."""

0 commit comments

Comments
 (0)