Skip to content

Commit 0e9cf8d

Browse files
authored
fix: Handle non-list term being emitted from Stream.chunk_while in SSE (#788)
1 parent c52eec8 commit 0e9cf8d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/tesla/middleware/sse.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ defmodule Tesla.Middleware.SSE do
5555
end,
5656
fn
5757
"" -> {:cont, ""}
58-
acc -> {:cont, acc, ""}
58+
acc -> {:cont, [acc], ""}
5959
end
6060
)
6161
|> Stream.flat_map(& &1)

test/tesla/middleware/sse_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ defmodule Tesla.Middleware.SSETest do
101101

102102
assert env.body == ["data1", "data2\ndata3", "data4"]
103103
end
104+
end
104105

106+
describe "streaming response" do
105107
test "handle stream data" do
106108
adapter = fn _env ->
107109
chunks = [
@@ -144,5 +146,23 @@ defmodule Tesla.Middleware.SSETest do
144146
%{event: "event4", data: "data4"}
145147
]
146148
end
149+
150+
test "handle unterminated final message" do
151+
adapter = fn _env ->
152+
chunks = [
153+
~s|data: data1\n\n|,
154+
~s|data: data2\n\n|,
155+
~s|data: data3|
156+
]
157+
158+
stream = Stream.map(chunks, & &1)
159+
160+
{:ok, %{@env | body: stream}}
161+
end
162+
163+
assert {:ok, env} = Tesla.Middleware.SSE.call(%Tesla.Env{}, [{:fn, adapter}], [])
164+
165+
assert Enum.to_list(env.body) == [%{data: "data1"}, %{data: "data2"}, %{data: "data3"}]
166+
end
147167
end
148168
end

0 commit comments

Comments
 (0)