Skip to content

Commit

Permalink
add test for request
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeMilan committed May 1, 2023
1 parent 301b43d commit 22d2d8b
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion test/httpoison_base_test.exs
Expand Up @@ -35,6 +35,10 @@ defmodule HTTPoisonBaseTest do
do: Keyword.merge(options, params: Map.merge(options[:params], %{key: "fizz"}))
end

defmodule ExampleRequest do
use HTTPoison.Base
end

setup do
on_exit(fn ->
System.delete_env("HTTP_PROXY")
Expand Down Expand Up @@ -125,7 +129,7 @@ defmodule HTTPoisonBaseTest do
}
end

test "request raises error tuple" do
test "get!/1 raises error tuple" do
reason = {:closed, "Something happened"}

expect(:hackney, :request, fn _, _, _, _, _ -> {:error, reason} end)
Expand Down Expand Up @@ -698,4 +702,35 @@ defmodule HTTPoisonBaseTest do
}
}}
end

test "request body using request/1 example" do
expect(:hackney, :request, fn :get, "http://localhost", [], "", [] ->
{:ok, 200, "headers", :client}
end)

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

request = %HTTPoison.Request{url: "http://localhost"}
assert ExampleRequest.request!(request) ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost",
request: request,
}
end

test "request! raises error tuple" do
reason = {:closed, "Something happened"}

expect(:hackney, :request, fn _, _, _, _, _ -> {:error, reason} end)
expect(:hackney, :request, fn _, _, _, _, _ -> {:error, reason} end)

assert_raise HTTPoison.Error, "{:closed, \"Something happened\"}", fn ->
HTTPoison.request!(:get, "http://localhost")
end

assert HTTPoison.request(:get, "http://localhost") == {:error, %HTTPoison.Error{reason: reason}}
end
end

0 comments on commit 22d2d8b

Please sign in to comment.