pubnub 3.5.8 → 3.5.12

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of pubnub might be problematic. Click here for more details.

@@ -0,0 +1,540 @@
1
+ require 'spec_helper'
2
+
3
+ describe "#leave" do
4
+ before(:each) do
5
+
6
+ EM.stop if EM.reactor_running?
7
+ while EM.reactor_running? do end
8
+ sleep(0.1)
9
+
10
+ @response_output = StringIO.new
11
+ @message_output = StringIO.new
12
+
13
+ @callback = lambda { |envelope|
14
+ $logger.debug 'FIRING CALLBACK FROM TEST'
15
+ @response_output.write envelope.response
16
+ @message_output.write envelope.msg
17
+ @after_callback = true
18
+ }
19
+
20
+ @error_callback = lambda { |envelope|
21
+ $logger.debug 'FIRING ERROR CALLBACK FROM TEST'
22
+ @response_output.write envelope.response
23
+ @message_output.write envelope.msg
24
+ @after_error_callback = true
25
+ }
26
+
27
+ @pn = Pubnub.new(:disable_persistent_connection => true, :max_retries => 0, :subscribe_key => :demo, :publish_key => :demo, :auth_key => :demoish_authkey, :secret_key => 'some_secret_key', :error_callback => @error_callback)
28
+ @pn.uuid = 'rubytests'
29
+ end
30
+ context "uses ssl" do
31
+ before(:each) { @ssl = true }
32
+ context "passess callback as block" do
33
+ context "gets valid json in response" do
34
+ context "gets status 200 in response" do
35
+ context "uses sync connection" do
36
+ it 'works fine' do
37
+ VCR.use_cassette("leave-ssl-block-valid-200-sync", :record => :none) do
38
+ @pn.leave(:force => true, :ssl => true, :http_sync => true, :channel => "demo", &@callback)
39
+
40
+ @after_callback.should eq true
41
+ @response_output.seek 0
42
+ @response_output.read.should eq '{"action": "leave"}'
43
+ @message_output.seek 0
44
+ @message_output.read.should eq '{"action"=>"leave"}'
45
+ end
46
+ end
47
+ end
48
+ context "uses async connection" do
49
+ it 'works fine' do
50
+ VCR.use_cassette("leave-ssl-block-valid-200-async", :record => :none) do
51
+ @pn.leave(:force => true, :ssl => true, :http_sync => false, :channel => "demo", &@callback)
52
+
53
+ eventually do
54
+ @after_callback.should eq true
55
+ @response_output.seek 0
56
+ @response_output.read.should eq '{"action": "leave"}'
57
+ @message_output.seek 0
58
+ @message_output.read.should eq '{"action"=>"leave"}'
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ context "gets status non-200 in response" do
65
+ context "uses sync connection" do
66
+ it 'works fine' do
67
+ VCR.use_cassette("leave-ssl-block-valid-non-200-sync", :record => :none) do
68
+ @pn.leave(:force => true, :ssl => true, :http_sync => true, :channel => "demo", &@callback)
69
+
70
+ @after_error_callback.should eq true
71
+ @response_output.seek 0
72
+ @response_output.read.should eq '{"action": "leave"}'
73
+ @message_output.seek 0
74
+ @message_output.read.should eq '[0,"Non 2xx server response."]'
75
+ end
76
+ end
77
+ end
78
+ context "uses async connection" do
79
+ it 'works fine' do
80
+ VCR.use_cassette("leave-ssl-block-valid-non-200-async", :record => :none) do
81
+ @pn.leave(:force => true, :ssl => true, :http_sync => false, :channel => "demo", &@callback)
82
+
83
+ eventually do
84
+ @after_error_callback.should eq true
85
+ @response_output.seek 0
86
+ @response_output.read.should eq '{"action": "leave"}'
87
+ @message_output.seek 0
88
+ @message_output.read.should eq '[0,"Non 2xx server response."]'
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ context "gets invalid json in response" do
96
+ context "gets status 200 in response" do
97
+ context "uses sync connection" do
98
+ it 'works fine' do
99
+ VCR.use_cassette("leave-ssl-block-invalid-200-sync", :record => :none) do
100
+ @pn.leave(:force => true, :ssl => true, :http_sync => true, :channel => "demo", &@callback)
101
+
102
+ @after_error_callback.should eq true
103
+ @response_output.seek 0
104
+ @response_output.read.should eq '{"action": "leav'
105
+ @message_output.seek 0
106
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
107
+ end
108
+ end
109
+ end
110
+ context "uses async connection" do
111
+ it 'works fine' do
112
+ VCR.use_cassette("leave-ssl-block-invalid-200-async", :record => :none) do
113
+ @pn.leave(:force => true, :ssl => true, :http_sync => false, :channel => "demo", &@callback)
114
+
115
+ eventually do
116
+ @after_error_callback.should eq true
117
+ @response_output.seek 0
118
+ @response_output.read.should eq '{"action": "leav'
119
+ @message_output.seek 0
120
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ context "gets status non-200 in response" do
127
+ context "uses sync connection" do
128
+ it 'works fine' do
129
+ VCR.use_cassette("leave-ssl-block-invalid-non-200-sync", :record => :none) do
130
+ @pn.leave(:force => true, :ssl => true, :http_sync => true, :channel => "demo", &@callback)
131
+
132
+ @after_error_callback.should eq true
133
+ @response_output.seek 0
134
+ @response_output.read.should eq '{"action": "leav'
135
+ @message_output.seek 0
136
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
137
+ end
138
+ end
139
+ end
140
+ context "uses async connection" do
141
+ it 'works fine' do
142
+ VCR.use_cassette("leave-ssl-block-invalid-non-200-async", :record => :none) do
143
+ @pn.leave(:force => true, :ssl => true, :http_sync => false, :channel => "demo", &@callback)
144
+
145
+ eventually do
146
+ @after_error_callback.should eq true
147
+ @response_output.seek 0
148
+ @response_output.read.should eq '{"action": "leav'
149
+ @message_output.seek 0
150
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
157
+ end
158
+ context "passess callback as parameter" do
159
+ context "gets valid json in response" do
160
+ context "gets status 200 in response" do
161
+ context "uses sync connection" do
162
+ it 'works fine' do
163
+ VCR.use_cassette("leave-ssl-parameter-valid-200-sync", :record => :none) do
164
+ @pn.leave(:force => true, :ssl => true, :http_sync => true, :channel => "demo", :callback => @callback)
165
+
166
+ @after_callback.should eq true
167
+ @response_output.seek 0
168
+ @response_output.read.should eq '{"action": "leave"}'
169
+ @message_output.seek 0
170
+ @message_output.read.should eq '{"action"=>"leave"}'
171
+ end
172
+ end
173
+ end
174
+ context "uses async connection" do
175
+ it 'works fine' do
176
+ VCR.use_cassette("leave-ssl-parameter-valid-200-async", :record => :none) do
177
+ @pn.leave(:force => true, :ssl => true, :http_sync => false, :channel => "demo", :callback => @callback)
178
+
179
+ eventually do
180
+ @after_callback.should eq true
181
+ @response_output.seek 0
182
+ @response_output.read.should eq '{"action": "leave"}'
183
+ @message_output.seek 0
184
+ @message_output.read.should eq '{"action"=>"leave"}'
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
190
+ context "gets status non-200 in response" do
191
+ context "uses sync connection" do
192
+ it 'works fine' do
193
+ VCR.use_cassette("leave-ssl-parameter-valid-non-200-sync", :record => :none) do
194
+ @pn.leave(:force => true, :ssl => true, :http_sync => true, :channel => "demo", :callback => @callback)
195
+
196
+ @after_error_callback.should eq true
197
+ @response_output.seek 0
198
+ @response_output.read.should eq '{"action": "leave"}'
199
+ @message_output.seek 0
200
+ @message_output.read.should eq '[0,"Non 2xx server response."]'
201
+ end
202
+ end
203
+ end
204
+ context "uses async connection" do
205
+ it 'works fine' do
206
+ VCR.use_cassette("leave-ssl-parameter-valid-non-200-async", :record => :none) do
207
+ @pn.leave(:force => true, :ssl => true, :http_sync => false, :channel => "demo", :callback => @callback)
208
+
209
+ eventually do
210
+ @after_error_callback.should eq true
211
+ @response_output.seek 0
212
+ @response_output.read.should eq '{"action": "leave"}'
213
+ @message_output.seek 0
214
+ @message_output.read.should eq '[0,"Non 2xx server response."]'
215
+ end
216
+ end
217
+ end
218
+ end
219
+ end
220
+ end
221
+ context "gets invalid json in response" do
222
+ context "gets status 200 in response" do
223
+ context "uses sync connection" do
224
+ it 'works fine' do
225
+ VCR.use_cassette("leave-ssl-parameter-invalid-200-sync", :record => :none) do
226
+ @pn.leave(:force => true, :ssl => true, :http_sync => true, :channel => "demo", :callback => @callback)
227
+
228
+ @after_error_callback.should eq true
229
+ @response_output.seek 0
230
+ @response_output.read.should eq '{"action": "leav'
231
+ @message_output.seek 0
232
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
233
+ end
234
+ end
235
+ end
236
+ context "uses async connection" do
237
+ it 'works fine' do
238
+ VCR.use_cassette("leave-ssl-parameter-invalid-200-async", :record => :none) do
239
+ @pn.leave(:force => true, :ssl => true, :http_sync => false, :channel => "demo", :callback => @callback)
240
+
241
+ eventually do
242
+ @after_error_callback.should eq true
243
+ @response_output.seek 0
244
+ @response_output.read.should eq '{"action": "leav'
245
+ @message_output.seek 0
246
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
247
+ end
248
+ end
249
+ end
250
+ end
251
+ end
252
+ context "gets status non-200 in response" do
253
+ context "uses sync connection" do
254
+ it 'works fine' do
255
+ VCR.use_cassette("leave-ssl-parameter-invalid-non-200-sync", :record => :none) do
256
+ @pn.leave(:force => true, :ssl => true, :http_sync => true, :channel => "demo", :callback => @callback)
257
+
258
+ @after_error_callback.should eq true
259
+ @response_output.seek 0
260
+ @response_output.read.should eq '{"action": "leav'
261
+ @message_output.seek 0
262
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
263
+ end
264
+ end
265
+ end
266
+ context "uses async connection" do
267
+ it 'works fine' do
268
+ VCR.use_cassette("leave-ssl-parameter-invalid-non-200-async", :record => :none) do
269
+ @pn.leave(:force => true, :ssl => true, :http_sync => false, :channel => "demo", :callback => @callback)
270
+
271
+ eventually do
272
+ @after_error_callback.should eq true
273
+ @response_output.seek 0
274
+ @response_output.read.should eq '{"action": "leav'
275
+ @message_output.seek 0
276
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
277
+ end
278
+ end
279
+ end
280
+ end
281
+ end
282
+ end
283
+ end
284
+ end
285
+ context "uses non-ssl" do
286
+ before(:each) { @ssl = false }
287
+ context "passess callback as block" do
288
+ context "gets valid json in response" do
289
+ context "gets status 200 in response" do
290
+ context "uses sync connection" do
291
+ it 'works fine' do
292
+ VCR.use_cassette("leave-nonssl-block-valid-200-sync", :record => :none) do
293
+ @pn.leave(:force => true, :ssl => false, :http_sync => true, :channel => "demo", &@callback)
294
+
295
+ @after_callback.should eq true
296
+ @response_output.seek 0
297
+ @response_output.read.should eq '{"action": "leave"}'
298
+ @message_output.seek 0
299
+ @message_output.read.should eq '{"action"=>"leave"}'
300
+ end
301
+ end
302
+ end
303
+ context "uses async connection" do
304
+ it 'works fine' do
305
+ VCR.use_cassette("leave-nonssl-block-valid-200-async", :record => :none) do
306
+ @pn.leave(:force => true, :ssl => false, :http_sync => false, :channel => "demo", &@callback)
307
+
308
+ eventually do
309
+ @after_callback.should eq true
310
+ @response_output.seek 0
311
+ @response_output.read.should eq '{"action": "leave"}'
312
+ @message_output.seek 0
313
+ @message_output.read.should eq '{"action"=>"leave"}'
314
+ end
315
+ end
316
+ end
317
+ end
318
+ end
319
+ context "gets status non-200 in response" do
320
+ context "uses sync connection" do
321
+ it 'works fine' do
322
+ VCR.use_cassette("leave-nonssl-block-valid-non-200-sync", :record => :none) do
323
+ @pn.leave(:force => true, :ssl => false, :http_sync => true, :channel => "demo", &@callback)
324
+
325
+ @after_error_callback.should eq true
326
+ @response_output.seek 0
327
+ @response_output.read.should eq '{"action": "leave"}'
328
+ @message_output.seek 0
329
+ @message_output.read.should eq '[0,"Non 2xx server response."]'
330
+ end
331
+ end
332
+ end
333
+ context "uses async connection" do
334
+ it 'works fine' do
335
+ VCR.use_cassette("leave-nonssl-block-valid-non-200-async", :record => :none) do
336
+ @pn.leave(:force => true, :ssl => false, :http_sync => false, :channel => "demo", &@callback)
337
+
338
+ eventually do
339
+ @after_error_callback.should eq true
340
+ @response_output.seek 0
341
+ @response_output.read.should eq '{"action": "leave"}'
342
+ @message_output.seek 0
343
+ @message_output.read.should eq '[0,"Non 2xx server response."]'
344
+ end
345
+ end
346
+ end
347
+ end
348
+ end
349
+ end
350
+ context "gets invalid json in response" do
351
+ context "gets status 200 in response" do
352
+ context "uses sync connection" do
353
+ it 'works fine' do
354
+ VCR.use_cassette("leave-nonssl-block-invalid-200-sync", :record => :none) do
355
+ @pn.leave(:force => true, :ssl => false, :http_sync => true, :channel => "demo", &@callback)
356
+
357
+ @after_error_callback.should eq true
358
+ @response_output.seek 0
359
+ @response_output.read.should eq '{"action": "leav'
360
+ @message_output.seek 0
361
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
362
+ end
363
+ end
364
+ end
365
+ context "uses async connection" do
366
+ it 'works fine' do
367
+ VCR.use_cassette("leave-nonssl-block-invalid-200-async", :record => :none) do
368
+ @pn.leave(:force => true, :ssl => false, :http_sync => false, :channel => "demo", &@callback)
369
+
370
+ eventually do
371
+ @after_error_callback.should eq true
372
+ @response_output.seek 0
373
+ @response_output.read.should eq '{"action": "leav'
374
+ @message_output.seek 0
375
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
376
+ end
377
+ end
378
+ end
379
+ end
380
+ end
381
+ context "gets status non-200 in response" do
382
+ context "uses sync connection" do
383
+ it 'works fine' do
384
+ VCR.use_cassette("leave-nonssl-block-invalid-non-200-sync", :record => :none) do
385
+ @pn.leave(:force => true, :ssl => false, :http_sync => true, :channel => "demo", &@callback)
386
+
387
+ @after_error_callback.should eq true
388
+ @response_output.seek 0
389
+ @response_output.read.should eq '{"action": "leav'
390
+ @message_output.seek 0
391
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
392
+ end
393
+ end
394
+ end
395
+ context "uses async connection" do
396
+ it 'works fine' do
397
+ VCR.use_cassette("leave-nonssl-block-invalid-non-200-async", :record => :none) do
398
+ @pn.leave(:force => true, :ssl => false, :http_sync => false, :channel => "demo", &@callback)
399
+
400
+ eventually do
401
+ @after_error_callback.should eq true
402
+ @response_output.seek 0
403
+ @response_output.read.should eq '{"action": "leav'
404
+ @message_output.seek 0
405
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
406
+ end
407
+ end
408
+ end
409
+ end
410
+ end
411
+ end
412
+ end
413
+ context "passess callback as parameter" do
414
+ context "gets valid json in response" do
415
+ context "gets status 200 in response" do
416
+ context "uses sync connection" do
417
+ it 'works fine' do
418
+ VCR.use_cassette("leave-nonssl-parameter-valid-200-sync", :record => :none) do
419
+ @pn.leave(:force => true, :ssl => false, :http_sync => true, :channel => "demo", :callback => @callback)
420
+
421
+ @after_callback.should eq true
422
+ @response_output.seek 0
423
+ @response_output.read.should eq '{"action": "leave"}'
424
+ @message_output.seek 0
425
+ @message_output.read.should eq '{"action"=>"leave"}'
426
+ end
427
+ end
428
+ end
429
+ context "uses async connection" do
430
+ it 'works fine' do
431
+ VCR.use_cassette("leave-nonssl-parameter-valid-200-async", :record => :none) do
432
+ @pn.leave(:force => true, :ssl => false, :http_sync => false, :channel => "demo", :callback => @callback)
433
+
434
+ eventually do
435
+ @after_callback.should eq true
436
+ @response_output.seek 0
437
+ @response_output.read.should eq '{"action": "leave"}'
438
+ @message_output.seek 0
439
+ @message_output.read.should eq '{"action"=>"leave"}'
440
+ end
441
+ end
442
+ end
443
+ end
444
+ end
445
+ context "gets status non-200 in response" do
446
+ context "uses sync connection" do
447
+ it 'works fine' do
448
+ VCR.use_cassette("leave-nonssl-parameter-valid-non-200-sync", :record => :none) do
449
+ @pn.leave(:force => true, :ssl => false, :http_sync => true, :channel => "demo", :callback => @callback)
450
+
451
+ @after_error_callback.should eq true
452
+ @response_output.seek 0
453
+ @response_output.read.should eq '{"action": "leave"}'
454
+ @message_output.seek 0
455
+ @message_output.read.should eq '[0,"Non 2xx server response."]'
456
+ end
457
+ end
458
+ end
459
+ context "uses async connection" do
460
+ it 'works fine' do
461
+ VCR.use_cassette("leave-nonssl-parameter-valid-non-200-async", :record => :none) do
462
+ @pn.leave(:force => true, :ssl => false, :http_sync => false, :channel => "demo", :callback => @callback)
463
+
464
+ eventually do
465
+ @after_error_callback.should eq true
466
+ @response_output.seek 0
467
+ @response_output.read.should eq '{"action": "leave"}'
468
+ @message_output.seek 0
469
+ @message_output.read.should eq '[0,"Non 2xx server response."]'
470
+ end
471
+ end
472
+ end
473
+ end
474
+ end
475
+ end
476
+ context "gets invalid json in response" do
477
+ context "gets status 200 in response" do
478
+ context "uses sync connection" do
479
+ it 'works fine' do
480
+ VCR.use_cassette("leave-nonssl-parameter-invalid-200-sync", :record => :none) do
481
+ @pn.leave(:force => true, :ssl => false, :http_sync => true, :channel => "demo", :callback => @callback)
482
+
483
+ @after_error_callback.should eq true
484
+ @response_output.seek 0
485
+ @response_output.read.should eq '{"action": "leav'
486
+ @message_output.seek 0
487
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
488
+ end
489
+ end
490
+ end
491
+ context "uses async connection" do
492
+ it 'works fine' do
493
+ VCR.use_cassette("leave-nonssl-parameter-invalid-200-async", :record => :none) do
494
+ @pn.leave(:force => true, :ssl => false, :http_sync => false, :channel => "demo", :callback => @callback)
495
+
496
+ eventually do
497
+ @after_error_callback.should eq true
498
+ @response_output.seek 0
499
+ @response_output.read.should eq '{"action": "leav'
500
+ @message_output.seek 0
501
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
502
+ end
503
+ end
504
+ end
505
+ end
506
+ end
507
+ context "gets status non-200 in response" do
508
+ context "uses sync connection" do
509
+ it 'works fine' do
510
+ VCR.use_cassette("leave-nonssl-parameter-invalid-non-200-sync", :record => :none) do
511
+ @pn.leave(:force => true, :ssl => false, :http_sync => true, :channel => "demo", :callback => @callback)
512
+
513
+ @after_error_callback.should eq true
514
+ @response_output.seek 0
515
+ @response_output.read.should eq '{"action": "leav'
516
+ @message_output.seek 0
517
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
518
+ end
519
+ end
520
+ end
521
+ context "uses async connection" do
522
+ it 'works fine' do
523
+ VCR.use_cassette("leave-nonssl-parameter-invalid-non-200-async", :record => :none) do
524
+ @pn.leave(:force => true, :ssl => false, :http_sync => false, :channel => "demo", :callback => @callback)
525
+
526
+ eventually do
527
+ @after_error_callback.should eq true
528
+ @response_output.seek 0
529
+ @response_output.read.should eq '{"action": "leav'
530
+ @message_output.seek 0
531
+ @message_output.read.should eq '[0,"Invalid JSON in response."]'
532
+ end
533
+ end
534
+ end
535
+ end
536
+ end
537
+ end
538
+ end
539
+ end
540
+ end