2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2018-05-13 12:00:54 -04:00
|
|
|
open_connection
|
2015-10-15 22:11:49 -04:00
|
|
|
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
|
|
|
|
server = TestServer.new
|
2015-07-12 14:36:42 -04:00
|
|
|
|
2018-05-13 12:00:54 -04:00
|
|
|
open_connection(server)
|
2015-10-15 22:11:49 -04:00
|
|
|
close_connection
|
2018-05-31 11:32:52 -04:00
|
|
|
wait_for_async
|
|
|
|
|
|
|
|
%w[subscribe unsubscribe].each do |method|
|
|
|
|
pubsub_call = server.pubsub.class.class_variable_get "@@#{method}_called"
|
|
|
|
|
|
|
|
assert_equal "action_cable/User#lifo", pubsub_call[:channel]
|
|
|
|
assert_instance_of Proc, pubsub_call[:callback]
|
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
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
|
2018-05-13 12:00:54 -04:00
|
|
|
open_connection
|
2015-07-12 14:36:42 -04:00
|
|
|
|
2018-04-22 12:33:40 -04:00
|
|
|
assert_called(@connection.websocket, :close) do
|
|
|
|
@connection.process_internal_message "type" => "disconnect"
|
|
|
|
end
|
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
|
2018-05-13 12:00:54 -04:00
|
|
|
open_connection
|
2015-07-12 14:36:42 -04:00
|
|
|
|
2018-04-22 07:31:43 -04:00
|
|
|
assert_not_called(@connection.websocket, :close) do
|
|
|
|
@connection.process_internal_message "type" => "unknown"
|
|
|
|
end
|
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
|
2018-05-13 12:00:54 -04:00
|
|
|
def open_connection(server = nil)
|
|
|
|
server ||= TestServer.new
|
2015-07-12 14:36:42 -04:00
|
|
|
|
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
|