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

78 lines
1.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
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
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
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
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
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
private
def open_connection(server = nil)
server ||= TestServer.new
2015-07-12 14:36:42 -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
@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