2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
require "test_helper"
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase
|
2015-07-13 12:30:43 -04:00
|
|
|
class Connection < ActionCable::Connection::Base
|
|
|
|
attr_reader :websocket
|
2015-10-15 22:11:49 -04:00
|
|
|
|
|
|
|
def send_async(method, *args)
|
|
|
|
send method, *args
|
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class ChatChannel < ActionCable::Channel::Base
|
|
|
|
attr_reader :room, :lines
|
|
|
|
|
|
|
|
def subscribed
|
|
|
|
@room = Room.new params[:id]
|
|
|
|
@lines = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def speak(data)
|
|
|
|
@lines << data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@server = TestServer.new
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@chat_identifier = ActiveSupport::JSON.encode(id: 1, channel: "ActionCable::Connection::SubscriptionsTest::ChatChannel")
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "subscribe command" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
channel = subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
assert_kind_of ChatChannel, channel
|
|
|
|
assert_equal 1, channel.room.id
|
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "subscribe command without an identifier" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@subscriptions.execute_command "command" => "subscribe"
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty @subscriptions.identifiers
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unsubscribe command" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
channel = subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2018-04-22 12:33:40 -04:00
|
|
|
assert_called(channel, :unsubscribe_from_channel) do
|
|
|
|
@subscriptions.execute_command "command" => "unsubscribe", "identifier" => @chat_identifier
|
|
|
|
end
|
|
|
|
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty @subscriptions.identifiers
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "unsubscribe command without an identifier" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@subscriptions.execute_command "command" => "unsubscribe"
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty @subscriptions.identifiers
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "message command" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
|
|
|
channel = subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
data = { "content" => "Hello World!", "action" => "speak" }
|
|
|
|
@subscriptions.execute_command "command" => "message", "identifier" => @chat_identifier, "data" => ActiveSupport::JSON.encode(data)
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
assert_equal [ data ], channel.lines
|
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
2016-02-12 21:13:37 -05:00
|
|
|
test "unsubscribe from all" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
setup_connection
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
channel1 = subscribe_to_chat_channel
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
channel2_id = ActiveSupport::JSON.encode(id: 2, channel: "ActionCable::Connection::SubscriptionsTest::ChatChannel")
|
2015-10-15 22:11:49 -04:00
|
|
|
channel2 = subscribe_to_chat_channel(channel2_id)
|
2015-07-13 12:30:43 -04:00
|
|
|
|
2018-04-22 12:33:40 -04:00
|
|
|
assert_called(channel1, :unsubscribe_from_channel) do
|
|
|
|
assert_called(channel2, :unsubscribe_from_channel) do
|
|
|
|
@subscriptions.unsubscribe_from_all
|
|
|
|
end
|
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def subscribe_to_chat_channel(identifier = @chat_identifier)
|
2016-08-06 13:15:15 -04:00
|
|
|
@subscriptions.execute_command "command" => "subscribe", "identifier" => identifier
|
2015-07-13 12:30:43 -04:00
|
|
|
assert_equal identifier, @subscriptions.identifiers.last
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
@subscriptions.send :find, "identifier" => identifier
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
|
|
|
|
def setup_connection
|
2016-08-06 13:15:15 -04:00
|
|
|
env = Rack::MockRequest.env_for "/test", "HTTP_HOST" => "localhost", "HTTP_CONNECTION" => "upgrade", "HTTP_UPGRADE" => "websocket"
|
2015-10-15 22:11:49 -04:00
|
|
|
@connection = Connection.new(@server, env)
|
|
|
|
|
|
|
|
@subscriptions = ActionCable::Connection::Subscriptions.new(@connection)
|
|
|
|
end
|
2015-07-13 12:30:43 -04:00
|
|
|
end
|