Skip to content

Commit

Permalink
bandit uncaught throw
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy committed Aug 19, 2024
1 parent 6900aff commit e8a7669
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions test/plug/tower_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,36 @@ defmodule TowerPlugTest do
assert Plug.Conn.request_url(plug_conn) == url
end

@tag capture_log: true
test "reports uncaught throw during plug dispatch with Bandit" do
# 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"

start_link_supervised!({Bandit, plug: Tower.TestPlug, scheme: :http, port: plug_port})

{:error, :socket_closed_remotely} = :httpc.request(url)

assert_eventually(
[
%{
id: id,
datetime: datetime,
level: :error,
kind: :exit,
reason: {:bad_return_value, "something"},
stacktrace: stacktrace,
# Bandit doesn't handle uncaught throws so it doesn't provide the conn in the metadata
plug_conn: nil
}
] = Tower.EphemeralReporter.events()
)

assert String.length(id) == 36
assert recent_datetime?(datetime)
assert is_list(stacktrace)
end

@tag capture_log: true
test "reports abnormal exit during plug dispatch with Bandit" do
# An ephemeral port hopefully not being in the host running this code
Expand All @@ -140,7 +170,7 @@ defmodule TowerPlugTest do

start_link_supervised!({Bandit, plug: Tower.TestPlug, scheme: :http, port: plug_port})

:httpc.request(url)
{:error, :socket_closed_remotely} = :httpc.request(url)

assert_eventually(
[
Expand All @@ -150,17 +180,16 @@ defmodule TowerPlugTest do
level: :error,
kind: :exit,
reason: :abnormal,
stacktrace: stacktrace
stacktrace: stacktrace,
# Bandit doesn't handle exits so it doesn't provide the conn in the metadata
# plug_conn: %Plug.Conn{} = plug_conn
plug_conn: nil
}
] = Tower.EphemeralReporter.events()
)

assert String.length(id) == 36
assert recent_datetime?(datetime)
assert is_list(stacktrace)
# assert Plug.Conn.request_url(plug_conn) == url
end

test "reports message plug_conn manually" do
Expand Down

0 comments on commit e8a7669

Please sign in to comment.