From 406b49b61cda113f8b9e05600414e79cb81f3335 Mon Sep 17 00:00:00 2001 From: Antonio Carrasco Date: Fri, 5 May 2023 09:40:37 +0200 Subject: [PATCH 1/2] Ensure connection errors return the expected error tuple --- lib/tesla/adapter/mint.ex | 11 +++++++++-- test/tesla/adapter/mint_test.exs | 22 +++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/tesla/adapter/mint.ex b/lib/tesla/adapter/mint.ex index 7dc08df9..c88057fb 100644 --- a/lib/tesla/adapter/mint.ex +++ b/lib/tesla/adapter/mint.ex @@ -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 diff --git a/test/tesla/adapter/mint_test.exs b/test/tesla/adapter/mint_test.exs index 80297bc4..8ac79783 100644 --- a/test/tesla/adapter/mint_test.exs +++ b/test/tesla/adapter/mint_test.exs @@ -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" @@ -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, From 122d8bcba582b976328b95a09c8bfe033070c89a Mon Sep 17 00:00:00 2001 From: Antonio Carrasco Date: Sat, 6 May 2023 10:13:38 +0200 Subject: [PATCH 2/2] Bump version to 1.6.1 --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index b38c1786..41118e0b 100644 --- a/mix.exs +++ b/mix.exs @@ -2,7 +2,7 @@ defmodule Tesla.Mixfile do use Mix.Project @source_url "https://github.com/teamon/tesla" - @version "1.6.0" + @version "1.6.1" def project do [