Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion replicate/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(
self.response = response
self.use_file_output = use_file_output or True
content_type, _, _ = response.headers["content-type"].partition(";")
if content_type != "text/event-stream":
if content_type.strip().lower() != "text/event-stream":
raise ValueError(
"Expected response Content-Type to be 'text/event-stream', "
f"got {content_type!r}"
Expand Down
21 changes: 20 additions & 1 deletion tests/test_stream.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import httpx
import pytest

import replicate
from replicate.exceptions import ReplicateError
from replicate.stream import ServerSentEvent
from replicate.stream import EventSource, ServerSentEvent

from .conftest import skip_if_no_token

Expand Down Expand Up @@ -71,3 +72,21 @@ async def test_stream_prediction(async_flag, record_mode):
pytest.skip("Skipping test due to authentication error")
else:
raise e


@pytest.mark.parametrize("async_flag", [True, False])
async def test_event_stream_media_type_is_case_insensitive(async_flag):
response = httpx.Response(
200,
headers={"Content-Type": "Text/Event-Stream; charset=utf-8"},
content=b"id: 1\nevent: output\ndata: hello\n\n",
)
source = EventSource(replicate.Client(), response)
events = [event async for event in source] if async_flag else list(source)
assert [str(event) for event in events] == ["hello"]


def test_event_stream_rejects_other_media_types():
response = httpx.Response(200, headers={"Content-Type": "text/plain"})
with pytest.raises(ValueError, match="text/plain"):
EventSource(replicate.Client(), response)