Skip to content

Commit

Permalink
handle a reject in c:Phoenix.Socket.connect/3 (#37)
Browse files Browse the repository at this point in the history
* handle a reject in c:Phoenix.Socket.connect/3

* force MIX_ENV=docs when running mix docs

* detail fix in changelog
  • Loading branch information
the-mikedavis committed Jun 21, 2021
1 parent d20c601 commit 31fda03
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The format is based on [Keep a
Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.7.0 - UNRELEASED
## 0.7.0 - 2021-06-21

### Added

Expand Down Expand Up @@ -37,6 +37,17 @@ def handle_leave(_topic, socket)

Note that this callback has a default behavior of performing a no-op.

### Fixed

- Failures to connect (via an `:error` return from `c:Phoenix.Socket.connect/3`)
now correctly trigger a `Slipstream.Events.ChannelConnectFailed` event
- when using the synchronous API, this will result in an error tuple with
`Slipstream.await_connect/2` in the format of
`{:error, {:connect_failure, %{resp_headers: resp_headers, status_code: status_code}}}`
where `status_code` will be `403`.
- when using the module-based API, this will invoke the
`c:Slipstream.handle_disconnect/2` callback with the same error tuple

## 0.6.2 - 2021-03-01

### Fixed
Expand Down
19 changes: 19 additions & 0 deletions lib/slipstream/connection/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ defmodule Slipstream.Connection.Pipeline do
end
end

defp decode_message(
%{
raw_message:
{:gun_response, conn, stream_ref, :fin, status_code, resp_headers},
state: %{conn: conn, stream_ref: stream_ref}
} = p
) do
failure_info = %{
status_code: status_code,
resp_headers: resp_headers
}

event = %Events.ChannelConnectFailed{
reason: {:connect_failure, failure_info}
}

put_message(p, event(event))
end

# coveralls-ignore-start
defp decode_message(%{raw_message: unknown_message} = p) do
Logger.error(
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ defmodule Slipstream.MixProject do
inch: :dev,
bless: :test,
test: :test,
dialyzer: :test
dialyzer: :test,
docs: :docs
],
test_coverage: [tool: ExCoveralls],
package: package(),
Expand Down
15 changes: 15 additions & 0 deletions test/slipstream/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ defmodule Slipstream.IntegrationTest do
@client Slipstream.GoodExample
@server SlipstreamWeb.TestChannel

describe "given c:Phoenix.Socket.connect/3 returns :error" do
setup do
[config: [uri: "ws://localhost:4001/socket/websocket?reject=yes"]]
end

test "the socket is disconnected with :connect_failure reason", c do
import Slipstream

assert {:error, {:connect_failure, %{status_code: 403}}} =
c.config
|> connect!()
|> await_connect(15_000)
end
end

describe "given a connection has been established through the #{@client}" do
setup do
pid = start_supervised!({@client, self()})
Expand Down
4 changes: 4 additions & 0 deletions test/support/lib/slipstream_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ defmodule SlipstreamWeb.UserSocket do
# See `Phoenix.Token` documentation for examples in
# performing token verification on connect.
@impl true
def connect(%{"reject" => "yes"}, _socket, _connect_info) do
:error
end

def connect(_params, socket, _connect_info) do
{:ok, socket}
end
Expand Down

0 comments on commit 31fda03

Please sign in to comment.