pusher 1.4.2 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/spec/client_spec.rb DELETED
@@ -1,786 +0,0 @@
1
- require 'base64'
2
-
3
- require 'rbnacl'
4
- require 'em-http'
5
-
6
- require 'spec_helper'
7
-
8
- encryption_master_key = RbNaCl::Random.random_bytes(32)
9
-
10
- describe Pusher do
11
- # The behaviour should be the same when using the Client object, or the
12
- # 'global' client delegated through the Pusher class
13
- [lambda { Pusher }, lambda { Pusher::Client.new }].each do |client_gen|
14
- before :each do
15
- @client = client_gen.call
16
- end
17
-
18
- describe 'default configuration' do
19
- it 'should be preconfigured for api host' do
20
- expect(@client.host).to eq('api.pusherapp.com')
21
- end
22
-
23
- it 'should be preconfigured for port 80' do
24
- expect(@client.port).to eq(80)
25
- end
26
-
27
- it 'should use standard logger if no other logger if defined' do
28
- Pusher.logger.debug('foo')
29
- expect(Pusher.logger).to be_kind_of(Logger)
30
- end
31
- end
32
-
33
- describe 'logging configuration' do
34
- it "can be configured to use any logger" do
35
- logger = double("ALogger")
36
- expect(logger).to receive(:debug).with('foo')
37
- Pusher.logger = logger
38
- Pusher.logger.debug('foo')
39
- Pusher.logger = nil
40
- end
41
- end
42
-
43
- describe "configuration using url" do
44
- it "should be possible to configure everything by setting the url" do
45
- @client.url = "test://somekey:somesecret@api.staging.pusherapp.com:8080/apps/87"
46
-
47
- expect(@client.scheme).to eq('test')
48
- expect(@client.host).to eq('api.staging.pusherapp.com')
49
- expect(@client.port).to eq(8080)
50
- expect(@client.key).to eq('somekey')
51
- expect(@client.secret).to eq('somesecret')
52
- expect(@client.app_id).to eq('87')
53
- end
54
-
55
- it "should override scheme and port when setting encrypted=true after url" do
56
- @client.url = "http://somekey:somesecret@api.staging.pusherapp.com:8080/apps/87"
57
- @client.encrypted = true
58
-
59
- expect(@client.scheme).to eq('https')
60
- expect(@client.port).to eq(443)
61
- end
62
-
63
- it "should fail on bad urls" do
64
- expect { @client.url = "gopher/somekey:somesecret@://api.staging.pusherapp.co://m:8080\apps\87" }.to raise_error(URI::InvalidURIError)
65
- end
66
-
67
- it "should raise exception if app_id is not configured" do
68
- @client.app_id = nil
69
- expect {
70
- @client.url
71
- }.to raise_error(Pusher::ConfigurationError)
72
- end
73
-
74
- end
75
-
76
- describe 'configuring the cluster' do
77
- it 'should set a new default host' do
78
- @client.cluster = 'eu'
79
- expect(@client.host).to eq('api-eu.pusher.com')
80
- end
81
-
82
- it 'should be overridden by host if it comes after' do
83
- @client.cluster = 'eu'
84
- @client.host = 'api.staging.pusher.com'
85
- expect(@client.host).to eq('api.staging.pusher.com')
86
- end
87
-
88
- it 'should be overridden by url if it comes after' do
89
- @client.cluster = 'eu'
90
- @client.url = "http://somekey:somesecret@api.staging.pusherapp.com:8080/apps/87"
91
-
92
- expect(@client.host).to eq('api.staging.pusherapp.com')
93
- end
94
-
95
- it 'should override the url configuration if it comes after' do
96
- @client.url = "http://somekey:somesecret@api.staging.pusherapp.com:8080/apps/87"
97
- @client.cluster = 'eu'
98
- expect(@client.host).to eq('api-eu.pusher.com')
99
- end
100
-
101
- it 'should override the host configuration if it comes after' do
102
- @client.host = 'api.staging.pusher.com'
103
- @client.cluster = 'eu'
104
- expect(@client.host).to eq('api-eu.pusher.com')
105
- end
106
- end
107
-
108
- describe 'configuring TLS' do
109
- it 'should set port and scheme if "use_tls" enabled' do
110
- client = Pusher::Client.new({
111
- :use_tls => true,
112
- })
113
- expect(client.scheme).to eq('https')
114
- expect(client.port).to eq(443)
115
- end
116
-
117
- it 'should set port and scheme if "encrypted" enabled' do
118
- client = Pusher::Client.new({
119
- :encrypted => true,
120
- })
121
- expect(client.scheme).to eq('https')
122
- expect(client.port).to eq(443)
123
- end
124
-
125
- it 'should use non-TLS port and scheme if "encrypted" or "use_tls" are not set' do
126
- client = Pusher::Client.new
127
- expect(client.scheme).to eq('http')
128
- expect(client.port).to eq(80)
129
- end
130
-
131
- it 'should override port if "use_tls" option set but a different port is specified' do
132
- client = Pusher::Client.new({
133
- :use_tls => true,
134
- :port => 8443
135
- })
136
- expect(client.scheme).to eq('https')
137
- expect(client.port).to eq(8443)
138
- end
139
-
140
- end
141
-
142
- describe 'configuring a http proxy' do
143
- it "should be possible to configure everything by setting the http_proxy" do
144
- @client.http_proxy = 'http://someuser:somepassword@proxy.host.com:8080'
145
-
146
- expect(@client.proxy).to eq({:scheme => 'http', :host => 'proxy.host.com', :port => 8080, :user => 'someuser', :password => 'somepassword'})
147
- end
148
- end
149
-
150
- describe 'configuring from env' do
151
- after do
152
- ENV['PUSHER_URL'] = nil
153
- end
154
-
155
- it "works" do
156
- url = "http://somekey:somesecret@api.staging.pusherapp.com:8080/apps/87"
157
- ENV['PUSHER_URL'] = url
158
-
159
- client = Pusher::Client.from_env
160
- expect(client.key).to eq("somekey")
161
- expect(client.secret).to eq("somesecret")
162
- expect(client.app_id).to eq("87")
163
- expect(client.url.to_s).to eq("http://api.staging.pusherapp.com:8080/apps/87")
164
- end
165
- end
166
-
167
- describe 'configuring from url' do
168
- it "works" do
169
- url = "http://somekey:somesecret@api.staging.pusherapp.com:8080/apps/87"
170
-
171
- client = Pusher::Client.from_url(url)
172
- expect(client.key).to eq("somekey")
173
- expect(client.secret).to eq("somesecret")
174
- expect(client.app_id).to eq("87")
175
- expect(client.url.to_s).to eq("http://api.staging.pusherapp.com:8080/apps/87")
176
- end
177
- end
178
-
179
- describe 'can set encryption_master_key_base64' do
180
- it "sets encryption_master_key" do
181
- @client.encryption_master_key_base64 =
182
- Base64.strict_encode64(encryption_master_key)
183
-
184
- expect(@client.encryption_master_key).to eq(encryption_master_key)
185
- end
186
- end
187
-
188
- describe 'when configured' do
189
- before :each do
190
- @client.app_id = '20'
191
- @client.key = '12345678900000001'
192
- @client.secret = '12345678900000001'
193
- @client.encryption_master_key_base64 =
194
- Base64.strict_encode64(encryption_master_key)
195
- end
196
-
197
- describe '#[]' do
198
- before do
199
- @channel = @client['test_channel']
200
- end
201
-
202
- it 'should return a channel' do
203
- expect(@channel).to be_kind_of(Pusher::Channel)
204
- end
205
-
206
- it "should raise exception if app_id is not configured" do
207
- @client.app_id = nil
208
- expect {
209
- @channel.trigger!('foo', 'bar')
210
- }.to raise_error(Pusher::ConfigurationError)
211
- end
212
- end
213
-
214
- describe '#channels' do
215
- it "should call the correct URL and symbolise response correctly" do
216
- api_path = %r{/apps/20/channels}
217
- stub_request(:get, api_path).to_return({
218
- :status => 200,
219
- :body => MultiJson.encode('channels' => {
220
- "channel1" => {},
221
- "channel2" => {}
222
- })
223
- })
224
- expect(@client.channels).to eq({
225
- :channels => {
226
- "channel1" => {},
227
- "channel2" => {}
228
- }
229
- })
230
- end
231
- end
232
-
233
- describe '#channel_info' do
234
- it "should call correct URL and symbolise response" do
235
- api_path = %r{/apps/20/channels/mychannel}
236
- stub_request(:get, api_path).to_return({
237
- :status => 200,
238
- :body => MultiJson.encode({
239
- 'occupied' => false,
240
- })
241
- })
242
- expect(@client.channel_info('mychannel')).to eq({
243
- :occupied => false,
244
- })
245
- end
246
- end
247
-
248
- describe '#channel_users' do
249
- it "should call correct URL and symbolise response" do
250
- api_path = %r{/apps/20/channels/mychannel/users}
251
- stub_request(:get, api_path).to_return({
252
- :status => 200,
253
- :body => MultiJson.encode({
254
- 'users' => [{ 'id' => 1 }]
255
- })
256
- })
257
- expect(@client.channel_users('mychannel')).to eq({
258
- :users => [{ 'id' => 1}]
259
- })
260
- end
261
- end
262
-
263
- describe '#authenticate' do
264
- before :each do
265
- @custom_data = {:uid => 123, :info => {:name => 'Foo'}}
266
- end
267
-
268
- it 'should return a hash with signature including custom data and data as json string' do
269
- allow(MultiJson).to receive(:encode).with(@custom_data).and_return 'a json string'
270
-
271
- response = @client.authenticate('test_channel', '1.1', @custom_data)
272
-
273
- expect(response).to eq({
274
- :auth => "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:a json string")}",
275
- :channel_data => 'a json string'
276
- })
277
- end
278
-
279
- it 'should include a shared_secret if the private-encrypted channel' do
280
- allow(MultiJson).to receive(:encode).with(@custom_data).and_return 'a json string'
281
- @client.instance_variable_set(:@encryption_master_key, '3W1pfB/Etr+ZIlfMWwZP3gz8jEeCt4s2pe6Vpr+2c3M=')
282
-
283
- response = @client.authenticate('private-encrypted-test_channel', '1.1', @custom_data)
284
-
285
- expect(response).to eq({
286
- :auth => "12345678900000001:#{hmac(@client.secret, "1.1:private-encrypted-test_channel:a json string")}",
287
- :shared_secret => "o0L3QnIovCeRC8KTD8KBRlmi31dGzHVS2M93uryqDdw=",
288
- :channel_data => 'a json string'
289
- })
290
- end
291
-
292
- end
293
-
294
- describe '#trigger' do
295
- before :each do
296
- @api_path = %r{/apps/20/events}
297
- stub_request(:post, @api_path).to_return({
298
- :status => 200,
299
- :body => MultiJson.encode({})
300
- })
301
- end
302
-
303
- it "should call correct URL" do
304
- expect(@client.trigger(['mychannel'], 'event', {'some' => 'data'})).
305
- to eq({})
306
- end
307
-
308
- it "should not allow too many channels" do
309
- expect {
310
- @client.trigger((0..11).map{|i| 'mychannel#{i}'},
311
- 'event', {'some' => 'data'}, {
312
- :socket_id => "12.34"
313
- })}.to raise_error(Pusher::Error)
314
- end
315
-
316
- it "should pass any parameters in the body of the request" do
317
- @client.trigger(['mychannel', 'c2'], 'event', {'some' => 'data'}, {
318
- :socket_id => "12.34"
319
- })
320
- expect(WebMock).to have_requested(:post, @api_path).with { |req|
321
- parsed = MultiJson.decode(req.body)
322
- expect(parsed["name"]).to eq('event')
323
- expect(parsed["channels"]).to eq(["mychannel", "c2"])
324
- expect(parsed["socket_id"]).to eq('12.34')
325
- }
326
- end
327
-
328
- it "should convert non string data to JSON before posting" do
329
- @client.trigger(['mychannel'], 'event', {'some' => 'data'})
330
- expect(WebMock).to have_requested(:post, @api_path).with { |req|
331
- expect(MultiJson.decode(req.body)["data"]).to eq('{"some":"data"}')
332
- }
333
- end
334
-
335
- it "should accept a single channel as well as an array" do
336
- @client.trigger('mychannel', 'event', {'some' => 'data'})
337
- expect(WebMock).to have_requested(:post, @api_path).with { |req|
338
- expect(MultiJson.decode(req.body)["channels"]).to eq(['mychannel'])
339
- }
340
- end
341
-
342
- %w[app_id key secret].each do |key|
343
- it "should fail in missing #{key}" do
344
- @client.public_send("#{key}=", nil)
345
- expect {
346
- @client.trigger('mychannel', 'event', {'some' => 'data'})
347
- }.to raise_error(Pusher::ConfigurationError)
348
- expect(WebMock).not_to have_requested(:post, @api_path).with { |req|
349
- expect(MultiJson.decode(req.body)["channels"]).to eq(['mychannel'])
350
- }
351
- end
352
- end
353
-
354
- it "should fail to publish to encrypted channels when missing key" do
355
- @client.encryption_master_key_base64 = nil
356
- expect {
357
- @client.trigger('private-encrypted-channel', 'event', {'some' => 'data'})
358
- }.to raise_error(Pusher::ConfigurationError)
359
- expect(WebMock).not_to have_requested(:post, @api_path)
360
- end
361
-
362
- it "should fail to publish to multiple channels if one is encrypted" do
363
- expect {
364
- @client.trigger(
365
- ['private-encrypted-channel', 'some-other-channel'],
366
- 'event',
367
- {'some' => 'data'},
368
- )
369
- }.to raise_error(Pusher::Error)
370
- expect(WebMock).not_to have_requested(:post, @api_path)
371
- end
372
-
373
- it "should encrypt publishes to encrypted channels" do
374
- @client.trigger(
375
- 'private-encrypted-channel',
376
- 'event',
377
- {'some' => 'data'},
378
- )
379
-
380
- expect(WebMock).to have_requested(:post, @api_path).with { |req|
381
- data = MultiJson.decode(MultiJson.decode(req.body)["data"])
382
-
383
- key = RbNaCl::Hash.sha256(
384
- 'private-encrypted-channel' + encryption_master_key
385
- )
386
-
387
- expect(MultiJson.decode(RbNaCl::SecretBox.new(key).decrypt(
388
- Base64.decode64(data["nonce"]),
389
- Base64.decode64(data["ciphertext"]),
390
- ))).to eq({ 'some' => 'data' })
391
- }
392
- end
393
- end
394
-
395
- describe '#trigger_batch' do
396
- before :each do
397
- @api_path = %r{/apps/20/batch_events}
398
- stub_request(:post, @api_path).to_return({
399
- :status => 200,
400
- :body => MultiJson.encode({})
401
- })
402
- end
403
-
404
- it "should call correct URL" do
405
- expect(@client.trigger_batch(channel: 'mychannel', name: 'event', data: {'some' => 'data'})).
406
- to eq({})
407
- end
408
-
409
- it "should convert non string data to JSON before posting" do
410
- @client.trigger_batch(
411
- {channel: 'mychannel', name: 'event', data: {'some' => 'data'}},
412
- {channel: 'mychannel', name: 'event', data: 'already encoded'},
413
- )
414
- expect(WebMock).to have_requested(:post, @api_path).with { |req|
415
- parsed = MultiJson.decode(req.body)
416
- expect(parsed).to eq(
417
- "batch" => [
418
- { "channel" => "mychannel", "name" => "event", "data" => "{\"some\":\"data\"}"},
419
- { "channel" => "mychannel", "name" => "event", "data" => "already encoded"}
420
- ]
421
- )
422
- }
423
- end
424
-
425
- it "should fail to publish to encrypted channels when missing key" do
426
- @client.encryption_master_key_base64 = nil
427
- expect {
428
- @client.trigger_batch(
429
- {
430
- channel: 'private-encrypted-channel',
431
- name: 'event',
432
- data: {'some' => 'data'},
433
- },
434
- {channel: 'mychannel', name: 'event', data: 'already encoded'},
435
- )
436
- }.to raise_error(Pusher::ConfigurationError)
437
- expect(WebMock).not_to have_requested(:post, @api_path)
438
- end
439
-
440
- it "should encrypt publishes to encrypted channels" do
441
- @client.trigger_batch(
442
- {
443
- channel: 'private-encrypted-channel',
444
- name: 'event',
445
- data: {'some' => 'data'},
446
- },
447
- {channel: 'mychannel', name: 'event', data: 'already encoded'},
448
- )
449
-
450
- expect(WebMock).to have_requested(:post, @api_path).with { |req|
451
- batch = MultiJson.decode(req.body)["batch"]
452
- expect(batch.length).to eq(2)
453
-
454
- expect(batch[0]["channel"]).to eq("private-encrypted-channel")
455
- expect(batch[0]["name"]).to eq("event")
456
-
457
- data = MultiJson.decode(batch[0]["data"])
458
-
459
- key = RbNaCl::Hash.sha256(
460
- 'private-encrypted-channel' + encryption_master_key
461
- )
462
-
463
- expect(MultiJson.decode(RbNaCl::SecretBox.new(key).decrypt(
464
- Base64.decode64(data["nonce"]),
465
- Base64.decode64(data["ciphertext"]),
466
- ))).to eq({ 'some' => 'data' })
467
-
468
- expect(batch[1]["channel"]).to eq("mychannel")
469
- expect(batch[1]["name"]).to eq("event")
470
- expect(batch[1]["data"]).to eq("already encoded")
471
- }
472
- end
473
- end
474
-
475
- describe '#trigger_async' do
476
- before :each do
477
- @api_path = %r{/apps/20/events}
478
- stub_request(:post, @api_path).to_return({
479
- :status => 200,
480
- :body => MultiJson.encode({})
481
- })
482
- end
483
-
484
- it "should call correct URL" do
485
- EM.run {
486
- @client.trigger_async('mychannel', 'event', {'some' => 'data'}).callback { |r|
487
- expect(r).to eq({})
488
- EM.stop
489
- }
490
- }
491
- end
492
-
493
- it "should pass any parameters in the body of the request" do
494
- EM.run {
495
- @client.trigger_async('mychannel', 'event', {'some' => 'data'}, {
496
- :socket_id => "12.34"
497
- }).callback {
498
- expect(WebMock).to have_requested(:post, @api_path).with { |req|
499
- expect(MultiJson.decode(req.body)["socket_id"]).to eq('12.34')
500
- }
501
- EM.stop
502
- }
503
- }
504
- end
505
-
506
- it "should convert non string data to JSON before posting" do
507
- EM.run {
508
- @client.trigger_async('mychannel', 'event', {'some' => 'data'}).callback {
509
- expect(WebMock).to have_requested(:post, @api_path).with { |req|
510
- expect(MultiJson.decode(req.body)["data"]).to eq('{"some":"data"}')
511
- }
512
- EM.stop
513
- }
514
- }
515
- end
516
- end
517
-
518
- [:get, :post].each do |verb|
519
- describe "##{verb}" do
520
- before :each do
521
- @url_regexp = %r{api.pusherapp.com}
522
- stub_request(verb, @url_regexp).
523
- to_return(:status => 200, :body => "{}")
524
- end
525
-
526
- let(:call_api) { @client.send(verb, '/path') }
527
-
528
- it "should use http by default" do
529
- call_api
530
- expect(WebMock).to have_requested(verb, %r{http://api.pusherapp.com/apps/20/path})
531
- end
532
-
533
- it "should use https if configured" do
534
- @client.encrypted = true
535
- call_api
536
- expect(WebMock).to have_requested(verb, %r{https://api.pusherapp.com})
537
- end
538
-
539
- it "should format the respose hash with symbols at first level" do
540
- stub_request(verb, @url_regexp).to_return({
541
- :status => 200,
542
- :body => MultiJson.encode({'something' => {'a' => 'hash'}})
543
- })
544
- expect(call_api).to eq({
545
- :something => {'a' => 'hash'}
546
- })
547
- end
548
-
549
- it "should catch all http exceptions and raise a Pusher::HTTPError wrapping the original error" do
550
- stub_request(verb, @url_regexp).to_raise(HTTPClient::TimeoutError)
551
-
552
- error = nil
553
- begin
554
- call_api
555
- rescue => e
556
- error = e
557
- end
558
-
559
- expect(error.class).to eq(Pusher::HTTPError)
560
- expect(error).to be_kind_of(Pusher::Error)
561
- expect(error.message).to eq('Exception from WebMock (HTTPClient::TimeoutError)')
562
- expect(error.original_error.class).to eq(HTTPClient::TimeoutError)
563
- end
564
-
565
- it "should raise Pusher::Error if call returns 400" do
566
- stub_request(verb, @url_regexp).to_return({:status => 400})
567
- expect { call_api }.to raise_error(Pusher::Error)
568
- end
569
-
570
- it "should raise AuthenticationError if pusher returns 401" do
571
- stub_request(verb, @url_regexp).to_return({:status => 401})
572
- expect { call_api }.to raise_error(Pusher::AuthenticationError)
573
- end
574
-
575
- it "should raise Pusher::Error if pusher returns 404" do
576
- stub_request(verb, @url_regexp).to_return({:status => 404})
577
- expect { call_api }.to raise_error(Pusher::Error, '404 Not found (/apps/20/path)')
578
- end
579
-
580
- it "should raise Pusher::Error if pusher returns 407" do
581
- stub_request(verb, @url_regexp).to_return({:status => 407})
582
- expect { call_api }.to raise_error(Pusher::Error, 'Proxy Authentication Required')
583
- end
584
-
585
- it "should raise Pusher::Error if pusher returns 413" do
586
- stub_request(verb, @url_regexp).to_return({:status => 413})
587
- expect { call_api }.to raise_error(Pusher::Error, 'Payload Too Large > 10KB')
588
- end
589
-
590
- it "should raise Pusher::Error if pusher returns 500" do
591
- stub_request(verb, @url_regexp).to_return({:status => 500, :body => "some error"})
592
- expect { call_api }.to raise_error(Pusher::Error, 'Unknown error (status code 500): some error')
593
- end
594
- end
595
- end
596
-
597
- describe "async calling without eventmachine" do
598
- [[:get, :get_async], [:post, :post_async]].each do |verb, method|
599
- describe "##{method}" do
600
- before :each do
601
- @url_regexp = %r{api.pusherapp.com}
602
- stub_request(verb, @url_regexp).
603
- to_return(:status => 200, :body => "{}")
604
- end
605
-
606
- let(:call_api) {
607
- @client.send(method, '/path').tap { |c|
608
- # Allow the async thread (inside httpclient) to run
609
- while !c.finished?
610
- sleep 0.01
611
- end
612
- }
613
- }
614
-
615
- it "should use http by default" do
616
- call_api
617
- expect(WebMock).to have_requested(verb, %r{http://api.pusherapp.com/apps/20/path})
618
- end
619
-
620
- it "should use https if configured" do
621
- @client.encrypted = true
622
- call_api
623
- expect(WebMock).to have_requested(verb, %r{https://api.pusherapp.com})
624
- end
625
-
626
- # Note that the raw httpclient connection object is returned and
627
- # the response isn't handled (by handle_response) in the normal way.
628
- it "should return a httpclient connection object" do
629
- connection = call_api
630
- expect(connection.finished?).to be_truthy
631
- response = connection.pop
632
- expect(response.status).to eq(200)
633
- expect(response.body.read).to eq("{}")
634
- end
635
- end
636
- end
637
- end
638
-
639
- describe "async calling with eventmachine" do
640
- [[:get, :get_async], [:post, :post_async]].each do |verb, method|
641
- describe "##{method}" do
642
- before :each do
643
- @url_regexp = %r{api.pusherapp.com}
644
- stub_request(verb, @url_regexp).
645
- to_return(:status => 200, :body => "{}")
646
- end
647
-
648
- let(:call_api) { @client.send(method, '/path') }
649
-
650
- it "should use http by default" do
651
- EM.run {
652
- call_api.callback {
653
- expect(WebMock).to have_requested(verb, %r{http://api.pusherapp.com/apps/20/path})
654
- EM.stop
655
- }
656
- }
657
- end
658
-
659
- it "should use https if configured" do
660
- EM.run {
661
- @client.encrypted = true
662
- call_api.callback {
663
- expect(WebMock).to have_requested(verb, %r{https://api.pusherapp.com})
664
- EM.stop
665
- }
666
- }
667
- end
668
-
669
- it "should format the respose hash with symbols at first level" do
670
- EM.run {
671
- stub_request(verb, @url_regexp).to_return({
672
- :status => 200,
673
- :body => MultiJson.encode({'something' => {'a' => 'hash'}})
674
- })
675
- call_api.callback { |response|
676
- expect(response).to eq({
677
- :something => {'a' => 'hash'}
678
- })
679
- EM.stop
680
- }
681
- }
682
- end
683
-
684
- it "should errback with Pusher::Error on unsuccessful response" do
685
- EM.run {
686
- stub_request(verb, @url_regexp).to_return({:status => 400})
687
-
688
- call_api.errback { |e|
689
- expect(e.class).to eq(Pusher::Error)
690
- EM.stop
691
- }.callback {
692
- fail
693
- }
694
- }
695
- end
696
- end
697
- end
698
- end
699
- end
700
-
701
- describe "native notifications" do
702
- before :each do
703
- @client.app_id = "20"
704
- @client.key = "testytest"
705
- @client.secret = "mysupersecretkey"
706
- end
707
-
708
- it "should configure a native notification client using the pusher client object" do
709
- expect(@client.notification_client).to_not be(nil)
710
- end
711
-
712
- it "should use the default host if not provided" do
713
- expect(@client.notification_host).to eq("nativepush-cluster1.pusher.com")
714
- end
715
-
716
- it "should use a newly provided host" do
717
- @client.notification_host = "test.com"
718
- expect(@client.notification_host).to eq("test.com")
719
- end
720
-
721
- it "should set the native notification client host to the same one" do
722
- expect(@client.notification_host).to eq(@client.notification_client.host)
723
- end
724
-
725
- it "should raise an error if no interest is provided" do
726
- payload = {
727
- gcm: {
728
- notification: {
729
- title: "Hello",
730
- icon: "icon",
731
- }
732
- }
733
- }
734
-
735
- expect { @client.notify([], payload) }.to raise_error(Pusher::Error)
736
- end
737
-
738
- it "should send a request to the notifications endpoint" do
739
- notification_host_regexp = %r{nativepush-cluster1.pusher.com}
740
- payload = {
741
- interests: ["test"],
742
- gcm: {
743
- notification: {
744
- title: "Hello",
745
- icon: "icon",
746
- }
747
- }
748
- }
749
-
750
- stub_request(
751
- :post,
752
- notification_host_regexp,
753
- ).with(
754
- body: MultiJson.encode(payload)
755
- ).to_return({
756
- :status => 200,
757
- :body => MultiJson.encode({ :foo => "bar" })
758
- })
759
-
760
- res = @client.notify(["test"], payload)
761
- expect(res).to eq({foo: "bar"})
762
- end
763
- end
764
- end
765
-
766
- describe 'configuring cluster' do
767
- it 'should allow clients to specify the cluster only with the default host' do
768
- client = Pusher::Client.new({
769
- :scheme => 'http',
770
- :cluster => 'eu',
771
- :port => 80
772
- })
773
- expect(client.host).to eq('api-eu.pusher.com')
774
- end
775
-
776
- it 'should always have host override any supplied cluster value' do
777
- client = Pusher::Client.new({
778
- :scheme => 'http',
779
- :host => 'api.staging.pusherapp.com',
780
- :cluster => 'eu',
781
- :port => 80
782
- })
783
- expect(client.host).to eq('api.staging.pusherapp.com')
784
- end
785
- end
786
- end