eventflit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gemtest +0 -0
- data/.gitignore +24 -0
- data/.travis.yml +16 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +2 -0
- data/LICENSE +20 -0
- data/README.md +301 -0
- data/Rakefile +11 -0
- data/eventflit.gemspec +33 -0
- data/examples/async_message.rb +28 -0
- data/lib/eventflit.rb +69 -0
- data/lib/eventflit/channel.rb +185 -0
- data/lib/eventflit/client.rb +437 -0
- data/lib/eventflit/native_notification/client.rb +68 -0
- data/lib/eventflit/request.rb +109 -0
- data/lib/eventflit/resource.rb +36 -0
- data/lib/eventflit/version.rb +3 -0
- data/lib/eventflit/webhook.rb +110 -0
- data/spec/channel_spec.rb +170 -0
- data/spec/client_spec.rb +629 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/web_hook_spec.rb +117 -0
- metadata +207 -0
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,629 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'em-http'
|
4
|
+
|
5
|
+
describe Eventflit do
|
6
|
+
# The behaviour should be the same when using the Client object, or the
|
7
|
+
# 'global' client delegated through the Eventflit class
|
8
|
+
[lambda { Eventflit }, lambda { Eventflit::Client.new }].each do |client_gen|
|
9
|
+
before :each do
|
10
|
+
@client = client_gen.call
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'default configuration' do
|
14
|
+
it 'should be preconfigured for api host' do
|
15
|
+
expect(@client.host).to eq('service.eventflit.com')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should be preconfigured for port 80' do
|
19
|
+
expect(@client.port).to eq(80)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should use standard logger if no other logger if defined' do
|
23
|
+
Eventflit.logger.debug('foo')
|
24
|
+
expect(Eventflit.logger).to be_kind_of(Logger)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'logging configuration' do
|
29
|
+
it "can be configured to use any logger" do
|
30
|
+
logger = double("ALogger")
|
31
|
+
expect(logger).to receive(:debug).with('foo')
|
32
|
+
Eventflit.logger = logger
|
33
|
+
Eventflit.logger.debug('foo')
|
34
|
+
Eventflit.logger = nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "configuration using url" do
|
39
|
+
it "should be possible to configure everything by setting the url" do
|
40
|
+
@client.url = "test://somekey:somesecret@service.eventflit.com:8080/apps/87"
|
41
|
+
|
42
|
+
expect(@client.scheme).to eq('test')
|
43
|
+
expect(@client.host).to eq('service.eventflit.com')
|
44
|
+
expect(@client.port).to eq(8080)
|
45
|
+
expect(@client.key).to eq('somekey')
|
46
|
+
expect(@client.secret).to eq('somesecret')
|
47
|
+
expect(@client.app_id).to eq('87')
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should override scheme and port when setting encrypted=true after url" do
|
51
|
+
@client.url = "http://somekey:somesecret@service.eventflit.com:8080/apps/87"
|
52
|
+
@client.encrypted = true
|
53
|
+
|
54
|
+
expect(@client.scheme).to eq('https')
|
55
|
+
expect(@client.port).to eq(443)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should fail on bad urls" do
|
59
|
+
expect { @client.url = "gopher/somekey:somesecret@://api.staging.eventflitapp.co://m:8080\apps\87" }.to raise_error(URI::InvalidURIError)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should raise exception if app_id is not configured" do
|
63
|
+
@client.app_id = nil
|
64
|
+
expect {
|
65
|
+
@client.url
|
66
|
+
}.to raise_error(Eventflit::ConfigurationError)
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
describe 'configuring the cluster' do
|
72
|
+
it 'should set a new default host' do
|
73
|
+
@client.cluster = 'eu'
|
74
|
+
expect(@client.host).to eq('api-eu.eventflit.com')
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should be overridden by host if it comes after' do
|
78
|
+
@client.cluster = 'eu'
|
79
|
+
@client.host = 'api.staging.eventflit.com'
|
80
|
+
expect(@client.host).to eq('api.staging.eventflit.com')
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should be overridden by url if it comes after' do
|
84
|
+
@client.cluster = 'eu'
|
85
|
+
@client.url = "http://somekey:somesecret@service.eventflit.com:8080/apps/87"
|
86
|
+
|
87
|
+
expect(@client.host).to eq('service.eventflit.com')
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should override the url configuration if it comes after' do
|
91
|
+
@client.url = "http://somekey:somesecret@service.eventflit.com:8080/apps/87"
|
92
|
+
@client.cluster = 'eu'
|
93
|
+
expect(@client.host).to eq('api-eu.eventflit.com')
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should override the host configuration if it comes after' do
|
97
|
+
@client.host = 'api.staging.eventflit.com'
|
98
|
+
@client.cluster = 'eu'
|
99
|
+
expect(@client.host).to eq('api-eu.eventflit.com')
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
describe 'configuring a http proxy' do
|
104
|
+
it "should be possible to configure everything by setting the http_proxy" do
|
105
|
+
@client.http_proxy = 'http://someuser:somepassword@proxy.host.com:8080'
|
106
|
+
|
107
|
+
expect(@client.proxy).to eq({:scheme => 'http', :host => 'proxy.host.com', :port => 8080, :user => 'someuser', :password => 'somepassword'})
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe 'configuring from env' do
|
112
|
+
after do
|
113
|
+
ENV['EVENTFLIT_URL'] = nil
|
114
|
+
end
|
115
|
+
|
116
|
+
it "works" do
|
117
|
+
url = "http://somekey:somesecret@service.eventflit.com:8080/apps/87"
|
118
|
+
ENV['EVENTFLIT_URL'] = url
|
119
|
+
|
120
|
+
client = Eventflit::Client.from_env
|
121
|
+
expect(client.key).to eq("somekey")
|
122
|
+
expect(client.secret).to eq("somesecret")
|
123
|
+
expect(client.app_id).to eq("87")
|
124
|
+
expect(client.url.to_s).to eq("http://service.eventflit.com:8080/apps/87")
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe 'configuring from url' do
|
129
|
+
it "works" do
|
130
|
+
url = "http://somekey:somesecret@service.eventflit.com:8080/apps/87"
|
131
|
+
|
132
|
+
client = Eventflit::Client.from_url(url)
|
133
|
+
expect(client.key).to eq("somekey")
|
134
|
+
expect(client.secret).to eq("somesecret")
|
135
|
+
expect(client.app_id).to eq("87")
|
136
|
+
expect(client.url.to_s).to eq("http://service.eventflit.com:8080/apps/87")
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe 'when configured' do
|
141
|
+
before :each do
|
142
|
+
@client.app_id = '20'
|
143
|
+
@client.key = '12345678900000001'
|
144
|
+
@client.secret = '12345678900000001'
|
145
|
+
end
|
146
|
+
|
147
|
+
describe '#[]' do
|
148
|
+
before do
|
149
|
+
@channel = @client['test_channel']
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'should return a channel' do
|
153
|
+
expect(@channel).to be_kind_of(Eventflit::Channel)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should raise exception if app_id is not configured" do
|
157
|
+
@client.app_id = nil
|
158
|
+
expect {
|
159
|
+
@channel.trigger!('foo', 'bar')
|
160
|
+
}.to raise_error(Eventflit::ConfigurationError)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
describe '#channels' do
|
165
|
+
it "should call the correct URL and symbolise response correctly" do
|
166
|
+
api_path = %r{/apps/20/channels}
|
167
|
+
stub_request(:get, api_path).to_return({
|
168
|
+
:status => 200,
|
169
|
+
:body => MultiJson.encode('channels' => {
|
170
|
+
"channel1" => {},
|
171
|
+
"channel2" => {}
|
172
|
+
})
|
173
|
+
})
|
174
|
+
expect(@client.channels).to eq({
|
175
|
+
:channels => {
|
176
|
+
"channel1" => {},
|
177
|
+
"channel2" => {}
|
178
|
+
}
|
179
|
+
})
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe '#channel_info' do
|
184
|
+
it "should call correct URL and symbolise response" do
|
185
|
+
api_path = %r{/apps/20/channels/mychannel}
|
186
|
+
stub_request(:get, api_path).to_return({
|
187
|
+
:status => 200,
|
188
|
+
:body => MultiJson.encode({
|
189
|
+
'occupied' => false,
|
190
|
+
})
|
191
|
+
})
|
192
|
+
expect(@client.channel_info('mychannel')).to eq({
|
193
|
+
:occupied => false,
|
194
|
+
})
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe '#channel_users' do
|
199
|
+
it "should call correct URL and symbolise response" do
|
200
|
+
api_path = %r{/apps/20/channels/mychannel/users}
|
201
|
+
stub_request(:get, api_path).to_return({
|
202
|
+
:status => 200,
|
203
|
+
:body => MultiJson.encode({
|
204
|
+
'users' => [{ 'id' => 1 }]
|
205
|
+
})
|
206
|
+
})
|
207
|
+
expect(@client.channel_users('mychannel')).to eq({
|
208
|
+
:users => [{ 'id' => 1}]
|
209
|
+
})
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
describe '#authenticate' do
|
214
|
+
before :each do
|
215
|
+
@custom_data = {:uid => 123, :info => {:name => 'Foo'}}
|
216
|
+
end
|
217
|
+
|
218
|
+
it 'should return a hash with signature including custom data and data as json string' do
|
219
|
+
allow(MultiJson).to receive(:encode).with(@custom_data).and_return 'a json string'
|
220
|
+
|
221
|
+
response = @client.authenticate('test_channel', '1.1', @custom_data)
|
222
|
+
|
223
|
+
expect(response).to eq({
|
224
|
+
:auth => "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:a json string")}",
|
225
|
+
:channel_data => 'a json string'
|
226
|
+
})
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
describe '#trigger' do
|
232
|
+
before :each do
|
233
|
+
@api_path = %r{/apps/20/events}
|
234
|
+
stub_request(:post, @api_path).to_return({
|
235
|
+
:status => 200,
|
236
|
+
:body => MultiJson.encode({})
|
237
|
+
})
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should call correct URL" do
|
241
|
+
expect(@client.trigger(['mychannel'], 'event', {'some' => 'data'})).
|
242
|
+
to eq({})
|
243
|
+
end
|
244
|
+
|
245
|
+
it "should not allow too many channels" do
|
246
|
+
expect {
|
247
|
+
@client.trigger((0..11).map{|i| 'mychannel#{i}'},
|
248
|
+
'event', {'some' => 'data'}, {
|
249
|
+
:socket_id => "12.34"
|
250
|
+
})}.to raise_error(Eventflit::Error)
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should pass any parameters in the body of the request" do
|
254
|
+
@client.trigger(['mychannel', 'c2'], 'event', {'some' => 'data'}, {
|
255
|
+
:socket_id => "12.34"
|
256
|
+
})
|
257
|
+
expect(WebMock).to have_requested(:post, @api_path).with { |req|
|
258
|
+
parsed = MultiJson.decode(req.body)
|
259
|
+
expect(parsed["name"]).to eq('event')
|
260
|
+
expect(parsed["channels"]).to eq(["mychannel", "c2"])
|
261
|
+
expect(parsed["socket_id"]).to eq('12.34')
|
262
|
+
}
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should convert non string data to JSON before posting" do
|
266
|
+
@client.trigger(['mychannel'], 'event', {'some' => 'data'})
|
267
|
+
expect(WebMock).to have_requested(:post, @api_path).with { |req|
|
268
|
+
expect(MultiJson.decode(req.body)["data"]).to eq('{"some":"data"}')
|
269
|
+
}
|
270
|
+
end
|
271
|
+
|
272
|
+
it "should accept a single channel as well as an array" do
|
273
|
+
@client.trigger('mychannel', 'event', {'some' => 'data'})
|
274
|
+
expect(WebMock).to have_requested(:post, @api_path).with { |req|
|
275
|
+
expect(MultiJson.decode(req.body)["channels"]).to eq(['mychannel'])
|
276
|
+
}
|
277
|
+
end
|
278
|
+
|
279
|
+
%w[app_id key secret].each do |key|
|
280
|
+
it "should fail in missing #{key}" do
|
281
|
+
@client.public_send("#{key}=", nil)
|
282
|
+
expect {
|
283
|
+
@client.trigger('mychannel', 'event', {'some' => 'data'})
|
284
|
+
}.to raise_error(Eventflit::ConfigurationError)
|
285
|
+
expect(WebMock).not_to have_requested(:post, @api_path).with { |req|
|
286
|
+
expect(MultiJson.decode(req.body)["channels"]).to eq(['mychannel'])
|
287
|
+
}
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
describe '#trigger_batch' do
|
293
|
+
before :each do
|
294
|
+
@api_path = %r{/apps/20/batch_events}
|
295
|
+
stub_request(:post, @api_path).to_return({
|
296
|
+
:status => 200,
|
297
|
+
:body => MultiJson.encode({})
|
298
|
+
})
|
299
|
+
end
|
300
|
+
|
301
|
+
it "should call correct URL" do
|
302
|
+
expect(@client.trigger_batch(channel: 'mychannel', name: 'event', data: {'some' => 'data'})).
|
303
|
+
to eq({})
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should convert non string data to JSON before posting" do
|
307
|
+
@client.trigger_batch(
|
308
|
+
{channel: 'mychannel', name: 'event', data: {'some' => 'data'}},
|
309
|
+
{channel: 'mychannel', name: 'event', data: 'already encoded'},
|
310
|
+
)
|
311
|
+
expect(WebMock).to have_requested(:post, @api_path).with { |req|
|
312
|
+
parsed = MultiJson.decode(req.body)
|
313
|
+
expect(parsed).to eq(
|
314
|
+
"batch" => [
|
315
|
+
{ "channel" => "mychannel", "name" => "event", "data" => "{\"some\":\"data\"}"},
|
316
|
+
{ "channel" => "mychannel", "name" => "event", "data" => "already encoded"}
|
317
|
+
]
|
318
|
+
)
|
319
|
+
}
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
describe '#trigger_async' do
|
324
|
+
before :each do
|
325
|
+
@api_path = %r{/apps/20/events}
|
326
|
+
stub_request(:post, @api_path).to_return({
|
327
|
+
:status => 200,
|
328
|
+
:body => MultiJson.encode({})
|
329
|
+
})
|
330
|
+
end
|
331
|
+
|
332
|
+
it "should call correct URL" do
|
333
|
+
EM.run {
|
334
|
+
@client.trigger_async('mychannel', 'event', {'some' => 'data'}).callback { |r|
|
335
|
+
expect(r).to eq({})
|
336
|
+
EM.stop
|
337
|
+
}
|
338
|
+
}
|
339
|
+
end
|
340
|
+
|
341
|
+
it "should pass any parameters in the body of the request" do
|
342
|
+
EM.run {
|
343
|
+
@client.trigger_async('mychannel', 'event', {'some' => 'data'}, {
|
344
|
+
:socket_id => "12.34"
|
345
|
+
}).callback {
|
346
|
+
expect(WebMock).to have_requested(:post, @api_path).with { |req|
|
347
|
+
expect(MultiJson.decode(req.body)["socket_id"]).to eq('12.34')
|
348
|
+
}
|
349
|
+
EM.stop
|
350
|
+
}
|
351
|
+
}
|
352
|
+
end
|
353
|
+
|
354
|
+
it "should convert non string data to JSON before posting" do
|
355
|
+
EM.run {
|
356
|
+
@client.trigger_async('mychannel', 'event', {'some' => 'data'}).callback {
|
357
|
+
expect(WebMock).to have_requested(:post, @api_path).with { |req|
|
358
|
+
expect(MultiJson.decode(req.body)["data"]).to eq('{"some":"data"}')
|
359
|
+
}
|
360
|
+
EM.stop
|
361
|
+
}
|
362
|
+
}
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
[:get, :post].each do |verb|
|
367
|
+
describe "##{verb}" do
|
368
|
+
before :each do
|
369
|
+
@url_regexp = %r{service.eventflit.com}
|
370
|
+
stub_request(verb, @url_regexp).
|
371
|
+
to_return(:status => 200, :body => "{}")
|
372
|
+
end
|
373
|
+
|
374
|
+
let(:call_api) { @client.send(verb, '/path') }
|
375
|
+
|
376
|
+
it "should use http by default" do
|
377
|
+
call_api
|
378
|
+
expect(WebMock).to have_requested(verb, %r{http://service.eventflit.com/apps/20/path})
|
379
|
+
end
|
380
|
+
|
381
|
+
it "should use https if configured" do
|
382
|
+
@client.encrypted = true
|
383
|
+
call_api
|
384
|
+
expect(WebMock).to have_requested(verb, %r{https://service.eventflit.com})
|
385
|
+
end
|
386
|
+
|
387
|
+
it "should format the respose hash with symbols at first level" do
|
388
|
+
stub_request(verb, @url_regexp).to_return({
|
389
|
+
:status => 200,
|
390
|
+
:body => MultiJson.encode({'something' => {'a' => 'hash'}})
|
391
|
+
})
|
392
|
+
expect(call_api).to eq({
|
393
|
+
:something => {'a' => 'hash'}
|
394
|
+
})
|
395
|
+
end
|
396
|
+
|
397
|
+
it "should catch all http exceptions and raise a Eventflit::HTTPError wrapping the original error" do
|
398
|
+
stub_request(verb, @url_regexp).to_raise(HTTPClient::TimeoutError)
|
399
|
+
|
400
|
+
error = nil
|
401
|
+
begin
|
402
|
+
call_api
|
403
|
+
rescue => e
|
404
|
+
error = e
|
405
|
+
end
|
406
|
+
|
407
|
+
expect(error.class).to eq(Eventflit::HTTPError)
|
408
|
+
expect(error).to be_kind_of(Eventflit::Error)
|
409
|
+
expect(error.message).to eq('Exception from WebMock (HTTPClient::TimeoutError)')
|
410
|
+
expect(error.original_error.class).to eq(HTTPClient::TimeoutError)
|
411
|
+
end
|
412
|
+
|
413
|
+
it "should raise Eventflit::Error if call returns 400" do
|
414
|
+
stub_request(verb, @url_regexp).to_return({:status => 400})
|
415
|
+
expect { call_api }.to raise_error(Eventflit::Error)
|
416
|
+
end
|
417
|
+
|
418
|
+
it "should raise AuthenticationError if eventflit returns 401" do
|
419
|
+
stub_request(verb, @url_regexp).to_return({:status => 401})
|
420
|
+
expect { call_api }.to raise_error(Eventflit::AuthenticationError)
|
421
|
+
end
|
422
|
+
|
423
|
+
it "should raise Eventflit::Error if eventflit returns 404" do
|
424
|
+
stub_request(verb, @url_regexp).to_return({:status => 404})
|
425
|
+
expect { call_api }.to raise_error(Eventflit::Error, '404 Not found (/apps/20/path)')
|
426
|
+
end
|
427
|
+
|
428
|
+
it "should raise Eventflit::Error if eventflit returns 407" do
|
429
|
+
stub_request(verb, @url_regexp).to_return({:status => 407})
|
430
|
+
expect { call_api }.to raise_error(Eventflit::Error, 'Proxy Authentication Required')
|
431
|
+
end
|
432
|
+
|
433
|
+
it "should raise Eventflit::Error if eventflit returns 500" do
|
434
|
+
stub_request(verb, @url_regexp).to_return({:status => 500, :body => "some error"})
|
435
|
+
expect { call_api }.to raise_error(Eventflit::Error, 'Unknown error (status code 500): some error')
|
436
|
+
end
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
describe "async calling without eventmachine" do
|
441
|
+
[[:get, :get_async], [:post, :post_async]].each do |verb, method|
|
442
|
+
describe "##{method}" do
|
443
|
+
before :each do
|
444
|
+
@url_regexp = %r{service.eventflit.com}
|
445
|
+
stub_request(verb, @url_regexp).
|
446
|
+
to_return(:status => 200, :body => "{}")
|
447
|
+
end
|
448
|
+
|
449
|
+
let(:call_api) {
|
450
|
+
@client.send(method, '/path').tap { |c|
|
451
|
+
# Allow the async thread (inside httpclient) to run
|
452
|
+
while !c.finished?
|
453
|
+
sleep 0.01
|
454
|
+
end
|
455
|
+
}
|
456
|
+
}
|
457
|
+
|
458
|
+
it "should use http by default" do
|
459
|
+
call_api
|
460
|
+
expect(WebMock).to have_requested(verb, %r{http://service.eventflit.com/apps/20/path})
|
461
|
+
end
|
462
|
+
|
463
|
+
it "should use https if configured" do
|
464
|
+
@client.encrypted = true
|
465
|
+
call_api
|
466
|
+
expect(WebMock).to have_requested(verb, %r{https://service.eventflit.com})
|
467
|
+
end
|
468
|
+
|
469
|
+
# Note that the raw httpclient connection object is returned and
|
470
|
+
# the response isn't handled (by handle_response) in the normal way.
|
471
|
+
it "should return a httpclient connection object" do
|
472
|
+
connection = call_api
|
473
|
+
expect(connection.finished?).to be_truthy
|
474
|
+
response = connection.pop
|
475
|
+
expect(response.status).to eq(200)
|
476
|
+
expect(response.body.read).to eq("{}")
|
477
|
+
end
|
478
|
+
end
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
482
|
+
describe "async calling with eventmachine" do
|
483
|
+
[[:get, :get_async], [:post, :post_async]].each do |verb, method|
|
484
|
+
describe "##{method}" do
|
485
|
+
before :each do
|
486
|
+
@url_regexp = %r{service.eventflit.com}
|
487
|
+
stub_request(verb, @url_regexp).
|
488
|
+
to_return(:status => 200, :body => "{}")
|
489
|
+
end
|
490
|
+
|
491
|
+
let(:call_api) { @client.send(method, '/path') }
|
492
|
+
|
493
|
+
it "should use http by default" do
|
494
|
+
EM.run {
|
495
|
+
call_api.callback {
|
496
|
+
expect(WebMock).to have_requested(verb, %r{http://service.eventflit.com/apps/20/path})
|
497
|
+
EM.stop
|
498
|
+
}
|
499
|
+
}
|
500
|
+
end
|
501
|
+
|
502
|
+
it "should use https if configured" do
|
503
|
+
EM.run {
|
504
|
+
@client.encrypted = true
|
505
|
+
call_api.callback {
|
506
|
+
expect(WebMock).to have_requested(verb, %r{https://service.eventflit.com})
|
507
|
+
EM.stop
|
508
|
+
}
|
509
|
+
}
|
510
|
+
end
|
511
|
+
|
512
|
+
it "should format the respose hash with symbols at first level" do
|
513
|
+
EM.run {
|
514
|
+
stub_request(verb, @url_regexp).to_return({
|
515
|
+
:status => 200,
|
516
|
+
:body => MultiJson.encode({'something' => {'a' => 'hash'}})
|
517
|
+
})
|
518
|
+
call_api.callback { |response|
|
519
|
+
expect(response).to eq({
|
520
|
+
:something => {'a' => 'hash'}
|
521
|
+
})
|
522
|
+
EM.stop
|
523
|
+
}
|
524
|
+
}
|
525
|
+
end
|
526
|
+
|
527
|
+
it "should errback with Eventflit::Error on unsuccessful response" do
|
528
|
+
EM.run {
|
529
|
+
stub_request(verb, @url_regexp).to_return({:status => 400})
|
530
|
+
|
531
|
+
call_api.errback { |e|
|
532
|
+
expect(e.class).to eq(Eventflit::Error)
|
533
|
+
EM.stop
|
534
|
+
}.callback {
|
535
|
+
fail
|
536
|
+
}
|
537
|
+
}
|
538
|
+
end
|
539
|
+
end
|
540
|
+
end
|
541
|
+
end
|
542
|
+
end
|
543
|
+
|
544
|
+
describe "native notifications" do
|
545
|
+
before :each do
|
546
|
+
@client.app_id = "20"
|
547
|
+
@client.key = "testytest"
|
548
|
+
@client.secret = "mysupersecretkey"
|
549
|
+
end
|
550
|
+
|
551
|
+
it "should configure a native notification client using the eventflit client object" do
|
552
|
+
expect(@client.notification_client).to_not be(nil)
|
553
|
+
end
|
554
|
+
|
555
|
+
it "should use the default host if not provided" do
|
556
|
+
expect(@client.notification_host).to eq("push.eventflit.com")
|
557
|
+
end
|
558
|
+
|
559
|
+
it "should use a newly provided host" do
|
560
|
+
@client.notification_host = "test.com"
|
561
|
+
expect(@client.notification_host).to eq("test.com")
|
562
|
+
end
|
563
|
+
|
564
|
+
it "should set the native notification client host to the same one" do
|
565
|
+
expect(@client.notification_host).to eq(@client.notification_client.host)
|
566
|
+
end
|
567
|
+
|
568
|
+
it "should raise an error if no interest is provided" do
|
569
|
+
payload = {
|
570
|
+
gcm: {
|
571
|
+
notification: {
|
572
|
+
title: "Hello",
|
573
|
+
icon: "icon",
|
574
|
+
}
|
575
|
+
}
|
576
|
+
}
|
577
|
+
|
578
|
+
expect { @client.notify([], payload) }.to raise_error(Eventflit::Error)
|
579
|
+
end
|
580
|
+
|
581
|
+
it "should send a request to the notifications endpoint" do
|
582
|
+
notification_host_regexp = %r{push.eventflit.com}
|
583
|
+
payload = {
|
584
|
+
interests: ["test"],
|
585
|
+
gcm: {
|
586
|
+
notification: {
|
587
|
+
title: "Hello",
|
588
|
+
icon: "icon",
|
589
|
+
}
|
590
|
+
}
|
591
|
+
}
|
592
|
+
|
593
|
+
stub_request(
|
594
|
+
:post,
|
595
|
+
notification_host_regexp,
|
596
|
+
).with(
|
597
|
+
body: MultiJson.encode(payload)
|
598
|
+
).to_return({
|
599
|
+
:status => 200,
|
600
|
+
:body => MultiJson.encode({ :foo => "bar" })
|
601
|
+
})
|
602
|
+
|
603
|
+
res = @client.notify(["test"], payload)
|
604
|
+
expect(res).to eq({foo: "bar"})
|
605
|
+
end
|
606
|
+
end
|
607
|
+
end
|
608
|
+
|
609
|
+
describe 'configuring cluster' do
|
610
|
+
it 'should allow clients to specify the cluster only with the default host' do
|
611
|
+
client = Eventflit::Client.new({
|
612
|
+
:scheme => 'http',
|
613
|
+
:cluster => 'eu',
|
614
|
+
:port => 80
|
615
|
+
})
|
616
|
+
expect(client.host).to eq('api-eu.eventflit.com')
|
617
|
+
end
|
618
|
+
|
619
|
+
it 'should always have host override any supplied cluster value' do
|
620
|
+
client = Eventflit::Client.new({
|
621
|
+
:scheme => 'http',
|
622
|
+
:host => 'service.eventflit.com',
|
623
|
+
:cluster => 'eu',
|
624
|
+
:port => 80
|
625
|
+
})
|
626
|
+
expect(client.host).to eq('service.eventflit.com')
|
627
|
+
end
|
628
|
+
end
|
629
|
+
end
|