diff --git a/src/restc.erl b/src/restc.erl index 4bc16f1..1251005 100644 --- a/src/restc.erl +++ b/src/restc.erl @@ -214,15 +214,9 @@ content_type(Headers, Type) -> default_content_type(Type) -> {<<"content-type">>, get_ctype(Type)}. -do_request(post, Type, Url, Headers, Body, Options) -> - Body2 = encode_body(Type, Body), - hackney:request(post, Url, Headers, Body2, Options); -do_request(put, Type, Url, Headers, Body, Options) -> - Body2 = encode_body(Type, Body), - hackney:request(put, Url, Headers, Body2, Options); -do_request(patch, Type, Url, Headers, Body, Options) -> - Body2 = encode_body(Type, Body), - hackney:request(patch, Url, Headers, Body2, Options); +do_request(Method, Type, Url, Headers, Body, Options) + when Method =:= post; Method =:= put; Method =:= patch; Method =:= delete -> + hackney:request(Method, Url, Headers, encode_body(Type, Body), Options); do_request(Method, _, Url, Headers, _, Options) when is_atom(Method) -> hackney:request(Method, Url, Headers, [], Options). diff --git a/test/restc_SUITE.erl b/test/restc_SUITE.erl index fd256b7..c8161fc 100644 --- a/test/restc_SUITE.erl +++ b/test/restc_SUITE.erl @@ -37,6 +37,8 @@ groups() -> , method_is_post__making_request__body_is_encoded , method_is_put__making_request__body_is_encoded , method_is_patch__making_request__body_is_encoded + , method_is_delete__making_request__body_is_encoded + , method_is_delete__making_request__body_is_empty , method_is_something_else__making_request__body_is_empty_list ]} ,{ response_body_decoding, @@ -193,11 +195,28 @@ method_is_patch__making_request__body_is_encoded(_Config) -> ?assert(meck:called(jsx, encode, '_')). +method_is_delete__making_request__body_is_encoded(_Config) -> + mock_hackney_success(200), + meck:new(jsx, [passthrough]), + + restc:request(delete, json, <<"http://any_url.com">>, [200], [], [{<<"any">>, <<"data">>}]), + + ?assert(meck:called(jsx, encode, '_')). + +method_is_delete__making_request__body_is_empty(_Config) -> + mock_hackney_success(200), + meck:new(jsx, [passthrough]), + + %% I feel it needs a case when the body can be empty + restc:request(delete, json, <<"http://any_url.com">>, [200], [], <<>>), + + ?assert(meck:called(jsx, encode, '_')). + method_is_something_else__making_request__body_is_empty_list(_Config) -> mock_hackney_success(200), ExpectedBody = [], - restc:request(delete, json, <<"http://any_url.com">>, [200], [], [{<<"any">>, <<"data">>}]), + restc:request(options, json, <<"http://any_url.com">>, [200], [], [{<<"any">>, <<"data">>}]), ActualBody = meck:capture(first, hackney, request, '_', 4, '_'), ?assertEqual(ExpectedBody, ActualBody).