module ActionCable::Connection::TestCase::Behavior

def build_test_request(path, params: nil, headers: {}, session: {}, env: {})

def build_test_request(path, params: nil, headers: {}, session: {}, env: {})
  wrapped_headers = ActionDispatch::Http::Headers.from_hash(headers)
  uri = URI.parse(path)
  query_string = params.nil? ? uri.query : params.to_query
  request_env = {
    "QUERY_STRING" => query_string,
    "PATH_INFO" => uri.path
  }.merge(env)
  if wrapped_headers.present?
    ActionDispatch::Http::Headers.from_hash(request_env).merge!(wrapped_headers)
  end
  TestRequest.create(request_env).tap do |request|
    request.session = session.with_indifferent_access
    request.cookie_jar = cookies
  end
end

def connect(path = ActionCable.server.config.mount_path, **request_params)

- env – additional Rack env configuration (Hash)
- session – session data (Hash)
- headers – request headers (Hash)
- params – URL parameters (Hash)

Accepts request path as the first argument and the following request options:

Performs connection attempt to exert #connect on the connection under test.
def connect(path = ActionCable.server.config.mount_path, **request_params)
  path ||= DEFAULT_PATH
  connection = self.class.connection_class.allocate
  connection.singleton_class.include(TestConnection)
  connection.send(:initialize, build_test_request(path, **request_params))
  connection.connect if connection.respond_to?(:connect)
  # Only set instance variable if connected successfully
  @connection = connection
end

def cookies

def cookies
  @cookie_jar ||= TestCookieJar.new
end

def disconnect

Exert #disconnect on the connection under test.
def disconnect
  raise "Must be connected!" if connection.nil?
  connection.disconnect if connection.respond_to?(:disconnect)
  @connection = nil
end