koala 1.0.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 (54) hide show
  1. data/.autotest +12 -0
  2. data/.gitignore +3 -1
  3. data/.travis.yml +9 -0
  4. data/CHANGELOG +62 -2
  5. data/Gemfile +8 -0
  6. data/Rakefile +0 -1
  7. data/autotest/discover.rb +1 -0
  8. data/koala.gemspec +13 -14
  9. data/lib/koala/batch_operation.rb +74 -0
  10. data/lib/koala/graph_api.rb +145 -132
  11. data/lib/koala/graph_batch_api.rb +97 -0
  12. data/lib/koala/graph_collection.rb +59 -0
  13. data/lib/koala/http_service.rb +176 -0
  14. data/lib/koala/oauth.rb +191 -0
  15. data/lib/koala/realtime_updates.rb +23 -29
  16. data/lib/koala/rest_api.rb +13 -8
  17. data/lib/koala/test_users.rb +33 -17
  18. data/lib/koala/uploadable_io.rb +153 -87
  19. data/lib/koala/utils.rb +11 -0
  20. data/lib/koala/version.rb +3 -0
  21. data/lib/koala.rb +59 -217
  22. data/readme.md +92 -53
  23. data/spec/cases/{api_base_spec.rb → api_spec.rb} +31 -6
  24. data/spec/cases/error_spec.rb +32 -0
  25. data/spec/cases/graph_and_rest_api_spec.rb +12 -21
  26. data/spec/cases/graph_api_batch_spec.rb +582 -0
  27. data/spec/cases/graph_api_spec.rb +11 -14
  28. data/spec/cases/graph_collection_spec.rb +116 -0
  29. data/spec/cases/http_service_spec.rb +446 -0
  30. data/spec/cases/koala_spec.rb +54 -0
  31. data/spec/cases/oauth_spec.rb +319 -213
  32. data/spec/cases/realtime_updates_spec.rb +45 -31
  33. data/spec/cases/rest_api_spec.rb +23 -7
  34. data/spec/cases/test_users_spec.rb +123 -75
  35. data/spec/cases/uploadable_io_spec.rb +120 -37
  36. data/spec/cases/utils_spec.rb +10 -0
  37. data/spec/fixtures/cat.m4v +0 -0
  38. data/spec/fixtures/facebook_data.yml +26 -24
  39. data/spec/fixtures/mock_facebook_responses.yml +203 -78
  40. data/spec/spec_helper.rb +30 -5
  41. data/spec/support/graph_api_shared_examples.rb +149 -118
  42. data/spec/support/json_testing_fix.rb +42 -0
  43. data/spec/support/koala_test.rb +187 -0
  44. data/spec/support/mock_http_service.rb +62 -58
  45. data/spec/support/ordered_hash.rb +205 -0
  46. data/spec/support/rest_api_shared_examples.rb +139 -15
  47. data/spec/support/uploadable_io_shared_examples.rb +2 -8
  48. metadata +90 -114
  49. data/lib/koala/http_services.rb +0 -146
  50. data/spec/cases/http_services/http_service_spec.rb +0 -54
  51. data/spec/cases/http_services/net_http_service_spec.rb +0 -350
  52. data/spec/cases/http_services/typhoeus_service_spec.rb +0 -144
  53. data/spec/support/live_testing_data_helper.rb +0 -40
  54. data/spec/support/setup_mocks_or_live.rb +0 -52
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+
3
+ describe Koala::Facebook::GraphCollection do
4
+ before(:each) do
5
+ @result = {
6
+ "data" => [1, 2, :three],
7
+ "paging" => {:paging => true}
8
+ }
9
+ @api = Koala::Facebook::API.new("123")
10
+ @collection = Koala::Facebook::GraphCollection.new(@result, @api)
11
+ end
12
+
13
+ it "subclasses Array" do
14
+ Koala::Facebook::GraphCollection.ancestors.should include(Array)
15
+ end
16
+
17
+ it "creates an array-like object" do
18
+ Koala::Facebook::GraphCollection.new(@result, @api).should be_an(Array)
19
+ end
20
+
21
+ it "contains the result data" do
22
+ @result["data"].each_with_index {|r, i| @collection[i].should == r}
23
+ end
24
+
25
+ it "has a read-only paging attribute" do
26
+ @collection.methods.map(&:to_sym).should include(:paging)
27
+ @collection.methods.map(&:to_sym).should_not include(:paging=)
28
+ end
29
+
30
+ it "sets paging to results['paging']" do
31
+ @collection.paging.should == @result["paging"]
32
+ end
33
+
34
+ it "sets raw_response to the original results" do
35
+ @collection.raw_response.should == @result
36
+ end
37
+
38
+ it "sets the API to the provided API" do
39
+ @collection.api.should == @api
40
+ end
41
+
42
+ describe "when getting a whole page" do
43
+ before(:each) do
44
+ @second_page = {
45
+ "data" => [:second, :page, :data],
46
+ "paging" => {}
47
+ }
48
+ @base = stub("base")
49
+ @args = stub("args")
50
+ @page_of_results = stub("page of results")
51
+ end
52
+
53
+ it "should return the previous page of results" do
54
+ @collection.should_receive(:previous_page_params).and_return([@base, @args])
55
+ @api.should_receive(:api).with(@base, @args, anything, anything).and_return(@second_page)
56
+ Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
57
+ @collection.previous_page.should == @page_of_results
58
+ end
59
+
60
+ it "should return the next page of results" do
61
+ @collection.should_receive(:next_page_params).and_return([@base, @args])
62
+ @api.should_receive(:api).with(@base, @args, anything, anything).and_return(@second_page)
63
+ Koala::Facebook::GraphCollection.should_receive(:new).with(@second_page, @api).and_return(@page_of_results)
64
+
65
+ @collection.next_page.should == @page_of_results
66
+ end
67
+
68
+ it "should return nil it there are no other pages" do
69
+ %w{next previous}.each do |this|
70
+ @collection.should_receive("#{this}_page_params".to_sym).and_return(nil)
71
+ @collection.send("#{this}_page").should == nil
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "when parsing page paramters" do
77
+ it "should return the base as the first array entry" do
78
+ base = "url_path"
79
+ @collection.parse_page_url("anything.com/#{base}?anything").first.should == base
80
+ end
81
+
82
+ it "should return the arguments as a hash as the last array entry" do
83
+ args_hash = {"one" => "val_one", "two" => "val_two"}
84
+ @collection.parse_page_url("anything.com/anything?#{args_hash.map {|k,v| "#{k}=#{v}" }.join("&")}").last.should == args_hash
85
+ end
86
+ end
87
+
88
+ describe "#evaluate" do
89
+ it "returns the original result if it's provided a non-hash result" do
90
+ result = []
91
+ Koala::Facebook::GraphCollection.evaluate(result, @api).should == result
92
+ end
93
+
94
+ it "returns the original result if it's provided a nil result" do
95
+ result = nil
96
+ Koala::Facebook::GraphCollection.evaluate(result, @api).should == result
97
+ end
98
+
99
+ it "returns the original result if the result doesn't have a data key" do
100
+ result = {"paging" => {}}
101
+ Koala::Facebook::GraphCollection.evaluate(result, @api).should == result
102
+ end
103
+
104
+ it "returns the original result if the result's data key isn't an array" do
105
+ result = {"data" => {}, "paging" => {}}
106
+ Koala::Facebook::GraphCollection.evaluate(result, @api).should == result
107
+ end
108
+
109
+ it "returns a new GraphCollection of the result if it has an array data key and a paging key" do
110
+ result = {"data" => [], "paging" => {}}
111
+ expected = :foo
112
+ Koala::Facebook::GraphCollection.should_receive(:new).with(result, @api).and_return(expected)
113
+ Koala::Facebook::GraphCollection.evaluate(result, @api).should == expected
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,446 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Koala::HTTPService" do
4
+ it "has a faraday_middleware accessor" do
5
+ Koala::HTTPService.methods.map(&:to_sym).should include(:faraday_middleware)
6
+ Koala::HTTPService.methods.map(&:to_sym).should include(:faraday_middleware=)
7
+ end
8
+
9
+ it "has an http_options accessor" do
10
+ Koala::HTTPService.should respond_to(:http_options)
11
+ Koala::HTTPService.should respond_to(:http_options=)
12
+ end
13
+
14
+ it "sets http_options to {} by default" do
15
+ Koala::HTTPService.http_options.should == {}
16
+ end
17
+
18
+ describe "DEFAULT_MIDDLEWARE" do
19
+ before :each do
20
+ @builder = stub("Faraday connection builder")
21
+ @builder.stub(:request)
22
+ @builder.stub(:adapter)
23
+ end
24
+
25
+ it "is defined" do
26
+ Koala::HTTPService.const_defined?("DEFAULT_MIDDLEWARE").should be_true
27
+ end
28
+
29
+ it "adds multipart" do
30
+ @builder.should_receive(:request).with(:multipart)
31
+ Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
32
+ end
33
+
34
+ it "adds url_encoded" do
35
+ @builder.should_receive(:request).with(:url_encoded)
36
+ Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
37
+ end
38
+
39
+ it "uses the default adapter" do
40
+ adapter = :testing_now
41
+ Faraday.stub(:default_adapter).and_return(adapter)
42
+ @builder.should_receive(:adapter).with(adapter)
43
+ Koala::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
44
+ end
45
+ end
46
+
47
+ describe "server" do
48
+ describe "with no options" do
49
+ it "returns the REST server if options[:rest_api]" do
50
+ Koala::HTTPService.server(:rest_api => true).should =~ Regexp.new(Koala::Facebook::REST_SERVER)
51
+ end
52
+
53
+ it "returns the graph server if !options[:rest_api]" do
54
+ Koala::HTTPService.server(:rest_api => false).should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER)
55
+ Koala::HTTPService.server({}).should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER)
56
+ end
57
+ end
58
+
59
+ describe "with options[:beta]" do
60
+ before :each do
61
+ @options = {:beta => true}
62
+ end
63
+
64
+ it "returns the beta REST server if options[:rest_api]" do
65
+ server = Koala::HTTPService.server(@options.merge(:rest_api => true))
66
+ server.should =~ Regexp.new("beta.#{Koala::Facebook::REST_SERVER}")
67
+ end
68
+
69
+ it "returns the beta rest server if !options[:rest_api]" do
70
+ server = Koala::HTTPService.server(@options)
71
+ server.should =~ Regexp.new("beta.#{Koala::Facebook::GRAPH_SERVER}")
72
+ end
73
+ end
74
+
75
+ describe "with options[:video]" do
76
+ before :each do
77
+ @options = {:video => true}
78
+ end
79
+
80
+ it "should return the REST video server if options[:rest_api]" do
81
+ server = Koala::HTTPService.server(@options.merge(:rest_api => true))
82
+ server.should =~ Regexp.new(Koala::Facebook::REST_SERVER.gsub(/\.facebook/, "-video.facebook"))
83
+ end
84
+
85
+ it "should return the graph video server if !options[:rest_api]" do
86
+ server = Koala::HTTPService.server(@options)
87
+ server.should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER.gsub(/\.facebook/, "-video.facebook"))
88
+ end
89
+ end
90
+ end
91
+
92
+ describe "#encode_params" do
93
+ it "should return an empty string if param_hash evaluates to false" do
94
+ Koala::HTTPService.encode_params(nil).should == ''
95
+ end
96
+
97
+ it "should convert values to JSON if the value is not a String" do
98
+ val = 'json_value'
99
+ not_a_string = 'not_a_string'
100
+ not_a_string.stub(:is_a?).and_return(false)
101
+ MultiJson.should_receive(:encode).with(not_a_string).and_return(val)
102
+
103
+ string = "hi"
104
+
105
+ args = {
106
+ not_a_string => not_a_string,
107
+ string => string
108
+ }
109
+
110
+ result = Koala::HTTPService.encode_params(args)
111
+ result.split('&').find do |key_and_val|
112
+ key_and_val.match("#{not_a_string}=#{val}")
113
+ end.should be_true
114
+ end
115
+
116
+ it "should escape all values" do
117
+ args = Hash[*(1..4).map {|i| [i.to_s, "Value #{i}($"]}.flatten]
118
+
119
+ result = Koala::HTTPService.encode_params(args)
120
+ result.split('&').each do |key_val|
121
+ key, val = key_val.split('=')
122
+ val.should == CGI.escape(args[key])
123
+ end
124
+ end
125
+
126
+ it "should convert all keys to Strings" do
127
+ args = Hash[*(1..4).map {|i| [i, "val#{i}"]}.flatten]
128
+
129
+ result = Koala::HTTPService.encode_params(args)
130
+ result.split('&').each do |key_val|
131
+ key, val = key_val.split('=')
132
+ key.should == args.find{|key_val_arr| key_val_arr.last == val}.first.to_s
133
+ end
134
+ end
135
+ end
136
+
137
+ describe "#make_request" do
138
+ before :each do
139
+ # Setup stubs for make_request to execute without exceptions
140
+ @mock_body = stub('Typhoeus response body')
141
+ @mock_headers_hash = stub({:value => "headers hash"})
142
+ @mock_http_response = stub("Faraday Response", :status => 200, :headers => @mock_headers_hash, :body => @mock_body)
143
+
144
+ @mock_connection = stub("Faraday connection")
145
+ @mock_connection.stub(:get).and_return(@mock_http_response)
146
+ @mock_connection.stub(:post).and_return(@mock_http_response)
147
+ Faraday.stub(:new).and_return(@mock_connection)
148
+ end
149
+
150
+ describe "creating the Faraday connection" do
151
+ it "creates a Faraday connection using the server" do
152
+ server = "foo"
153
+ Koala::HTTPService.stub(:server).and_return(server)
154
+ Faraday.should_receive(:new).with(server, anything).and_return(@mock_connection)
155
+ Koala::HTTPService.make_request("anything", {}, "anything")
156
+ end
157
+
158
+ it "merges Koala::HTTPService.http_options into the request params" do
159
+ http_options = {:a => 2, :c => "3"}
160
+ Koala::HTTPService.http_options = http_options
161
+ Faraday.should_receive(:new).with(anything, hash_including(http_options)).and_return(@mock_connection)
162
+ Koala::HTTPService.make_request("anything", {}, "get")
163
+ end
164
+
165
+ it "merges any provided options into the request params" do
166
+ options = {:a => 2, :c => "3"}
167
+ Faraday.should_receive(:new).with(anything, hash_including(options)).and_return(@mock_connection)
168
+ Koala::HTTPService.make_request("anything", {}, "get", options)
169
+ end
170
+
171
+ it "overrides Koala::HTTPService.http_options with any provided options for the request params" do
172
+ options = {:a => 2, :c => "3"}
173
+ http_options = {:a => :a}
174
+ Koala::HTTPService.stub(:http_options).and_return(http_options)
175
+
176
+ Faraday.should_receive(:new).with(anything, hash_including(http_options.merge(options))).and_return(@mock_connection)
177
+ Koala::HTTPService.make_request("anything", {}, "get", options)
178
+ end
179
+
180
+ it "forces use_ssl to true if an access token is present" do
181
+ options = {:use_ssl => false}
182
+ Koala::HTTPService.stub(:http_options).and_return(:use_ssl => false)
183
+ Faraday.should_receive(:new).with(anything, hash_including(:use_ssl => true)).and_return(@mock_connection)
184
+ Koala::HTTPService.make_request("anything", {"access_token" => "foo"}, "get", options)
185
+ end
186
+
187
+ it "calls server with the composite options" do
188
+ options = {:a => 2, :c => "3"}
189
+ http_options = {:a => :a}
190
+ Koala::HTTPService.stub(:http_options).and_return(http_options)
191
+ Koala::HTTPService.should_receive(:server).with(hash_including(http_options.merge(options))).and_return("foo")
192
+ Koala::HTTPService.make_request("anything", {}, "get", options)
193
+ end
194
+
195
+ it "uses the default builder block if HTTPService.faraday_middleware block is not defined" do
196
+ Koala::HTTPService.stub(:faraday_middleware).and_return(nil)
197
+ Faraday.should_receive(:new).with(anything, anything, &Koala::HTTPService::DEFAULT_MIDDLEWARE).and_return(@mock_connection)
198
+ Koala::HTTPService.make_request("anything", {}, "get")
199
+ end
200
+
201
+ it "uses the defined HTTPService.faraday_middleware block if defined" do
202
+ block = Proc.new { }
203
+ Koala::HTTPService.should_receive(:faraday_middleware).and_return(block)
204
+ Faraday.should_receive(:new).with(anything, anything, &block).and_return(@mock_connection)
205
+ Koala::HTTPService.make_request("anything", {}, "get")
206
+ end
207
+ end
208
+
209
+ it "makes a POST request if the verb isn't get" do
210
+ @mock_connection.should_receive(:post).and_return(@mock_http_response)
211
+ Koala::HTTPService.make_request("anything", {}, "anything")
212
+ end
213
+
214
+ it "includes the verb in the body if the verb isn't get" do
215
+ verb = "eat"
216
+ @mock_connection.should_receive(:post).with(anything, hash_including("method" => verb)).and_return(@mock_http_response)
217
+ Koala::HTTPService.make_request("anything", {}, verb)
218
+ end
219
+
220
+ it "makes a GET request if the verb is get" do
221
+ @mock_connection.should_receive(:get).and_return(@mock_http_response)
222
+ Koala::HTTPService.make_request("anything", {}, "get")
223
+ end
224
+
225
+ describe "for GETs" do
226
+ it "submits the arguments in the body" do
227
+ # technically this is done for all requests, but you don't send GET requests with files
228
+ args = {"a" => :b, "c" => 3}
229
+ Faraday.should_receive(:new).with(anything, hash_including(:params => args)).and_return(@mock_connection)
230
+ Koala::HTTPService.make_request("anything", args, "get")
231
+ end
232
+
233
+ it "submits nothing to the body" do
234
+ # technically this is done for all requests, but you don't send GET requests with files
235
+ args = {"a" => :b, "c" => 3}
236
+ @mock_connection.should_receive(:get).with(anything, {}).and_return(@mock_http_response)
237
+ Koala::HTTPService.make_request("anything", args, "get")
238
+ end
239
+ end
240
+
241
+ describe "for POSTs" do
242
+ it "submits the arguments in the body" do
243
+ # technically this is done for all requests, but you don't send GET requests with files
244
+ args = {"a" => :b, "c" => 3}
245
+ @mock_connection.should_receive(:post).with(anything, hash_including(args)).and_return(@mock_http_response)
246
+ Koala::HTTPService.make_request("anything", args, "post")
247
+ end
248
+
249
+ it "turns any UploadableIOs to UploadIOs" do
250
+ # technically this is done for all requests, but you don't send GET requests with files
251
+ upload_io = stub("UploadIO")
252
+ u = Koala::UploadableIO.new("/path/to/stuff", "img/jpg")
253
+ u.stub(:to_upload_io).and_return(upload_io)
254
+ @mock_connection.should_receive(:post).with(anything, hash_including("source" => upload_io)).and_return(@mock_http_response)
255
+ Koala::HTTPService.make_request("anything", {:source => u}, "post")
256
+ end
257
+ end
258
+ end
259
+
260
+ describe "deprecated options" do
261
+ before :each do
262
+ Koala::HTTPService.stub(:http_options).and_return({})
263
+ @service = Koala.http_service
264
+ end
265
+
266
+ after :each do
267
+ Koala.http_service = @service
268
+ end
269
+
270
+ {
271
+ :timeout => :timeout,
272
+ :always_use_ssl => :use_ssl,
273
+ :proxy => :proxy
274
+ }.each_pair do |deprecated_method, parameter|
275
+ describe "##{deprecated_method}" do
276
+ context "read" do
277
+ it "reads http_options[:#{parameter}]" do
278
+ value = "foo"
279
+ Koala::HTTPService.http_options[parameter] = value
280
+ Koala::HTTPService.send(deprecated_method).should == value
281
+ end
282
+
283
+ it "generates a deprecation warning" do
284
+ Koala::Utils.should_receive(:deprecate)
285
+ Koala::HTTPService.send(deprecated_method)
286
+ end
287
+ end
288
+
289
+ context "write" do
290
+ it "writes to http_options[:#{parameter}]" do
291
+ Koala::HTTPService.http_options[parameter] = nil
292
+ value = "foo"
293
+ Koala::HTTPService.send(:"#{deprecated_method}=", value)
294
+ Koala::HTTPService.http_options[parameter].should == value
295
+ end
296
+
297
+ it "generates a deprecation warning" do
298
+ Koala::Utils.should_receive(:deprecate)
299
+ Koala::HTTPService.send(:"#{deprecated_method}=", 2)
300
+ end
301
+ end
302
+ end
303
+ end
304
+
305
+ # ssl options
306
+ [:ca_path, :ca_file, :verify_mode].each do |deprecated_method|
307
+ describe "##{deprecated_method}" do
308
+ context "read" do
309
+ it "reads http_options[:ssl][:#{deprecated_method}] if http_options[:ssl]" do
310
+ value = "foo"
311
+ Koala::HTTPService.http_options[:ssl] = {deprecated_method => value}
312
+ Koala::HTTPService.send(deprecated_method).should == value
313
+ end
314
+
315
+ it "returns nil if http_options[:ssl] is not defined" do
316
+ Koala::HTTPService.send(deprecated_method).should be_nil
317
+ end
318
+
319
+ it "generates a deprecation warning" do
320
+ Koala::Utils.should_receive(:deprecate)
321
+ Koala::HTTPService.send(deprecated_method)
322
+ end
323
+ end
324
+
325
+ context "write" do
326
+ it "defines http_options[:ssl] if not defined" do
327
+ Koala::HTTPService.http_options[:ssl] = nil
328
+ value = "foo"
329
+ Koala::HTTPService.send(:"#{deprecated_method}=", value)
330
+ Koala::HTTPService.http_options[:ssl].should
331
+ end
332
+
333
+ it "writes to http_options[:ssl][:#{deprecated_method}]" do
334
+ value = "foo"
335
+ Koala::HTTPService.send(:"#{deprecated_method}=", value)
336
+ Koala::HTTPService.http_options[:ssl].should
337
+ Koala::HTTPService.http_options[:ssl][deprecated_method].should == value
338
+ end
339
+
340
+ it "does not redefine http_options[:ssl] if already defined" do
341
+ hash = {:a => 2}
342
+ Koala::HTTPService.http_options[:ssl] = hash
343
+ Koala::HTTPService.send(:"#{deprecated_method}=", 3)
344
+ Koala::HTTPService.http_options[:ssl].should include(hash)
345
+ end
346
+
347
+ it "generates a deprecation warning" do
348
+ Koala::Utils.should_receive(:deprecate)
349
+ Koala::HTTPService.send(:"#{deprecated_method}=", 2)
350
+ end
351
+ end
352
+ end
353
+ end
354
+
355
+ describe "per-request options" do
356
+ before :each do
357
+ # Setup stubs for make_request to execute without exceptions
358
+ @mock_body = stub('Typhoeus response body')
359
+ @mock_headers_hash = stub({:value => "headers hash"})
360
+ @mock_http_response = stub("Faraday Response", :status => 200, :headers => @mock_headers_hash, :body => @mock_body)
361
+
362
+ @mock_connection = stub("Faraday connection")
363
+ @mock_connection.stub(:get).and_return(@mock_http_response)
364
+ @mock_connection.stub(:post).and_return(@mock_http_response)
365
+ Faraday.stub(:new).and_return(@mock_connection)
366
+ end
367
+
368
+ describe ":typhoeus_options" do
369
+ it "merges any typhoeus_options into options" do
370
+ typhoeus_options = {:a => 2}
371
+ Faraday.should_receive(:new).with(anything, hash_including(typhoeus_options)).and_return(@mock_connection)
372
+ Koala::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
373
+ end
374
+
375
+ it "deletes the typhoeus_options key" do
376
+ typhoeus_options = {:a => 2}
377
+ Faraday.should_receive(:new).with(anything, hash_not_including(:typhoeus_options => typhoeus_options)).and_return(@mock_connection)
378
+ Koala::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
379
+ end
380
+ end
381
+
382
+ describe ":ca_path" do
383
+ it "sets any ca_path into options[:ssl]" do
384
+ ca_path = :foo
385
+ Faraday.should_receive(:new).with(anything, hash_including(:ssl => hash_including(:ca_path => ca_path))).and_return(@mock_connection)
386
+ Koala::HTTPService.make_request("anything", {}, "get", :ca_path => ca_path)
387
+ end
388
+
389
+ it "deletes the ca_path key" do
390
+ ca_path = :foo
391
+ Faraday.should_receive(:new).with(anything, hash_not_including(:ca_path => ca_path)).and_return(@mock_connection)
392
+ Koala::HTTPService.make_request("anything", {}, "get", :ca_path => ca_path)
393
+ end
394
+ end
395
+
396
+ describe ":ca_file" do
397
+ it "sets any ca_file into options[:ssl]" do
398
+ ca_file = :foo
399
+ Faraday.should_receive(:new).with(anything, hash_including(:ssl => hash_including(:ca_file => ca_file))).and_return(@mock_connection)
400
+ Koala::HTTPService.make_request("anything", {}, "get", :ca_file => ca_file)
401
+ end
402
+
403
+ it "deletes the ca_file key" do
404
+ ca_file = :foo
405
+ Faraday.should_receive(:new).with(anything, hash_not_including(:ca_file => ca_file)).and_return(@mock_connection)
406
+ Koala::HTTPService.make_request("anything", {}, "get", :ca_file => ca_file)
407
+ end
408
+ end
409
+
410
+ describe ":verify_mode" do
411
+ it "sets any verify_mode into options[:ssl]" do
412
+ verify_mode = :foo
413
+ Faraday.should_receive(:new).with(anything, hash_including(:ssl => hash_including(:verify_mode => verify_mode))).and_return(@mock_connection)
414
+ Koala::HTTPService.make_request("anything", {}, "get", :verify_mode => verify_mode)
415
+ end
416
+
417
+ it "deletes the verify_mode key" do
418
+ verify_mode = :foo
419
+ Faraday.should_receive(:new).with(anything, hash_not_including(:verify_mode => verify_mode)).and_return(@mock_connection)
420
+ Koala::HTTPService.make_request("anything", {}, "get", :verify_mode => verify_mode)
421
+ end
422
+ end
423
+ end
424
+
425
+ {
426
+ :typhoeus => Koala::TyphoeusService,
427
+ :net_http => Koala::NetHTTPService
428
+ }.each_pair do |adapter, module_class|
429
+ describe module_class.to_s do
430
+ it "should respond to deprecated_interface" do
431
+ module_class.should respond_to(:deprecated_interface)
432
+ end
433
+
434
+ it "should issue a deprecation warning" do
435
+ Koala::Utils.should_receive(:deprecate)
436
+ module_class.deprecated_interface
437
+ end
438
+
439
+ it "should set the default adapter to #{adapter}" do
440
+ module_class.deprecated_interface
441
+ Faraday.default_adapter.should == adapter
442
+ end
443
+ end
444
+ end
445
+ end
446
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Koala" do
4
+ it "has a version" do
5
+ Koala.const_defined?("VERSION").should be_true
6
+ end
7
+
8
+ it "has an http_service accessor" do
9
+ Koala.should respond_to(:http_service)
10
+ Koala.should respond_to(:http_service=)
11
+ end
12
+
13
+ context "for deprecated services" do
14
+ before :each do
15
+ @service = Koala.http_service
16
+ end
17
+
18
+ after :each do
19
+ Koala.http_service = @service
20
+ end
21
+
22
+ it "invokes deprecated_interface if present" do
23
+ mock_service = stub("http service")
24
+ mock_service.should_receive(:deprecated_interface)
25
+ Koala.http_service = mock_service
26
+ end
27
+
28
+ it "does not set the service if it's deprecated" do
29
+ mock_service = stub("http service")
30
+ mock_service.stub(:deprecated_interface)
31
+ Koala.http_service = mock_service
32
+ Koala.http_service.should == @service
33
+ end
34
+
35
+ it "sets the service if it's not deprecated" do
36
+ mock_service = stub("http service")
37
+ Koala.http_service = mock_service
38
+ Koala.http_service.should == mock_service
39
+ end
40
+ end
41
+
42
+ describe "make_request" do
43
+ it "passes all its arguments to the http_service" do
44
+ path = "foo"
45
+ args = {:a => 2}
46
+ verb = "get"
47
+ options = {:c => :d}
48
+
49
+ Koala.http_service.should_receive(:make_request).with(path, args, verb, options)
50
+ Koala.make_request(path, args, verb, options)
51
+ end
52
+ end
53
+
54
+ end