koala 1.1.0 → 1.2.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.
Files changed (52) hide show
  1. data/.travis.yml +2 -1
  2. data/CHANGELOG +36 -0
  3. data/Gemfile +6 -2
  4. data/Rakefile +0 -1
  5. data/koala.gemspec +7 -8
  6. data/lib/koala/batch_operation.rb +15 -15
  7. data/lib/koala/graph_api.rb +85 -73
  8. data/lib/koala/graph_batch_api.rb +21 -11
  9. data/lib/koala/graph_collection.rb +13 -8
  10. data/lib/koala/http_service.rb +176 -0
  11. data/lib/koala/oauth.rb +34 -24
  12. data/lib/koala/realtime_updates.rb +21 -18
  13. data/lib/koala/rest_api.rb +1 -1
  14. data/lib/koala/test_users.rb +33 -17
  15. data/lib/koala/uploadable_io.rb +49 -43
  16. data/lib/koala/utils.rb +11 -0
  17. data/lib/koala/version.rb +3 -0
  18. data/lib/koala.rb +47 -45
  19. data/readme.md +58 -38
  20. data/spec/cases/{api_base_spec.rb → api_spec.rb} +27 -2
  21. data/spec/cases/error_spec.rb +32 -0
  22. data/spec/cases/graph_and_rest_api_spec.rb +12 -21
  23. data/spec/cases/graph_api_batch_spec.rb +92 -119
  24. data/spec/cases/graph_api_spec.rb +11 -14
  25. data/spec/cases/graph_collection_spec.rb +116 -0
  26. data/spec/cases/http_service_spec.rb +446 -0
  27. data/spec/cases/koala_spec.rb +36 -37
  28. data/spec/cases/oauth_spec.rb +318 -212
  29. data/spec/cases/realtime_updates_spec.rb +45 -31
  30. data/spec/cases/rest_api_spec.rb +23 -7
  31. data/spec/cases/test_users_spec.rb +123 -75
  32. data/spec/cases/uploadable_io_spec.rb +77 -36
  33. data/spec/cases/utils_spec.rb +10 -0
  34. data/spec/fixtures/facebook_data.yml +26 -24
  35. data/spec/fixtures/mock_facebook_responses.yml +131 -101
  36. data/spec/spec_helper.rb +29 -5
  37. data/spec/support/graph_api_shared_examples.rb +80 -120
  38. data/spec/support/json_testing_fix.rb +35 -11
  39. data/spec/support/koala_test.rb +187 -0
  40. data/spec/support/mock_http_service.rb +8 -5
  41. data/spec/support/ordered_hash.rb +205 -0
  42. data/spec/support/rest_api_shared_examples.rb +37 -37
  43. data/spec/support/uploadable_io_shared_examples.rb +2 -8
  44. metadata +72 -83
  45. data/lib/koala/http_services/net_http_service.rb +0 -92
  46. data/lib/koala/http_services/typhoeus_service.rb +0 -37
  47. data/lib/koala/http_services.rb +0 -46
  48. data/spec/cases/http_services/http_service_spec.rb +0 -129
  49. data/spec/cases/http_services/net_http_service_spec.rb +0 -532
  50. data/spec/cases/http_services/typhoeus_service_spec.rb +0 -152
  51. data/spec/support/live_testing_data_helper.rb +0 -40
  52. data/spec/support/setup_mocks_or_live.rb +0 -51
@@ -1,532 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- Horse = Koala::NetHTTPService
5
-
6
- describe "NetHTTPService module holder class Horse" do
7
- before :each do
8
- # reset global settings
9
- Horse.always_use_ssl = Horse.proxy = Horse.timeout = nil
10
- end
11
-
12
- it "has a ca_file accessor" do
13
- Horse.methods.collect {|m| m.to_sym}.should include(:ca_file)
14
- Horse.methods.collect {|m| m.to_sym}.should include(:ca_file=)
15
- end
16
-
17
- it "has a ca_path accessor" do
18
- Horse.methods.collect {|m| m.to_sym}.should include(:ca_path)
19
- Horse.methods.collect {|m| m.to_sym}.should include(:ca_path=)
20
- end
21
-
22
- it "has a verify_mode accessor" do
23
- Horse.methods.collect {|m| m.to_sym}.should include(:verify_mode)
24
- Horse.methods.collect {|m| m.to_sym}.should include(:verify_mode=)
25
- end
26
-
27
- it "should define a make_request static module method" do
28
- Horse.respond_to?(:make_request).should be_true
29
- end
30
-
31
- it "should include the Koala::HTTPService module defining common features" do
32
- Horse.included_modules.include?(Koala::HTTPService).should be_true
33
- end
34
-
35
- describe "when making a request" do
36
- before(:each) do
37
- # Setup stubs for make_request to execute without exceptions
38
- @mock_body = stub('Net::HTTPResponse body')
39
- @mock_http_response = stub('Net::HTTPResponse', :code => 1, :body => @mock_body)
40
-
41
- @http_yield_mock = mock('Net::HTTP start yielded object')
42
-
43
- @http_yield_mock.stub(:post).and_return(@mock_http_response)
44
- @http_yield_mock.stub(:get).and_return(@mock_http_response)
45
-
46
- @http_mock = stub('Net::HTTP object', 'use_ssl=' => true, 'verify_mode=' => true)
47
- @http_mock.stub(:start).and_yield(@http_yield_mock)
48
- @http_mock.stub(:ca_path=)
49
- @http_mock.stub(:ca_file=)
50
- @http_mock.stub(:verify_mode=)
51
-
52
- Net::HTTP.stub(:new).and_return(@http_mock)
53
- end
54
-
55
- describe "the connection" do
56
- it "should use POST if verb is not GET" do
57
- @http_yield_mock.should_receive(:post).and_return(@mock_http_response)
58
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
59
-
60
- Horse.make_request('anything', {}, 'anything')
61
- end
62
-
63
- it "should use GET if that verb is specified" do
64
- @http_yield_mock.should_receive(:get).and_return(@mock_http_response)
65
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
66
-
67
- Horse.make_request('anything', {}, 'get')
68
- end
69
-
70
- it "should add the method to the arguments if it's not get or post" do
71
- args = {}
72
- method = "telekenesis"
73
- # since the arguments get encoded later, we'll test for merge!
74
- # even though that's somewhat testing internal implementation
75
- args.should_receive(:merge!).with(:method => method)
76
-
77
- Horse.make_request('anything', args, method)
78
- end
79
- end
80
-
81
- describe "if the request has an access token" do
82
- before :each do
83
- @args = {"access_token" => "123"}
84
- end
85
-
86
- it "should use SSL" do
87
- @http_mock.should_receive('use_ssl=').with(true)
88
-
89
- Horse.make_request('anything', @args, 'anything')
90
- end
91
-
92
- it "should set the port to 443" do
93
- Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
94
-
95
- Horse.make_request('anything', @args, 'anything')
96
- end
97
- end
98
-
99
- describe "if always_use_ssl is true" do
100
- before :each do
101
- Horse.always_use_ssl = true
102
- end
103
-
104
- it "should use SSL" do
105
- @http_mock.should_receive('use_ssl=').with(true)
106
-
107
- Horse.make_request('anything', {}, 'anything')
108
- end
109
-
110
- it "should set the port to 443" do
111
- Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
112
-
113
- Horse.make_request('anything', {}, 'anything')
114
- end
115
- end
116
-
117
- describe "if the use_ssl option is provided" do
118
- it "should use SSL" do
119
- @http_mock.should_receive('use_ssl=').with(true)
120
-
121
- Horse.make_request('anything', {}, 'anything', :use_ssl => true)
122
- end
123
-
124
- it "should set the port to 443" do
125
- Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
126
-
127
- Horse.make_request('anything', {}, 'anything', :use_ssl => true)
128
- end
129
- end
130
-
131
- describe "if there's no token and always_use_ssl isn't true" do
132
- it "should not use SSL" do
133
- @http_mock.should_not_receive('use_ssl=')
134
- Horse.make_request('anything', {}, 'anything')
135
- end
136
-
137
- it "should not set the port" do
138
- Net::HTTP.should_receive(:new).with(anything, nil).and_return(@http_mock)
139
- Horse.make_request('anything', {}, 'anything')
140
- end
141
- end
142
-
143
- describe "proxy options" do
144
- before :each do
145
- Horse.proxy = "http://defaultproxy"
146
- end
147
- after :all do
148
- Horse.proxy = nil
149
- end
150
-
151
- it "should use passed proxy option if provided" do
152
- Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything, "passedproxy", 80, nil, nil).and_return(@http_mock)
153
- Horse.make_request('anything', {} , 'anything', {:proxy => "http://passedproxy"})
154
- end
155
-
156
- it "should use default proxy if default is provided and NO proxy option passed" do
157
- Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything, "defaultproxy", 80, nil, nil).and_return(@http_mock)
158
- Horse.make_request('anything', {} , 'anything', {})
159
- end
160
-
161
- it "should NOT use a proxy if default is NOT provided and NO proxy option passed" do
162
- Horse.proxy = nil
163
- Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
164
- Horse.make_request('anything', {} , 'anything', {})
165
- end
166
- end
167
-
168
- describe "timeout options" do
169
- before :each do
170
- Horse.timeout = 20 # seconds
171
- end
172
- after :all do
173
- Horse.timeout = nil # seconds
174
- end
175
-
176
- it "should use passed timeout option if provided" do
177
- @http_mock.should_receive('open_timeout=').with(10)
178
- @http_mock.should_receive('read_timeout=').with(10)
179
- Horse.make_request('anything', {} , 'anything', {:timeout => 10})
180
- end
181
-
182
- it "should use default timout if default is provided and NO timeout option passed" do
183
- @http_mock.should_receive('open_timeout=').with(20)
184
- @http_mock.should_receive('read_timeout=').with(20)
185
- Horse.make_request('anything', {} , 'anything', {})
186
- end
187
-
188
- it "should NOT use a timeout if default is NOT provided and NO timeout option passed" do
189
- Horse.timeout = nil # seconds
190
- @http_mock.should_not_receive('open_timeout=')
191
- @http_mock.should_not_receive('read_timeout=')
192
- Horse.make_request('anything', {} , 'anything', {})
193
- end
194
- end
195
-
196
- describe "ca_file options" do
197
- after :each do
198
- Horse.always_use_ssl = nil
199
- Horse.ca_file = nil
200
- end
201
-
202
- it "should not use a ca_file if the request is not via SSL" do
203
- Horse.always_use_ssl = false
204
- @http_mock.should_not_receive(:ca_file=)
205
- Horse.make_request('anything', {} , 'anything', {:ca_file => '/no/file'})
206
- end
207
-
208
- describe "when via SSL" do
209
- before :each do
210
- Horse.always_use_ssl = true
211
- @global_ca_file_path = '/global/ca/file/path'
212
- end
213
-
214
- context "if the file doesn't exist" do
215
- it "raises Errno::ENOENT if the default ca_file does not exist" do
216
- Horse.ca_file = @global_ca_file_path
217
-
218
- File.should_receive(:exists?).with(@global_ca_file_path).and_return(false)
219
- expect { Horse.make_request('anything', {} , 'anything', {}) }.to raise_exception(Errno::ENOENT)
220
- end
221
-
222
- it "raises Errno::ENOENT if options[:ca_file] does not exist" do
223
- File.should_receive(:exists?).with(@global_ca_file_path).and_return(false)
224
- expect { Horse.make_request('anything', {} , 'anything', {:ca_file => @global_ca_file_path}) }.to raise_exception(Errno::ENOENT)
225
- end
226
- end
227
-
228
- context "if the file exists" do
229
- before :each do
230
- File.stub(:exists?).and_return(true)
231
- end
232
-
233
- it "should use options[:ca_file] if provided" do
234
- given_ca_file = '/ca/file'
235
-
236
- Horse.ca_file = @global_ca_file_path
237
- @http_mock.should_not_receive(:ca_file=).with(@global_ca_file_path)
238
- @http_mock.should_receive(:ca_file=).with(given_ca_file)
239
-
240
- Horse.make_request('anything', {} , 'anything', {:ca_file => given_ca_file})
241
- end
242
-
243
- it "should use default ca_file if default is provided and NO ca_file option is passed" do
244
- Horse.ca_file = @global_ca_file_path
245
- @http_mock.should_receive(:ca_file=).with(@global_ca_file_path)
246
-
247
- Horse.make_request('anything', {} , 'anything', {})
248
- end
249
-
250
- it "should NOT use a ca_file if default is NOT provided and NO ca_file option is passed" do
251
- @http_mock.should_not_receive(:ca_file=)
252
-
253
- Horse.make_request('anything', {} , 'anything', {})
254
- end
255
- end
256
- end
257
- end
258
-
259
- describe "ca_path options" do
260
- after :each do
261
- Horse.always_use_ssl = nil
262
- Horse.ca_path = nil
263
- end
264
-
265
- it "should not use a ca_path if the request is not via SSL" do
266
- Horse.always_use_ssl = false
267
- @http_mock.should_not_receive('ca_path=')
268
- Horse.make_request('anything', {} , 'anything', {:ca_file => '/no/file'})
269
- end
270
-
271
- describe "when via SSL" do
272
- before :each do
273
- Horse.always_use_ssl = true
274
- @global_ca_path = '/global/ca/path'
275
- end
276
-
277
- context "if the directory doesn't exist" do
278
- it "should not use a default ca_path if the default ca_path does not exist" do
279
- Horse.ca_path = @global_ca_path
280
-
281
- File.should_receive(:directory?).with(@global_ca_path).and_return(false)
282
- expect { Horse.make_request('anything', {} , 'anything', {}) }.to raise_exception(Errno::ENOENT)
283
- end
284
-
285
- it "should not use a default ca_path if the default ca_path does not exist" do
286
- File.should_receive(:directory?).with(@global_ca_path).and_return(false)
287
- expect { Horse.make_request('anything', {} , 'anything', {:ca_path => @global_ca_path}) }.to raise_exception(Errno::ENOENT)
288
- end
289
- end
290
-
291
- context "if the directory exists" do
292
- before :each do
293
- File.stub(:directory?).and_return(true)
294
- end
295
-
296
- it "should use passed ca_path options if provided" do
297
- given_ca_path = '/ca/path'
298
-
299
- Horse.ca_path = @global_ca_path
300
- @http_mock.should_not_receive(:ca_ath=).with(@global_ca_path)
301
- @http_mock.should_receive(:ca_path=).with(given_ca_path)
302
-
303
- Horse.make_request('anything', {} , 'anything', {:ca_path => given_ca_path})
304
- end
305
-
306
- it "should use default ca_path if default is provided and NO ca_path option is passed" do
307
- Horse.ca_path = @global_ca_path
308
- @http_mock.should_receive(:ca_path=).with(@global_ca_path)
309
-
310
- Horse.make_request('anything', {} , 'anything', {})
311
- end
312
-
313
- it "should NOT use a ca_path if default is NOT provided and NO ca_path option is passed" do
314
- @http_mock.should_not_receive(:ca_path=)
315
-
316
- Horse.make_request('anything', {} , 'anything', {})
317
- end
318
- end
319
- end
320
- end
321
-
322
- describe "verify_mode options" do
323
- after :each do
324
- Horse.always_use_ssl = nil
325
- Horse.verify_mode = nil
326
- end
327
-
328
- it "does not set verify mode if it's not SSL" do
329
- Horse.always_use_ssl = nil
330
- @http_mock.should_not_receive(:verify_mode=)
331
- Horse.make_request('anything', {} , 'anything', {:verify_mode => "abc"})
332
- end
333
-
334
- context "when making an SSL request" do
335
- before :each do
336
- Horse.always_use_ssl = true
337
- end
338
-
339
- it "sets verify mode if provided in the options" do
340
- mode = "foo"
341
- @http_mock.should_receive(:verify_mode=).with(mode)
342
- Horse.make_request('anything', {} , 'anything', {:verify_mode => mode})
343
- end
344
-
345
- it "sets verify mode to the default if provided (and none set in options)" do
346
- Horse.verify_mode = "foo"
347
- @http_mock.should_receive(:verify_mode=).with(Horse.verify_mode)
348
- Horse.make_request('anything', {} , 'anything', {})
349
- end
350
-
351
- it "sets verify mode to the default if provided (and none set in options)" do
352
- mode = "bar"
353
- Horse.verify_mode = "foo"
354
- @http_mock.should_receive(:verify_mode=).with(mode)
355
- Horse.make_request('anything', {} , 'anything', {:verify_mode => mode})
356
- end
357
-
358
- it "sets verify mode to OpenSSL::SSL::VERIFY_PEER if no default or option is provided" do
359
- @http_mock.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
360
- Horse.make_request('anything', {} , 'anything', {})
361
- end
362
- end
363
- end
364
-
365
- it "should use the graph server by default" do
366
- Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
367
- Horse.make_request('anything', {}, 'anything')
368
- end
369
-
370
- it "should use the REST server if the :rest_api option is true" do
371
- Net::HTTP.should_receive(:new).with(Koala::Facebook::REST_SERVER, anything).and_return(@http_mock)
372
- Horse.make_request('anything', {}, 'anything', :rest_api => true)
373
- end
374
-
375
- it "should start an HTTP connection" do
376
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
377
- Horse.make_request('anything', {}, 'anything')
378
- end
379
-
380
- it 'creates a HTTP Proxy object when options contain a proxy' do
381
- Net::HTTP.should_receive(:new).with(anything, anything, 'proxy', 1234, 'user', 'pass').and_return(@http_mock)
382
- Horse.make_request('anything', {}, 'anything', {:proxy => 'http://user:pass@proxy:1234'})
383
- end
384
-
385
- it 'sets both timeouts when options contains a timeout' do
386
- @http_mock.should_receive(:open_timeout=).with(10)
387
- @http_mock.should_receive(:read_timeout=).with(10)
388
- Horse.make_request('anything', {}, 'anything', {:timeout => 10})
389
- end
390
-
391
- describe "via POST" do
392
- it "should use Net::HTTP to make a POST request" do
393
- @http_yield_mock.should_receive(:post).and_return(@mock_http_response)
394
-
395
- Horse.make_request('anything', {}, 'post')
396
- end
397
-
398
- it "should go to the specified path adding a / if it doesn't exist" do
399
- path = mock('Path')
400
- @http_yield_mock.should_receive(:post).with(path, anything).and_return(@mock_http_response)
401
-
402
- Horse.make_request(path, {}, 'post')
403
- end
404
-
405
- it "should use encoded parameters" do
406
- args = {}
407
- params = mock('Encoded parameters')
408
- Horse.should_receive(:encode_params).with(args).and_return(params)
409
-
410
- @http_yield_mock.should_receive(:post).with(anything, params).and_return(@mock_http_response)
411
-
412
- Horse.make_request('anything', args, 'post')
413
- end
414
-
415
- describe "with multipart/form-data" do
416
- before(:each) do
417
- Horse.stub(:encode_multipart_params)
418
- Horse.stub("params_require_multipart?").and_return(true)
419
-
420
- @multipart_request_stub = stub('Stub Multipart Request')
421
- Net::HTTP::Post::Multipart.stub(:new).and_return(@multipart_request_stub)
422
-
423
- @file_stub = stub('fake File', "kind_of?" => true, "path" => 'anypath.jpg')
424
-
425
- @http_yield_mock.stub(:request).with(@multipart_request_stub).and_return(@mock_http_response)
426
- end
427
-
428
- it "should use multipart/form-data if any parameter is a valid file hash" do
429
- @http_yield_mock.should_receive(:request).with(@multipart_request_stub).and_return(@mock_http_response)
430
-
431
- Horse.make_request('anything', {}, 'post')
432
- end
433
-
434
- it "should use the given request path for the request" do
435
- args = {"file" => @file_stub}
436
- expected_path = 'expected/path'
437
-
438
- Net::HTTP::Post::Multipart.should_receive(:new).with(expected_path, anything).and_return(@multipart_request_stub)
439
-
440
- Horse.make_request(expected_path, {}, 'post')
441
- end
442
-
443
- it "should use multipart encoded arguments for the request" do
444
- args = {"file" => @file_stub}
445
- expected_params = stub('Stub Multipart Params')
446
-
447
- Horse.should_receive(:encode_multipart_params).with(args).and_return(expected_params)
448
- Net::HTTP::Post::Multipart.should_receive(:new).with(anything, expected_params).and_return(@multipart_request_stub)
449
-
450
- Horse.make_request('anything', args, 'post')
451
- end
452
- end
453
- end
454
-
455
- describe "via GET" do
456
- it "should use Net::HTTP to make a GET request" do
457
- @http_yield_mock.should_receive(:get).and_return(@mock_http_response)
458
-
459
- Horse.make_request('anything', {}, 'get')
460
- end
461
-
462
- it "should use the correct path, including arguments" do
463
- path = mock('Path')
464
- params = mock('Encoded parameters')
465
- args = {}
466
-
467
- Horse.should_receive(:encode_params).with(args).and_return(params)
468
- @http_yield_mock.should_receive(:get).with("#{path}?#{params}").and_return(@mock_http_response)
469
-
470
- Horse.make_request(path, args, 'get')
471
- end
472
- end
473
-
474
- describe "the returned value" do
475
- before(:each) do
476
- @response = Horse.make_request('anything', {}, 'anything')
477
- end
478
-
479
- it "should return a Koala::Response object" do
480
- @response.class.should == Koala::Response
481
- end
482
-
483
- it "should return a Koala::Response with the right status" do
484
- @response.status.should == @mock_http_response.code
485
- end
486
-
487
- it "should reutrn a Koala::Response with the right body" do
488
- @response.body.should == @mock_body
489
- end
490
-
491
- it "should return a Koala::Response with the Net::HTTPResponse object as headers" do
492
- @response.headers.should == @mock_http_response
493
- end
494
- end # describe return value
495
- end # describe when making a request
496
-
497
- describe "when detecting if multipart posting is needed" do
498
- it "should be true if any parameter value requires multipart post" do
499
- koala_io = mock("Koala::IO")
500
- koala_io.should_receive(:kind_of?).with(Koala::UploadableIO).and_return(true)
501
-
502
- args = {
503
- "key1" => "val",
504
- "key2" => "val",
505
- "key3" => koala_io,
506
- "key4" => "val"
507
- }
508
-
509
- Horse.params_require_multipart?(args).should be_true
510
- end
511
-
512
- describe "when encoding multipart/form-data params" do
513
- it "should replace Koala::UploadableIO values with UploadIO values" do
514
- upload_io = UploadIO.new(__FILE__, "fake type")
515
-
516
- uploadable_io = stub('Koala::UploadableIO')
517
- uploadable_io.should_receive(:kind_of?).with(Koala::UploadableIO).and_return(true)
518
- uploadable_io.should_receive(:to_upload_io).and_return(upload_io)
519
- args = {
520
- "not_a_file" => "not a file",
521
- "file" => uploadable_io
522
- }
523
-
524
- result = Horse.encode_multipart_params(args)
525
-
526
- result["not_a_file"] == args["not_a_file"]
527
- result["file"] == upload_io
528
- end
529
- end
530
-
531
- end
532
- end
@@ -1,152 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- Deer = Koala::TyphoeusService
5
-
6
- describe "TyphoeusService" do
7
-
8
- describe "TyphoeusService module holder class Deer" do
9
- before :each do
10
- # reset global settings
11
- Deer.always_use_ssl = Deer.proxy = Deer.timeout = nil
12
- end
13
-
14
- it "should define a make_request static module method" do
15
- Deer.respond_to?(:make_request).should be_true
16
- end
17
-
18
- it "should include the Koala::HTTPService module defining common features" do
19
- Deer.included_modules.include?(Koala::HTTPService).should be_true
20
- end
21
-
22
- describe "when making a request" do
23
- before(:each) do
24
- # Setup stubs for make_request to execute without exceptions
25
- @mock_body = stub('Typhoeus response body')
26
- @mock_headers_hash = stub({:value => "headers hash"})
27
- @mock_http_response = stub(Typhoeus::Response, :code => 1, :headers_hash => @mock_headers_hash, :body => @mock_body)
28
-
29
- # Typhoeus is an included module, so we stub methods on Deer itself
30
- Typhoeus::Request.stub(:post).and_return(@mock_http_response)
31
- Typhoeus::Request.stub(:get).and_return(@mock_http_response)
32
- end
33
-
34
- it "should use POST if verb is not GET" do
35
- Typhoeus::Request.should_receive(:post).and_return(@mock_http_response)
36
- Deer.make_request('anything', {}, 'anything')
37
- end
38
-
39
- it "should use GET if that verb is specified" do
40
- Typhoeus::Request.should_receive(:get).and_return(@mock_http_response)
41
- Deer.make_request('anything', {}, 'get')
42
- end
43
-
44
- describe "the connection" do
45
- it "should use SSL if the request has an access token" do
46
- Typhoeus::Request.should_receive(:post).with(/https\:/, anything)
47
-
48
- Deer.make_request('anything', {"access_token" => "123"}, 'anything')
49
- end
50
-
51
- it "should use SSL if always_use_ssl is true, even if there's no token" do
52
- Typhoeus::Request.should_receive(:post).with(/https\:/, anything)
53
-
54
- Deer.always_use_ssl = true
55
- Deer.make_request('anything', {}, 'anything')
56
- end
57
-
58
- it "should use SSL if the :use_ssl option is provided, even if there's no token" do
59
- Typhoeus::Request.should_receive(:post).with(/https\:/, anything)
60
-
61
- Deer.always_use_ssl = true
62
- Deer.make_request('anything', {}, 'anything', :use_ssl => true)
63
- end
64
-
65
- it "should not use SSL if always_use_ssl is false and there's no token" do
66
- Typhoeus::Request.should_receive(:post).with(/http\:/, anything)
67
-
68
- Deer.make_request('anything', {}, 'anything')
69
- end
70
-
71
- it "should use the graph server by default" do
72
- Typhoeus::Request.should_receive(:post).with(Regexp.new(Koala::Facebook::GRAPH_SERVER), anything)
73
-
74
- Deer.make_request('anything', {}, 'anything')
75
- end
76
-
77
- it "should use the REST server if the :rest_api option is true" do
78
- Typhoeus::Request.should_receive(:post).with(Regexp.new(Koala::Facebook::REST_SERVER), anything)
79
-
80
- Deer.make_request('anything', {}, 'anything', :rest_api => true)
81
- end
82
- end
83
-
84
- it "should pass the arguments to Typhoeus under the :params key" do
85
- args = {:a => 2}
86
- Typhoeus::Request.should_receive(:post).with(anything, hash_including(:params => args))
87
-
88
- Deer.make_request('anything', args, "post")
89
- end
90
-
91
- it "should add the method to the arguments if the method isn't get or post" do
92
- method = "telekenesis"
93
- Typhoeus::Request.should_receive(:post).with(anything, hash_including(:params => {:method => method}))
94
-
95
- Deer.make_request('anything', {}, method)
96
- end
97
-
98
- it "should pass :typhoeus_options to Typhoeus if provided" do
99
- t_options = {:a => :b}
100
- Typhoeus::Request.should_receive(:post).with(anything, hash_including(t_options))
101
-
102
- Deer.make_request("anything", {}, "post", :typhoeus_options => t_options)
103
- end
104
-
105
- it "should pass proxy and timeout :typhoeus_options to Typhoeus if set globally" do
106
- Deer.proxy = "http://defaultproxy"
107
- Deer.timeout = 20
108
-
109
- t_options = {:proxy => "http://defaultproxy", :timeout => 20}
110
- Typhoeus::Request.should_receive(:post).with(anything, hash_including(t_options))
111
-
112
- Deer.make_request("anything", {}, "post")
113
- end
114
-
115
- # for live tests, run the Graph API tests with Typhoues, which will run file uploads
116
- it "should pass any files directly on to Typhoues" do
117
- args = {:file => File.new(__FILE__, "r")}
118
- Typhoeus::Request.should_receive(:post).with(anything, hash_including(:params => args)).and_return(Typhoeus::Response.new)
119
- Deer.make_request("anything", args, :post)
120
- end
121
-
122
- it "should include the path in the request" do
123
- path = "/a/b/c/1"
124
- Typhoeus::Request.should_receive(:post).with(Regexp.new(path), anything)
125
-
126
- Deer.make_request(path, {}, "post")
127
- end
128
-
129
- describe "the returned value" do
130
- before(:each) do
131
- @response = Deer.make_request('anything', {}, 'anything')
132
- end
133
-
134
- it "should return a Koala::Response object" do
135
- @response.class.should == Koala::Response
136
- end
137
-
138
- it "should return a Koala::Response with the right status" do
139
- @response.status.should == @mock_http_response.code
140
- end
141
-
142
- it "should reutrn a Koala::Response with the right body" do
143
- @response.body.should == @mock_body
144
- end
145
-
146
- it "should return a Koala::Response with the Typhoeus headers as headers" do
147
- @response.headers.should == @mock_headers_hash
148
- end
149
- end # describe return value
150
- end
151
- end
152
- end