1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actioncable/test/connection/identifier_test.rb

76 lines
2 KiB
Ruby
Raw Normal View History

2015-07-12 14:36:42 -04:00
require 'test_helper'
require 'stubs/test_server'
require 'stubs/user'
2015-10-15 22:11:49 -04:00
class ActionCable::Connection::IdentifierTest < ActionCable::TestCase
2015-07-12 14:36:42 -04:00
class Connection < ActionCable::Connection::Base
identified_by :current_user
attr_reader :websocket
public :process_internal_message
def connect
self.current_user = User.new "lifo"
end
end
test "connection identifier" do
2015-10-15 22:11:49 -04:00
run_in_eventmachine do
open_connection_with_stubbed_pubsub
assert_equal "User#lifo", @connection.connection_identifier
end
2015-07-12 14:36:42 -04:00
end
2015-10-15 22:11:49 -04:00
test "should subscribe to internal channel on open and unsubscribe on close" do
run_in_eventmachine do
pubsub = mock('pubsub_adapter')
pubsub.expects(:subscribe).with('action_cable/User#lifo', kind_of(Proc))
pubsub.expects(:unsubscribe).with('action_cable/User#lifo', kind_of(Proc))
2015-07-12 14:36:42 -04:00
2015-10-15 22:11:49 -04:00
server = TestServer.new
server.stubs(:pubsub).returns(pubsub)
2015-07-12 14:36:42 -04:00
2015-10-15 22:11:49 -04:00
open_connection server: server
close_connection
end
2015-07-12 14:36:42 -04:00
end
test "processing disconnect message" do
2015-10-15 22:11:49 -04:00
run_in_eventmachine do
open_connection_with_stubbed_pubsub
2015-07-12 14:36:42 -04:00
2015-10-15 22:11:49 -04:00
@connection.websocket.expects(:close)
@connection.process_internal_message 'type' => 'disconnect'
2015-10-15 22:11:49 -04:00
end
2015-07-12 14:36:42 -04:00
end
test "processing invalid message" do
2015-10-15 22:11:49 -04:00
run_in_eventmachine do
open_connection_with_stubbed_pubsub
2015-07-12 14:36:42 -04:00
2015-10-15 22:11:49 -04:00
@connection.websocket.expects(:close).never
@connection.process_internal_message 'type' => 'unknown'
2015-10-15 22:11:49 -04:00
end
2015-07-12 14:36:42 -04:00
end
protected
def open_connection_with_stubbed_pubsub
2015-10-15 22:11:49 -04:00
server = TestServer.new
2016-01-15 17:11:30 -05:00
server.stubs(:adapter).returns(stub_everything('adapter'))
2015-10-15 22:11:49 -04:00
open_connection server: server
2015-07-12 14:36:42 -04:00
end
2015-10-15 22:11:49 -04:00
def open_connection(server:)
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)
2015-07-12 14:36:42 -04:00
@connection.process
@connection.send :handle_open
2015-07-12 14:36:42 -04:00
end
def close_connection
@connection.send :handle_close
2015-07-12 14:36:42 -04:00
end
end