aboutsummaryrefslogtreecommitdiffstats
path: root/test/connection/subscriptions_test.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2015-07-13 11:30:43 -0500
committerPratik Naik <pratiknaik@gmail.com>2015-07-13 11:30:52 -0500
commit56891644d6a2ce2059f557d8cae44aca6cdfaf45 (patch)
treec78c9a2fe190193620fb9a7454c82ae21cd6e24f /test/connection/subscriptions_test.rb
parente1da6814b44110bc640a297bbca98bd87950b192 (diff)
downloadrails-56891644d6a2ce2059f557d8cae44aca6cdfaf45.tar.gz
rails-56891644d6a2ce2059f557d8cae44aca6cdfaf45.tar.bz2
rails-56891644d6a2ce2059f557d8cae44aca6cdfaf45.zip
Tests for channel subscriptions
Diffstat (limited to 'test/connection/subscriptions_test.rb')
-rw-r--r--test/connection/subscriptions_test.rb87
1 files changed, 87 insertions, 0 deletions
diff --git a/test/connection/subscriptions_test.rb b/test/connection/subscriptions_test.rb
new file mode 100644
index 0000000000..4e134b6420
--- /dev/null
+++ b/test/connection/subscriptions_test.rb
@@ -0,0 +1,87 @@
+require 'test_helper'
+
+class ActionCable::Connection::SubscriptionsTest < ActiveSupport::TestCase
+ class Connection < ActionCable::Connection::Base
+ attr_reader :websocket
+ 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
+ @server.stubs(:channel_classes).returns([ ChatChannel ])
+
+ env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket'
+ @connection = Connection.new(@server, env)
+
+ @subscriptions = ActionCable::Connection::Subscriptions.new(@connection)
+ @chat_identifier = { id: 1, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel' }.to_json
+ end
+
+ test "subscribe command" do
+ channel = subscribe_to_chat_channel
+
+ assert_kind_of ChatChannel, channel
+ assert_equal 1, channel.room.id
+ end
+
+ test "subscribe command without an identifier" do
+ @subscriptions.execute_command 'command' => 'subscribe'
+ assert @subscriptions.identifiers.empty?
+ end
+
+ test "unsubscribe command" do
+ subscribe_to_chat_channel
+
+ channel = subscribe_to_chat_channel
+ channel.expects(:unsubscribe_from_channel)
+
+ @subscriptions.execute_command 'command' => 'unsubscribe', 'identifier' => @chat_identifier
+ assert @subscriptions.identifiers.empty?
+ end
+
+ test "unsubscribe command without an identifier" do
+ @subscriptions.execute_command 'command' => 'unsubscribe'
+ assert @subscriptions.identifiers.empty?
+ end
+
+ test "message command" do
+ channel = subscribe_to_chat_channel
+
+ data = { 'content' => 'Hello World!', 'action' => 'speak' }
+ @subscriptions.execute_command 'command' => 'message', 'identifier' => @chat_identifier, 'data' => data.to_json
+
+ assert_equal [ data ], channel.lines
+ end
+
+ test "unsubscrib from all" do
+ channel1 = subscribe_to_chat_channel
+
+ channel2_id = { id: 2, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel' }.to_json
+ channel2 = subscribe_to_chat_channel(channel2_id)
+
+ channel1.expects(:unsubscribe_from_channel)
+ channel2.expects(:unsubscribe_from_channel)
+
+ @subscriptions.unsubscribe_from_all
+ end
+
+ private
+ def subscribe_to_chat_channel(identifier = @chat_identifier)
+ @subscriptions.execute_command 'command' => 'subscribe', 'identifier' => identifier
+ assert_equal identifier, @subscriptions.identifiers.last
+
+ @subscriptions.send :find, 'identifier' => identifier
+ end
+end