aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test/connection/subscriptions_test.rb
blob: 7bc8c4241c53c5cfae894b8c199a6f8387eac3c9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# frozen_string_literal: true

require "test_helper"
require "active_support/testing/method_call_assertions"

class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase
  include ActiveSupport::Testing::MethodCallAssertions

  class Connection < ActionCable::Connection::Base
    attr_reader :websocket

    def send_async(method, *args)
      send method, *args
    end
  end

  class ChatChannel < ActionCable::Channel::Base
    attr_reader :room, :lines

    def subscribed
      @room = Room.new params[:id]
      @lines = []
    end

    def speak(data)
      @lines << data
    end
  end

  setup do
    @server = TestServer.new

    @chat_identifier = ActiveSupport::JSON.encode(id: 1, channel: "ActionCable::Connection::SubscriptionsTest::ChatChannel")
  end

  test "subscribe command" do
    run_in_eventmachine do
      setup_connection
      channel = subscribe_to_chat_channel

      assert_kind_of ChatChannel, channel
      assert_equal 1, channel.room.id
    end
  end

  test "subscribe command without an identifier" do
    run_in_eventmachine do
      setup_connection

      @subscriptions.execute_command "command" => "subscribe"
      assert_empty @subscriptions.identifiers
    end
  end

  test "unsubscribe command" do
    run_in_eventmachine do
      setup_connection
      subscribe_to_chat_channel

      channel = subscribe_to_chat_channel

      assert_called(channel, :unsubscribe_from_channel) do
        @subscriptions.execute_command "command" => "unsubscribe", "identifier" => @chat_identifier
      end

      assert_empty @subscriptions.identifiers
    end
  end

  test "unsubscribe command without an identifier" do
    run_in_eventmachine do
      setup_connection

      @subscriptions.execute_command "command" => "unsubscribe"
      assert_empty @subscriptions.identifiers
    end
  end

  test "message command" do
    run_in_eventmachine do
      setup_connection
      channel = subscribe_to_chat_channel

      data = { "content" => "Hello World!", "action" => "speak" }
      @subscriptions.execute_command "command" => "message", "identifier" => @chat_identifier, "data" => ActiveSupport::JSON.encode(data)

      assert_equal [ data ], channel.lines
    end
  end

  test "unsubscribe from all" do
    run_in_eventmachine do
      setup_connection

      channel1 = subscribe_to_chat_channel

      channel2_id = ActiveSupport::JSON.encode(id: 2, channel: "ActionCable::Connection::SubscriptionsTest::ChatChannel")
      channel2 = subscribe_to_chat_channel(channel2_id)

      assert_called(channel1, :unsubscribe_from_channel) do
        assert_called(channel2, :unsubscribe_from_channel) do
          @subscriptions.unsubscribe_from_all
        end
      end
    end
  end

  private
    def subscribe_to_chat_channel(identifier = @chat_identifier)
      @subscriptions.execute_command "command" => "subscribe", "identifier" => identifier
      assert_equal identifier, @subscriptions.identifiers.last

      @subscriptions.send :find, "identifier" => identifier
    end

    def setup_connection
      env = Rack::MockRequest.env_for "/test", "HTTP_HOST" => "localhost", "HTTP_CONNECTION" => "upgrade", "HTTP_UPGRADE" => "websocket"
      @connection = Connection.new(@server, env)

      @subscriptions = ActionCable::Connection::Subscriptions.new(@connection)
    end
end