diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/channel/base_test.rb | 15 | ||||
-rw-r--r-- | test/channel/rejection_test.rb | 25 | ||||
-rw-r--r-- | test/channel/stream_test.rb | 70 | ||||
-rw-r--r-- | test/connection/authorization_test.rb | 26 | ||||
-rw-r--r-- | test/connection/base_test.rb | 113 | ||||
-rw-r--r-- | test/connection/cross_site_forgery_test.rb | 68 | ||||
-rw-r--r-- | test/connection/identifier_test.rb | 70 | ||||
-rw-r--r-- | test/connection/multiple_identifiers_test.rb | 41 | ||||
-rw-r--r-- | test/connection/string_identifier_test.rb | 44 | ||||
-rw-r--r-- | test/connection/subscriptions_test.rb | 85 | ||||
-rw-r--r-- | test/stubs/global_id.rb | 8 | ||||
-rw-r--r-- | test/stubs/room.rb | 2 | ||||
-rw-r--r-- | test/stubs/test_server.rb | 3 | ||||
-rw-r--r-- | test/stubs/user.rb | 6 | ||||
-rw-r--r-- | test/test_helper.rb | 26 | ||||
-rw-r--r-- | test/worker_test.rb | 3 |
16 files changed, 480 insertions, 125 deletions
diff --git a/test/channel/base_test.rb b/test/channel/base_test.rb index e7944ff06b..580338b44a 100644 --- a/test/channel/base_test.rb +++ b/test/channel/base_test.rb @@ -20,8 +20,13 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase class ChatChannel < BasicChannel attr_reader :room, :last_action - on_subscribe :toggle_subscribed - on_unsubscribe :toggle_subscribed + after_subscribe :toggle_subscribed + after_unsubscribe :toggle_subscribed + + def initialize(*) + @subscribed = false + super + end def subscribed @room = Room.new params[:id] @@ -134,4 +139,10 @@ class ActionCable::Channel::BaseTest < ActiveSupport::TestCase expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "message" => { "data" => "latest" } assert_equal expected, @connection.last_transmission end + + test "subscription confirmation" do + expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "type" => "confirm_subscription" + assert_equal expected, @connection.last_transmission + end + end diff --git a/test/channel/rejection_test.rb b/test/channel/rejection_test.rb new file mode 100644 index 0000000000..aa93396d44 --- /dev/null +++ b/test/channel/rejection_test.rb @@ -0,0 +1,25 @@ +require 'test_helper' +require 'stubs/test_connection' +require 'stubs/room' + +class ActionCable::Channel::RejectionTest < ActiveSupport::TestCase + class SecretChannel < ActionCable::Channel::Base + def subscribed + reject if params[:id] > 0 + end + end + + setup do + @user = User.new "lifo" + @connection = TestConnection.new(@user) + end + + test "subscription rejection" do + @connection.expects(:subscriptions).returns mock().tap { |m| m.expects(:remove_subscription).with instance_of(SecretChannel) } + @channel = SecretChannel.new @connection, "{id: 1}", { id: 1 } + + expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "type" => "reject_subscription" + assert_equal expected, @connection.last_transmission + end + +end diff --git a/test/channel/stream_test.rb b/test/channel/stream_test.rb index b0a6f49072..5e4e01abbf 100644 --- a/test/channel/stream_test.rb +++ b/test/channel/stream_test.rb @@ -2,7 +2,7 @@ require 'test_helper' require 'stubs/test_connection' require 'stubs/room' -class ActionCable::Channel::StreamTest < ActiveSupport::TestCase +class ActionCable::Channel::StreamTest < ActionCable::TestCase class ChatChannel < ActionCable::Channel::Base def subscribed if params[:id] @@ -10,23 +10,71 @@ class ActionCable::Channel::StreamTest < ActiveSupport::TestCase stream_from "test_room_#{@room.id}" end end - end - setup do - @connection = TestConnection.new + def send_confirmation + transmit_subscription_confirmation + end + end test "streaming start and stop" do - @connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("test_room_1") } - channel = ChatChannel.new @connection, "{id: 1}", { id: 1 } + run_in_eventmachine do + connection = TestConnection.new + connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("test_room_1").returns stub_everything(:pubsub) } + channel = ChatChannel.new connection, "{id: 1}", { id: 1 } - @connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe_proc) } - channel.unsubscribe_from_channel + connection.expects(:pubsub).returns mock().tap { |m| m.expects(:unsubscribe_proc) } + channel.unsubscribe_from_channel + end end test "stream_for" do - @connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("action_cable:channel:stream_test:chat:Room#1-Campfire") } - channel = ChatChannel.new @connection, "" - channel.stream_for Room.new(1) + run_in_eventmachine do + connection = TestConnection.new + EM.next_tick do + connection.expects(:pubsub).returns mock().tap { |m| m.expects(:subscribe).with("action_cable:channel:stream_test:chat:Room#1-Campfire").returns stub_everything(:pubsub) } + end + + channel = ChatChannel.new connection, "" + channel.stream_for Room.new(1) + end + end + + test "stream_from subscription confirmation" do + EM.run do + connection = TestConnection.new + connection.expects(:pubsub).returns EM::Hiredis.connect.pubsub + + channel = ChatChannel.new connection, "{id: 1}", { id: 1 } + assert_nil connection.last_transmission + + EM::Timer.new(0.1) do + expected = ActiveSupport::JSON.encode "identifier" => "{id: 1}", "type" => "confirm_subscription" + assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation within 0.1s" + + EM.run_deferred_callbacks + EM.stop + end + end end + + test "subscription confirmation should only be sent out once" do + EM.run do + connection = TestConnection.new + connection.stubs(:pubsub).returns EM::Hiredis.connect.pubsub + + channel = ChatChannel.new connection, "test_channel" + channel.send_confirmation + channel.send_confirmation + + EM.run_deferred_callbacks + + expected = ActiveSupport::JSON.encode "identifier" => "test_channel", "type" => "confirm_subscription" + assert_equal expected, connection.last_transmission, "Did not receive subscription confirmation" + + assert_equal 1, connection.transmissions.size + EM.stop + end + end + end diff --git a/test/connection/authorization_test.rb b/test/connection/authorization_test.rb index 09dfead8c8..68668b2835 100644 --- a/test/connection/authorization_test.rb +++ b/test/connection/authorization_test.rb @@ -1,26 +1,32 @@ require 'test_helper' require 'stubs/test_server' -class ActionCable::Connection::AuthorizationTest < ActiveSupport::TestCase +class ActionCable::Connection::AuthorizationTest < ActionCable::TestCase class Connection < ActionCable::Connection::Base attr_reader :websocket def connect reject_unauthorized_connection end - end - - setup do - @server = TestServer.new - env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' - @connection = Connection.new(@server, env) + def send_async(method, *args) + # Bypass Celluloid + send method, *args + end end test "unauthorized connection" do - @connection.websocket.expects(:close) + run_in_eventmachine do + server = TestServer.new + server.config.allowed_request_origins = %w( http://rubyonrails.com ) - @connection.process - @connection.send :on_open + env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket', + 'HTTP_ORIGIN' => 'http://rubyonrails.com' + + connection = Connection.new(server, env) + connection.websocket.expects(:close) + + connection.process + end end end diff --git a/test/connection/base_test.rb b/test/connection/base_test.rb index 2f008652ee..da6041db4a 100644 --- a/test/connection/base_test.rb +++ b/test/connection/base_test.rb @@ -1,9 +1,9 @@ require 'test_helper' require 'stubs/test_server' -class ActionCable::Connection::BaseTest < ActiveSupport::TestCase +class ActionCable::Connection::BaseTest < ActionCable::TestCase class Connection < ActionCable::Connection::Base - attr_reader :websocket, :heartbeat, :subscriptions, :message_buffer, :connected + attr_reader :websocket, :subscriptions, :message_buffer, :connected def connect @connected = true @@ -12,68 +12,107 @@ class ActionCable::Connection::BaseTest < ActiveSupport::TestCase def disconnect @connected = false end + + def send_async(method, *args) + # Bypass Celluloid + send method, *args + end end setup do @server = TestServer.new - - env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' - @connection = Connection.new(@server, env) - @response = @connection.process + @server.config.allowed_request_origins = %w( http://rubyonrails.com ) end test "making a connection with invalid headers" do - connection = ActionCable::Connection::Base.new(@server, Rack::MockRequest.env_for("/test")) - response = connection.process - assert_equal 404, response[0] + run_in_eventmachine do + connection = ActionCable::Connection::Base.new(@server, Rack::MockRequest.env_for("/test")) + response = connection.process + assert_equal 404, response[0] + end end test "websocket connection" do - assert @connection.websocket.possible? - assert @connection.websocket.alive? + run_in_eventmachine do + connection = open_connection + connection.process + + assert connection.websocket.possible? + assert connection.websocket.alive? + end end test "rack response" do - assert_equal [ -1, {}, [] ], @response + run_in_eventmachine do + connection = open_connection + response = connection.process + + assert_equal [ -1, {}, [] ], response + end end test "on connection open" do - assert ! @connection.connected - - EventMachine.expects(:add_periodic_timer) - @connection.websocket.expects(:transmit).with(regexp_matches(/\_ping/)) - @connection.message_buffer.expects(:process!) - - @connection.send :on_open - - assert_equal [ @connection ], @server.connections - assert @connection.connected + run_in_eventmachine do + connection = open_connection + connection.process + + connection.websocket.expects(:transmit).with(regexp_matches(/\_ping/)) + connection.message_buffer.expects(:process!) + + # Allow EM to run on_open callback + EM.next_tick do + assert_equal [ connection ], @server.connections + assert connection.connected + end + end end test "on connection close" do - # Setup the connection - EventMachine.stubs(:add_periodic_timer).returns(true) - @connection.send :on_open - assert @connection.connected + run_in_eventmachine do + connection = open_connection + connection.process + + # Setup the connection + EventMachine.stubs(:add_periodic_timer).returns(true) + connection.send :on_open + assert connection.connected - EventMachine.expects(:cancel_timer) - @connection.subscriptions.expects(:unsubscribe_from_all) - @connection.send :on_close + connection.subscriptions.expects(:unsubscribe_from_all) + connection.send :on_close - assert ! @connection.connected - assert_equal [], @server.connections + assert ! connection.connected + assert_equal [], @server.connections + end end test "connection statistics" do - statistics = @connection.statistics + run_in_eventmachine do + connection = open_connection + connection.process - assert statistics[:identifier].blank? - assert_kind_of Time, statistics[:started_at] - assert_equal [], statistics[:subscriptions] + statistics = connection.statistics + + assert statistics[:identifier].blank? + assert_kind_of Time, statistics[:started_at] + assert_equal [], statistics[:subscriptions] + end end test "explicitly closing a connection" do - @connection.websocket.expects(:close) - @connection.close + run_in_eventmachine do + connection = open_connection + connection.process + + connection.websocket.expects(:close) + connection.close + end end + + private + def open_connection + env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket', + 'HTTP_ORIGIN' => 'http://rubyonrails.com' + + Connection.new(@server, env) + end end diff --git a/test/connection/cross_site_forgery_test.rb b/test/connection/cross_site_forgery_test.rb new file mode 100644 index 0000000000..166abb7b38 --- /dev/null +++ b/test/connection/cross_site_forgery_test.rb @@ -0,0 +1,68 @@ +require 'test_helper' +require 'stubs/test_server' + +class ActionCable::Connection::CrossSiteForgeryTest < ActionCable::TestCase + HOST = 'rubyonrails.com' + + class Connection < ActionCable::Connection::Base + def send_async(method, *args) + # Bypass Celluloid + send method, *args + end + end + + setup do + @server = TestServer.new + @server.config.allowed_request_origins = %w( http://rubyonrails.com ) + end + + teardown do + @server.config.disable_request_forgery_protection = false + @server.config.allowed_request_origins = [] + end + + test "disable forgery protection" do + @server.config.disable_request_forgery_protection = true + assert_origin_allowed 'http://rubyonrails.com' + assert_origin_allowed 'http://hax.com' + end + + test "explicitly specified a single allowed origin" do + @server.config.allowed_request_origins = 'http://hax.com' + assert_origin_not_allowed 'http://rubyonrails.com' + assert_origin_allowed 'http://hax.com' + end + + test "explicitly specified multiple allowed origins" do + @server.config.allowed_request_origins = %w( http://rubyonrails.com http://www.rubyonrails.com ) + assert_origin_allowed 'http://rubyonrails.com' + assert_origin_allowed 'http://www.rubyonrails.com' + assert_origin_not_allowed 'http://hax.com' + end + + private + def assert_origin_allowed(origin) + response = connect_with_origin origin + assert_equal -1, response[0] + end + + def assert_origin_not_allowed(origin) + response = connect_with_origin origin + assert_equal 404, response[0] + end + + def connect_with_origin(origin) + response = nil + + run_in_eventmachine do + response = Connection.new(@server, env_for_origin(origin)).process + end + + response + end + + def env_for_origin(origin) + Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket', 'SERVER_NAME' => HOST, + 'HTTP_ORIGIN' => origin + end +end diff --git a/test/connection/identifier_test.rb b/test/connection/identifier_test.rb index 745cf308d0..02e6b21845 100644 --- a/test/connection/identifier_test.rb +++ b/test/connection/identifier_test.rb @@ -2,7 +2,7 @@ require 'test_helper' require 'stubs/test_server' require 'stubs/user' -class ActionCable::Connection::IdentifierTest < ActiveSupport::TestCase +class ActionCable::Connection::IdentifierTest < ActionCable::TestCase class Connection < ActionCable::Connection::Base identified_by :current_user attr_reader :websocket @@ -14,59 +14,59 @@ class ActionCable::Connection::IdentifierTest < ActiveSupport::TestCase end end - setup do - @server = TestServer.new - - env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' - @connection = Connection.new(@server, env) - end - test "connection identifier" do - open_connection_with_stubbed_pubsub - assert_equal "User#lifo", @connection.connection_identifier - end - - test "should subscribe to internal channel on open" do - pubsub = mock('pubsub') - pubsub.expects(:subscribe).with('action_cable/User#lifo') - @server.expects(:pubsub).returns(pubsub) - - open_connection + run_in_eventmachine do + open_connection_with_stubbed_pubsub + assert_equal "User#lifo", @connection.connection_identifier + end end - test "should unsubscribe from internal channel on close" do - open_connection_with_stubbed_pubsub + test "should subscribe to internal channel on open and unsubscribe on close" do + run_in_eventmachine do + pubsub = mock('pubsub') + pubsub.expects(:subscribe).with('action_cable/User#lifo') + pubsub.expects(:unsubscribe_proc).with('action_cable/User#lifo', kind_of(Proc)) - pubsub = mock('pubsub') - pubsub.expects(:unsubscribe_proc).with('action_cable/User#lifo', kind_of(Proc)) - @server.expects(:pubsub).returns(pubsub) + server = TestServer.new + server.stubs(:pubsub).returns(pubsub) - close_connection + open_connection server: server + close_connection + end end test "processing disconnect message" do - open_connection_with_stubbed_pubsub + run_in_eventmachine do + open_connection_with_stubbed_pubsub - @connection.websocket.expects(:close) - message = { 'type' => 'disconnect' }.to_json - @connection.process_internal_message message + @connection.websocket.expects(:close) + message = ActiveSupport::JSON.encode('type' => 'disconnect') + @connection.process_internal_message message + end end test "processing invalid message" do - open_connection_with_stubbed_pubsub + run_in_eventmachine do + open_connection_with_stubbed_pubsub - @connection.websocket.expects(:close).never - message = { 'type' => 'unknown' }.to_json - @connection.process_internal_message message + @connection.websocket.expects(:close).never + message = ActiveSupport::JSON.encode('type' => 'unknown') + @connection.process_internal_message message + end end protected def open_connection_with_stubbed_pubsub - @server.stubs(:pubsub).returns(stub_everything('pubsub')) - open_connection + server = TestServer.new + server.stubs(:pubsub).returns(stub_everything('pubsub')) + + open_connection server: server end - def open_connection + def open_connection(server:) + env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' + @connection = Connection.new(server, env) + @connection.process @connection.send :on_open end diff --git a/test/connection/multiple_identifiers_test.rb b/test/connection/multiple_identifiers_test.rb new file mode 100644 index 0000000000..55a9f96cb3 --- /dev/null +++ b/test/connection/multiple_identifiers_test.rb @@ -0,0 +1,41 @@ +require 'test_helper' +require 'stubs/test_server' +require 'stubs/user' + +class ActionCable::Connection::MultipleIdentifiersTest < ActionCable::TestCase + class Connection < ActionCable::Connection::Base + identified_by :current_user, :current_room + + def connect + self.current_user = User.new "lifo" + self.current_room = Room.new "my", "room" + end + end + + test "multiple connection identifiers" do + run_in_eventmachine do + open_connection_with_stubbed_pubsub + assert_equal "Room#my-room:User#lifo", @connection.connection_identifier + end + end + + protected + def open_connection_with_stubbed_pubsub + server = TestServer.new + server.stubs(:pubsub).returns(stub_everything('pubsub')) + + open_connection server: server + end + + def open_connection(server:) + env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' + @connection = Connection.new(server, env) + + @connection.process + @connection.send :on_open + end + + def close_connection + @connection.send :on_close + end +end diff --git a/test/connection/string_identifier_test.rb b/test/connection/string_identifier_test.rb new file mode 100644 index 0000000000..ab69df57b3 --- /dev/null +++ b/test/connection/string_identifier_test.rb @@ -0,0 +1,44 @@ +require 'test_helper' +require 'stubs/test_server' + +class ActionCable::Connection::StringIdentifierTest < ActionCable::TestCase + class Connection < ActionCable::Connection::Base + identified_by :current_token + + def connect + self.current_token = "random-string" + end + + def send_async(method, *args) + # Bypass Celluloid + send method, *args + end + end + + test "connection identifier" do + run_in_eventmachine do + open_connection_with_stubbed_pubsub + assert_equal "random-string", @connection.connection_identifier + end + end + + protected + def open_connection_with_stubbed_pubsub + @server = TestServer.new + @server.stubs(:pubsub).returns(stub_everything('pubsub')) + + open_connection + end + + def open_connection + env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' + @connection = Connection.new(@server, env) + + @connection.process + @connection.send :on_open + end + + def close_connection + @connection.send :on_close + end +end diff --git a/test/connection/subscriptions_test.rb b/test/connection/subscriptions_test.rb index 24fe8f9300..4f6760827e 100644 --- a/test/connection/subscriptions_test.rb +++ b/test/connection/subscriptions_test.rb @@ -1,8 +1,13 @@ require 'test_helper' -class ActionCable::Connection::SubscriptionsTest < ActiveSupport::TestCase +class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase class Connection < ActionCable::Connection::Base attr_reader :websocket + + def send_async(method, *args) + # Bypass Celluloid + send method, *args + end end class ChatChannel < ActionCable::Channel::Base @@ -22,59 +27,76 @@ class ActionCable::Connection::SubscriptionsTest < ActiveSupport::TestCase @server = TestServer.new @server.stubs(:channel_classes).returns(ChatChannel.name => 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 + @chat_identifier = ActiveSupport::JSON.encode(id: 1, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel') end test "subscribe command" do - channel = subscribe_to_chat_channel + run_in_eventmachine do + setup_connection + channel = subscribe_to_chat_channel - assert_kind_of ChatChannel, channel - assert_equal 1, channel.room.id + assert_kind_of ChatChannel, channel + assert_equal 1, channel.room.id + end end test "subscribe command without an identifier" do - @subscriptions.execute_command 'command' => 'subscribe' - assert @subscriptions.identifiers.empty? + run_in_eventmachine do + setup_connection + + @subscriptions.execute_command 'command' => 'subscribe' + assert @subscriptions.identifiers.empty? + end end test "unsubscribe command" do - subscribe_to_chat_channel + run_in_eventmachine do + setup_connection + subscribe_to_chat_channel - channel = subscribe_to_chat_channel - channel.expects(:unsubscribe_from_channel) + channel = subscribe_to_chat_channel + channel.expects(:unsubscribe_from_channel) - @subscriptions.execute_command 'command' => 'unsubscribe', 'identifier' => @chat_identifier - assert @subscriptions.identifiers.empty? + @subscriptions.execute_command 'command' => 'unsubscribe', 'identifier' => @chat_identifier + assert @subscriptions.identifiers.empty? + end end test "unsubscribe command without an identifier" do - @subscriptions.execute_command 'command' => 'unsubscribe' - assert @subscriptions.identifiers.empty? + run_in_eventmachine do + setup_connection + + @subscriptions.execute_command 'command' => 'unsubscribe' + assert @subscriptions.identifiers.empty? + end end test "message command" do - channel = subscribe_to_chat_channel + run_in_eventmachine do + setup_connection + channel = subscribe_to_chat_channel - data = { 'content' => 'Hello World!', 'action' => 'speak' } - @subscriptions.execute_command 'command' => 'message', 'identifier' => @chat_identifier, 'data' => data.to_json + data = { 'content' => 'Hello World!', 'action' => 'speak' } + @subscriptions.execute_command 'command' => 'message', 'identifier' => @chat_identifier, 'data' => ActiveSupport::JSON.encode(data) - assert_equal [ data ], channel.lines + assert_equal [ data ], channel.lines + end end test "unsubscrib from all" do - channel1 = subscribe_to_chat_channel + run_in_eventmachine do + setup_connection - channel2_id = { id: 2, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel' }.to_json - channel2 = subscribe_to_chat_channel(channel2_id) + channel1 = subscribe_to_chat_channel - channel1.expects(:unsubscribe_from_channel) - channel2.expects(:unsubscribe_from_channel) + channel2_id = ActiveSupport::JSON.encode(id: 2, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel') + channel2 = subscribe_to_chat_channel(channel2_id) - @subscriptions.unsubscribe_from_all + channel1.expects(:unsubscribe_from_channel) + channel2.expects(:unsubscribe_from_channel) + + @subscriptions.unsubscribe_from_all + end end private @@ -84,4 +106,11 @@ class ActionCable::Connection::SubscriptionsTest < ActiveSupport::TestCase @subscriptions.send :find, 'identifier' => identifier end + + def setup_connection + env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' + @connection = Connection.new(@server, env) + + @subscriptions = ActionCable::Connection::Subscriptions.new(@connection) + end end diff --git a/test/stubs/global_id.rb b/test/stubs/global_id.rb new file mode 100644 index 0000000000..334f0d03e8 --- /dev/null +++ b/test/stubs/global_id.rb @@ -0,0 +1,8 @@ +class GlobalID + attr_reader :uri + delegate :to_param, :to_s, to: :uri + + def initialize(gid, options = {}) + @uri = gid + end +end diff --git a/test/stubs/room.rb b/test/stubs/room.rb index 246d6a98af..cd66a0b687 100644 --- a/test/stubs/room.rb +++ b/test/stubs/room.rb @@ -7,7 +7,7 @@ class Room end def to_global_id - "Room##{id}-#{name}" + GlobalID.new("Room##{id}-#{name}") end def to_gid_param diff --git a/test/stubs/test_server.rb b/test/stubs/test_server.rb index 2a7ac3e927..f9168f9b78 100644 --- a/test/stubs/test_server.rb +++ b/test/stubs/test_server.rb @@ -9,4 +9,7 @@ class TestServer @logger = ActiveSupport::TaggedLogging.new ActiveSupport::Logger.new(StringIO.new) @config = OpenStruct.new(log_tags: []) end + + def send_async + end end diff --git a/test/stubs/user.rb b/test/stubs/user.rb index bce7dfc49e..a66b4f87d5 100644 --- a/test/stubs/user.rb +++ b/test/stubs/user.rb @@ -6,6 +6,10 @@ class User end def to_global_id - "User##{name}" + GlobalID.new("User##{name}") + end + + def to_gid_param + to_global_id.to_param end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 5640178f34..935e50e900 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,6 +8,7 @@ Bundler.setup Bundler.require :default, :test require 'puma' +require 'em-hiredis' require 'mocha/mini_test' require 'rack/mock' @@ -18,4 +19,29 @@ ActiveSupport.test_order = :sorted # Require all the stubs and models Dir[File.dirname(__FILE__) + '/stubs/*.rb'].each {|file| require file } +require 'celluloid' +$CELLULOID_DEBUG = false +$CELLULOID_TEST = false Celluloid.logger = Logger.new(StringIO.new) + +require 'faye/websocket' +class << Faye::WebSocket + remove_method :ensure_reactor_running + + # We don't want Faye to start the EM reactor in tests because it makes testing much harder. + # We want to be able to start and stop EM loop in tests to make things simpler. + def ensure_reactor_running + # no-op + end +end + +class ActionCable::TestCase < ActiveSupport::TestCase + def run_in_eventmachine + EM.run do + yield + + EM.run_deferred_callbacks + EM.stop + end + end +end diff --git a/test/worker_test.rb b/test/worker_test.rb index e1fa6f561b..69c4b6529d 100644 --- a/test/worker_test.rb +++ b/test/worker_test.rb @@ -11,6 +11,9 @@ class WorkerTest < ActiveSupport::TestCase def process(message) @last_action = [ :process, message ] end + + def connection + end end setup do |