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 "active_support/core_ext/object/json"
|
2015-07-12 11:07:31 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
class ActionCable::Connection::BaseTest < ActionCable::TestCase
|
2015-07-12 12:44:46 -04:00
|
|
|
class Connection < ActionCable::Connection::Base
|
2015-10-08 15:30:58 -04:00
|
|
|
attr_reader :websocket, :subscriptions, :message_buffer, :connected
|
2015-07-12 12:44:46 -04:00
|
|
|
|
|
|
|
def connect
|
|
|
|
@connected = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def disconnect
|
|
|
|
@connected = false
|
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
|
|
|
|
def send_async(method, *args)
|
|
|
|
send method, *args
|
|
|
|
end
|
2015-07-12 12:44:46 -04:00
|
|
|
end
|
|
|
|
|
2015-07-12 11:07:31 -04:00
|
|
|
setup do
|
|
|
|
@server = TestServer.new
|
2015-10-12 19:14:14 -04:00
|
|
|
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "making a connection with invalid headers" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
connection = ActionCable::Connection::Base.new(@server, Rack::MockRequest.env_for("/test"))
|
|
|
|
response = connection.process
|
|
|
|
assert_equal 404, response[0]
|
|
|
|
end
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|
2015-07-12 12:44:46 -04:00
|
|
|
|
|
|
|
test "websocket connection" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
connection.process
|
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate connection.websocket, :possible?
|
2016-01-27 23:55:31 -05:00
|
|
|
|
|
|
|
wait_for_async
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate connection.websocket, :alive?
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-12 12:44:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "rack response" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
response = connection.process
|
|
|
|
|
|
|
|
assert_equal [ -1, {}, [] ], response
|
|
|
|
end
|
2015-07-12 12:44:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "on connection open" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
|
2018-04-22 16:46:29 -04:00
|
|
|
assert_called_with(connection.websocket, :transmit, [{ type: "welcome" }.to_json]) do
|
|
|
|
assert_called(connection.message_buffer, :process!) do
|
|
|
|
connection.process
|
|
|
|
wait_for_async
|
|
|
|
end
|
2018-04-22 12:33:40 -04:00
|
|
|
end
|
2016-01-27 23:55:31 -05:00
|
|
|
|
|
|
|
assert_equal [ connection ], @server.connections
|
|
|
|
assert connection.connected
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-12 12:44:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "on connection close" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
connection.process
|
|
|
|
|
2019-08-09 17:30:15 -04:00
|
|
|
# Set up the connection
|
2016-01-27 23:55:31 -05:00
|
|
|
connection.send :handle_open
|
2015-10-15 22:11:49 -04:00
|
|
|
assert connection.connected
|
2015-07-12 12:44:46 -04:00
|
|
|
|
2018-04-22 12:33:40 -04:00
|
|
|
assert_called(connection.subscriptions, :unsubscribe_from_all) do
|
|
|
|
connection.send :handle_close
|
|
|
|
end
|
2015-07-12 12:44:46 -04:00
|
|
|
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not connection.connected
|
2015-10-15 22:11:49 -04:00
|
|
|
assert_equal [], @server.connections
|
|
|
|
end
|
2015-07-12 12:44:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "connection statistics" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
connection.process
|
|
|
|
|
|
|
|
statistics = connection.statistics
|
2015-07-12 12:44:46 -04:00
|
|
|
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate statistics[:identifier], :blank?
|
2015-10-15 22:11:49 -04:00
|
|
|
assert_kind_of Time, statistics[:started_at]
|
|
|
|
assert_equal [], statistics[:subscriptions]
|
|
|
|
end
|
2015-07-12 12:44:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "explicitly closing a connection" do
|
2015-10-15 22:11:49 -04:00
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
connection.process
|
|
|
|
|
2018-04-22 12:33:40 -04:00
|
|
|
assert_called(connection.websocket, :close) do
|
2018-10-11 16:47:16 -04:00
|
|
|
connection.close(reason: "testing")
|
2018-04-22 12:33:40 -04:00
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
end
|
2015-07-12 12:44:46 -04:00
|
|
|
end
|
2015-10-15 22:11:49 -04:00
|
|
|
|
2016-02-24 21:48:59 -05:00
|
|
|
test "rejecting a connection causes a 404" do
|
|
|
|
run_in_eventmachine do
|
|
|
|
class CallMeMaybe
|
|
|
|
def call(*)
|
2016-08-06 13:15:15 -04:00
|
|
|
raise "Do not call me!"
|
2016-02-24 21:48:59 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
env = Rack::MockRequest.env_for(
|
|
|
|
"/test",
|
2016-08-06 13:44:11 -04:00
|
|
|
"HTTP_CONNECTION" => "upgrade", "HTTP_UPGRADE" => "websocket",
|
|
|
|
"HTTP_HOST" => "localhost", "HTTP_ORIGIN" => "http://rubyonrails.org", "rack.hijack" => CallMeMaybe.new
|
2016-02-24 21:48:59 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
connection = ActionCable::Connection::Base.new(@server, env)
|
|
|
|
response = connection.process
|
|
|
|
assert_equal 404, response[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
private
|
|
|
|
def open_connection
|
2016-08-06 13:15:15 -04:00
|
|
|
env = Rack::MockRequest.env_for "/test", "HTTP_CONNECTION" => "upgrade", "HTTP_UPGRADE" => "websocket",
|
|
|
|
"HTTP_HOST" => "localhost", "HTTP_ORIGIN" => "http://rubyonrails.com"
|
2015-10-15 22:11:49 -04:00
|
|
|
|
|
|
|
Connection.new(@server, env)
|
|
|
|
end
|
2015-07-12 11:07:31 -04:00
|
|
|
end
|