2016-08-06 13:15:15 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "stubs/test_server"
|
|
|
|
require "stubs/user"
|
2015-07-12 14:36:42 -04:00
|
|
|
|
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
|
2016-08-06 13:15:15 -04:00
|
|
|
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
|
2016-01-16 10:33:50 -05:00
|
|
|
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)
|
2016-08-06 13:15:15 -04:00
|
|
|
@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
|
2016-08-06 13:15:15 -04:00
|
|
|
@connection.process_internal_message "type" => "unknown"
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-12 14:36:42 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 09:44:10 -05:00
|
|
|
private
|
2015-07-12 14:36:42 -04:00
|
|
|
def open_connection_with_stubbed_pubsub
|
2015-10-15 22:11:49 -04:00
|
|
|
server = TestServer.new
|
2016-08-06 13:15:15 -04: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:)
|
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)
|
|
|
|
|
2015-07-12 14:36:42 -04:00
|
|
|
@connection.process
|
2016-01-27 23:55:31 -05:00
|
|
|
@connection.send :handle_open
|
2015-07-12 14:36:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def close_connection
|
2016-01-27 23:55:31 -05:00
|
|
|
@connection.send :handle_close
|
2015-07-12 14:36:42 -04:00
|
|
|
end
|
|
|
|
end
|