Skip to content

Commit

Permalink
Merge pull request #528 from thiagopromano/hackney-max-body
Browse files Browse the repository at this point in the history
Uses `:max_body` in `hackney` Adapter
  • Loading branch information
yordis authored Dec 4, 2022
2 parents 34b1713 + 88c5286 commit 5e983c1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/tesla/adapter/hackney.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,17 +72,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

Expand All @@ -91,20 +95,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
Expand Down
12 changes: 12 additions & 0 deletions test/tesla/adapter/hackney_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5e983c1

Please sign in to comment.