Skip to content

Commit 8558396

Browse files
committed
fix: enhance response handling in Mint adapter
fixes #450 Signed-off-by: Yordis Prieto <[email protected]>
1 parent dd8b206 commit 8558396

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

lib/tesla/adapter/mint.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,22 @@ if Code.ensure_loaded?(Mint.HTTP) do
350350

351351
{:done, ^ref}, acc ->
352352
Map.put(acc, :done, true)
353+
354+
{:error, ^ref, _reason}, acc ->
355+
# Error responses are handled by Mint at a higher level
356+
acc
357+
358+
{:pong, ^ref}, acc ->
359+
# HTTP/2 pong response - no action needed for the current request
360+
acc
361+
362+
{:push_promise, ^ref, _promised_ref, _headers}, acc ->
363+
# HTTP/2 server push promise - not currently supported, ignore
364+
acc
365+
366+
_other, acc ->
367+
# Unknown response type - ignore to maintain forward compatibility
368+
acc
353369
end)
354370
end
355371
end

test/tesla/adapter/mint_test.exs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,75 @@ defmodule Tesla.Adapter.MintTest do
305305
read_body(conn, ref, opts, acc <> part)
306306
end
307307
end
308+
309+
describe "response handling - issue #450 fix" do
310+
test "adapter handles normal requests without crashes" do
311+
request = %Env{
312+
method: :get,
313+
url: "#{@http}/get"
314+
}
315+
316+
assert {:ok, %Env{} = response} = call(request)
317+
assert response.status == 200
318+
end
319+
320+
test "adapter handles connection errors gracefully" do
321+
request = %Env{
322+
method: :get,
323+
url: "#{@http}/status/500"
324+
}
325+
326+
assert {:ok, %Env{} = response} = call(request)
327+
assert response.status == 500
328+
end
329+
330+
test "adapter handles timeout scenarios" do
331+
request = %Env{
332+
method: :get,
333+
url: "#{@http}/delay/1"
334+
}
335+
336+
assert {:error, :timeout} = call(request, timeout: 100)
337+
end
338+
339+
test "adapter handles streaming responses" do
340+
request = %Env{
341+
method: :get,
342+
url: "#{@http}/stream-bytes/100"
343+
}
344+
345+
assert {:ok, %Env{} = response} = call(request, body_as: :stream)
346+
assert response.status == 200
347+
assert is_function(response.body)
348+
349+
data = Enum.join(response.body)
350+
assert byte_size(data) > 0
351+
end
352+
353+
test "adapter handles chunked responses" do
354+
request = %Env{
355+
method: :get,
356+
url: "#{@http}/stream-bytes/50"
357+
}
358+
359+
assert {:ok, %Env{} = response} = call(request, body_as: :chunks)
360+
assert response.status == 200
361+
362+
%{conn: conn, ref: ref, opts: opts, body: body} = response.body
363+
364+
{:ok, _conn, received_body} = read_body(conn, ref, opts, body)
365+
assert byte_size(received_body) > 0
366+
end
367+
368+
test "adapter handles large responses without crashes" do
369+
request = %Env{
370+
method: :get,
371+
url: "#{@http}/stream-bytes/1000"
372+
}
373+
374+
assert {:ok, %Env{} = response} = call(request)
375+
assert response.status == 200
376+
assert byte_size(response.body) > 1000
377+
end
378+
end
308379
end

0 commit comments

Comments
 (0)