Skip to content

Commit

Permalink
Merge pull request #355 from pedep/fix/max_body_length_doc
Browse files Browse the repository at this point in the history
correct :max_body_length documentation and spec
  • Loading branch information
edgurgel authored Sep 26, 2018
2 parents f0a867c + 25772e2 commit da41878
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/httpoison/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ defmodule HTTPoison.Base do
* `:follow_redirect` - a boolean that causes redirects to be followed
* `:max_redirect` - an integer denoting the maximum number of redirects to follow
* `:params` - an enumerable consisting of two-item tuples that will be appended to the url as query string parameters
* `:max_body_length` - a non-negative integer denoting the max response body length. Errors when body length exceeds. See :hackney.body/2
* `:max_body_length` - a non-negative integer denoting the max response body length. See :hackney.body/2
Timeouts can be an integer or `:infinity`
Expand Down
10 changes: 8 additions & 2 deletions test/httpoison_base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,15 @@ defmodule HTTPoisonBaseTest do
{:ok, 200, "headers", :client}
end)

expect(:hackney, :body, fn _, _ -> {:error, "some error"} end)
expect(:hackney, :body, fn _, _ -> {:ok, "res"} end)

assert HTTPoison.get("localhost", [], max_body_length: 3) ==
{:error, %HTTPoison.Error{id: nil, reason: "some error"}}
{:ok,
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "res",
request_url: "http://localhost"
}}
end
end
20 changes: 20 additions & 0 deletions test/httpoison_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,26 @@ defmodule HTTPoisonTest do
assert JSX.decode!(body)["json"] == expected
end

test "max_body_length limits body size" do
{:ok, socket} = :gen_tcp.listen(0, [:binary, {:active, false}, {:packet, :raw}])
{:ok, [buffer: buffer_size]} = :inet.getopts(socket, [:buffer])
:ok = :gen_tcp.close(socket)

max_length = Kernel.trunc(buffer_size * 1.5)

expected_length =
Float.ceil(max_length / buffer_size)
|> Kernel.*(buffer_size)
|> Kernel.trunc()

resp = HTTPoison.get("localhost:8080/stream/20", [], max_body_length: max_length)

assert_response(resp, fn response ->
assert byte_size(response.body) <= expected_length
assert byte_size(response.body) >= max_length
end)
end

defp assert_response({:ok, response}, function \\ nil) do
assert is_list(response.headers)
assert response.status_code == 200
Expand Down

0 comments on commit da41878

Please sign in to comment.