Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better stack traces #480

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/httpoison/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ defmodule HTTPoison.Base do
@type params :: Request.params()

defmacro __using__(_) do
quote do
quote location: :keep do
@behaviour HTTPoison.Base

@type request :: HTTPoison.Base.request()
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
}
51 changes: 33 additions & 18 deletions test/httpoison_base_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,32 @@ defmodule HTTPoisonBaseTest do
end

test "request body using params example" do
expect(:hackney, :request, fn :get, "http://localhost?foo=bar&key=fizz", [], "", [] ->
expected_query = %{"foo" => "bar", "key" => "fizz"}

expect(:hackney, :request, fn :get, "http://localhost?" <> query, [], "", [] ->
assert expected_query == URI.decode_query(query)
{:ok, 200, "headers", :client}
end)

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

assert ExampleParamsOptions.get!("localhost", [], params: %{foo: "bar"}) ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost?foo=bar&key=fizz",
request: %HTTPoison.Request{
body: "",
headers: [],
method: :get,
options: [params: %{foo: "bar", key: "fizz"}],
params: %{foo: "bar", key: "fizz"},
url: "http://localhost?foo=bar&key=fizz"
}
assert %HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost?" <> request_url_query,
request: %HTTPoison.Request{
body: "",
headers: [],
method: :get,
options: [params: %{foo: "bar", key: "fizz"}],
params: %{foo: "bar", key: "fizz"},
url: "http://localhost?" <> url_query
}
} = ExampleParamsOptions.get!("localhost", [], params: %{foo: "bar"})

assert expected_query == URI.decode_query(request_url_query)
assert expected_query == URI.decode_query(url_query)
end

test "get!/1 raises error tuple" do
Expand All @@ -142,6 +147,16 @@ defmodule HTTPoisonBaseTest do
assert HTTPoison.get("http://localhost") == {:error, %HTTPoison.Error{reason: reason}}
end

test "stack trace preserved on error" do
expect(:hackney, :request, fn _, _, _, _, _ -> {:error, {:error, "whatevs"}} end)

HTTPoison.get!("http://localhost")
rescue
_e ->
[{HTTPoison, :request!, 5, file_line} | _] = __STACKTRACE__
assert {:ok, ~c"lib/httpoison/base.ex"} = Keyword.fetch(file_line, :file)
end

test "passing connect_timeout option" do
expect(:hackney, :request, fn
:post, "http://localhost", [], "body", [connect_timeout: 12345] ->
Expand Down Expand Up @@ -221,7 +236,7 @@ defmodule HTTPoisonBaseTest do
[
socks5_pass: "secret",
socks5_user: "user",
proxy: {:socks5, 'localhost', 1080}
proxy: {:socks5, ~c"localhost", 1080}
] ->
{:ok, 200, "headers", :client}
end)
Expand All @@ -232,7 +247,7 @@ defmodule HTTPoisonBaseTest do
"localhost",
"body",
[],
proxy: {:socks5, 'localhost', 1080},
proxy: {:socks5, ~c"localhost", 1080},
socks5_user: "user",
socks5_pass: "secret"
) ==
Expand All @@ -246,7 +261,7 @@ defmodule HTTPoisonBaseTest do
headers: [],
method: :post,
options: [
proxy: {:socks5, 'localhost', 1080},
proxy: {:socks5, ~c"localhost", 1080},
socks5_user: "user",
socks5_pass: "secret"
],
Expand Down
10 changes: 6 additions & 4 deletions test/httpoison_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ defmodule HTTPoisonTest do
assert args["baz"] == "bong"
assert args |> Map.keys() |> length == 2

assert Request.to_curl(response.request) ==
{:ok, "curl -X GET http://localhost:4002/get?baz=bong&foo=bar"}
assert {:ok, "curl -X GET http://localhost:4002/get?" <> query} =
Request.to_curl(response.request)

assert %{"baz" => "bong", "foo" => "bar"} == URI.decode_query(query)
end)
end

Expand Down Expand Up @@ -53,7 +55,7 @@ defmodule HTTPoisonTest do
end

test "post charlist body" do
assert_response(HTTPoison.post("localhost:4002/post", 'test'), fn response ->
assert_response(HTTPoison.post("localhost:4002/post", ~c"test"), fn response ->
assert Request.to_curl(response.request) == {:ok, "curl -X POST http://localhost:4002/post"}
end)
end
Expand Down Expand Up @@ -233,7 +235,7 @@ defmodule HTTPoisonTest do
end

test "char list URL" do
assert_response(HTTPoison.head('localhost:4002/get'), fn response ->
assert_response(HTTPoison.head(~c"localhost:4002/get"), fn response ->
assert Request.to_curl(response.request) ==
{:ok, "curl -X HEAD http://localhost:4002/get"}
end)
Expand Down