summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2020-09-10 18:40:54 +0000
committerrinpatch <rinpatch@sdf.org>2020-09-17 19:50:51 +0300
commit608017b7df5916b607e707627dc09d89599129ff (patch)
tree71cbb9862a06fb9969bf9e51e2e13fc0b5e0f323
parent0465bdbd49ad729e9aec0b3f330607386f433d5d (diff)
Merge branch 'fix/streaming-termination-errors' into 'develop'
Fix two pseudo-errors in websocket handler Closes #2131 See merge request pleroma/pleroma!2982
-rw-r--r--lib/pleroma/web/mastodon_api/websocket_handler.ex12
-rw-r--r--test/integration/mastodon_websocket_test.exs26
2 files changed, 22 insertions, 16 deletions
diff --git a/lib/pleroma/web/mastodon_api/websocket_handler.ex b/lib/pleroma/web/mastodon_api/websocket_handler.ex
index 94e4595d8..cf923ded8 100644
--- a/lib/pleroma/web/mastodon_api/websocket_handler.ex
+++ b/lib/pleroma/web/mastodon_api/websocket_handler.ex
@@ -37,12 +37,12 @@ defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
else
{:error, :bad_topic} ->
Logger.debug("#{__MODULE__} bad topic #{inspect(req)}")
- {:ok, req} = :cowboy_req.reply(404, req)
+ req = :cowboy_req.reply(404, req)
{:ok, req, state}
{:error, :unauthorized} ->
Logger.debug("#{__MODULE__} authentication error: #{inspect(req)}")
- {:ok, req} = :cowboy_req.reply(401, req)
+ req = :cowboy_req.reply(401, req)
{:ok, req, state}
end
end
@@ -64,7 +64,9 @@ defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
{:ok, %{state | timer: timer()}}
end
- # We never receive messages.
+ # We only receive pings for now
+ def websocket_handle(:ping, state), do: {:ok, state}
+
def websocket_handle(frame, state) do
Logger.error("#{__MODULE__} received frame: #{inspect(frame)}")
{:ok, state}
@@ -98,6 +100,10 @@ defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
{:reply, :ping, %{state | timer: nil, count: 0}, :hibernate}
end
+ # State can be `[]` only in case we terminate before switching to websocket,
+ # we already log errors for these cases in `init/1`, so just do nothing here
+ def terminate(_reason, _req, []), do: :ok
+
def terminate(reason, _req, state) do
Logger.debug(
"#{__MODULE__} terminating websocket connection for user #{
diff --git a/test/integration/mastodon_websocket_test.exs b/test/integration/mastodon_websocket_test.exs
index ea17e9feb..76fbc8bda 100644
--- a/test/integration/mastodon_websocket_test.exs
+++ b/test/integration/mastodon_websocket_test.exs
@@ -99,30 +99,30 @@ defmodule Pleroma.Integration.MastodonWebsocketTest do
test "accepts the 'user' stream", %{token: token} = _state do
assert {:ok, _} = start_socket("?stream=user&access_token=#{token.token}")
- assert capture_log(fn ->
- assert {:error, {401, _}} = start_socket("?stream=user")
- Process.sleep(30)
- end) =~ ":badarg"
+ capture_log(fn ->
+ assert {:error, {401, _}} = start_socket("?stream=user")
+ Process.sleep(30)
+ end)
end
test "accepts the 'user:notification' stream", %{token: token} = _state do
assert {:ok, _} = start_socket("?stream=user:notification&access_token=#{token.token}")
- assert capture_log(fn ->
- assert {:error, {401, _}} = start_socket("?stream=user:notification")
- Process.sleep(30)
- end) =~ ":badarg"
+ capture_log(fn ->
+ assert {:error, {401, _}} = start_socket("?stream=user:notification")
+ Process.sleep(30)
+ end)
end
test "accepts valid token on Sec-WebSocket-Protocol header", %{token: token} do
assert {:ok, _} = start_socket("?stream=user", [{"Sec-WebSocket-Protocol", token.token}])
- assert capture_log(fn ->
- assert {:error, {401, _}} =
- start_socket("?stream=user", [{"Sec-WebSocket-Protocol", "I am a friend"}])
+ capture_log(fn ->
+ assert {:error, {401, _}} =
+ start_socket("?stream=user", [{"Sec-WebSocket-Protocol", "I am a friend"}])
- Process.sleep(30)
- end) =~ ":badarg"
+ Process.sleep(30)
+ end)
end
end
end