From 158b24aa0b4d3753e142b246df4071091ee13c40 Mon Sep 17 00:00:00 2001 From: Thiago Romano Date: Fri, 6 May 2022 10:17:18 +0200 Subject: [PATCH 1/2] Uses `:max_body` --- lib/tesla/adapter/hackney.ex | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/tesla/adapter/hackney.ex b/lib/tesla/adapter/hackney.ex index 6a1a4ed7..1070fb50 100644 --- a/lib/tesla/adapter/hackney.ex +++ b/lib/tesla/adapter/hackney.ex @@ -68,17 +68,17 @@ if Code.ensure_loaded?(:hackney) do end defp request(method, url, headers, body, opts) do - handle(:hackney.request(method, url, headers, body || '', opts)) + handle(:hackney.request(method, url, headers, body || '', opts), opts) end defp request_stream(method, url, headers, body, opts) do with {:ok, ref} <- :hackney.request(method, url, headers, :stream, opts) do case send_stream(ref, body) do - :ok -> handle(:hackney.start_response(ref)) - error -> handle(error) + :ok -> handle(:hackney.start_response(ref), opts) + error -> handle(error, opts) end else - e -> handle(e) + e -> handle(e, opts) end end @@ -91,20 +91,20 @@ if Code.ensure_loaded?(:hackney) do end) end - defp handle({:error, _} = error), do: error - defp handle({:ok, status, headers}), do: {:ok, status, headers, []} + defp handle({:error, _} = error, _opts), do: error + defp handle({:ok, status, headers}, _opts), do: {:ok, status, headers, []} - defp handle({:ok, ref}) when is_reference(ref) do + defp handle({:ok, ref}, _opts) when is_reference(ref) do handle_async_response({ref, %{status: nil, headers: nil}}) end - defp handle({:ok, status, headers, ref}) when is_reference(ref) do - with {:ok, body} <- :hackney.body(ref) do + defp handle({:ok, status, headers, ref}, opts) when is_reference(ref) do + with {:ok, body} <- :hackney.body(ref, Keyword.get(opts, :max_body, :infinity)) do {:ok, status, headers, body} end end - defp handle({:ok, status, headers, body}), do: {:ok, status, headers, body} + defp handle({:ok, status, headers, body}, _opts), do: {:ok, status, headers, body} defp handle_async_response({ref, %{headers: headers, status: status}}) when not (is_nil(headers) or is_nil(status)) do From 88c52865e3ef64dad036214c973a14eda967bdbc Mon Sep 17 00:00:00 2001 From: Thiago Romano Date: Mon, 9 May 2022 16:48:44 +0200 Subject: [PATCH 2/2] Added test case and documentation --- lib/tesla/adapter/hackney.ex | 4 ++++ test/tesla/adapter/hackney_test.exs | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/tesla/adapter/hackney.ex b/lib/tesla/adapter/hackney.ex index 1070fb50..e5965857 100644 --- a/lib/tesla/adapter/hackney.ex +++ b/lib/tesla/adapter/hackney.ex @@ -24,6 +24,10 @@ if Code.ensure_loaded?(:hackney) do adapter Tesla.Adapter.Hackney end ``` + + ## Adapter specific options + + - `:max_body` - Max response body size in bytes. Actual response may be bigger because hackney stops after the last chunk that surpasses `:max_body`. """ @behaviour Tesla.Adapter alias Tesla.Multipart diff --git a/test/tesla/adapter/hackney_test.exs b/test/tesla/adapter/hackney_test.exs index 3b6414bc..794a1c9d 100644 --- a/test/tesla/adapter/hackney_test.exs +++ b/test/tesla/adapter/hackney_test.exs @@ -36,6 +36,18 @@ defmodule Tesla.Adapter.HackneyTest do assert is_reference(response.body) == true end + test "get with `:max_body` option" do + request = %Env{ + method: :post, + url: "#{@http}/post", + body: String.duplicate("long response", 1000) + } + + assert {:ok, %Env{} = response} = call(request, with_body: true, max_body: 100) + assert response.status == 200 + assert byte_size(response.body) < 2000 + end + test "request timeout error" do request = %Env{ method: :get,