diff options
Diffstat (limited to 'actioncable/test')
-rw-r--r-- | actioncable/test/channel/base_test.rb | 124 | ||||
-rw-r--r-- | actioncable/test/connection/authorization_test.rb | 7 | ||||
-rw-r--r-- | actioncable/test/connection/base_test.rb | 2 | ||||
-rw-r--r-- | actioncable/test/connection/client_socket_test.rb | 2 | ||||
-rw-r--r-- | actioncable/test/javascript/src/test.js | 1 | ||||
-rw-r--r-- | actioncable/test/javascript/src/test_helpers/consumer_test_helper.js | 4 | ||||
-rw-r--r-- | actioncable/test/javascript/src/test_helpers/index.js | 6 | ||||
-rw-r--r-- | actioncable/test/javascript/src/unit/action_cable_test.js | 16 | ||||
-rw-r--r-- | actioncable/test/javascript/src/unit/connection_test.js | 28 | ||||
-rw-r--r-- | actioncable/test/server/broadcasting_test.rb | 76 |
10 files changed, 146 insertions, 120 deletions
diff --git a/actioncable/test/channel/base_test.rb b/actioncable/test/channel/base_test.rb index eb0e1673b0..39b5879607 100644 --- a/actioncable/test/channel/base_test.rb +++ b/actioncable/test/channel/base_test.rb @@ -26,6 +26,9 @@ class ActionCable::Channel::BaseTest < ActionCable::TestCase after_subscribe :toggle_subscribed after_unsubscribe :toggle_subscribed + class SomeCustomError < StandardError; end + rescue_from SomeCustomError, with: :error_handler + def initialize(*) @subscribed = false super @@ -68,10 +71,18 @@ class ActionCable::Channel::BaseTest < ActionCable::TestCase @last_action = [ :receive ] end + def error_action + raise SomeCustomError + end + private def rm_rf @last_action = [ :rm_rf ] end + + def error_handler + @last_action = [ :error_action ] + end end setup do @@ -168,7 +179,7 @@ class ActionCable::Channel::BaseTest < ActionCable::TestCase end test "actions available on Channel" do - available_actions = %w(room last_action subscribed unsubscribed toggle_subscribed leave speak subscribed? get_latest receive chatters topic).to_set + available_actions = %w(room last_action subscribed unsubscribed toggle_subscribed leave speak subscribed? get_latest receive chatters topic error_action).to_set assert_equal available_actions, ChatChannel.action_methods end @@ -179,81 +190,78 @@ class ActionCable::Channel::BaseTest < ActionCable::TestCase end test "notification for perform_action" do - begin - events = [] - ActiveSupport::Notifications.subscribe "perform_action.action_cable" do |*args| - events << ActiveSupport::Notifications::Event.new(*args) - end + events = [] + ActiveSupport::Notifications.subscribe "perform_action.action_cable" do |*args| + events << ActiveSupport::Notifications::Event.new(*args) + end - data = { "action" => :speak, "content" => "hello" } - @channel.perform_action data + data = { "action" => :speak, "content" => "hello" } + @channel.perform_action data - assert_equal 1, events.length - assert_equal "perform_action.action_cable", events[0].name - assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class] - assert_equal :speak, events[0].payload[:action] - assert_equal data, events[0].payload[:data] - ensure - ActiveSupport::Notifications.unsubscribe "perform_action.action_cable" - end + assert_equal 1, events.length + assert_equal "perform_action.action_cable", events[0].name + assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class] + assert_equal :speak, events[0].payload[:action] + assert_equal data, events[0].payload[:data] + ensure + ActiveSupport::Notifications.unsubscribe "perform_action.action_cable" end test "notification for transmit" do - begin - events = [] - ActiveSupport::Notifications.subscribe "transmit.action_cable" do |*args| - events << ActiveSupport::Notifications::Event.new(*args) - end - - @channel.perform_action "action" => :get_latest - expected_data = { data: "latest" } - - assert_equal 1, events.length - assert_equal "transmit.action_cable", events[0].name - assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class] - assert_equal expected_data, events[0].payload[:data] - assert_nil events[0].payload[:via] - ensure - ActiveSupport::Notifications.unsubscribe "transmit.action_cable" + events = [] + ActiveSupport::Notifications.subscribe "transmit.action_cable" do |*args| + events << ActiveSupport::Notifications::Event.new(*args) end + + @channel.perform_action "action" => :get_latest + expected_data = { data: "latest" } + + assert_equal 1, events.length + assert_equal "transmit.action_cable", events[0].name + assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class] + assert_equal expected_data, events[0].payload[:data] + assert_nil events[0].payload[:via] + ensure + ActiveSupport::Notifications.unsubscribe "transmit.action_cable" end test "notification for transmit_subscription_confirmation" do - begin - @channel.subscribe_to_channel + @channel.subscribe_to_channel - events = [] - ActiveSupport::Notifications.subscribe "transmit_subscription_confirmation.action_cable" do |*args| - events << ActiveSupport::Notifications::Event.new(*args) - end + events = [] + ActiveSupport::Notifications.subscribe "transmit_subscription_confirmation.action_cable" do |*args| + events << ActiveSupport::Notifications::Event.new(*args) + end - @channel.stub(:subscription_confirmation_sent?, false) do - @channel.send(:transmit_subscription_confirmation) + @channel.stub(:subscription_confirmation_sent?, false) do + @channel.send(:transmit_subscription_confirmation) - assert_equal 1, events.length - assert_equal "transmit_subscription_confirmation.action_cable", events[0].name - assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class] - end - ensure - ActiveSupport::Notifications.unsubscribe "transmit_subscription_confirmation.action_cable" + assert_equal 1, events.length + assert_equal "transmit_subscription_confirmation.action_cable", events[0].name + assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class] end + ensure + ActiveSupport::Notifications.unsubscribe "transmit_subscription_confirmation.action_cable" end test "notification for transmit_subscription_rejection" do - begin - events = [] - ActiveSupport::Notifications.subscribe "transmit_subscription_rejection.action_cable" do |*args| - events << ActiveSupport::Notifications::Event.new(*args) - end + events = [] + ActiveSupport::Notifications.subscribe "transmit_subscription_rejection.action_cable" do |*args| + events << ActiveSupport::Notifications::Event.new(*args) + end - @channel.send(:transmit_subscription_rejection) + @channel.send(:transmit_subscription_rejection) - assert_equal 1, events.length - assert_equal "transmit_subscription_rejection.action_cable", events[0].name - assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class] - ensure - ActiveSupport::Notifications.unsubscribe "transmit_subscription_rejection.action_cable" - end + assert_equal 1, events.length + assert_equal "transmit_subscription_rejection.action_cable", events[0].name + assert_equal "ActionCable::Channel::BaseTest::ChatChannel", events[0].payload[:channel_class] + ensure + ActiveSupport::Notifications.unsubscribe "transmit_subscription_rejection.action_cable" + end + + test "behaves like rescuable" do + @channel.perform_action "action" => :error_action + assert_equal [ :error_action ], @channel.last_action end private diff --git a/actioncable/test/connection/authorization_test.rb b/actioncable/test/connection/authorization_test.rb index f77e543435..ac5c128135 100644 --- a/actioncable/test/connection/authorization_test.rb +++ b/actioncable/test/connection/authorization_test.rb @@ -25,8 +25,11 @@ class ActionCable::Connection::AuthorizationTest < ActionCable::TestCase "HTTP_HOST" => "localhost", "HTTP_ORIGIN" => "http://rubyonrails.com" connection = Connection.new(server, env) - assert_called(connection.websocket, :close) do - connection.process + + assert_called_with(connection.websocket, :transmit, [{ type: "disconnect", reason: "unauthorized", reconnect: false }.to_json]) do + assert_called(connection.websocket, :close) do + connection.process + end end end end diff --git a/actioncable/test/connection/base_test.rb b/actioncable/test/connection/base_test.rb index 6ffa0961bc..299879ad4c 100644 --- a/actioncable/test/connection/base_test.rb +++ b/actioncable/test/connection/base_test.rb @@ -108,7 +108,7 @@ class ActionCable::Connection::BaseTest < ActionCable::TestCase connection.process assert_called(connection.websocket, :close) do - connection.close + connection.close(reason: "testing") end end end diff --git a/actioncable/test/connection/client_socket_test.rb b/actioncable/test/connection/client_socket_test.rb index a7db32c3e4..1ab3c3b71d 100644 --- a/actioncable/test/connection/client_socket_test.rb +++ b/actioncable/test/connection/client_socket_test.rb @@ -58,7 +58,7 @@ class ActionCable::Connection::ClientSocketTest < ActionCable::TestCase client.instance_variable_get("@stream") .instance_variable_get("@rack_hijack_io") .define_singleton_method(:close) { event.set } - connection.close + connection.close(reason: "testing") event.wait end end diff --git a/actioncable/test/javascript/src/test.js b/actioncable/test/javascript/src/test.js index 1fca7ab4d3..eea1c0a408 100644 --- a/actioncable/test/javascript/src/test.js +++ b/actioncable/test/javascript/src/test.js @@ -1,5 +1,6 @@ import "./test_helpers/index" import "./unit/action_cable_test" +import "./unit/connection_test" import "./unit/consumer_test" import "./unit/subscription_test" import "./unit/subscriptions_test" diff --git a/actioncable/test/javascript/src/test_helpers/consumer_test_helper.js b/actioncable/test/javascript/src/test_helpers/consumer_test_helper.js index f1abea331b..d1dabc9fc4 100644 --- a/actioncable/test/javascript/src/test_helpers/consumer_test_helper.js +++ b/actioncable/test/javascript/src/test_helpers/consumer_test_helper.js @@ -1,5 +1,5 @@ import { WebSocket as MockWebSocket, Server as MockServer } from "mock-socket" -import ActionCable from "../../../../app/javascript/action_cable/index" +import * as ActionCable from "../../../../app/javascript/action_cable/index" import {defer, testURL} from "./index" export default function(name, options, callback) { @@ -14,7 +14,7 @@ export default function(name, options, callback) { return QUnit.test(name, function(assert) { const doneAsync = assert.async() - ActionCable.WebSocket = MockWebSocket + ActionCable.adapters.WebSocket = MockWebSocket const server = new MockServer(options.url) const consumer = ActionCable.createConsumer(options.url) diff --git a/actioncable/test/javascript/src/test_helpers/index.js b/actioncable/test/javascript/src/test_helpers/index.js index 5fa46c21ae..0cd4e260b3 100644 --- a/actioncable/test/javascript/src/test_helpers/index.js +++ b/actioncable/test/javascript/src/test_helpers/index.js @@ -1,4 +1,4 @@ -import ActionCable from "../../../../app/javascript/action_cable/index" +import * as ActionCable from "../../../../app/javascript/action_cable/index" export const testURL = "ws://cable.example.com/" @@ -6,5 +6,5 @@ export function defer(callback) { setTimeout(callback, 1) } -const originalWebSocket = ActionCable.WebSocket -QUnit.testDone(() => ActionCable.WebSocket = originalWebSocket) +const originalWebSocket = ActionCable.adapters.WebSocket +QUnit.testDone(() => ActionCable.adapters.WebSocket = originalWebSocket) diff --git a/actioncable/test/javascript/src/unit/action_cable_test.js b/actioncable/test/javascript/src/unit/action_cable_test.js index 8847d87545..daad900aca 100644 --- a/actioncable/test/javascript/src/unit/action_cable_test.js +++ b/actioncable/test/javascript/src/unit/action_cable_test.js @@ -1,4 +1,4 @@ -import ActionCable from "../../../../app/javascript/action_cable/index" +import * as ActionCable from "../../../../app/javascript/action_cable/index" import {testURL} from "../test_helpers/index" const {module, test} = QUnit @@ -7,23 +7,13 @@ module("ActionCable", () => { module("Adapters", () => { module("WebSocket", () => { test("default is window.WebSocket", assert => { - assert.equal(ActionCable.WebSocket, window.WebSocket) - }) - - test("configurable", assert => { - ActionCable.WebSocket = "" - assert.equal(ActionCable.WebSocket, "") + assert.equal(ActionCable.adapters.WebSocket, window.WebSocket) }) }) module("logger", () => { test("default is window.console", assert => { - assert.equal(ActionCable.logger, window.console) - }) - - test("configurable", assert => { - ActionCable.logger = "" - assert.equal(ActionCable.logger, "") + assert.equal(ActionCable.adapters.logger, window.console) }) }) }) diff --git a/actioncable/test/javascript/src/unit/connection_test.js b/actioncable/test/javascript/src/unit/connection_test.js new file mode 100644 index 0000000000..9b1a975bfb --- /dev/null +++ b/actioncable/test/javascript/src/unit/connection_test.js @@ -0,0 +1,28 @@ +import * as ActionCable from "../../../../app/javascript/action_cable/index" + +const {module, test} = QUnit + +module("ActionCable.Connection", () => { + module("#getState", () => { + test("uses the configured WebSocket adapter", assert => { + ActionCable.adapters.WebSocket = { foo: 1, BAR: "42" } + const connection = new ActionCable.Connection({}) + connection.webSocket = {} + connection.webSocket.readyState = 1 + assert.equal(connection.getState(), "foo") + connection.webSocket.readyState = "42" + assert.equal(connection.getState(), "bar") + }) + }) + + module("#open", () => { + test("uses the configured WebSocket adapter", assert => { + const FakeWebSocket = function() {} + ActionCable.adapters.WebSocket = FakeWebSocket + const connection = new ActionCable.Connection({}) + connection.monitor = { start() {} } + connection.open() + assert.equal(connection.webSocket instanceof FakeWebSocket, true) + }) + }) +}) diff --git a/actioncable/test/server/broadcasting_test.rb b/actioncable/test/server/broadcasting_test.rb index 03c900182a..860e79b821 100644 --- a/actioncable/test/server/broadcasting_test.rb +++ b/actioncable/test/server/broadcasting_test.rb @@ -13,50 +13,46 @@ class BroadcastingTest < ActionCable::TestCase end test "broadcast generates notification" do - begin - server = TestServer.new - - events = [] - ActiveSupport::Notifications.subscribe "broadcast.action_cable" do |*args| - events << ActiveSupport::Notifications::Event.new(*args) - end - - broadcasting = "test_queue" - message = { body: "test message" } - server.broadcast(broadcasting, message) - - assert_equal 1, events.length - assert_equal "broadcast.action_cable", events[0].name - assert_equal broadcasting, events[0].payload[:broadcasting] - assert_equal message, events[0].payload[:message] - assert_equal ActiveSupport::JSON, events[0].payload[:coder] - ensure - ActiveSupport::Notifications.unsubscribe "broadcast.action_cable" + server = TestServer.new + + events = [] + ActiveSupport::Notifications.subscribe "broadcast.action_cable" do |*args| + events << ActiveSupport::Notifications::Event.new(*args) end + + broadcasting = "test_queue" + message = { body: "test message" } + server.broadcast(broadcasting, message) + + assert_equal 1, events.length + assert_equal "broadcast.action_cable", events[0].name + assert_equal broadcasting, events[0].payload[:broadcasting] + assert_equal message, events[0].payload[:message] + assert_equal ActiveSupport::JSON, events[0].payload[:coder] + ensure + ActiveSupport::Notifications.unsubscribe "broadcast.action_cable" end test "broadcaster from broadcaster_for generates notification" do - begin - server = TestServer.new - - events = [] - ActiveSupport::Notifications.subscribe "broadcast.action_cable" do |*args| - events << ActiveSupport::Notifications::Event.new(*args) - end - - broadcasting = "test_queue" - message = { body: "test message" } - - broadcaster = server.broadcaster_for(broadcasting) - broadcaster.broadcast(message) - - assert_equal 1, events.length - assert_equal "broadcast.action_cable", events[0].name - assert_equal broadcasting, events[0].payload[:broadcasting] - assert_equal message, events[0].payload[:message] - assert_equal ActiveSupport::JSON, events[0].payload[:coder] - ensure - ActiveSupport::Notifications.unsubscribe "broadcast.action_cable" + server = TestServer.new + + events = [] + ActiveSupport::Notifications.subscribe "broadcast.action_cable" do |*args| + events << ActiveSupport::Notifications::Event.new(*args) end + + broadcasting = "test_queue" + message = { body: "test message" } + + broadcaster = server.broadcaster_for(broadcasting) + broadcaster.broadcast(message) + + assert_equal 1, events.length + assert_equal "broadcast.action_cable", events[0].name + assert_equal broadcasting, events[0].payload[:broadcasting] + assert_equal message, events[0].payload[:message] + assert_equal ActiveSupport::JSON, events[0].payload[:coder] + ensure + ActiveSupport::Notifications.unsubscribe "broadcast.action_cable" end end |