From 30de7fb22bdbdaf4587fa0c2dabc185364582d36 Mon Sep 17 00:00:00 2001 From: Gonzalo <456459+grzuy@users.noreply.github.com> Date: Thu, 31 Oct 2024 13:38:41 -0300 Subject: [PATCH] WIP Phoenix.ActionClauseError --- test/support/phoenix_app/endpoint.ex | 3 +- test/support/phoenix_app/error_html.ex | 4 +-- test/support/phoenix_app/home_controller.ex | 25 +++++++++++++++ test/support/phoenix_app/router.ex | 8 +++++ test/tower/phoenix_test.exs | 35 +++++++++++++++++++-- 5 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 test/support/phoenix_app/home_controller.ex create mode 100644 test/support/phoenix_app/router.ex diff --git a/test/support/phoenix_app/endpoint.ex b/test/support/phoenix_app/endpoint.ex index 450c157..6a7608a 100644 --- a/test/support/phoenix_app/endpoint.ex +++ b/test/support/phoenix_app/endpoint.ex @@ -1,5 +1,6 @@ defmodule Tower.PhoenixApp.Endpoint do use Phoenix.Endpoint, otp_app: :phoenix_app - plug(Tower.TestPlug) + plug Plug.Parsers, parsers: [] # So that it fetches query params + plug(Tower.PhoenixApp.Router) end diff --git a/test/support/phoenix_app/error_html.ex b/test/support/phoenix_app/error_html.ex index f20503f..b815a20 100644 --- a/test/support/phoenix_app/error_html.ex +++ b/test/support/phoenix_app/error_html.ex @@ -1,5 +1,5 @@ defmodule Tower.PhoenixApp.ErrorHTML do - def render("500.html", _assigns) do - "Internal Server Error" + def render(template, _assigns) do + Phoenix.Controller.status_message_from_template(template) end end diff --git a/test/support/phoenix_app/home_controller.ex b/test/support/phoenix_app/home_controller.ex new file mode 100644 index 0000000..691659f --- /dev/null +++ b/test/support/phoenix_app/home_controller.ex @@ -0,0 +1,25 @@ +defmodule Tower.PhoenixApp.HomeController do + use Phoenix.Controller, formats: [:html], put_default_views: false + + def show(conn, %{"param" => "valid"}) do + Plug.Conn.send_resp(conn, 200, "Hey!") + end + + def runtime_error(conn, _params) do + raise "an error" + + Plug.Conn.send_resp(conn, 200, "OK") + end + + def abnormal_exit(conn, _params) do + exit(:abnormal) + + Plug.Conn.send_resp(conn, 200, "OK") + end + + def uncaught_throw(conn, _params) do + throw("something") + + Plug.Conn.send_resp(conn, 200, "OK") + end +end diff --git a/test/support/phoenix_app/router.ex b/test/support/phoenix_app/router.ex new file mode 100644 index 0000000..795e102 --- /dev/null +++ b/test/support/phoenix_app/router.ex @@ -0,0 +1,8 @@ +defmodule Tower.PhoenixApp.Router do + use Phoenix.Router, helpers: false + + get("/runtime-error", Tower.PhoenixApp.HomeController, :runtime_error) + get("/abnormal-exit", Tower.PhoenixApp.HomeController, :abnormal_exit) + get("/uncaught-throw", Tower.PhoenixApp.HomeController, :uncaught_throw) + get("/show", Tower.PhoenixApp.HomeController, :show) +end diff --git a/test/tower/phoenix_test.exs b/test/tower/phoenix_test.exs index a691504..35ddc05 100644 --- a/test/tower/phoenix_test.exs +++ b/test/tower/phoenix_test.exs @@ -5,7 +5,7 @@ defmodule TowerPhoenixTest do import ExUnit.CaptureLog, only: [capture_log: 1] - setup do + setup context do on_exit(fn -> Tower.EphemeralReporter.reset() end) @@ -17,7 +17,11 @@ defmodule TowerPhoenixTest do Application.put_env( :phoenix_app, Tower.PhoenixApp.Endpoint, - adapter: Bandit.PhoenixAdapter, + adapter: + case context[:adapter] do + :bandit -> Bandit.PhoenixAdapter + :cowboy -> Phoenix.Endpoint.Cowboy2Adapter + end, server: true, http: [port: port], url: [scheme: "http", port: port, host: host], @@ -31,6 +35,7 @@ defmodule TowerPhoenixTest do %{base_url: "http://#{host}:#{port}"} end + @tag adapter: :bandit test "reports runtime error during Phoenix.Endpoint dispatch with Bandit", %{base_url: base_url} do url = base_url <> "/runtime-error" @@ -58,6 +63,7 @@ defmodule TowerPhoenixTest do assert Plug.Conn.request_url(plug_conn) == url end + @tag adapter: :bandit test "reports uncaught throw during Phoenix.Endpoint dispatch with Bandit", %{ base_url: base_url } do @@ -89,6 +95,7 @@ defmodule TowerPhoenixTest do assert [] = stacktrace end + @tag adapter: :bandit test "reports abnormal exit during Phoenix.Endpoint dispatch with Bandit", %{base_url: base_url} do capture_log(fn -> {:ok, {{_, 500, _}, _, _}} = :httpc.request(base_url <> "/abnormal-exit") @@ -114,6 +121,30 @@ defmodule TowerPhoenixTest do assert [_ | _] = stacktrace end + @tag adapter: :cowboy + test "doesn't report exceptions that return 4xx status codes with Cowboy", %{base_url: base_url} do + # Forcing Phoenix.ActionClauseError + url = base_url <> "/show?param=invalid" + + capture_log(fn -> + {:ok, {{_, 400, _}, _, _}} = :httpc.request(url) + end) + + assert [] = Tower.EphemeralReporter.events() + end + + @tag adapter: :bandit + test "doesn't report exceptions that return 4xx status codes with Bandit", %{base_url: base_url} do + # Forcing Phoenix.ActionClauseError + url = base_url <> "/show?param=invalid" + + capture_log(fn -> + {:ok, {{_, 400, _}, _, _}} = :httpc.request(url) + end) + + assert [] = Tower.EphemeralReporter.events() + end + defp recent_datetime?(datetime) do diff = :logger.timestamp()