Skip to content

Commit

Permalink
Add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 18, 2024
1 parent aa1f5d2 commit e68acc4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 9 deletions.
4 changes: 4 additions & 0 deletions fixtures/test_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require "action_cable"

class TestChannel < ActionCable::Channel::Base
end
7 changes: 6 additions & 1 deletion gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

source "https://rubygems.org"

gem "rails"
gemspec

group :maintenance, optional: true do
gem "bake-gem"
Expand All @@ -15,4 +15,9 @@
gem "covered"
gem "decode"
gem "rubocop"

gem "sus-fixtures-async-http"
gem "sus-fixtures-console"

gem "async-websocket"
end
9 changes: 6 additions & 3 deletions lib/async/cable/middleware.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'async/websocket/adapters/rack'
require "async/websocket/adapters/rack"
require "action_cable"

require_relative 'socket'
require_relative "socket"

module Async
module Cable
Expand All @@ -15,6 +16,8 @@ def initialize(app, server: ActionCable.server)
@protocols = ::ActionCable::INTERNAL[:protocols]
end

attr :server

def call(env)
if Async::WebSocket::Adapters::Rack.websocket?(env) and allow_request_origin?(env)
Async::WebSocket::Adapters::Rack.open(env, protocols: @protocols) do |websocket|
Expand All @@ -37,7 +40,7 @@ def handle_incoming_websocket(env, websocket)
@server.setup_heartbeat_timer

while message = websocket.read
Console.debug(self, "Received cable data:", message)
Console.debug(self, "Received cable data:", message.buffer)
connection.handle_incoming(@coder.decode(message.buffer))
end
rescue Protocol::WebSocket::ClosedError, EOFError
Expand Down
10 changes: 5 additions & 5 deletions lib/async/cable/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@

module Async::Cable
class Socket
#== Action Cable socket interface ==
attr_reader :env, :logger, :protocol

delegate :worker_pool, :logger, to: :@server

def initialize(env, websocket, server, coder: ActiveSupport::JSON)
@env = env
@websocket = websocket
Expand All @@ -17,8 +12,13 @@ def initialize(env, websocket, server, coder: ActiveSupport::JSON)
@write_guard = Mutex.new
end

attr :env
attr :output

def logger
Console
end

def request
# Copied from ActionCable::Server::Socket#request
@request ||= begin
Expand Down
60 changes: 60 additions & 0 deletions test/async/cable/middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require "async/cable/middleware"

require "protocol/rack/adapter"
require "async/websocket/client"
require "sus/fixtures/async/http/server_context"

require "test_channel"

describe Async::Cable::Middleware do
include Sus::Fixtures::Async::HTTP::ServerContext

let(:cable_server) {::ActionCable::Server::Base.new}

before do
cable_server.config.disable_request_forgery_protection = true
end

after do
@cable_server&.restart
end

let(:app) do
Protocol::Rack::Adapter.new(subject.new(nil, server: cable_server))
end

let(:connection) {Async::WebSocket::Client.connect(client_endpoint)}

it "can connect and receive welcome messages" do
welcome_message = connection.read.parse

expect(welcome_message).to have_keys(
type: be == "welcome"
)
ensure
connection.close
end

it "can connect and send broadcast messages" do
subscribe_message = ::Protocol::WebSocket::TextMessage.generate({
command: "subscribe",
identifier: JSON.dump({"channel" => "TestChannel"}),
})

subscribe_message.send(connection)

while message = connection.read
confirmation = message.parse

if confirmation[:type] == "confirm_subscription"
break
end
end

expect(confirmation).to have_keys(
identifier: be == JSON.dump({"channel" => "TestChannel"})
)
ensure
connection.close
end
end

0 comments on commit e68acc4

Please sign in to comment.