Skip to content

Commit

Permalink
Ensure connection errors return the expected error tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
carrascoacd committed May 5, 2023
1 parent 04a94e6 commit 406b49b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/tesla/adapter/mint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,15 @@ if Code.ensure_loaded?(Mint.HTTP) do
end
end

defp make_request(conn, method, path, headers, body),
do: HTTP.request(conn, method, path, headers, body)
defp make_request(conn, method, path, headers, body) do
case HTTP.request(conn, method, path, headers, body) do
{:ok, conn, ref} ->
{:ok, conn, ref}

{:error, _conn, error} ->
{:error, error}
end
end

defp stream_request(conn, ref, fun) do
case next_chunk(fun) do
Expand Down
22 changes: 21 additions & 1 deletion test/tesla/adapter/mint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule Tesla.Adapter.MintTest do
cacertfile: Path.join([to_string(:code.priv_dir(:httparrot)), "/ssl/server-ca.crt"])
]

test "Delay request" do
test "timeout request" do
request = %Env{
method: :head,
url: "#{@http}/delay/1"
Expand Down Expand Up @@ -179,6 +179,26 @@ defmodule Tesla.Adapter.MintTest do
assert conn.state == :closed
end

test "body_as :plain - returns error tuple matching the specification when connection is closed",
%{conn: conn, original: original} do
request = %Env{
method: :get,
url: "#{@http}/stream-bytes/10"
}

assert {:ok, %Env{} = response} =
call(request, conn: conn, original: original, close_conn: false)

assert response.status == 200
assert byte_size(response.body) == 16

{:ok, conn} = Tesla.Adapter.Mint.close(conn)
assert conn.state == :closed

assert {:error, %Mint.HTTPError{reason: :closed, module: Mint.HTTP1}} =
call(request, conn: conn, original: original, close_conn: false)
end

test "body_as :stream", %{conn: conn, original: original} do
request = %Env{
method: :get,
Expand Down

0 comments on commit 406b49b

Please sign in to comment.