urbanairship-ruby 1.0.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.
@@ -0,0 +1,1088 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for "an Urbanairship client" do
4
+ before(:all) do
5
+ FakeWeb.allow_net_connect = false
6
+
7
+ # register_device
8
+ FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/apids/new_device_token", :status => ["201", "Created"])
9
+ FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/new_device_token", :status => ["201", "Created"])
10
+ FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/existing_device_token", :status => ["200", "OK"])
11
+ FakeWeb.register_uri(:put, "https://my_app_key:my_app_secret@go.urbanairship.com/api/device_tokens/device_token_one", :status => ["201", "Created"])
12
+ FakeWeb.register_uri(:put, /bad_key\:my_app_secret\@go\.urbanairship\.com/, :status => ["401", "Unauthorized"])
13
+
14
+ # unregister_device
15
+ FakeWeb.register_uri(:delete, /my_app_key\:my_app_secret\@go\.urbanairship.com\/api\/apids\/.+/, :status => ["204", "No Content"])
16
+ FakeWeb.register_uri(:delete, /my_app_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["204", "No Content"])
17
+ FakeWeb.register_uri(:delete, /bad_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["401", "Unauthorized"])
18
+
19
+ # device_info
20
+ FakeWeb.register_uri(:get, /my_app_key\:my_app_secret\@go\.urbanairship.com\/api\/apids\/.+/, :status => ["200", "OK"], :body => "{\"active\":true,\"alias\":null}")
21
+ FakeWeb.register_uri(:get, /my_app_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["200", "OK"], :body => "{\"active\":true,\"alias\":null}")
22
+ FakeWeb.register_uri(:get, /bad_key\:my_app_secret\@go\.urbanairship.com\/api\/device_tokens\/.+/, :status => ["401", "Unauthorized"])
23
+
24
+ # device_tokens
25
+ FakeWeb.register_uri(:get, "https://my_app_key:my_master_secret@go.urbanairship.com/api/device_tokens/", :status => ["200", "OK"], :body => '{"device_tokens":[{"device_token": "0101F9929660BAD9FFF31A0B5FA32620FA988507DFFA52BD6C1C1F4783EDA2DB","active": false,"alias": null, "tags": []}], "device_tokens_count":50, "active_device_tokens_count":55}')
26
+ FakeWeb.register_uri(:get, "https://my_app_key2:my_master_secret2@go.urbanairship.com/api/device_tokens/", :status => ["401", "OK"])
27
+
28
+ # push
29
+ FakeWeb.register_uri(:post, "https://my_app_key:my_master_secret@go.urbanairship.com/api/push/", :status => ["200", "OK"])
30
+ FakeWeb.register_uri(:post, "https://my_app_key2:my_master_secret2@go.urbanairship.com/api/push/", :status => ["400", "Bad Request"])
31
+ FakeWeb.register_uri(:post, /bad_key\:my_master_secret\@go\.urbanairship\.com/, :status => ["401", "Unauthorized"])
32
+
33
+ #schedules
34
+ FakeWeb.register_uri(:get, "https://my_app_key:my_master_secret@go.urbanairship.com/api/schedules/", :status => ["200", "OK"], :body => "[{\"schedules\":[\"schedule1\",\"schedule2\"]}]")
35
+ FakeWeb.register_uri(:get, "https://my_app_key2:my_master_secret2@go.urbanairship.com/api/schedules/", :status => ["500", "Internal Server Error"])
36
+ FakeWeb.register_uri(:get, "https://bad_key:my_master_secret@go.urbanairship.com/api/schedules/", :status => ["401", "Unauthorized"])
37
+
38
+ #schedule
39
+ FakeWeb.register_uri(:get, "https://my_app_key:my_master_secret@go.urbanairship.com/api/schedules/a_schedule", :status => ["200", "OK"], :body => "{\"schedule\":\"a_schedule\"}")
40
+ FakeWeb.register_uri(:get, "https://my_app_key2:my_master_secret2@go.urbanairship.com/api/schedules/a_schedule", :status => ["500", "Internal Server Error"])
41
+ FakeWeb.register_uri(:get, "https://bad_key:my_master_secret@go.urbanairship.com/api/schedules/a_schedule", :status => ["401", "Unauthorized"])
42
+
43
+ #create_schedule
44
+ FakeWeb.register_uri(:post, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/schedules/, :status => ["200", "OK"], :body => "{\"schedule\":\"new_schedule\"}")
45
+ FakeWeb.register_uri(:post, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/schedules/, :status => ["500", "Internal Server Error"])
46
+
47
+ #update_schedule
48
+ FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/schedules\/existing_schedule/, :status => ["201", "OK"], :body => "{\"schedule\":\"new_schedule\"}")
49
+ FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/schedules\/new_schedule/, :status => ["500", "Internal Server Error"])
50
+ FakeWeb.register_uri(:put, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/schedules\/a_schedule/, :status => ["500", "Internal Server Error"])
51
+
52
+ # delete_schedule
53
+ FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/schedules\/[0-9]+/, :status => ["204", "No Content"])
54
+ FakeWeb.register_uri(:delete, /bad_key\:my_master_secret\@go\.urbanairship.com\/api\/schedules\/[0-9]+/, :status => ["401", "Unauthorized"])
55
+
56
+ # feedback
57
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["200", "OK"], :body => "[{\"device_token\":\"token\",\"marked_inactive_on\":\"2010-10-14T19:15:13Z\",\"alias\":\"my_alias\"}]")
58
+ FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/device_tokens\/feedback/, :status => ["500", "Internal Server Error"])
59
+
60
+ #tags
61
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags/, :status => ["200", "OK"], :body => "[{\"tags\":[\"tag1\",\"tag2\"]}]")
62
+ FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/tags/, :status => ["500", "Internal Server Error"])
63
+
64
+ #add_tag
65
+ FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/new_tag/, :status => ["200", "OK"])
66
+ FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/existing_tag/, :status => ["201", "OK"])
67
+ FakeWeb.register_uri(:put, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/tags\/a_tag/, :status => ["500", "Internal Server Error"])
68
+
69
+ #remove_tag
70
+ FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/non_deleted_tag/, :status => ["204", "OK"])
71
+ FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/deleted_tag/, :status => ["404", "OK"])
72
+ FakeWeb.register_uri(:delete, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/tags\/a_tag/, :status => ["500", "Internal Server Error"])
73
+
74
+ #tags_for_device_tokens
75
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/valid_device_token\/tags/, :status => ["200", "OK"], :body => "[{\"tags\":[\"tag1\",\"tag2\"]}]")
76
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/invalid_device_token\/tags/, :status => ["404", "OK"])
77
+ FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/device_tokens\/a_device_token\/tags/, :status => ["500", "Internal Server Error"])
78
+
79
+ ##tag_device
80
+ FakeWeb.register_uri(:post, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/new_tag/, :status => ["201", "OK"], :body => '{"device_tokens":{"add":["valid_device_token"]}')
81
+ FakeWeb.register_uri(:post, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/existing_tag/, :status => ["200", "OK"], :body => '{"device_tokens":{"add":["valid_device_token"]}')
82
+ FakeWeb.register_uri(:post, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/tags\/a_tag/, :status => ["500", "Internal Server Error"], :body => '{"device_tokens":{"add":["a_device_token"]}')
83
+
84
+ #untag_device
85
+ FakeWeb.register_uri(:post, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/existing_tag/, :status => ["204", "OK"], :body => '{"device_tokens":{"remove":["valid_device_token"]}')
86
+ FakeWeb.register_uri(:post, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/tags\/non_existant_tag/, :status => ["404", "OK"], :body => '{"device_tokens":{"remove":["valid_device_token"]}')
87
+ FakeWeb.register_uri(:post, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/tags\/a_tag/, :status => ["500", "Internal Server Error"], :body => '{"device_tokens":{"remove":["a_device_token"]}')
88
+
89
+ #Get Segment
90
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/segments\/valid_id/, :status => ["200", "OK"], :body => '{"display_name":"Male in NY", "criteria":{"and":[{"tag":"male"}, {"tag":"in NY"}]}}')
91
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/segments\/not_found/, :status => ["404", "OK"])
92
+ FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/segments\/invalid_segment/, :status => ["500", "Internal Server Error"])
93
+
94
+ #Get List of Segments
95
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/segments$/, :status => ["200", "OK"], :body => '{"segments":[{"id":"abcd-efgh-ijkl", "display_name":"test1", "creation_date":1360950614201, "modification_date":1360950614201}, {"id": "mnop-qrst-uvwx", "display_name": "test2", "creation_date":1360950614202, "modification_date":1360950614202}]}')
96
+ FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/segments$/, :status => ["500", "Internal Server Error"])
97
+
98
+ #Create Segment
99
+ FakeWeb.register_uri(:post, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/segments/, :status => ["201", "OK"])
100
+ FakeWeb.register_uri(:post, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/segments/, :status => ["500", "Internal Server Error"])
101
+
102
+ #Update Segment
103
+ FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/segments\/valid_segment/, :status => ["200", "OK"])
104
+ FakeWeb.register_uri(:put, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/segments\/invalid_segment/, :status => ["404", "OK"])
105
+ FakeWeb.register_uri(:put, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/segments\/a_segment/, :status => ["500", "Internal Server Error"])
106
+
107
+ #Delete Segment
108
+ FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/segments\/valid_segment/, :status => ["204", "OK"])
109
+ FakeWeb.register_uri(:delete, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/segments\/invalid_segment/, :status => ["404", "OK"])
110
+ FakeWeb.register_uri(:delete, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/segments\/a_segment/, :status => ["500", "Internal Server Error"])
111
+
112
+ #device_tokens_count
113
+ FakeWeb.register_uri(:get, /my_app_key\:my_master_secret\@go\.urbanairship.com\/api\/device_tokens\/count/, :status => ["200", "OK"], :body => "{\"device_tokens_count\":50, \"active_device_tokens_count\":55}")
114
+ FakeWeb.register_uri(:get, /my_app_key2\:my_master_secret2\@go\.urbanairship.com\/api\/device_tokens\/count/, :status => ["500", "Internal Server Error"])
115
+ end
116
+
117
+ describe "configuration" do
118
+ it "enables you to configure the application key" do
119
+ subject.application_key.should be_nil
120
+ subject.application_key = "asdf1234"
121
+ subject.application_key.should == "asdf1234"
122
+ end
123
+
124
+ it "enables you to configure the application secret" do
125
+ subject.application_secret.should be_nil
126
+ subject.application_secret = "asdf1234"
127
+ subject.application_secret.should == "asdf1234"
128
+ end
129
+
130
+ it "enables you to configure the master secret" do
131
+ subject.master_secret.should be_nil
132
+ subject.master_secret = "asdf1234"
133
+ subject.master_secret.should == "asdf1234"
134
+ end
135
+ end
136
+
137
+ describe "::tags" do
138
+ before(:each) do
139
+ subject.application_key = "my_app_key"
140
+ subject.master_secret = "my_master_secret"
141
+ end
142
+
143
+ it "raises an error if call is made without an app key and master secret configured" do
144
+ subject.application_key = nil
145
+ subject.master_secret = nil
146
+
147
+ lambda {
148
+ subject.tags
149
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
150
+ end
151
+
152
+ it "uses app key and secret to sign the request" do
153
+ subject.tags
154
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
155
+ end
156
+
157
+ it "returns valid tags" do
158
+ response = subject.tags
159
+ response.first.should include("tags")
160
+ response.first["tags"].should include("tag1")
161
+ response.first["tags"].should include("tag2")
162
+ end
163
+
164
+ it "success? is false when the call doesn't return 200" do
165
+ subject.application_key = "my_app_key2"
166
+ subject.master_secret = "my_master_secret2"
167
+ subject.tags.success?.should == false
168
+ end
169
+
170
+ end
171
+
172
+ describe "::add_tag" do
173
+ before(:each) do
174
+ subject.application_key = "my_app_key"
175
+ subject.master_secret = "my_master_secret"
176
+ end
177
+
178
+ it "raises an error if call is made without an app key and master secret configured" do
179
+ subject.application_key = nil
180
+ subject.master_secret = nil
181
+
182
+ lambda {
183
+ subject.add_tag('a_tag')
184
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
185
+ end
186
+
187
+ it "uses app key and secret to sign the request" do
188
+ subject.add_tag('new_tag')
189
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
190
+ end
191
+
192
+ it "adds a new tag" do
193
+ subject.add_tag('new_tag').success?.should == true
194
+ subject.add_tag('new_tag').code.should == "200"
195
+ end
196
+
197
+ it "adds an exisiting tag" do
198
+ subject.add_tag('existing_tag').success?.should == true
199
+ subject.add_tag('existing_tag').code.should == "201"
200
+ end
201
+
202
+ it "success? is false when the call doesn't return 200 or 201" do
203
+ subject.application_key = "my_app_key2"
204
+ subject.master_secret = "my_master_secret2"
205
+ subject.add_tag('a_tag').success?.should == false
206
+ end
207
+
208
+ it "sets the content-type to text/plain" do
209
+ subject.add_tag('new_tag')
210
+ FakeWeb.last_request['content-type'].should == "text/plain"
211
+ end
212
+ end
213
+
214
+ describe "::remove_tag" do
215
+ before(:each) do
216
+ subject.application_key = "my_app_key"
217
+ subject.master_secret = "my_master_secret"
218
+ end
219
+
220
+ it "raises an error if call is made without an app key and master secret configured" do
221
+ subject.application_key = nil
222
+ subject.master_secret = nil
223
+
224
+ lambda {
225
+ subject.remove_tag('a_tag')
226
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
227
+ end
228
+
229
+ it "uses app key and secret to sign the request" do
230
+ subject.remove_tag('non_deleted_tag')
231
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
232
+ end
233
+
234
+ it "removes a tag that has not been removed yet" do
235
+ subject.remove_tag('non_deleted_tag').success?.should == true
236
+ subject.remove_tag('non_deleted_tag').code.should == "204"
237
+ end
238
+
239
+ it "attempts to remove an already deleted tag" do
240
+ subject.remove_tag('deleted_tag').success?.should == false
241
+ subject.remove_tag('deleted_tag').code.should == "404"
242
+ end
243
+
244
+ it "success? is false when the call doesn't return 204" do
245
+ subject.application_key = "my_app_key2"
246
+ subject.master_secret = "my_master_secret2"
247
+ subject.add_tag('a_tag').success?.should == false
248
+ end
249
+ end
250
+
251
+ describe "::tags_for_device" do
252
+ before(:each) do
253
+ subject.application_key = "my_app_key"
254
+ subject.master_secret = "my_master_secret"
255
+ end
256
+
257
+ it "raises an error if call is made without an app key and master secret configured" do
258
+ subject.application_key = nil
259
+ subject.master_secret = nil
260
+
261
+ lambda {
262
+ subject.tags_for_device('a_device_token')
263
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
264
+ end
265
+
266
+ it "uses app key and secret to sign the request" do
267
+ subject.tags_for_device('valid_device_token')
268
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
269
+ end
270
+
271
+ it "returns valid tags for a device" do
272
+ response = subject.tags_for_device('valid_device_token')
273
+ response.first.should include("tags")
274
+ response.first["tags"].should include("tag1")
275
+ response.first["tags"].should include("tag2")
276
+ response.code.should == "200"
277
+ end
278
+
279
+ it "returns invalid response for device token that is not found or registered" do
280
+ response = subject.tags_for_device('invalid_device_token')
281
+ response.code.should == "404"
282
+ end
283
+
284
+ it "success? is false when the call doesn't return 200" do
285
+ subject.application_key = "my_app_key2"
286
+ subject.master_secret = "my_master_secret2"
287
+ subject.tags_for_device('a_device_token').success?.should == false
288
+ end
289
+ end
290
+
291
+ describe "::tag_device" do
292
+ before(:each) do
293
+ subject.application_key = "my_app_key"
294
+ subject.master_secret = "my_master_secret"
295
+ end
296
+
297
+ it "raises an error if call is made without an app key and master secret configured" do
298
+ subject.application_key = nil
299
+ subject.master_secret = nil
300
+
301
+ lambda {
302
+ subject.tag_device({:device_token => 'a_device_token', :tag => 'a_tag'})
303
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
304
+ end
305
+
306
+ it "uses app key and secret to sign the request" do
307
+ subject.tag_device({:device_token => 'valid_device_token', :tag => 'new_tag'})
308
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
309
+ end
310
+
311
+ it "adds a valid device token to tag" do
312
+ response = subject.tag_device({:device_token => 'valid_device_token', :tag => 'new_tag'})
313
+ response.code.should == "201"
314
+ response.success?.should == true
315
+ end
316
+
317
+ it "adds a valid device token to an existing tag" do
318
+ response = subject.tag_device({:device_token => 'valid_device_token', :tag => 'existing_tag'})
319
+ response.code.should == "204"
320
+ response.success?.should == true
321
+ end
322
+ end
323
+
324
+ describe "::untag_device" do
325
+ before(:each) do
326
+ subject.application_key = "my_app_key"
327
+ subject.master_secret = "my_master_secret"
328
+ end
329
+
330
+ it "raises an error if call is made without an app key and master secret configured" do
331
+ subject.application_key = nil
332
+ subject.master_secret = nil
333
+
334
+ lambda {
335
+ subject.untag_device({:device_token => 'a_device_token', :tag => 'a_tag'})
336
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
337
+ end
338
+
339
+ it "uses app key and secret to sign the request" do
340
+ subject.untag_device({:device_token => 'valid_device_token', :tag => 'existing_tag'})
341
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
342
+ end
343
+
344
+ it "removes a valid device token from a tag" do
345
+ response = subject.untag_device({:device_token => 'valid_device_token', :tag => 'existing_tag'})
346
+ response.code.should == "204"
347
+ response.success?.should == true
348
+ end
349
+
350
+ it "removes a device token from a tag that it is not associated with" do
351
+ response = subject.untag_device({:device_token => 'valid_device_token', :tag => 'non_existant_tag'})
352
+ response.code.should == "404"
353
+ response.success?.should == false
354
+ end
355
+ end
356
+
357
+ describe "::register_device" do
358
+ before(:each) do
359
+ @valid_params = {:alias => 'one'}
360
+ subject.application_key = "my_app_key"
361
+ subject.application_secret = "my_app_secret"
362
+ end
363
+
364
+ it "raises an error if call is made without an app key and secret configured" do
365
+ subject.application_key = nil
366
+ subject.application_secret = nil
367
+
368
+ lambda {
369
+ subject.register_device("asdf1234")
370
+ }.should raise_error(RuntimeError, "Must configure application_key, application_secret before making this request.")
371
+ end
372
+
373
+ it "uses app key and secret to sign the request" do
374
+ subject.register_device("new_device_token")
375
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_app_secret').chomp}"
376
+ end
377
+
378
+ it "takes and sends a device token" do
379
+ subject.register_device("new_device_token")
380
+ FakeWeb.last_request.path.should == "/api/device_tokens/new_device_token"
381
+ end
382
+
383
+ it "returns true when the device is registered for the first time" do
384
+ subject.register_device("new_device_token").success?.should == true
385
+ end
386
+
387
+ it "returns true when the device is registered again" do
388
+ subject.register_device("existing_device_token").success?.should == true
389
+ end
390
+
391
+ it "returns false when the authorization is invalid" do
392
+ subject.application_key = "bad_key"
393
+ subject.register_device("new_device_token").success?.should == false
394
+ end
395
+
396
+ it "accepts an alias" do
397
+ subject.register_device("device_token_one", @valid_params).success?.should == true
398
+ end
399
+
400
+ it "adds alias to the JSON payload" do
401
+ subject.register_device("device_token_one", @valid_params)
402
+ request_json['alias'].should == "one"
403
+ end
404
+
405
+ it "converts alias param to string" do
406
+ subject.register_device("device_token_one", :alias => 11)
407
+ request_json['alias'].should be_a_kind_of String
408
+ end
409
+
410
+ it "uses the iOS interface by default" do
411
+ subject.register_device("new_device_token")
412
+ FakeWeb.last_request.path.should == "/api/device_tokens/new_device_token"
413
+ end
414
+
415
+ it "uses the android interface if 'provider' configuration option is set to :android Symbol" do
416
+ subject.provider = :android
417
+ subject.register_device("new_device_token")
418
+ FakeWeb.last_request.path.should == "/api/apids/new_device_token"
419
+ subject.provider = nil
420
+ end
421
+
422
+ it "uses the android interface if 'provider' configuration option is set to 'android' String" do
423
+ subject.provider = 'android'
424
+ subject.register_device("new_device_token")
425
+ FakeWeb.last_request.path.should == "/api/apids/new_device_token"
426
+ subject.provider = nil
427
+ end
428
+
429
+ it "uses the android interface if :provider Symbol key is passed an :android Symbol value" do
430
+ subject.register_device("new_device_token", :provider => :android)
431
+ FakeWeb.last_request.path.should == "/api/apids/new_device_token"
432
+ end
433
+
434
+ it "uses the android interface if 'provider' Symbol key is passed an 'android' String value" do
435
+ subject.register_device("new_device_token", :provider => "android")
436
+ FakeWeb.last_request.path.should == "/api/apids/new_device_token"
437
+ end
438
+
439
+ end
440
+
441
+ describe "::unregister_device" do
442
+ before(:each) do
443
+ subject.application_key = "my_app_key"
444
+ subject.application_secret = "my_app_secret"
445
+ end
446
+
447
+ it "raises an error if call is made without an app key and secret configured" do
448
+ subject.application_key = nil
449
+ subject.application_secret = nil
450
+
451
+ lambda {
452
+ subject.unregister_device("asdf1234")
453
+ }.should raise_error(RuntimeError, "Must configure application_key, application_secret before making this request.")
454
+ end
455
+
456
+ it "uses app key and secret to sign the request" do
457
+ subject.unregister_device("key_to_delete")
458
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_app_secret').chomp}"
459
+ end
460
+
461
+ it "sends the key that needs to be deleted" do
462
+ subject.unregister_device("key_to_delete")
463
+ FakeWeb.last_request.path.should == "/api/device_tokens/key_to_delete"
464
+ end
465
+
466
+ it "returns true when the device is successfully unregistered" do
467
+ subject.unregister_device("key_to_delete").success?.should == true
468
+ FakeWeb.last_request.body.should be_nil
469
+ end
470
+
471
+ it "returns false when the authorization is invalid" do
472
+ subject.application_key = "bad_key"
473
+ subject.unregister_device("key_to_delete").success?.should == false
474
+ end
475
+
476
+ it "uses the android interface if 'provider' configuration option is set to :android Symbol" do
477
+ subject.provider = :android
478
+ subject.unregister_device("new_device_token")
479
+ FakeWeb.last_request.path.should == "/api/apids/new_device_token"
480
+ subject.provider = nil
481
+ end
482
+
483
+ it "uses the android interface if 'provider' configuration option is set to 'android' String" do
484
+ subject.provider = 'android'
485
+ subject.unregister_device("new_device_token")
486
+ FakeWeb.last_request.path.should == "/api/apids/new_device_token"
487
+ subject.provider = nil
488
+ end
489
+
490
+ it "uses the android interface if :provider Symbol key is passed with an :android Symbol value" do
491
+ subject.unregister_device("new_device_token", :provider => :android)
492
+ FakeWeb.last_request.path.should == "/api/apids/new_device_token"
493
+ end
494
+
495
+ it "uses the android interface if :provider Symbol key is passed with an 'android' String value" do
496
+ subject.unregister_device("new_device_token", :provider => "android")
497
+ FakeWeb.last_request.path.should == "/api/apids/new_device_token"
498
+ end
499
+
500
+ end
501
+
502
+ describe "::device_info" do
503
+ before(:each) do
504
+ @valid_params = {:alias => 'one'}
505
+ subject.application_key = "my_app_key"
506
+ subject.application_secret = "my_app_secret"
507
+ end
508
+
509
+ it "raises an error if call is made without an app key and secret configured" do
510
+ subject.application_key = nil
511
+ subject.application_secret = nil
512
+
513
+ lambda {
514
+ subject.device_info("asdf1234")
515
+ }.should raise_error(RuntimeError, "Must configure application_key, application_secret before making this request.")
516
+ end
517
+
518
+ it "uses app key and secret to sign the request" do
519
+ subject.device_info("device_token")
520
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_app_secret').chomp}"
521
+ end
522
+
523
+ it "takes and sends a device token" do
524
+ subject.device_info("device_token")
525
+ FakeWeb.last_request.path.should == "/api/device_tokens/device_token"
526
+ end
527
+
528
+ it "returns false when the authorization is invalid" do
529
+ subject.application_key = "bad_key"
530
+ subject.device_info("device_token").success?.should == false
531
+ end
532
+
533
+ it "uses the iOS interface by default" do
534
+ subject.device_info("device_token")
535
+ FakeWeb.last_request.path.should == "/api/device_tokens/device_token"
536
+ end
537
+
538
+ it "uses the android interface if 'provider' configuration option is set to :android Symbol" do
539
+ subject.provider = :android
540
+ subject.device_info("device_token")
541
+ FakeWeb.last_request.path.should == "/api/apids/device_token"
542
+ subject.provider = nil
543
+ end
544
+
545
+ it "uses the android interface if 'provider' configuration option is set to 'android' String" do
546
+ subject.provider = 'android'
547
+ subject.device_info("device_token")
548
+ FakeWeb.last_request.path.should == "/api/apids/device_token"
549
+ subject.provider = nil
550
+ end
551
+
552
+ it "uses the android interface if :provider Symbol key is passed an :android Symbol value" do
553
+ subject.device_info("device_token", :provider => :android)
554
+ FakeWeb.last_request.path.should == "/api/apids/device_token"
555
+ end
556
+
557
+ it "uses the android interface if 'provider' Symbol key is passed an 'android' String value" do
558
+ subject.device_info("device_token", :provider => "android")
559
+ FakeWeb.last_request.path.should == "/api/apids/device_token"
560
+ end
561
+ end
562
+
563
+ describe "::schedules" do
564
+ before(:each) do
565
+ subject.application_key = "my_app_key"
566
+ subject.master_secret = "my_master_secret"
567
+ end
568
+
569
+ it "raises an error if call is made without an app key and master secret configured" do
570
+ subject.application_key = nil
571
+ subject.master_secret = nil
572
+
573
+ lambda {
574
+ subject.schedules
575
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
576
+ end
577
+
578
+ it "uses app key and secret to sign the request" do
579
+ subject.schedules
580
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
581
+ end
582
+
583
+ it "returns false when the authorization is invalid" do
584
+ subject.application_key = "bad_key"
585
+ subject.schedules.success?.should == false
586
+ end
587
+
588
+ it "returns valid schedules" do
589
+ response = subject.schedules
590
+ response.first.should include("schedules")
591
+ response.first["schedules"].should include("schedule1")
592
+ response.first["schedules"].should include("schedule2")
593
+ end
594
+
595
+ it "success? is false when the call doesn't return 200" do
596
+ subject.application_key = "my_app_key2"
597
+ subject.master_secret = "my_master_secret2"
598
+ subject.tags.success?.should == false
599
+ end
600
+ end
601
+
602
+ describe "::schedule" do
603
+ before(:each) do
604
+ subject.application_key = "my_app_key"
605
+ subject.master_secret = "my_master_secret"
606
+ end
607
+
608
+ it "raises an error if call is made without an app key and master secret configured" do
609
+ subject.application_key = nil
610
+ subject.master_secret = nil
611
+
612
+ lambda {
613
+ subject.schedule('a_schedule')
614
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
615
+ end
616
+
617
+ it "uses app key and secret to sign the request" do
618
+ subject.schedule('a_schedule')
619
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
620
+ end
621
+
622
+ it "returns false when the authorization is invalid" do
623
+ subject.application_key = "bad_key"
624
+ subject.schedule('a_schedule').success?.should == false
625
+ end
626
+
627
+ it "sends the key of schedule that needs to be returned" do
628
+ subject.schedule("a_schedule")
629
+ FakeWeb.last_request.path.should == "/api/schedules/a_schedule"
630
+ end
631
+
632
+ it "returns valid schedule" do
633
+ response = subject.schedule("a_schedule")
634
+ response.success?.should == true
635
+ response.should include("schedule")
636
+ response["schedule"].should include("a_schedule")
637
+ end
638
+
639
+ it "success? is false when the call doesn't return 200" do
640
+ subject.application_key = "my_app_key2"
641
+ subject.master_secret = "my_master_secret2"
642
+ subject.tags.success?.should == false
643
+ end
644
+ end
645
+
646
+ describe "::create_schedule" do
647
+ before(:each) do
648
+ subject.application_key = "my_app_key"
649
+ subject.master_secret = "my_master_secret"
650
+ end
651
+
652
+ it "raises an error if call is made without an app key and master secret configured" do
653
+ subject.application_key = nil
654
+ subject.master_secret = nil
655
+
656
+ lambda {
657
+ subject.create_schedule({})
658
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
659
+ end
660
+
661
+ it "uses app key and secret to sign the request" do
662
+ subject.create_schedule({})
663
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
664
+ end
665
+
666
+ it "create a new schedule" do
667
+ subject.create_schedule({}).success?.should == true
668
+ subject.create_schedule({}).code.should == "200"
669
+ end
670
+
671
+ it "success? is false when the call doesn't return 200" do
672
+ subject.application_key = "my_app_key2"
673
+ subject.master_secret = "my_master_secret2"
674
+ subject.create_schedule({}).success?.should == false
675
+ end
676
+
677
+ it "sets the content-type to application/json" do
678
+ subject.create_schedule({})
679
+ FakeWeb.last_request['content-type'].should == "application/json"
680
+ end
681
+ end
682
+
683
+ describe "::update_schedule" do
684
+ before(:each) do
685
+ subject.application_key = "my_app_key"
686
+ subject.master_secret = "my_master_secret"
687
+ end
688
+
689
+ it "raises an error if call is made without an app key and master secret configured" do
690
+ subject.application_key = nil
691
+ subject.master_secret = nil
692
+
693
+ lambda {
694
+ subject.update_schedule('existing_schedule', {})
695
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
696
+ end
697
+
698
+ it "uses app key and secret to sign the request" do
699
+ subject.update_schedule('existing_schedule', {})
700
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
701
+ end
702
+
703
+ it "sends the key that needs to be updated" do
704
+ subject.update_schedule('existing_schedule', {})
705
+ FakeWeb.last_request.path.should == "/api/schedules/existing_schedule"
706
+ end
707
+
708
+ it "updates an existing schedule" do
709
+ subject.update_schedule('existing_schedule', {}).success?.should == true
710
+ subject.update_schedule('existing_schedule', {}).code.should == "201"
711
+ end
712
+
713
+ it "success? is false when updating a new schedule" do
714
+ subject.update_schedule('new_schedule', {}).success?.should == false
715
+ end
716
+
717
+ it "success? is false when the call doesn't return 201" do
718
+ subject.application_key = "my_app_key2"
719
+ subject.master_secret = "my_master_secret2"
720
+ subject.update_schedule('a_schedule', {}).success?.should == false
721
+ end
722
+
723
+ it "sets the content-type to application/json" do
724
+ subject.update_schedule('existing_schedule', {})
725
+ FakeWeb.last_request['content-type'].should == "application/json"
726
+ end
727
+ end
728
+
729
+ describe "::delete_schedule" do
730
+ before(:each) do
731
+ subject.application_key = "my_app_key"
732
+ subject.master_secret = "my_master_secret"
733
+ end
734
+
735
+ it "raises an error if call is made without an app key and master secret configured" do
736
+ subject.application_key = nil
737
+ subject.master_secret = nil
738
+
739
+ lambda {
740
+ subject.delete_schedule("123456789")
741
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
742
+ end
743
+
744
+ it "uses app key and secret to sign the request" do
745
+ subject.delete_schedule("123456789")
746
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
747
+ end
748
+
749
+ it "sends the key that needs to be deleted" do
750
+ subject.delete_schedule("123456789")
751
+ FakeWeb.last_request.path.should == "/api/schedules/123456789"
752
+ end
753
+
754
+ it "sends the key that needs to be deleted" do
755
+ subject.delete_schedule(123456789)
756
+ FakeWeb.last_request.path.should == "/api/schedules/123456789"
757
+ end
758
+
759
+ it "returns true when the push notification is successfully deleted" do
760
+ subject.delete_schedule("123456789").success?.should == true
761
+ FakeWeb.last_request.body.should be_nil
762
+ end
763
+
764
+ it "returns false when the authorization is invalid" do
765
+ subject.application_key = "bad_key"
766
+ subject.delete_schedule("123456789").success?.should == false
767
+ end
768
+ end
769
+
770
+ describe "::push" do
771
+ before(:each) do
772
+ @valid_params = {:device_tokens => ['device_token_one', 'device_token_two'], :aps => {:alert => 'foo'}}
773
+ subject.application_key = "my_app_key"
774
+ subject.master_secret = "my_master_secret"
775
+ end
776
+
777
+ it "raises an error if call is made without an app key and master secret configured" do
778
+ subject.application_key = nil
779
+ subject.master_secret = nil
780
+
781
+ lambda {
782
+ subject.push(@valid_params)
783
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
784
+ end
785
+
786
+ it "uses app key and secret to sign the request" do
787
+ subject.push(@valid_params)
788
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
789
+ end
790
+
791
+ it "returns true when it successfully pushes a notification" do
792
+ subject.push(@valid_params).success?.should == true
793
+ end
794
+
795
+ it "returns false when the authorization is invalid" do
796
+ subject.application_key = "bad_key"
797
+ subject.push(@valid_params).success?.should == false
798
+ end
799
+
800
+ it "uses v3 of the API by default" do
801
+ subject.push(@valid_params).success?.should == true
802
+ FakeWeb.last_request["Accept"].should == "application/vnd.urbanairship+json; version=3;"
803
+ end
804
+
805
+ it "returns false if urbanairship responds with a non-200 response" do
806
+ subject.application_key = "my_app_key2"
807
+ subject.master_secret = "my_master_secret2"
808
+ subject.push.success?.should == false
809
+ end
810
+
811
+ it "converts aliases to strings" do
812
+ subject.push(@valid_params.merge(:aliases => [:one, 2]))
813
+ request_json['aliases'].should == ['one', '2']
814
+ end
815
+ end
816
+
817
+ describe "::feedback" do
818
+ before(:each) do
819
+ subject.application_key = "my_app_key"
820
+ subject.master_secret = "my_master_secret"
821
+ end
822
+
823
+ it "raises an error if call is made without an app key and master secret configured" do
824
+ subject.application_key = nil
825
+ subject.master_secret = nil
826
+
827
+ lambda {
828
+ subject.feedback(Time.now)
829
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
830
+ end
831
+
832
+ it "uses app key and secret to sign the request" do
833
+ subject.feedback(Time.now)
834
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
835
+ end
836
+
837
+ it "encodes the time argument in UTC, ISO 8601 format" do
838
+ time = Time.parse("October 10, 2010, 8:00pm")
839
+ formatted_time = time.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
840
+ subject.feedback(time)
841
+ FakeWeb.last_request.path.should include(formatted_time)
842
+ end
843
+
844
+ it "accepts a string as the time argument" do
845
+ subject.feedback("Oct 07, 2010 8:00AM UTC")
846
+ FakeWeb.last_request.path.should include("2010-10-07T08:00:00Z")
847
+ end
848
+
849
+ it "returns an array of responses from the feedback API" do
850
+ response = subject.feedback(Time.now)
851
+ response[0].should include("device_token")
852
+ response[0].should include("marked_inactive_on")
853
+ response[0].should include("alias")
854
+ end
855
+
856
+ it "success? is false when the call doesn't return 200" do
857
+ subject.application_key = "my_app_key2"
858
+ subject.master_secret = "my_master_secret2"
859
+ subject.feedback(Time.now).success?.should == false
860
+ end
861
+ end
862
+
863
+ describe "::device_tokens" do
864
+
865
+ before(:each) do
866
+ subject.application_key = "my_app_key"
867
+ subject.master_secret = "my_master_secret"
868
+ end
869
+
870
+ it "raises an error if call is made without an app key and master secret configured" do
871
+ subject.application_key = nil
872
+ subject.master_secret = nil
873
+
874
+ lambda {
875
+ subject.device_tokens_count
876
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
877
+ end
878
+
879
+ it "uses app key and secret to sign the request" do
880
+ subject.device_tokens_count
881
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
882
+ end
883
+
884
+ it "returns a hash as response from the Device Token List API with an array of device tokens" do
885
+ response = subject.device_tokens
886
+ response["device_tokens"].class.should == Array
887
+ response["device_tokens_count"].should == 50
888
+ response["active_device_tokens_count"].should == 55
889
+ end
890
+
891
+ it "success? is false when the call doesn't return 200" do
892
+ subject.application_key = "my_app_key2"
893
+ subject.master_secret = "my_master_secret2"
894
+ subject.device_tokens.success?.should == false
895
+ end
896
+
897
+ end
898
+
899
+ describe "::device_tokens_count" do
900
+
901
+ before(:each) do
902
+ subject.application_key = "my_app_key"
903
+ subject.master_secret = "my_master_secret"
904
+ end
905
+
906
+ it "raises an error if call is made without an app key and master secret configured" do
907
+ subject.application_key = nil
908
+ subject.master_secret = nil
909
+
910
+ lambda {
911
+ subject.device_tokens_count
912
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
913
+ end
914
+
915
+ it "uses app key and secret to sign the request" do
916
+ subject.device_tokens_count
917
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
918
+ end
919
+
920
+ it "returns a hash as response from the Device Token List API" do
921
+ response = subject.device_tokens_count
922
+ response["device_tokens_count"].should == 50
923
+ response["active_device_tokens_count"].should == 55
924
+ end
925
+
926
+ it "success? is false when the call doesn't return 200" do
927
+ subject.application_key = "my_app_key2"
928
+ subject.master_secret = "my_master_secret2"
929
+ subject.device_tokens_count.success?.should == false
930
+ end
931
+
932
+ end
933
+
934
+ describe "::segments" do
935
+ before(:each) do
936
+ subject.application_key = "my_app_key"
937
+ subject.master_secret = "my_master_secret"
938
+ @valid_segment = {:display_name => "Test Male NY", :criteria => {:and => [ {:tag => "male"}, {:tag => "NY"}]} }
939
+ end
940
+
941
+ it "raises an error if call is made without an app key and master secret configured" do
942
+ subject.application_key = nil
943
+ subject.master_secret = nil
944
+
945
+ lambda {
946
+ subject.segments
947
+ }.should raise_error(RuntimeError, "Must configure application_key, master_secret before making this request.")
948
+ end
949
+
950
+ it "uses app key and secret to sign the request" do
951
+ subject.segments
952
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64('my_app_key:my_master_secret').chomp}"
953
+ end
954
+
955
+ it "returns valid segments" do
956
+ response = subject.segments
957
+ response.first.should include("segments")
958
+ response["segments"].each do |s|
959
+ ["id", "display_name", "creation_date", "modification_date"].each do |k|
960
+ s.should include(k)
961
+ end
962
+ end
963
+ end
964
+
965
+ it "can create a valid segment" do
966
+ response = subject.create_segment(@valid_segment)
967
+ response.code.should == "201"
968
+ end
969
+
970
+ it "can get a segment given its id" do
971
+ response = subject.segment("valid_id")
972
+ response.code.should == "200"
973
+ response.should include("display_name")
974
+ response.should include("criteria")
975
+ end
976
+
977
+ it "returns not found if invalid segment id is given" do
978
+ response = subject.segment("not_found")
979
+ response.code.should == "404"
980
+ end
981
+
982
+ it "can update a segment given a valid id" do
983
+ response = subject.update_segment("valid_segment", @valid_segment)
984
+ response.code.should == "200"
985
+ end
986
+
987
+ it "cant update a segment when given an invalid id" do
988
+ response = subject.update_segment("invalid_segment", @valid_segment)
989
+ response.code.should == "404"
990
+ end
991
+
992
+ it "can delete a segment given a valid id" do
993
+ response = subject.delete_segment("valid_segment")
994
+ response.code.should == "204"
995
+ end
996
+
997
+ it "cant delete a segment given an invalid id" do
998
+ response = subject.delete_segment("invalid_segment")
999
+ response.code.should == "404"
1000
+ end
1001
+
1002
+ it "success? is false when the call doesn't return 200" do
1003
+ subject.application_key = "my_app_key2"
1004
+ subject.master_secret = "my_master_secret2"
1005
+ subject.segments.success?.should == false
1006
+ end
1007
+
1008
+ end
1009
+
1010
+
1011
+ describe "logging" do
1012
+
1013
+ before(:each) do
1014
+ @logger = double("logger", :info => true)
1015
+ subject.application_key = "my_app_key"
1016
+ subject.application_secret = "my_app_secret"
1017
+ subject.master_secret = "my_master_secret"
1018
+ subject.logger = @logger
1019
+ end
1020
+
1021
+ it "logs request and response information when registering a device" do
1022
+ @logger.should_receive(:info).with(/\/api\/device_tokens\/new_device_token/)
1023
+ subject.register_device('new_device_token')
1024
+ end
1025
+
1026
+ it "logs request and response information when sending push notifications" do
1027
+ @logger.should_receive(:info).with(/\/api\/push/)
1028
+ subject.push(:device_tokens => ["device_token"], :aps => {:alert => "foo"})
1029
+ end
1030
+
1031
+ it "logs request and response information when sending feedback requests" do
1032
+ @logger.should_receive(:info).with(/\/api\/device_tokens\/feedback/)
1033
+ subject.feedback(Time.now)
1034
+ end
1035
+
1036
+ it "flushes the logger buffer if it's an ActiveSupport::BufferedLogger (Default Rails logger)" do
1037
+ @logger.stub(:flush).and_return("message in the buffer\n")
1038
+ @logger.should_receive(:flush)
1039
+ subject.feedback(Time.now)
1040
+ end
1041
+ end
1042
+
1043
+ describe "request timeout" do
1044
+ before(:each) do
1045
+ @logger = double("logger", :info => true)
1046
+ subject.application_key = "my_app_key"
1047
+ subject.application_secret = "my_app_secret"
1048
+ subject.master_secret = "my_master_secret"
1049
+ subject.logger = @logger
1050
+ end
1051
+
1052
+ it "does not attempt to log if logger is not set" do
1053
+ Urbanairship::Timer.should_receive(:timeout).with(5.0).and_raise(Timeout::Error)
1054
+ subject.logger = nil
1055
+
1056
+ lambda {
1057
+ subject.register_device('new_device_token')
1058
+ }.should_not raise_exception(NoMethodError)
1059
+ end
1060
+
1061
+ it "uses a default request_timeout value of five seconds" do
1062
+ Urbanairship::Timer.should_receive(:timeout).with(5.0).and_raise(Timeout::Error)
1063
+ @logger.should_receive(:error).with(/Urbanairship request timed out/)
1064
+
1065
+ subject.register_device('new_device_token')
1066
+ end
1067
+
1068
+ it "accepts a configured request_timeout value" do
1069
+ Urbanairship::Timer.should_receive(:timeout).with(1.23).and_raise(Timeout::Error)
1070
+ @logger.should_receive(:error).with(/Urbanairship request timed out/)
1071
+
1072
+ subject.request_timeout = 1.23
1073
+ subject.register_device('new_device_token')
1074
+ end
1075
+ end
1076
+ end
1077
+
1078
+ describe Urbanairship do
1079
+ it_should_behave_like "an Urbanairship client"
1080
+ end
1081
+
1082
+ describe Urbanairship::Client do
1083
+ it_should_behave_like "an Urbanairship client"
1084
+ end
1085
+
1086
+ def request_json
1087
+ JSON.parse FakeWeb.last_request.body
1088
+ end