Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/hackney_request.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,18 @@ perform(Client0, {Method0, Path0, Headers0, Body0}) ->
Size, Boundary, Client0);
<<>> when Method =:= <<"POST">> orelse Method =:= <<"PUT">> ->
handle_body(Headers2, ReqType0, Body0, Client0);
[] when Method =:= <<"POST">> orelse Method =:= <<"PUT">> ->
handle_body(Headers2, ReqType0, Body0, Client0);
<<>> ->
{Headers2, ReqType0, Body0, Client0};
[] ->
{Headers2, ReqType0, Body0, Client0};
_ when is_list(Body0) ->
Body1 = iolist_to_binary(Body0),
case Body1 of
<<>> when Method =:= <<"POST">> orelse Method =:= <<"PUT">> ->
handle_body(Headers2, ReqType0, Body1, Client0);
<<>> ->
{Headers2, ReqType0, Body1, Client0};
_ ->
handle_body(Headers2, ReqType0, Body1, Client0)
end;
_ ->
handle_body(Headers2, ReqType0, Body0, Client0)
end,
Expand Down Expand Up @@ -370,13 +376,6 @@ handle_body(Headers, ReqType0, Body0, Client) ->
S = hackney_headers_new:get_value(<<"content-length">>, Headers),
{S, CT, Body0};

_ when is_list(Body0) -> % iolist case
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case is now handled before calling handle_body.

Body1 = iolist_to_binary(Body0),
S = erlang:byte_size(Body1),
CT = hackney_headers_new:get_value(
<<"content-type">>, Headers, <<"application/octet-stream">>
),
{S, CT, Body1};
_ when is_binary(Body0) ->
S = erlang:byte_size(Body0),
CT = hackney_headers_new:get_value(
Expand Down
43 changes: 43 additions & 0 deletions test/hackney_integration_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ all_tests() ->
% fun relative_redirect_request_no_follow/0,
fun relative_redirect_request_follow/0,
fun test_duplicate_headers/0,
fun test_post_includes_content_length_with_body/0,
fun test_post_includes_content_length_with_empty_body/0,
fun test_get_includes_content_length_with_body/0,
fun test_get_excludes_content_length_with_empty_body/0,
fun test_custom_host_headers/0,
fun async_request/0,
fun async_head_request/0,
Expand Down Expand Up @@ -186,6 +190,45 @@ test_custom_host_headers() ->
ReqHeaders = proplists:get_value(<<"headers">>, Obj),
?assertEqual(<<"myhost.com">>, proplists:get_value(<<"Host">>, ReqHeaders)).

test_post_includes_content_length_with_body() ->
URL = <<"http://localhost:8000/post">>,
Body = <<"{\"test\": \"ok\" }">>,
Options = [with_body],
{ok, 200, _H, JsonBody} = hackney:post(URL, [], Body, Options),
Obj = jsone:decode(JsonBody, [{object_format, proplist}]),
ReqHeaders = proplists:get_value(<<"headers">>, Obj),
?assertEqual(<<"15">>, proplists:get_value(<<"Content-Length">>, ReqHeaders)).

test_post_includes_content_length_with_empty_body() ->
URL = <<"http://localhost:8000/post">>,
Body = <<>>,
Options = [with_body],
{ok, 200, _H, JsonBody} = hackney:post(URL, [], Body, Options),
Obj = jsone:decode(JsonBody, [{object_format, proplist}]),
ReqHeaders = proplists:get_value(<<"headers">>, Obj),
?assertEqual(<<"0">>, proplists:get_value(<<"Content-Length">>, ReqHeaders)).

test_get_includes_content_length_with_body() ->
URL = <<"http://localhost:8000/post">>,
Body = <<"{\"test\": \"ok\" }">>,
Options = [with_body],
{ok, 200, _H, JsonBody} = hackney:post(URL, [], Body, Options),
Obj = jsone:decode(JsonBody, [{object_format, proplist}]),
ReqHeaders = proplists:get_value(<<"headers">>, Obj),
?assertEqual(<<"15">>, proplists:get_value(<<"Content-Length">>, ReqHeaders)).

test_get_excludes_content_length_with_empty_body() ->
URL = <<"http://localhost:8000/get">>,
EmptyBodies = [<<>>, [], [<<>>]],
Options = [with_body],
lists:foreach(fun(Body) ->
{ok, 200, _H, JsonBody} = hackney:get(URL, [], Body, Options),
Obj = jsone:decode(JsonBody, [{object_format, proplist}]),
ReqHeaders = proplists:get_value(<<"headers">>, Obj),
?assertEqual(undefined, proplists:get_value(<<"Content-Type">>, ReqHeaders)),
?assertEqual(undefined, proplists:get_value(<<"Content-Length">>, ReqHeaders))
end, EmptyBodies).

test_frees_manager_ets_when_body_is_in_client() ->
URL = <<"http://localhost:8000/get">>,
BeforeCount = ets:info(hackney_manager_refs, size),
Expand Down