aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/test/subscription_adapter/base_test.rb
blob: 256dce673f9e878058aa35ae97b7fcf88e131783 (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
require 'test_helper'
require 'stubs/test_server'

class ActionCable::SubscriptionAdapter::BaseTest < ActionCable::TestCase
  ## TEST THAT ERRORS ARE RETURNED FOR INHERITORS THAT DON'T OVERRIDE METHODS

  class BrokenAdapter < ActionCable::SubscriptionAdapter::Base
  end

  setup do
    @server = TestServer.new
    @server.config.subscription_adapter = BrokenAdapter
    @server.config.allowed_request_origins = %w( http://rubyonrails.com )
  end

  test "#broadcast returns NotImplementedError by default" do
    assert_raises NotImplementedError do
      BrokenAdapter.new(@server).broadcast('channel', 'payload')
    end
  end

  test "#subscribe returns NotImplementedError by default" do
    callback = lambda { puts 'callback' }
    success_callback = lambda { puts 'success' }

    assert_raises NotImplementedError do
      BrokenAdapter.new(@server).subscribe('channel', callback, success_callback)
    end
  end

  test "#unsubscribe returns NotImplementedError by default" do
    callback = lambda { puts 'callback' }

    assert_raises NotImplementedError do
      BrokenAdapter.new(@server).unsubscribe('channel', callback)
    end
  end

  # TEST METHODS THAT ARE REQUIRED OF THE ADAPTER'S BACKEND STORAGE OBJECT

  test "#broadcast is implemented" do
    broadcast = SuccessAdapter.new(@server).broadcast('channel', 'payload')

    assert_respond_to(SuccessAdapter.new(@server), :broadcast)

    assert_nothing_raised do
      broadcast
    end
  end

  test "#subscribe is implemented" do
    callback = lambda { puts 'callback' }
    success_callback = lambda { puts 'success' }
    subscribe = SuccessAdapter.new(@server).subscribe('channel', callback, success_callback)

    assert_respond_to(SuccessAdapter.new(@server), :subscribe)

    assert_nothing_raised do
      subscribe
    end
  end

  test "#unsubscribe is implemented" do
    callback = lambda { puts 'callback' }
    unsubscribe = SuccessAdapter.new(@server).unsubscribe('channel', callback)

    assert_respond_to(SuccessAdapter.new(@server), :unsubscribe)

    assert_nothing_raised do
      unsubscribe
    end
  end
end