pusher-fake 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pusher-fake.rb +4 -1
  3. data/lib/pusher-fake/channel.rb +9 -3
  4. data/lib/pusher-fake/channel/presence.rb +11 -9
  5. data/lib/pusher-fake/channel/private.rb +8 -5
  6. data/lib/pusher-fake/channel/public.rb +13 -14
  7. data/lib/pusher-fake/configuration.rb +2 -1
  8. data/lib/pusher-fake/connection.rb +41 -29
  9. data/lib/pusher-fake/cucumber.rb +2 -1
  10. data/lib/pusher-fake/server.rb +46 -45
  11. data/lib/pusher-fake/server/application.rb +89 -50
  12. data/lib/pusher-fake/support/base.rb +4 -9
  13. data/lib/pusher-fake/webhook.rb +3 -1
  14. data/spec/features/api/channels_spec.rb +7 -5
  15. data/spec/features/api/users_spec.rb +1 -1
  16. data/spec/features/client/event_spec.rb +6 -4
  17. data/spec/features/client/subscribe_spec.rb +8 -6
  18. data/spec/features/server/event_spec.rb +7 -7
  19. data/spec/features/server/webhooks_spec.rb +16 -4
  20. data/spec/lib/pusher-fake/channel/presence_spec.rb +57 -49
  21. data/spec/lib/pusher-fake/channel/private_spec.rb +42 -31
  22. data/spec/lib/pusher-fake/channel/public_spec.rb +37 -27
  23. data/spec/lib/pusher-fake/channel_spec.rb +51 -91
  24. data/spec/lib/pusher-fake/configuration_spec.rb +11 -5
  25. data/spec/lib/pusher-fake/connection_spec.rb +65 -39
  26. data/spec/lib/pusher-fake/server/application_spec.rb +219 -94
  27. data/spec/lib/pusher-fake/server_spec.rb +31 -41
  28. data/spec/lib/pusher-fake/webhook_spec.rb +29 -18
  29. data/spec/lib/pusher_fake_spec.rb +17 -15
  30. data/spec/support/application.rb +21 -19
  31. data/spec/support/application/public/javascripts/vendor/{pusher-3.1.0.js → pusher-3.2.1.js} +69 -48
  32. data/spec/support/application/views/index.erb +1 -1
  33. data/spec/support/capybara.rb +1 -3
  34. data/spec/support/helpers/connect.rb +1 -3
  35. data/spec/support/matchers/have_configuration_option.rb +2 -2
  36. data/spec/support/{pusher-fake.rb → pusher_fake.rb} +2 -1
  37. data/spec/support/webhooks.rb +5 -3
  38. metadata +38 -10
@@ -1,27 +1,33 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe PusherFake::Configuration do
4
- it { should have_configuration_option(:app_id).with_default("PUSHER_APP_ID") }
5
4
  it { should have_configuration_option(:key).with_default("PUSHER_API_KEY") }
6
5
  it { should have_configuration_option(:logger).with_default(STDOUT.to_io) }
7
- it { should have_configuration_option(:secret).with_default("PUSHER_API_SECRET") }
8
6
  it { should have_configuration_option(:verbose).with_default(false) }
9
7
  it { should have_configuration_option(:webhooks).with_default([]) }
10
8
 
11
- it "should have configuration option :socket_options" do
9
+ it do
10
+ should have_configuration_option(:app_id).with_default("PUSHER_APP_ID")
11
+ end
12
+
13
+ it do
14
+ should have_configuration_option(:secret).with_default("PUSHER_API_SECRET")
15
+ end
16
+
17
+ it "has configuration option :socket_options" do
12
18
  expect(subject.socket_options).to be_a(Hash)
13
19
  expect(subject.socket_options[:host]).to eq("127.0.0.1")
14
20
  expect(subject.socket_options[:port]).to be_a(Integer)
15
21
  end
16
22
 
17
- it "should have configuration option :web_options" do
23
+ it "has configuration option :web_options" do
18
24
  expect(subject.web_options).to be_a(Hash)
19
25
  expect(subject.web_options[:host]).to eq("127.0.0.1")
20
26
  expect(subject.web_options[:port]).to be_a(Integer)
21
27
  end
22
28
 
23
29
  it "defaults socket and web ports to different values" do
24
- expect(subject.socket_options[:port]).to_not eq(subject.web_options[:port])
30
+ expect(subject.socket_options[:port]).not_to eq(subject.web_options[:port])
25
31
  end
26
32
  end
27
33
 
@@ -1,10 +1,10 @@
1
1
  require "spec_helper"
2
2
 
3
3
  shared_examples_for "#process" do
4
- let(:json) { double }
5
-
6
4
  subject { PusherFake::Connection.new(double) }
7
5
 
6
+ let(:json) { double }
7
+
8
8
  before do
9
9
  allow(PusherFake).to receive(:log)
10
10
  allow(MultiJson).to receive(:load).and_return(message)
@@ -19,14 +19,15 @@ shared_examples_for "#process" do
19
19
  it "logs receiving the event" do
20
20
  subject.process(json)
21
21
 
22
- expect(PusherFake).to have_received(:log).with("RECV #{subject.id}: #{message}")
22
+ expect(PusherFake).to have_received(:log)
23
+ .with("RECV #{subject.id}: #{message}")
23
24
  end
24
25
  end
25
26
 
26
27
  describe PusherFake::Connection do
27
- let(:socket) { double }
28
+ subject { described_class }
28
29
 
29
- subject { PusherFake::Connection }
30
+ let(:socket) { double }
30
31
 
31
32
  it "assigns the provided socket" do
32
33
  connection = subject.new(socket)
@@ -36,15 +37,18 @@ describe PusherFake::Connection do
36
37
  end
37
38
 
38
39
  describe PusherFake::Connection, "#emit" do
40
+ subject { described_class.new(socket) }
41
+
39
42
  let(:data) { { some: "data", good: true } }
40
43
  let(:json) { MultiJson.dump(message) }
41
44
  let(:event) { "name" }
42
- let(:socket) { double(:socket, send: nil) }
43
45
  let(:channel) { "channel" }
44
46
  let(:message) { { event: event, data: MultiJson.dump(data) } }
45
47
  let(:channel_json) { MultiJson.dump(message.merge(channel: channel)) }
46
48
 
47
- subject { PusherFake::Connection.new(socket) }
49
+ let(:socket) do
50
+ instance_double(EventMachine::WebSocket::Connection, send: nil)
51
+ end
48
52
 
49
53
  before do
50
54
  allow(PusherFake).to receive(:log)
@@ -65,14 +69,15 @@ describe PusherFake::Connection, "#emit" do
65
69
  it "logs sending the event" do
66
70
  subject.emit(event, data)
67
71
 
68
- expect(PusherFake).to have_received(:log).with("SEND #{subject.id}: #{message}")
72
+ expect(PusherFake).to have_received(:log)
73
+ .with("SEND #{subject.id}: #{message}")
69
74
  end
70
75
  end
71
76
 
72
77
  describe PusherFake::Connection, "#establish" do
73
- let(:socket) { double }
78
+ subject { described_class.new(socket) }
74
79
 
75
- subject { PusherFake::Connection.new(socket) }
80
+ let(:socket) { double }
76
81
 
77
82
  before do
78
83
  allow(subject).to receive(:emit)
@@ -82,15 +87,16 @@ describe PusherFake::Connection, "#establish" do
82
87
  subject.establish
83
88
 
84
89
  expect(subject).to have_received(:emit)
85
- .with("pusher:connection_established", socket_id: subject.id, activity_timeout: 120)
90
+ .with("pusher:connection_established",
91
+ socket_id: subject.id, activity_timeout: 120)
86
92
  end
87
93
  end
88
94
 
89
95
  describe PusherFake::Connection, "#id" do
90
- let(:id) { "123.456" }
91
- let(:socket) { double(object_id: 123456) }
96
+ subject { described_class.new(socket) }
92
97
 
93
- subject { PusherFake::Connection.new(socket) }
98
+ let(:id) { "123.456" }
99
+ let(:socket) { instance_double(Object, object_id: 123_456) }
94
100
 
95
101
  it "returns the object ID of the socket" do
96
102
  expect(subject.id).to eq(id)
@@ -101,9 +107,12 @@ describe PusherFake::Connection, "#process, with a subscribe event" do
101
107
  it_should_behave_like "#process" do
102
108
  let(:data) { { channel: name, auth: "auth" } }
103
109
  let(:name) { "channel" }
104
- let(:channel) { double(:channel, add: nil) }
105
110
  let(:message) { { event: "pusher:subscribe", data: data } }
106
111
 
112
+ let(:channel) do
113
+ instance_double(PusherFake::Channel::Presence, add: nil)
114
+ end
115
+
107
116
  before do
108
117
  allow(PusherFake::Channel).to receive(:factory).and_return(channel)
109
118
  end
@@ -125,9 +134,12 @@ end
125
134
  describe PusherFake::Connection, "#process, with an unsubscribe event" do
126
135
  it_should_behave_like "#process" do
127
136
  let(:name) { "channel" }
128
- let(:channel) { double(:channel, remove: nil) }
129
137
  let(:message) { { event: "pusher:unsubscribe", channel: name } }
130
138
 
139
+ let(:channel) do
140
+ instance_double(PusherFake::Channel::Presence, remove: nil)
141
+ end
142
+
131
143
  before do
132
144
  allow(PusherFake::Channel).to receive(:factory).and_return(channel)
133
145
  end
@@ -158,7 +170,7 @@ describe PusherFake::Connection, "#process, with a ping event" do
158
170
  it "does not create a channel" do
159
171
  subject.process(json)
160
172
 
161
- expect(PusherFake::Channel).to_not have_received(:factory)
173
+ expect(PusherFake::Channel).not_to have_received(:factory)
162
174
  end
163
175
 
164
176
  it "emits a pong event" do
@@ -174,9 +186,13 @@ describe PusherFake::Connection, "#process, with a client event" do
174
186
  let(:data) { {} }
175
187
  let(:name) { "channel" }
176
188
  let(:event) { "client-hello-world" }
177
- let(:channel) { double(:channel, emit: nil, includes?: nil, is_a?: true) }
178
189
  let(:message) { { event: event, data: data, channel: name } }
179
190
 
191
+ let(:channel) do
192
+ instance_double(PusherFake::Channel::Private,
193
+ emit: nil, includes?: nil, is_a?: true)
194
+ end
195
+
180
196
  before do
181
197
  allow(subject).to receive(:trigger)
182
198
  allow(PusherFake::Channel).to receive(:factory).and_return(channel)
@@ -191,7 +207,8 @@ describe PusherFake::Connection, "#process, with a client event" do
191
207
  it "ensures the channel is private" do
192
208
  subject.process(json)
193
209
 
194
- expect(channel).to have_received(:is_a?).with(PusherFake::Channel::Private)
210
+ expect(channel).to have_received(:is_a?)
211
+ .with(PusherFake::Channel::Private)
195
212
  end
196
213
 
197
214
  it "checks if the connection is in the channel" do
@@ -200,48 +217,57 @@ describe PusherFake::Connection, "#process, with a client event" do
200
217
  expect(channel).to have_received(:includes?).with(subject)
201
218
  end
202
219
 
203
- it "emits the event to the channel when the connection is in the channel" do
220
+ it "emits the event when the connection is in the channel" do
204
221
  allow(channel).to receive(:includes?).and_return(true)
205
222
 
206
223
  subject.process(json)
207
224
 
208
- expect(channel).to have_received(:emit).with(event, data, socket_id: subject.id)
225
+ expect(channel).to have_received(:emit)
226
+ .with(event, data, socket_id: subject.id)
209
227
  end
210
228
 
211
- it "does not emit the event to the channel when the channel is not private" do
229
+ it "does not emit the event when the channel is not private" do
212
230
  allow(channel).to receive(:is_a?).and_return(false)
213
231
  allow(channel).to receive(:includes?).and_return(true)
214
232
 
215
233
  subject.process(json)
216
234
 
217
- expect(channel).to_not have_received(:emit)
235
+ expect(channel).not_to have_received(:emit)
218
236
  end
219
237
 
220
- it "does not emit the event to the channel when the connection is not in the channel" do
238
+ it "does not emit the event when the connection is not in the channel" do
221
239
  allow(channel).to receive(:includes?).and_return(false)
222
240
 
223
241
  subject.process(json)
224
242
 
225
- expect(channel).to_not have_received(:emit)
243
+ expect(channel).not_to have_received(:emit)
226
244
  end
227
245
  end
228
246
  end
229
247
 
230
- describe PusherFake::Connection, "#process, with a client event trigger a webhook" do
248
+ describe PusherFake::Connection,
249
+ "#process, with a client event trigger a webhook" do
231
250
  it_should_behave_like "#process" do
232
251
  let(:data) { { example: "data" } }
233
252
  let(:name) { "channel" }
234
253
  let(:event) { "client-hello-world" }
235
254
  let(:user_id) { 1 }
236
- let(:channel) { double(:channel, name: name, emit: nil, includes?: nil) }
255
+ let(:members) { { subject => { user_id: user_id } } }
237
256
  let(:message) { { event: event, channel: name } }
238
257
  let(:options) { { channel: name, event: event, socket_id: subject.id } }
239
258
 
259
+ let(:channel) do
260
+ instance_double(PusherFake::Channel::Presence,
261
+ name: name, emit: nil, includes?: nil)
262
+ end
263
+
240
264
  before do
241
265
  allow(channel).to receive(:trigger)
242
266
  allow(channel).to receive(:includes?).with(subject).and_return(true)
243
- allow(channel).to receive(:is_a?).with(PusherFake::Channel::Private).and_return(true)
244
- allow(channel).to receive(:is_a?).with(PusherFake::Channel::Presence).and_return(false)
267
+ allow(channel).to receive(:is_a?)
268
+ .with(PusherFake::Channel::Private).and_return(true)
269
+ allow(channel).to receive(:is_a?)
270
+ .with(PusherFake::Channel::Presence).and_return(false)
245
271
 
246
272
  # NOTE: Hack to avoid race condition in unit tests.
247
273
  allow(Thread).to receive(:new).and_yield
@@ -261,18 +287,18 @@ describe PusherFake::Connection, "#process, with a client event trigger a webhoo
261
287
 
262
288
  subject.process(json)
263
289
 
264
- expect(channel).to have_received(:trigger).
265
- with("client_event", options.merge(data: MultiJson.dump(data))).once
290
+ expect(channel).to have_received(:trigger)
291
+ .with("client_event", options.merge(data: MultiJson.dump(data))).once
266
292
  end
267
293
 
268
294
  it "includes user ID in event when on a presence channel" do
269
- allow(channel).to receive(:is_a?).with(PusherFake::Channel::Presence).and_return(true)
270
- allow(channel).to receive(:members).and_return({ subject => { user_id: user_id } })
295
+ allow(channel).to receive(:is_a?).and_return(true)
296
+ allow(channel).to receive(:members).and_return(members)
271
297
 
272
298
  subject.process(json)
273
299
 
274
- expect(channel).to have_received(:trigger).
275
- with("client_event", options.merge(user_id: user_id)).once
300
+ expect(channel).to have_received(:trigger)
301
+ .with("client_event", options.merge(user_id: user_id)).once
276
302
  end
277
303
  end
278
304
  end
@@ -282,23 +308,23 @@ describe PusherFake::Connection, "#process, with an unknown event" do
282
308
  let(:data) { {} }
283
309
  let(:name) { "channel" }
284
310
  let(:event) { "hello-world" }
285
- let(:channel) { double(:channel, emit: nil) }
311
+ let(:channel) { instance_double(PusherFake::Channel::Public, emit: nil) }
286
312
  let(:message) { { event: event, data: data, channel: name } }
287
313
 
288
314
  before do
289
315
  allow(PusherFake::Channel).to receive(:factory).and_return(channel)
290
316
  end
291
317
 
292
- it "creates a channel from the event data" do
318
+ it "does not create a channel" do
293
319
  subject.process(json)
294
320
 
295
- expect(PusherFake::Channel).to have_received(:factory).with(name)
321
+ expect(PusherFake::Channel).not_to have_received(:factory)
296
322
  end
297
323
 
298
324
  it "does not emit the event" do
299
325
  subject.process(json)
300
326
 
301
- expect(channel).to_not have_received(:emit)
327
+ expect(channel).not_to have_received(:emit)
302
328
  end
303
329
  end
304
330
  end
@@ -1,14 +1,14 @@
1
1
  require "spec_helper"
2
2
 
3
3
  shared_examples_for "an API request" do
4
+ subject { PusherFake::Server::Application }
5
+
4
6
  let(:hash) { double }
5
7
  let(:string) { double }
6
- let(:request) { double(:request, path: path) }
8
+ let(:request) { instance_double(Rack::Request, path: path) }
7
9
  let(:response) { double }
8
10
  let(:environment) { double }
9
11
 
10
- subject { PusherFake::Server::Application }
11
-
12
12
  before do
13
13
  allow(response).to receive(:finish).and_return(response)
14
14
 
@@ -65,7 +65,26 @@ describe PusherFake::Server::Application, ".call, for triggering events" do
65
65
  end
66
66
  end
67
67
 
68
- describe PusherFake::Server::Application, ".call, for retrieving occupied channels" do
68
+ describe PusherFake::Server::Application,
69
+ ".call, for triggering batch events" do
70
+ it_should_behave_like "an API request" do
71
+ let(:id) { PusherFake.configuration.app_id }
72
+ let(:path) { "/apps/#{id}/batch_events" }
73
+
74
+ before do
75
+ allow(subject).to receive(:batch_events).and_return(hash)
76
+ end
77
+
78
+ it "emits batch events" do
79
+ subject.call(environment)
80
+
81
+ expect(subject).to have_received(:batch_events).with(request)
82
+ end
83
+ end
84
+ end
85
+
86
+ describe PusherFake::Server::Application,
87
+ ".call, retrieving occupied channels" do
69
88
  it_should_behave_like "an API request" do
70
89
  let(:id) { PusherFake.configuration.app_id }
71
90
  let(:path) { "/apps/#{id}/channels" }
@@ -83,8 +102,10 @@ describe PusherFake::Server::Application, ".call, for retrieving occupied channe
83
102
  end
84
103
 
85
104
  describe PusherFake::Server::Application, ".call, with unknown path" do
105
+ subject { described_class }
106
+
86
107
  let(:path) { "/apps/fake/events" }
87
- let(:request) { double(:request, path: path) }
108
+ let(:request) { instance_double(Rack::Request, path: path) }
88
109
  let(:message) { "Unknown path: #{path}" }
89
110
  let(:response) { double }
90
111
  let(:environment) { double }
@@ -96,8 +117,6 @@ describe PusherFake::Server::Application, ".call, with unknown path" do
96
117
  allow(Rack::Response).to receive(:new).and_return(response)
97
118
  end
98
119
 
99
- subject { PusherFake::Server::Application }
100
-
101
120
  it "creates a request" do
102
121
  subject.call(environment)
103
122
 
@@ -124,15 +143,15 @@ describe PusherFake::Server::Application, ".call, with unknown path" do
124
143
  end
125
144
 
126
145
  describe PusherFake::Server::Application, ".call, raising an error" do
146
+ subject { described_class }
147
+
127
148
  let(:id) { PusherFake.configuration.app_id }
128
149
  let(:path) { "/apps/#{id}/channels" }
129
150
  let(:message) { "Example error message." }
130
- let(:request) { double(:request, path: path) }
151
+ let(:request) { instance_double(Rack::Request, path: path) }
131
152
  let(:response) { double }
132
153
  let(:environment) { double }
133
154
 
134
- subject { PusherFake::Server::Application }
135
-
136
155
  before do
137
156
  allow(subject).to receive(:channels).and_raise(message)
138
157
 
@@ -168,25 +187,35 @@ describe PusherFake::Server::Application, ".call, raising an error" do
168
187
  end
169
188
 
170
189
  describe PusherFake::Server::Application, ".events" do
171
- let(:body) { double(:body, read: event_json) }
190
+ subject { described_class }
191
+
192
+ let(:body) { instance_double(StringIO, read: event_json) }
172
193
  let(:data) { { "example" => "data" } }
173
194
  let(:name) { "event-name" }
174
- let(:event) { { "channels" => channels, "name" => name, "data" => data_json, "socket_id" => socket_id } }
175
- let(:request) { double(:request, body: body) }
195
+ let(:request) { instance_double(Rack::Request, body: body) }
176
196
  let(:channels) { ["channel-1", "channel-2"] }
177
- let(:channel_1) { double(:channel, emit: true) }
178
- let(:channel_2) { double(:channel, emit: true) }
197
+ let(:channel_1) { instance_double(PusherFake::Channel::Public, emit: true) }
198
+ let(:channel_2) { instance_double(PusherFake::Channel::Public, emit: true) }
179
199
  let(:data_json) { data.to_json }
180
200
  let(:socket_id) { double }
181
201
  let(:event_json) { double }
182
202
 
183
- subject { PusherFake::Server::Application }
203
+ let(:event) do
204
+ {
205
+ "channels" => channels,
206
+ "name" => name,
207
+ "data" => data_json,
208
+ "socket_id" => socket_id
209
+ }
210
+ end
184
211
 
185
212
  before do
186
213
  allow(MultiJson).to receive(:load).with(event_json).and_return(event)
187
214
  allow(MultiJson).to receive(:load).with(data_json).and_return(data)
188
- allow(PusherFake::Channel).to receive(:factory).with(channels[0]).and_return(channel_1)
189
- allow(PusherFake::Channel).to receive(:factory).with(channels[1]).and_return(channel_2)
215
+ allow(PusherFake::Channel).to receive(:factory)
216
+ .with(channels[0]).and_return(channel_1)
217
+ allow(PusherFake::Channel).to receive(:factory)
218
+ .with(channels[1]).and_return(channel_2)
190
219
  end
191
220
 
192
221
  it "parses the request body as JSON" do
@@ -204,14 +233,10 @@ describe PusherFake::Server::Application, ".events" do
204
233
  it "handles invalid JSON for event data" do
205
234
  event["data"] = data = "fake"
206
235
 
207
- allow(MultiJson).to receive(:load).with(data).and_raise(MultiJson::LoadError)
208
-
209
- expect {
210
- subject.events(request)
211
- }.to_not raise_error
236
+ allow(MultiJson).to receive(:load)
237
+ .with(data).and_raise(MultiJson::LoadError)
212
238
 
213
- expect(channel_1).to have_received(:emit).with(name, data, socket_id: socket_id)
214
- expect(channel_2).to have_received(:emit).with(name, data, socket_id: socket_id)
239
+ expect { subject.events(request) }.not_to raise_error
215
240
  end
216
241
 
217
242
  it "creates channels by name" do
@@ -225,16 +250,91 @@ describe PusherFake::Server::Application, ".events" do
225
250
  it "emits the event to the channels" do
226
251
  subject.events(request)
227
252
 
228
- expect(channel_1).to have_received(:emit).with(name, data, socket_id: socket_id)
229
- expect(channel_2).to have_received(:emit).with(name, data, socket_id: socket_id)
253
+ expect(channel_1).to have_received(:emit)
254
+ .with(name, data, socket_id: socket_id)
255
+ expect(channel_2).to have_received(:emit)
256
+ .with(name, data, socket_id: socket_id)
230
257
  end
231
258
  end
232
259
 
233
- describe PusherFake::Server::Application, ".channels, requesting all channels" do
234
- let(:request) { double(:request, params: {}) }
235
- let(:channels) { { "channel-1" => double, "channel-2" => double } }
260
+ describe PusherFake::Server::Application, ".batch_events" do
261
+ subject { described_class }
236
262
 
237
- subject { PusherFake::Server::Application }
263
+ let(:body) { instance_double(StringIO, read: event_json) }
264
+ let(:data) { { "example" => "data" } }
265
+ let(:name) { "event-name" }
266
+ let(:request) { instance_double(Rack::Request, body: body) }
267
+ let(:channels) { ["channel-1", "channel-2"] }
268
+ let(:channel_1) { instance_double(PusherFake::Channel::Public, emit: true) }
269
+ let(:channel_2) { instance_double(PusherFake::Channel::Public, emit: true) }
270
+ let(:data_json) { data.to_json }
271
+ let(:socket_id) { double }
272
+ let(:event_json) { double }
273
+
274
+ let(:batch) do
275
+ {
276
+ "batch" => [{
277
+ "channels" => channels,
278
+ "name" => name,
279
+ "data" => data_json,
280
+ "socket_id" => socket_id
281
+ }]
282
+ }
283
+ end
284
+
285
+ before do
286
+ allow(MultiJson).to receive(:load).with(event_json).and_return(batch)
287
+ allow(MultiJson).to receive(:load).with(data_json).and_return(data)
288
+ allow(PusherFake::Channel).to receive(:factory)
289
+ .with(channels[0]).and_return(channel_1)
290
+ allow(PusherFake::Channel).to receive(:factory)
291
+ .with(channels[1]).and_return(channel_2)
292
+ end
293
+
294
+ it "parses the request body as JSON" do
295
+ subject.batch_events(request)
296
+
297
+ expect(MultiJson).to have_received(:load).with(event_json)
298
+ end
299
+
300
+ it "parses the event data as JSON" do
301
+ subject.batch_events(request)
302
+
303
+ expect(MultiJson).to have_received(:load).with(data_json)
304
+ end
305
+
306
+ it "handles invalid JSON for event data" do
307
+ batch["batch"].first["data"] = data = "fake"
308
+
309
+ allow(MultiJson).to receive(:load)
310
+ .with(data).and_raise(MultiJson::LoadError)
311
+
312
+ expect { subject.batch_events(request) }.not_to raise_error
313
+ end
314
+
315
+ it "creates channels by name" do
316
+ subject.batch_events(request)
317
+
318
+ channels.each do |channel|
319
+ expect(PusherFake::Channel).to have_received(:factory).with(channel)
320
+ end
321
+ end
322
+
323
+ it "emits the event to the channels" do
324
+ subject.batch_events(request)
325
+
326
+ expect(channel_1).to have_received(:emit)
327
+ .with(name, data, socket_id: socket_id)
328
+ expect(channel_2).to have_received(:emit)
329
+ .with(name, data, socket_id: socket_id)
330
+ end
331
+ end
332
+
333
+ describe PusherFake::Server::Application, ".channels, requesting all" do
334
+ subject { described_class }
335
+
336
+ let(:request) { instance_double(Rack::Request, params: {}) }
337
+ let(:channels) { { "channel-1" => double, "channel-2" => double } }
238
338
 
239
339
  before do
240
340
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
@@ -243,17 +343,18 @@ describe PusherFake::Server::Application, ".channels, requesting all channels" d
243
343
  it "returns a hash of all the channels" do
244
344
  hash = subject.channels(request)
245
345
 
246
- expect(hash).to eq({ channels: { "channel-1" => {}, "channel-2" => {} } })
346
+ expect(hash).to eq(channels: { "channel-1" => {}, "channel-2" => {} })
247
347
  end
248
348
  end
249
349
 
250
- describe PusherFake::Server::Application, ".channels, requesting channels with a filter" do
350
+ describe PusherFake::Server::Application,
351
+ ".channels, requesting channels with a filter" do
352
+ subject { described_class }
353
+
251
354
  let(:params) { { "filter_by_prefix" => "public-" } }
252
- let(:request) { double(:request, params: params) }
355
+ let(:request) { instance_double(Rack::Request, params: params) }
253
356
  let(:channels) { { "public-1" => double, "presence-1" => double } }
254
357
 
255
- subject { PusherFake::Server::Application }
256
-
257
358
  before do
258
359
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
259
360
  end
@@ -261,66 +362,78 @@ describe PusherFake::Server::Application, ".channels, requesting channels with a
261
362
  it "returns a hash of the channels matching the filter" do
262
363
  hash = subject.channels(request)
263
364
 
264
- expect(hash).to eq({ channels: { "public-1" => {} } })
365
+ expect(hash).to eq(channels: { "public-1" => {} })
265
366
  end
266
367
  end
267
368
 
268
- describe PusherFake::Server::Application, ".channels, requesting user count for channels with a filter" do
269
- let(:params) { { "filter_by_prefix" => "presence-", "info" => "user_count" } }
270
- let(:request) { double(:request, params: params) }
271
- let(:channel) { double(:channel, connections: [double, double]) }
369
+ describe PusherFake::Server::Application,
370
+ ".channels, requesting user count for channels with a filter" do
371
+ subject { described_class }
372
+
373
+ let(:request) { instance_double(Rack::Request, params: params) }
272
374
  let(:channels) { { "public-1" => double, "presence-1" => channel } }
273
375
 
274
- subject { PusherFake::Server::Application }
376
+ let(:channel) do
377
+ instance_double(PusherFake::Channel::Presence,
378
+ connections: [double, double])
379
+ end
380
+
381
+ let(:params) do
382
+ { "filter_by_prefix" => "presence-", "info" => "user_count" }
383
+ end
275
384
 
276
385
  before do
277
386
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
278
387
  end
279
388
 
280
- it "returns a hash of the channels matching the filter and include the user count" do
389
+ it "returns hash of channels matching the filter and includes user count" do
281
390
  hash = subject.channels(request)
282
391
 
283
- expect(hash).to eq({ channels: { "presence-1" => { user_count: 2 } } })
392
+ expect(hash).to eq(channels: { "presence-1" => { user_count: 2 } })
284
393
  end
285
394
  end
286
395
 
287
- describe PusherFake::Server::Application, ".channels, requesting all channels with no channels occupied" do
288
- let(:request) { double(:request, params: {}) }
289
- let(:channels) { nil }
396
+ describe PusherFake::Server::Application,
397
+ ".channels, requesting all channels with no channels occupied" do
398
+ subject { described_class }
290
399
 
291
- subject { PusherFake::Server::Application }
400
+ let(:request) { instance_double(Rack::Request, params: {}) }
292
401
 
293
402
  before do
294
- allow(PusherFake::Channel).to receive(:channels).and_return(channels)
403
+ allow(PusherFake::Channel).to receive(:channels).and_return({})
295
404
  end
296
405
 
297
406
  it "returns a hash of no channels" do
298
407
  hash = subject.channels(request)
299
408
 
300
- expect(hash).to eq({ channels: {} })
409
+ expect(hash).to eq(channels: {})
301
410
  end
302
411
  end
303
412
 
304
- describe PusherFake::Server::Application, ".channels, requesting a user count on a non-presence channel" do
305
- let(:params) { { "filter_by_prefix" => "public-", "info" => "user_count" } }
306
- let(:request) { double(:request, params: params) }
413
+ describe PusherFake::Server::Application,
414
+ ".channels, requesting a user count on a non-presence channel" do
415
+ subject { described_class }
307
416
 
308
- subject { PusherFake::Server::Application }
417
+ let(:params) { { "filter_by_prefix" => "public-", "info" => "user_count" } }
418
+ let(:request) { instance_double(Rack::Request, params: params) }
309
419
 
310
420
  it "raises an error" do
311
- expect {
421
+ expect do
312
422
  subject.channels(request)
313
- }.to raise_error(subject::CHANNEL_FILTER_ERROR)
423
+ end.to raise_error(subject::CHANNEL_FILTER_ERROR)
314
424
  end
315
425
  end
316
426
 
317
427
  describe PusherFake::Server::Application, ".channel, for an occupied channel" do
428
+ subject { described_class }
429
+
318
430
  let(:name) { "public-1" }
319
- let(:request) { double(:request, params: {}) }
320
- let(:channel) { double(:channel, connections: [double]) }
431
+ let(:request) { instance_double(Rack::Request, params: {}) }
321
432
  let(:channels) { { name => channel } }
322
433
 
323
- subject { PusherFake::Server::Application }
434
+ let(:channel) do
435
+ instance_double(PusherFake::Channel::Presence, connections: [double])
436
+ end
324
437
 
325
438
  before do
326
439
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
@@ -329,17 +442,20 @@ describe PusherFake::Server::Application, ".channel, for an occupied channel" do
329
442
  it "returns a hash with the occupied status" do
330
443
  hash = subject.channel(name, request)
331
444
 
332
- expect(hash).to eq({ occupied: true })
445
+ expect(hash).to eq(occupied: true)
333
446
  end
334
447
  end
335
448
 
336
- describe PusherFake::Server::Application, ".channel, for an unoccupied channel" do
449
+ describe PusherFake::Server::Application, ".channel, for unoccupied channel" do
450
+ subject { described_class }
451
+
337
452
  let(:name) { "public-1" }
338
- let(:request) { double(:request, params: {}) }
339
- let(:channel) { double(:channel, connections: []) }
453
+ let(:request) { instance_double(Rack::Request, params: {}) }
340
454
  let(:channels) { { name => channel } }
341
455
 
342
- subject { PusherFake::Server::Application }
456
+ let(:channel) do
457
+ instance_double(PusherFake::Channel::Presence, connections: [])
458
+ end
343
459
 
344
460
  before do
345
461
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
@@ -348,15 +464,15 @@ describe PusherFake::Server::Application, ".channel, for an unoccupied channel"
348
464
  it "returns a hash with the occupied status" do
349
465
  hash = subject.channel(name, request)
350
466
 
351
- expect(hash).to eq({ occupied: false })
467
+ expect(hash).to eq(occupied: false)
352
468
  end
353
469
  end
354
470
 
355
471
  describe PusherFake::Server::Application, ".channel, for an unknown channel" do
356
- let(:request) { double(:request, params: {}) }
357
- let(:channels) { {} }
472
+ subject { described_class }
358
473
 
359
- subject { PusherFake::Server::Application }
474
+ let(:request) { instance_double(Rack::Request, params: {}) }
475
+ let(:channels) { {} }
360
476
 
361
477
  before do
362
478
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
@@ -365,18 +481,23 @@ describe PusherFake::Server::Application, ".channel, for an unknown channel" do
365
481
  it "returns a hash with the occupied status" do
366
482
  hash = subject.channel("fake", request)
367
483
 
368
- expect(hash).to eq({ occupied: false })
484
+ expect(hash).to eq(occupied: false)
369
485
  end
370
486
  end
371
487
 
372
- describe PusherFake::Server::Application, ".channel, request user count for a presence channel" do
488
+ describe PusherFake::Server::Application,
489
+ ".channel, request user count for a presence channel" do
490
+ subject { described_class }
491
+
373
492
  let(:name) { "presence-1" }
374
493
  let(:params) { { "info" => "user_count" } }
375
- let(:request) { double(:request, params: params) }
376
- let(:channel) { double(:channel, connections: [double, double]) }
494
+ let(:request) { instance_double(Rack::Request, params: params) }
377
495
  let(:channels) { { name => channel } }
378
496
 
379
- subject { PusherFake::Server::Application }
497
+ let(:channel) do
498
+ instance_double(PusherFake::Channel::Presence,
499
+ connections: [double, double])
500
+ end
380
501
 
381
502
  before do
382
503
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
@@ -385,31 +506,35 @@ describe PusherFake::Server::Application, ".channel, request user count for a pr
385
506
  it "returns a hash with the occupied status" do
386
507
  hash = subject.channel(name, request)
387
508
 
388
- expect(hash).to eq({ occupied: true, user_count: 2 })
509
+ expect(hash).to eq(occupied: true, user_count: 2)
389
510
  end
390
511
  end
391
512
 
392
- describe PusherFake::Server::Application, ".channel, requesting a user count on a non-presence channel" do
393
- let(:params) { { "info" => "user_count" } }
394
- let(:request) { double(:request, params: params) }
513
+ describe PusherFake::Server::Application,
514
+ ".channel, requesting a user count on a non-presence channel" do
515
+ subject { described_class }
395
516
 
396
- subject { PusherFake::Server::Application }
517
+ let(:params) { { "info" => "user_count" } }
518
+ let(:request) { instance_double(Rack::Request, params: params) }
397
519
 
398
520
  it "raises an error" do
399
- expect {
521
+ expect do
400
522
  subject.channel("public-1", request)
401
- }.to raise_error(subject::CHANNEL_USER_COUNT_ERROR)
523
+ end.to raise_error(subject::CHANNEL_USER_COUNT_ERROR)
402
524
  end
403
525
  end
404
526
 
405
527
  describe PusherFake::Server::Application, ".users, for an occupied channel" do
528
+ subject { described_class }
529
+
406
530
  let(:name) { "public-1" }
407
- let(:user_1) { double(:user, id: "1") }
408
- let(:user_2) { double(:user, id: "2") }
409
- let(:channel) { double(:channel, connections: [user_1, user_2]) }
531
+ let(:user_1) { instance_double(PusherFake::Connection, id: "1") }
532
+ let(:user_2) { instance_double(PusherFake::Connection, id: "2") }
410
533
  let(:channels) { { name => channel } }
411
534
 
412
- subject { PusherFake::Server::Application }
535
+ let(:channel) do
536
+ instance_double(PusherFake::Channel::Public, connections: [user_1, user_2])
537
+ end
413
538
 
414
539
  before do
415
540
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
@@ -418,19 +543,19 @@ describe PusherFake::Server::Application, ".users, for an occupied channel" do
418
543
  it "returns a hash with the occupied status" do
419
544
  hash = subject.users(name)
420
545
 
421
- expect(hash).to eq({ users: [
422
- { id: user_1.id },
423
- { id: user_2.id }
424
- ] })
546
+ expect(hash).to eq(users: [{ id: user_1.id }, { id: user_2.id }])
425
547
  end
426
548
  end
427
549
 
428
550
  describe PusherFake::Server::Application, ".users, for an empty channel" do
551
+ subject { described_class }
552
+
429
553
  let(:name) { "public-1" }
430
- let(:channel) { double(:channel, connections: []) }
431
554
  let(:channels) { { name => channel } }
432
555
 
433
- subject { PusherFake::Server::Application }
556
+ let(:channel) do
557
+ instance_double(PusherFake::Channel::Public, connections: [])
558
+ end
434
559
 
435
560
  before do
436
561
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
@@ -439,14 +564,14 @@ describe PusherFake::Server::Application, ".users, for an empty channel" do
439
564
  it "returns a hash with the occupied status" do
440
565
  hash = subject.users(name)
441
566
 
442
- expect(hash).to eq({ users: [] })
567
+ expect(hash).to eq(users: [])
443
568
  end
444
569
  end
445
570
 
446
571
  describe PusherFake::Server::Application, ".users, for an unknown channel" do
447
- let(:channels) { {} }
572
+ subject { described_class }
448
573
 
449
- subject { PusherFake::Server::Application }
574
+ let(:channels) { {} }
450
575
 
451
576
  before do
452
577
  allow(PusherFake::Channel).to receive(:channels).and_return(channels)
@@ -455,6 +580,6 @@ describe PusherFake::Server::Application, ".users, for an unknown channel" do
455
580
  it "returns a hash with the occupied status" do
456
581
  hash = subject.users("fake")
457
582
 
458
- expect(hash).to eq({ users: [] })
583
+ expect(hash).to eq(users: [])
459
584
  end
460
585
  end