diff --git a/lib/cassette/client/generate_st.ex b/lib/cassette/client/generate_st.ex index cadf947..4bcfa9c 100644 --- a/lib/cassette/client/generate_st.ex +++ b/lib/cassette/client/generate_st.ex @@ -7,6 +7,8 @@ defmodule Cassette.Client.GenerateSt do alias Cassette.Config alias Cassette.Client + alias HTTPoison.Error + alias HTTPoison.Response @type response :: {:error, :bad_tgt} @@ -26,15 +28,18 @@ defmodule Cassette.Client.GenerateSt do headers = [] case post(url, params, headers, options) do - {:ok, %HTTPoison.Response{status_code: 404}} -> + {:ok, %Response{status_code: 404}} -> {:error, :bad_tgt} - {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> + {:ok, %Response{status_code: 200, body: body}} -> {:ok, body} - {:ok, %HTTPoison.Response{status_code: status_code, body: body}} -> + {:ok, %Response{status_code: status_code, body: body}} -> {:fail, status_code, body} + {:error, %Error{reason: reason}} -> + {:fail, reason} + _ -> {:fail, :unknown} end diff --git a/lib/cassette/client/generate_tgt.ex b/lib/cassette/client/generate_tgt.ex index a26d1cb..ab55953 100644 --- a/lib/cassette/client/generate_tgt.ex +++ b/lib/cassette/client/generate_tgt.ex @@ -7,13 +7,13 @@ defmodule Cassette.Client.GenerateTgt do alias Cassette.Config alias Cassette.Client + alias HTTPoison.Error alias HTTPoison.Response @type response :: {:error, :bad_credentials} | {:ok, String.t()} - | {:fail, pos_integer()} - | {:fail, :unknown} + | {:fail, term()} @spec process_headers([{String.t(), String.t()}]) :: %{String.t() => String.t()} defp process_headers(headers) do @@ -42,6 +42,9 @@ defmodule Cassette.Client.GenerateTgt do {:ok, %Response{status_code: status_code}} -> {:fail, status_code} + {:error, %Error{reason: reason}} when is_atom(reason) -> + {:fail, reason} + _ -> {:fail, :unknown} end diff --git a/lib/cassette/client/validate_ticket.ex b/lib/cassette/client/validate_ticket.ex index bdd01e3..ab23e43 100644 --- a/lib/cassette/client/validate_ticket.ex +++ b/lib/cassette/client/validate_ticket.ex @@ -7,9 +7,10 @@ defmodule Cassette.Client.ValidateTicket do alias Cassette.Config alias Cassette.Client + alias HTTPoison.Error alias HTTPoison.Response - @type response :: {:ok, String.t()} | {:fail, :unknown} + @type response :: {:ok, String.t()} | {:fail, term()} @doc """ Do request to cas service to validate a service ticket @@ -22,6 +23,7 @@ defmodule Cassette.Client.ValidateTicket do case get(url, headers, options) do {:ok, %Response{status_code: 200, body: body}} -> {:ok, body} + {:error, %Error{reason: reason}} when is_atom(reason) -> {:fail, reason} _ -> {:fail, :unknown} end end diff --git a/lib/cassette/server.ex b/lib/cassette/server.ex index fa59491..7f0cbd6 100644 --- a/lib/cassette/server.ex +++ b/lib/cassette/server.ex @@ -62,7 +62,7 @@ defmodule Cassette.Server do GenServer.start_link(__MODULE__, {:ok, config}) end - version(">= 1.5.0") do + version ">= 1.5.0" do def child_spec([name, config = %Config{}]) do defaults = %{id: name, start: {__MODULE__, :start_link, [name, config]}} Supervisor.child_spec(defaults, []) @@ -178,6 +178,9 @@ defmodule Cassette.Server do {:fail, :unknown} -> {:reply, {:error, "Failed for unknown reason"}, state} + {:fail, reason} when is_atom(reason) -> + {:reply, {:error, "Failed because #{reason}"}, state} + {:fail, status_code} -> {:reply, {:error, "Failed with status #{status_code}"}, state} end @@ -236,6 +239,9 @@ defmodule Cassette.Server do {:fail, :unknown} -> {:error, "Failed for unknown reason"} + + {:fail, reason} -> + {:error, "Failed because #{reason}"} end end diff --git a/test/cassette/client/generate_st_test.exs b/test/cassette/client/generate_st_test.exs index 241cca9..78fb82d 100644 --- a/test/cassette/client/generate_st_test.exs +++ b/test/cassette/client/generate_st_test.exs @@ -27,10 +27,10 @@ defmodule Cassette.Client.GenerateStTest do assert {:fail, 418, "I. am. a. freaking. teapot."} = Cassette.Client.GenerateSt.perform(config, tgt, service) end - test "perform returns {:fail, :unknown} then http fails", %{bypass: bypass, config: config, tgt: tgt, service: service} do + test "perform returns {:fail, reason} then http fails with an atom", %{bypass: bypass, config: config, tgt: tgt, service: service} do Bypass.down(bypass) - assert {:fail, :unknown} = Cassette.Client.GenerateSt.perform(config, tgt, service) + assert {:fail, :econnrefused} = Cassette.Client.GenerateSt.perform(config, tgt, service) end test "perform generates a ST", %{bypass: bypass, config: config, tgt: tgt, service: service} do diff --git a/test/cassette/client/generate_tgt_test.exs b/test/cassette/client/generate_tgt_test.exs index d9ed9d5..ccb4c6e 100644 --- a/test/cassette/client/generate_tgt_test.exs +++ b/test/cassette/client/generate_tgt_test.exs @@ -17,6 +17,12 @@ defmodule Cassette.Client.GenerateTgtTest do assert {:error, :bad_credentials} = Cassette.Client.GenerateTgt.perform(config) end + test "perform returns {:fail, reason} for atom error reasons", %{bypass: bypass, config: config} do + Bypass.down(bypass) + + assert {:fail, :econnrefused} = Cassette.Client.GenerateTgt.perform(config) + end + test "perform returns {:fail, status_code} for other error statuses", %{bypass: bypass, config: config} do Bypass.expect bypass, fn conn -> conn |> Plug.Conn.resp(404, "not found") diff --git a/test/cassette/client/validate_ticket_test.exs b/test/cassette/client/validate_ticket_test.exs index 99dcbe1..f30d693 100644 --- a/test/cassette/client/validate_ticket_test.exs +++ b/test/cassette/client/validate_ticket_test.exs @@ -11,6 +11,12 @@ defmodule Cassette.Client.ValidateTicketTest do {:ok, bypass: bypass, config: config, ticket: ticket, service: service} end + test "perform returns {:fail, reason} for atom failure reasons", %{bypass: bypass, config: config, ticket: ticket, service: service} do + Bypass.down(bypass) + + assert {:fail, :econnrefused} = Cassette.Client.ValidateTicket.perform(config, ticket, service) + end + test "perform returns {:fail, :unknown} for not-200 response", %{bypass: bypass, config: config, ticket: ticket, service: service} do Bypass.expect bypass, fn conn -> conn |> Plug.Conn.resp(404, "not found") @@ -19,12 +25,6 @@ defmodule Cassette.Client.ValidateTicketTest do assert {:fail, :unknown} = Cassette.Client.ValidateTicket.perform(config, ticket, service) end - test "perform returns {:fail, :unknown} then http fails", %{bypass: bypass, config: config, ticket: ticket, service: service} do - Bypass.down(bypass) - - assert {:fail, :unknown} = Cassette.Client.ValidateTicket.perform(config, ticket, service) - end - test "perform returns the validation body", %{bypass: bypass, config: config, ticket: ticket, service: service} do body = "" diff --git a/test/cassette/server_integration_test.exs b/test/cassette/server_integration_test.exs index ea57fd2..9e4f487 100644 --- a/test/cassette/server_integration_test.exs +++ b/test/cassette/server_integration_test.exs @@ -19,7 +19,7 @@ defmodule Cassette.ServerIntegrationTest do assert {:ok, ^tgt} = Server.tgt(pid) end - test "fails with unknown reason when cas is down", + test "fails when cas is down", %{pid: pid, config: config} do {:ok, fake_cas_pid} = FakeCas.Server.start_link([]) @@ -28,7 +28,7 @@ defmodule Cassette.ServerIntegrationTest do :ok = Cassette.Server.reload(pid, config) FakeCas.Server.stop(fake_cas_pid) - assert {:error, "Failed for unknown reason"} = Server.tgt(pid) + assert {:error, "Failed because econnrefused"} = Server.tgt(pid) end test "fails with {:error, _} when username/password is invalid",