Packages

Phy provides a number of generators for Elixir Phoenix projects.

Current section

Files

Jump to
phy priv templates http_client_test.exs.eex
Raw

priv/templates/http_client_test.exs.eex

defmodule MockHTTPClient do
@behaviour #{module}.HTTPClient
def get("https://www.example.com") do
{:ok, %Req.Response{status: 200, body: "Some HTML"}}
end
def get("https://www.doesntexist.com/some/path") do
{:ok, %Req.Response{status: 404}}
end
def get("https://return.error") do
{:error, "Boom!"}
end
@doc \"\"\"
It makes no sense to mock get/2, as it is simply for testing
\"\"\"
def get(_url, _mock), do: nil
end
defmodule #{module}.HttpClientTest do
use #{module}.DataCase
alias #{module}.HTTPClient
test "get returns the body of the response" do
{:ok, body} = HTTPClient.get("https://www.example.com", MockHTTPClient)
assert String.contains?(body, "Some HTML")
end
test "get returns :not_found when the response is 404" do
{:error, :not_found} = HTTPClient.get("https://www.doesntexist.com/some/path", MockHTTPClient)
end
test "get returns :not_found for other errors" do
{:error, :not_found} = HTTPClient.get("https://return.error", MockHTTPClient)
end
end