2016-08-06 13:15:15 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "stubs/test_server"
|
2016-03-20 16:59:02 -04:00
|
|
|
|
2016-04-15 06:33:21 -04:00
|
|
|
class ActionCable::Connection::ClientSocketTest < ActionCable::TestCase
|
2016-03-20 16:59:02 -04:00
|
|
|
class Connection < ActionCable::Connection::Base
|
2016-04-15 06:33:21 -04:00
|
|
|
attr_reader :connected, :websocket, :errors
|
2016-03-20 16:59:02 -04:00
|
|
|
|
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
@errors = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect
|
|
|
|
@connected = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def disconnect
|
|
|
|
@connected = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_async(method, *args)
|
|
|
|
send method, *args
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_error(message)
|
|
|
|
@errors << message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@server = TestServer.new
|
|
|
|
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
test "delegate socket errors to on_error handler" do
|
|
|
|
skip if ENV["FAYE"].present?
|
2016-03-20 16:59:02 -04:00
|
|
|
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
|
|
|
|
# Internal hax = :(
|
|
|
|
client = connection.websocket.send(:websocket)
|
2016-08-06 13:15:15 -04:00
|
|
|
client.instance_variable_get("@stream").expects(:write).raises("foo")
|
2016-03-20 16:59:02 -04:00
|
|
|
client.expects(:client_gone).never
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
client.write("boo")
|
2016-03-20 16:59:02 -04:00
|
|
|
assert_equal %w[ foo ], connection.errors
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
test "closes hijacked i/o socket at shutdown" do
|
|
|
|
skip if ENV["FAYE"].present?
|
2016-06-30 18:42:51 -04:00
|
|
|
|
|
|
|
run_in_eventmachine do
|
|
|
|
connection = open_connection
|
|
|
|
|
|
|
|
client = connection.websocket.send(:websocket)
|
2016-08-06 13:15:15 -04:00
|
|
|
client.instance_variable_get("@stream")
|
|
|
|
.instance_variable_get("@rack_hijack_io")
|
2016-06-30 18:42:51 -04:00
|
|
|
.expects(:close)
|
|
|
|
connection.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-20 16:59:02 -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"
|
|
|
|
env["rack.hijack"] = -> { env["rack.hijack_io"] = StringIO.new }
|
2016-03-20 16:59:02 -04:00
|
|
|
|
|
|
|
Connection.new(@server, env).tap do |connection|
|
|
|
|
connection.process
|
|
|
|
connection.send :handle_open
|
|
|
|
assert connection.connected
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|