diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/http_test.exs | 36 | ||||
-rw-r--r-- | test/support/test_http_client.ex | 48 |
2 files changed, 84 insertions, 0 deletions
diff --git a/test/http_test.exs b/test/http_test.exs new file mode 100644 index 0000000..61847d4 --- /dev/null +++ b/test/http_test.exs @@ -0,0 +1,36 @@ +defmodule ExPing.HTTP.Test do + use ExUnit.Case + + describe "head requests" do + test "can get ok response" do + for code <- [200, 405] do + {:ok, _} = ExPing.HTTP.head(URI.parse("localhost/#{code}")) + end + end + + test "can get error response" do + {:error, _} = ExPing.HTTP.head(URI.parse("localhost/500")) + end + + test "can get timeout response" do + {:error, :timeout} = ExPing.HTTP.head(URI.parse("localhost/timeout")) + end + end + + describe "get requests" do + test "can get ok response" do + for code <- [200, 400, 404] do + {:ok, _} = ExPing.HTTP.get(URI.parse("localhost/#{code}")) + end + end + + test "can get error response" do + {:error, _} = ExPing.HTTP.get(URI.parse("localhost/500")) + end + + test "can get timeout response" do + {:error, :timeout} = ExPing.HTTP.get(URI.parse("localhost/timeout")) + end + end + +end diff --git a/test/support/test_http_client.ex b/test/support/test_http_client.ex new file mode 100644 index 0000000..24ca509 --- /dev/null +++ b/test/support/test_http_client.ex @@ -0,0 +1,48 @@ +defmodule ExPing.Test.HTTPClient do + @moduledoc """ + Mock HTTP client provides interface similar to `:httpc`. + """ + + def request(url), do: request(:get, {url, []}, [], []) + def request(:head, {url, _}, _, _) do + cond do + String.contains?(to_string(url), "/200") -> + {:ok, {status_line(200), [], []}} + String.contains?(to_string(url), "/405") -> + {:ok, {status_line(405), [], []}} + String.contains?(to_string(url), "/500") -> + Process.exit(self, :kill) + String.contains?(to_string(url), "/timeout") -> + :timer.sleep(20) + {:ok, {status_line(500), [], []}} + true -> + {:ok, {status_line(404), [], []}} + end + end + def request(:get, {url, _}, _, _) do + cond do + String.contains?(to_string(url), "/200") -> + {:ok, {status_line(200), [], []}} + String.contains?(to_string(url), "/400") -> + {:ok, {status_line(400), [], []}} + String.contains?(to_string(url), "/500") -> + Process.exit(self, :kill) + String.contains?(to_string(url), "/timeout") -> + :timer.sleep(20) + {:ok, {status_line(500), [], []}} + true -> + {:ok, {status_line(404), [], []}} + end + {:ok, {status_line(404), [], []}} + end + + for {code, reason} <- [{200, 'OK'}, + {400, 'Bad Request'}, + {404, 'Not Found'}, + {405, 'Method Not Allowed'}, + {500, 'Internal Server Error'}] do + defp status_line(unquote(code)) do + {'HTTP/1.1', unquote(code), unquote(reason)} + end + end +end |