Skip to content

Commit

Permalink
bandit
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Nov 27, 2024
1 parent 22f9c16 commit dd74d12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/support/test_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ defmodule Tower.TestPlug do
plug(:dispatch)

get "/runtime-error" do
Logger.metadata(user_id: 123, secret: "secret")

raise "an error"

send_resp(conn, 200, "OK")
end

get "/abnormal-exit" do
Logger.metadata(user_id: 123, secret: "secret")

exit(:abnormal)

send_resp(conn, 200, "OK")
end

get "/uncaught-throw" do
Logger.metadata(user_id: 123, secret: "secret")

throw("something")

send_resp(conn, 200, "OK")
Expand Down
25 changes: 25 additions & 0 deletions test/tower/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ defmodule TowerPlugTest do
end

test "reports runtime error during plug dispatch with Bandit" do
put_env(:logger_metadata, [:user_id])

# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/runtime-error"
Expand All @@ -128,6 +130,7 @@ defmodule TowerPlugTest do
kind: :error,
reason: %RuntimeError{message: "an error"},
stacktrace: stacktrace,
metadata: metadata,
plug_conn: %Plug.Conn{} = plug_conn,
by: Tower.LoggerHandler
}
Expand All @@ -137,10 +140,13 @@ defmodule TowerPlugTest do
assert String.length(id) == 36
assert recent_datetime?(datetime)
assert [_ | _] = stacktrace
assert metadata == %{user_id: 123}
assert Plug.Conn.request_url(plug_conn) == url
end

test "reports uncaught throw during plug dispatch with Bandit" do
put_env(:logger_metadata, [:user_id])

# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/uncaught-throw"
Expand All @@ -160,6 +166,7 @@ defmodule TowerPlugTest do
kind: :throw,
reason: "something",
stacktrace: stacktrace,
metadata: metadata,
plug_conn: %Plug.Conn{} = plug_conn,
by: Tower.LoggerHandler
}
Expand All @@ -169,10 +176,13 @@ defmodule TowerPlugTest do
assert String.length(id) == 36
assert recent_datetime?(datetime)
assert [_ | _] = stacktrace
assert metadata == %{user_id: 123}
assert Plug.Conn.request_url(plug_conn) == url
end

test "reports abnormal exit during plug dispatch with Bandit" do
put_env(:logger_metadata, [:user_id])

# An ephemeral port hopefully not being in the host running this code
plug_port = 51111
url = "http://127.0.0.1:#{plug_port}/abnormal-exit"
Expand All @@ -192,6 +202,7 @@ defmodule TowerPlugTest do
kind: :exit,
reason: :abnormal,
stacktrace: stacktrace,
metadata: metadata,
# Bandit doesn't handle exits so it doesn't provide the conn in the metadata
plug_conn: nil,
by: Tower.LoggerHandler
Expand All @@ -202,6 +213,7 @@ defmodule TowerPlugTest do
assert String.length(id) == 36
assert recent_datetime?(datetime)
assert [_ | _] = stacktrace
assert metadata == %{user_id: 123}
end

test "reports message plug_conn manually" do
Expand All @@ -224,6 +236,19 @@ defmodule TowerPlugTest do
)
end

defp put_env(key, value) do
original_value = Application.get_env(:tower, key)
Application.put_env(:tower, key, value)

on_exit(fn ->
if original_value == nil do
Application.delete_env(:tower, key)
else
Application.put_env(:tower, key, original_value)
end
end)
end

defp recent_datetime?(datetime) do
diff =
:logger.timestamp()
Expand Down

0 comments on commit dd74d12

Please sign in to comment.