Skip to content

Commit 76539bf

Browse files
committed
more robust phoenix endpoint/router testing
1 parent 7344588 commit 76539bf

File tree

1 file changed

+70
-18
lines changed

1 file changed

+70
-18
lines changed

test/plug_capture_test.exs

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,31 @@ defmodule Sentry.PlugCaptureTest do
22
use ExUnit.Case
33
use Plug.Test
44
import Sentry.TestEnvironmentHelper
5+
import ExUnit.CaptureLog
6+
7+
defmodule PhoenixController do
8+
use Phoenix.Controller
9+
def error(_conn, _params), do: raise("PhoenixError")
10+
end
11+
12+
defmodule PhoenixRouter do
13+
use Phoenix.Router
14+
15+
get "/error_route", PhoenixController, :error
16+
end
517

618
defmodule PhoenixEndpoint do
719
use Sentry.PlugCapture
820
use Phoenix.Endpoint, otp_app: :sentry
9-
plug(:error)
10-
plug(Sentry.ExamplePlugApplication)
1121

12-
def error(_conn, _opts) do
13-
raise "EndpointError"
14-
end
22+
plug Plug.Parsers,
23+
parsers: [:urlencoded, :multipart, :json],
24+
pass: ["*/*"],
25+
json_decoder: Jason
26+
27+
plug Sentry.PlugContext
28+
29+
plug PhoenixRouter
1530
end
1631

1732
test "sends error to Sentry" do
@@ -84,8 +99,8 @@ defmodule Sentry.PlugCaptureTest do
8499
Bypass.expect(bypass, fn conn ->
85100
{:ok, body, conn} = Plug.Conn.read_body(conn)
86101
json = Jason.decode!(body)
87-
assert json["culprit"] == "Sentry.PlugCaptureTest.PhoenixEndpoint.error/2"
88-
assert json["message"] == "(RuntimeError) EndpointError"
102+
assert json["culprit"] == "Sentry.PlugCaptureTest.PhoenixController.error/2"
103+
assert json["message"] == "(RuntimeError) PhoenixError"
89104
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
90105
end)
91106

@@ -96,13 +111,14 @@ defmodule Sentry.PlugCaptureTest do
96111
]
97112
)
98113

99-
modify_env(:phoenix, format_encoders: [])
100114
{:ok, _} = PhoenixEndpoint.start_link()
101115

102-
assert_raise RuntimeError, "EndpointError", fn ->
103-
conn(:get, "/")
104-
|> PhoenixEndpoint.call([])
105-
end
116+
capture_log(fn ->
117+
assert_raise RuntimeError, "PhoenixError", fn ->
118+
conn(:get, "/error_route")
119+
|> PhoenixEndpoint.call([])
120+
end
121+
end)
106122
end
107123

108124
test "can render feedback form in Phoenix ErrorView" do
@@ -118,17 +134,18 @@ defmodule Sentry.PlugCaptureTest do
118134
dsn: "http://public:secret@localhost:#{bypass.port}/1",
119135
"#{__MODULE__.PhoenixEndpoint}": [
120136
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
121-
],
122-
phoenix: [format_encoders: []]
137+
]
123138
)
124139

125140
{:ok, _} = PhoenixEndpoint.start_link()
126141

127-
conn = conn(:get, "/")
142+
conn = conn(:get, "/error_route")
128143

129-
assert_raise RuntimeError, "EndpointError", fn ->
130-
PhoenixEndpoint.call(conn, [])
131-
end
144+
capture_log(fn ->
145+
assert_raise RuntimeError, "PhoenixError", fn ->
146+
PhoenixEndpoint.call(conn, [])
147+
end
148+
end)
132149

133150
{event_id, _} = Sentry.last_event_id_and_source()
134151

@@ -139,6 +156,41 @@ defmodule Sentry.PlugCaptureTest do
139156
assert body =~ ~s{"title":"Testing"}
140157
end
141158

159+
test "does not send NoRouteError in Phoenix application" do
160+
bypass = Bypass.open()
161+
162+
Bypass.expect_once(bypass, fn conn ->
163+
{:ok, body, conn} = Plug.Conn.read_body(conn)
164+
_json = Jason.decode!(body)
165+
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
166+
end)
167+
168+
modify_env(:sentry,
169+
dsn: "http://public:secret@localhost:#{bypass.port}/1",
170+
"#{__MODULE__.PhoenixEndpoint}": [
171+
render_errors: [view: Sentry.ErrorView, accepts: ~w(html)]
172+
]
173+
)
174+
175+
{:ok, _} = PhoenixEndpoint.start_link()
176+
177+
capture_log(fn ->
178+
assert_raise RuntimeError, "PhoenixError", fn ->
179+
conn(:get, "/error_route")
180+
|> PhoenixEndpoint.call([])
181+
end
182+
183+
assert_raise(
184+
Phoenix.Router.NoRouteError,
185+
"no route found for GET /not_found (Sentry.PlugCaptureTest.PhoenixRouter)",
186+
fn ->
187+
conn(:get, "/not_found")
188+
|> PhoenixEndpoint.call([])
189+
end
190+
)
191+
end)
192+
end
193+
142194
test "can render feedback form in Plug application" do
143195
bypass = Bypass.open()
144196

0 commit comments

Comments
 (0)