Skip to content

Commit c241b17

Browse files
committed
Add suspended routes and handlers to error message
1 parent 6396d28 commit c241b17

File tree

5 files changed

+90
-42
lines changed

5 files changed

+90
-42
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.1.6 (2022-09-13)
2+
3+
- Added suspended routes and web socket handlers to error messages
4+
15
## v0.1.5 (2022-09-13)
26

37
- `TestServer.websocket_info/2` now takes the callback function as second argument

lib/test_server.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ defmodule TestServer do
8888

8989
active_routes ->
9090
raise """
91-
The test ended before the following #{inspect(Instance)} route(s) received a request:
91+
The test ended before the following #{inspect(Instance)} #{inspect(instance)} route(s) received a request:
9292
9393
#{Instance.format_routes(active_routes)}
9494
"""
@@ -105,7 +105,7 @@ defmodule TestServer do
105105

106106
active_websocket_handlers ->
107107
raise """
108-
The test ended before the following #{inspect(Instance)} websocket handler(s) received a message:
108+
The test ended before the following #{inspect(Instance)} #{inspect(instance)} websocket handler(s) received a message:
109109
110110
#{Instance.format_websocket_handlers(active_websocket_handlers)}
111111
"""

lib/test_server/plug_cowboy/handler.ex

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@ defmodule TestServer.Plug.Cowboy.Handler do
3737
handle_reply(result, socket)
3838

3939
{:error, :not_found} ->
40-
message = """
41-
Unexpected message received for WebSocket.
42-
43-
Frame:
44-
#{inspect(frame)}
45-
46-
#{format_active_websocket_handlers(socket)}
47-
"""
40+
message =
41+
"Unexpected message received for WebSocket"
42+
|> append_formatted_frame(frame)
43+
|> append_formatted_websocket_handlers(socket)
4844

4945
reply_with_error({socket, state}, {RuntimeError.exception(message), []})
5046

@@ -59,21 +55,49 @@ defmodule TestServer.Plug.Cowboy.Handler do
5955
{:reply, {:text, Exception.format(:error, exception, stacktrace)}, state}
6056
end
6157

62-
defp format_active_websocket_handlers({instance, route_ref}) do
63-
active_websocket_handlers =
64-
Enum.filter(
65-
Instance.websocket_handlers(instance),
66-
&(not &1.suspended and &1.route_ref == route_ref)
67-
)
58+
defp append_formatted_frame(message, frame) do
59+
"""
60+
#{message} with frame:
61+
62+
#{inspect(frame)}
63+
"""
64+
end
65+
66+
defp append_formatted_websocket_handlers(message, {instance, route_ref}) do
67+
websocket_handlers =
68+
Instance.websocket_handlers(instance)
69+
|> Enum.filter(&(&1.route_ref == route_ref))
70+
|> Enum.split_with(&(not &1.suspended))
6871

69-
format_active_websocket_handlers(active_websocket_handlers)
72+
"""
73+
#{message}
74+
75+
#{format_websocket_handlers(websocket_handlers, instance)}
76+
"""
77+
end
78+
79+
defp format_websocket_handlers({[], suspended_websocket_handlers}, instance) do
80+
message = "No active websocket handlers for #{inspect(Instance)} #{inspect(instance)}."
81+
82+
case suspended_websocket_handlers do
83+
[] ->
84+
message
85+
86+
websocket_handlers ->
87+
"""
88+
#{message} The following websocket handler(s) have been processed:
89+
90+
#{Instance.format_routes(websocket_handlers)}"
91+
"""
92+
end
7093
end
7194

72-
defp format_active_websocket_handlers([]),
73-
do: "\n\nNo active websocket handlers"
95+
defp format_websocket_handlers({active_websocket_handlers, _}, instance) do
96+
"""
97+
Active websocket handler(s) #{inspect(Instance)} #{inspect(instance)}:
7498
75-
defp format_active_websocket_handlers(active_websocket_handlers) do
76-
"\n\nActive websocket handler(s):\n\n#{Instance.format_websocket_handlers(active_websocket_handlers)}"
99+
#{Instance.format_websocket_handlers(active_websocket_handlers)}
100+
"""
77101
end
78102

79103
defp handle_reply({:reply, frame, state}, socket), do: {:reply, frame, {socket, state}}

lib/test_server/plug_cowboy/plug.ex

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ defmodule TestServer.Plug.Cowboy.Plug do
1919
{:error, {:not_found, conn}} ->
2020
message =
2121
"Unexpected #{conn.method} request received at #{conn.request_path}"
22-
|> append_params(conn)
23-
|> format_active_routes(instance)
22+
|> append_formatted_params(conn)
23+
|> append_formatted_routes(instance)
2424

2525
resp_error(conn, instance, {RuntimeError.exception(message), []})
2626

@@ -29,7 +29,7 @@ defmodule TestServer.Plug.Cowboy.Plug do
2929
end
3030
end
3131

32-
defp append_params(message, conn) do
32+
defp append_formatted_params(message, conn) do
3333
conn
3434
|> Map.take([:query_params, :body_params])
3535
|> Enum.filter(fn
@@ -43,18 +43,38 @@ defmodule TestServer.Plug.Cowboy.Plug do
4343
end
4444
end
4545

46-
defp format_active_routes(message, instance) do
47-
active_routes = Enum.reject(Instance.routes(instance), & &1.suspended)
46+
defp append_formatted_routes(message, instance) do
47+
routes = Enum.split_with(Instance.routes(instance), &(not &1.suspended))
4848

49-
format_active_routes(message, active_routes, instance)
49+
"""
50+
#{message}
51+
52+
#{format_routes(routes, instance)}
53+
"""
54+
end
55+
56+
defp format_routes({[], suspended_routes}, instance) do
57+
message = "No active routes for #{inspect(Instance)} #{inspect(instance)}."
58+
59+
case suspended_routes do
60+
[] ->
61+
message
62+
63+
suspended_routes ->
64+
"""
65+
#{message} The following route(s) have been processed:
66+
67+
#{Instance.format_routes(suspended_routes)}
68+
"""
69+
end
5070
end
5171

52-
defp format_active_routes(message, [], instance),
53-
do: message <> "\n\nNo active routes for #{inspect(Instance)} #{inspect(instance)}"
72+
defp format_routes({active_routes, _suspended_routes}, instance) do
73+
"""
74+
Active route(s) for #{inspect(Instance)} #{inspect(instance)}:
5475
55-
defp format_active_routes(message, active_routes, instance) do
56-
message <>
57-
"\n\nActive routes for #{inspect(Instance)} #{inspect(instance)}:\n\n#{Instance.format_routes(active_routes)}"
76+
#{Instance.format_routes(active_routes)}
77+
"""
5878
end
5979

6080
defp resp_error(conn, instance, {exception, stacktrace}) do

test/test_server_test.exs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ defmodule TestServerTest do
7070
TestServer.stop()
7171
end
7272

73-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<.*\> is not running/, fn ->
73+
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<[0-9.]+\> is not running/, fn ->
7474
{:ok, instance} = TestServer.start()
7575

7676
assert :ok = TestServer.stop()
@@ -131,7 +131,7 @@ defmodule TestServerTest do
131131
TestServer.url()
132132
end
133133

134-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<.*\> is not running/, fn ->
134+
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<[0-9.]+\> is not running/, fn ->
135135
{:ok, instance} = TestServer.start()
136136

137137
assert :ok = TestServer.stop()
@@ -172,7 +172,7 @@ defmodule TestServerTest do
172172

173173
describe "add/3" do
174174
test "when instance not running" do
175-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<.*\> is not running/, fn ->
175+
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<[0-9.]+\> is not running/, fn ->
176176
{:ok, instance} = TestServer.start()
177177

178178
assert :ok = TestServer.stop()
@@ -263,7 +263,7 @@ defmodule TestServerTest do
263263
end
264264

265265
assert capture_io(fn -> ExUnit.run() end) =~
266-
"The test ended before the following TestServer.Instance route(s) received a request"
266+
~r/The test ended before the following TestServer\.Instance \#PID\<[0-9.]+\> route\(s\) received a request/
267267
end
268268

269269
test "with callback plug" do
@@ -404,7 +404,7 @@ defmodule TestServerTest do
404404
TestServer.x509_suite()
405405
end
406406

407-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<.*\> is not running/, fn ->
407+
assert_raise RuntimeError, ~r/The TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
408408
{:ok, instance} = TestServer.start()
409409

410410
assert :ok = TestServer.stop()
@@ -434,7 +434,7 @@ defmodule TestServerTest do
434434

435435
TestServer.start(scheme: :https, cowboy_options: cowboy_options)
436436

437-
assert_raise RuntimeError, ~r/The TestServer.Instance is running with custom SSL/, fn ->
437+
assert_raise RuntimeError, ~r/The TestServer\.Instance is running with custom SSL/, fn ->
438438
TestServer.x509_suite()
439439
end
440440
end
@@ -445,7 +445,7 @@ defmodule TestServerTest do
445445
{:ok, instance} = TestServer.start()
446446
assert :ok = TestServer.stop()
447447

448-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<.*\> is not running/, fn ->
448+
assert_raise RuntimeError, ~r/The TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
449449
TestServer.websocket_init(instance, "/ws")
450450
end
451451
end
@@ -474,7 +474,7 @@ defmodule TestServerTest do
474474
assert {:ok, socket} = TestServer.websocket_init("/ws")
475475
assert :ok = TestServer.stop(instance)
476476

477-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<.*\> is not running/, fn ->
477+
assert_raise RuntimeError, ~r/The TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
478478
TestServer.websocket_handle(socket)
479479
end
480480
end
@@ -501,7 +501,7 @@ defmodule TestServerTest do
501501
end
502502

503503
assert capture_io(fn -> ExUnit.run() end) =~
504-
"The test ended before the following TestServer.Instance websocket handler(s) received a message"
504+
~r/The test ended before the following TestServer\.Instance \#PID\<[0-9.]+\> websocket handler\(s\) received a message/
505505
end
506506

507507
test "when receiving unexpected message" do
@@ -593,7 +593,7 @@ defmodule TestServerTest do
593593
assert {:ok, socket} = TestServer.websocket_init("/ws")
594594
assert :ok = TestServer.stop(instance)
595595

596-
assert_raise RuntimeError, ~r/The TestServer.Instance \#PID\<.*\> is not running/, fn ->
596+
assert_raise RuntimeError, ~r/The TestServer\.Instance \#PID\<[0-9.]+\> is not running/, fn ->
597597
TestServer.websocket_info(socket)
598598
end
599599
end

0 commit comments

Comments
 (0)