File: subscribe_spec.rb

package info (click to toggle)
ruby-faye 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,792 kB
  • sloc: javascript: 14,833; ruby: 5,068; makefile: 30
file content (247 lines) | stat: -rw-r--r-- 7,431 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
require "spec_helper"

describe "server subscribe" do
  let(:engine) { double "engine" }
  let(:server) { Faye::Server.new }

  before do
    Faye::Engine.stub(:get).and_return engine
  end

  describe :subscribe do
    let(:client_id) { "fakeclientid" }
    let(:message) { { "channel" => "/meta/subscribe",
                      "clientId" => "fakeclientid",
                      "subscription" => "/foo"
                  } }

    describe "with valid parameters" do
      before do
        engine.should_receive(:client_exists).with(client_id).and_yield true
      end

      it "subscribes the client to the channel" do
        engine.should_receive(:subscribe).with(client_id, "/foo")
        server.subscribe(message) {}
      end

      it "returns a successful response" do
        engine.stub(:subscribe)
        server.subscribe(message) do |response|
          response.should == {
            "channel"      => "/meta/subscribe",
            "successful"   => true,
            "clientId"     => client_id,
            "subscription" => "/foo"
          }
        end
      end

      describe "with a list of subscriptions" do
        before do
          message["subscription"] = ["/foo", "/bar"]
        end

        it "creates multiple subscriptions" do
          engine.should_receive(:subscribe).with(client_id, "/foo")
          engine.should_receive(:subscribe).with(client_id, "/bar")
          server.subscribe(message) {}
        end

        it "returns a successful response" do
          engine.stub(:subscribe)
          server.subscribe(message) do |response|
            response.should == {
              "channel"      => "/meta/subscribe",
              "successful"   => true,
              "clientId"     => client_id,
              "subscription" => ["/foo", "/bar"]
            }
          end
        end
      end

      describe "with a subscription pattern" do
        before do
          message["subscription"] = "/foo/**"
        end

        it "subscribes the client to the channel pattern" do
          engine.should_receive(:subscribe).with(client_id, "/foo/**")
          server.subscribe(message) {}
        end

        it "returns a successful response" do
          engine.stub(:subscribe)
          server.subscribe(message) do |response|
            response.should == {
              "channel"      => "/meta/subscribe",
              "successful"   => true,
              "clientId"     => client_id,
              "subscription" => "/foo/**"
            }
          end
        end
      end
    end

    describe "with an unknown client" do
      before do
        engine.should_receive(:client_exists).with(client_id).and_yield false
      end

      it "does not subscribe the client to the channel" do
        engine.should_not_receive(:subscribe)
        server.subscribe(message) {}
      end

      it "returns an unsuccessful response" do
        server.subscribe(message) do |response|
          response.should == {
            "channel"      => "/meta/subscribe",
            "successful"   => false,
            "error"        => "401:fakeclientid:Unknown client",
            "clientId"     => client_id,
            "subscription" => "/foo"
          }
        end
      end
    end

    describe "missing clientId" do
      before do
        message.delete("clientId")
        engine.should_receive(:client_exists).with(nil).and_yield false
      end

      it "does not subscribe the client to the channel" do
        engine.should_not_receive(:subscribe)
        server.subscribe(message) {}
      end

      it "returns an unsuccessful response" do
        server.subscribe(message) do |response|
          response.should == {
            "channel"      => "/meta/subscribe",
            "successful"   => false,
            "error"        => "402:clientId:Missing required parameter",
            "subscription" => "/foo"
          }
        end
      end
    end

    describe "missing subscription" do
      before do
        message.delete("subscription")
        engine.should_receive(:client_exists).with(client_id).and_yield true
      end

      it "does not subscribe the client to the channel" do
        engine.should_not_receive(:subscribe)
        server.subscribe(message) {}
      end

      it "returns an unsuccessful response" do
        server.subscribe(message) do |response|
          response.should == {
            "channel"      => "/meta/subscribe",
            "successful"   => false,
            "error"        => "402:subscription:Missing required parameter",
            "clientId"     => client_id,
            "subscription" => []
          }
        end
      end
    end

    describe "with an invalid channel" do
      before do
        message["subscription"] = "foo"
        engine.should_receive(:client_exists).with(client_id).and_yield true
      end

      it "does not subscribe the client to the channel" do
        engine.should_not_receive(:subscribe)
        server.subscribe(message) {}
      end

      it "returns an unsuccessful response" do
        server.subscribe(message) do |response|
          response.should == {
            "channel"      => "/meta/subscribe",
            "successful"   => false,
            "error"        => "405:foo:Invalid channel",
            "clientId"     => client_id,
            "subscription" => "foo"
          }
        end
      end
    end

    describe "with a /meta/* channel" do
      before do
        message["subscription"] = "/meta/foo"
        engine.should_receive(:client_exists).with(client_id).and_yield true
      end

      it "does not subscribe the client to the channel" do
        engine.should_not_receive(:subscribe)
        server.subscribe(message) {}
      end

      it "returns an unsuccessful response" do
        server.subscribe(message) do |response|
          response.should == {
            "channel"      => "/meta/subscribe",
            "successful"   => false,
            "error"        => "403:/meta/foo:Forbidden channel",
            "clientId"     => client_id,
            "subscription" => "/meta/foo"
          }
        end
      end

      it "subscribes local clients to the channel" do
        engine.should_receive(:subscribe).with(client_id, "/meta/foo")
        server.subscribe(message, true) {}
      end

      it "returns a successful response for local clients" do
        engine.stub(:subscribe)
        server.subscribe(message, true) do |response|
          response.should == {
            "channel"      => "/meta/subscribe",
            "successful"   => true,
            "clientId"     => client_id,
            "subscription" => "/meta/foo"
          }
        end
      end
    end

    describe "with an error" do
      before do
        message["error"] = "invalid"
        engine.should_receive(:client_exists).with(client_id).and_yield true
      end

      it "does not subscribe the client to the channel" do
        engine.should_not_receive(:subscribe)
        server.subscribe(message) {}
      end

      it "returns an unsuccessful response" do
        server.subscribe(message) do |response|
          response.should == {
            "channel"      => "/meta/subscribe",
            "successful"   => false,
            "error"        => "invalid",
            "clientId"     => client_id,
            "subscription" => "/foo"
          }
        end
      end
    end
  end
end