koala 1.0.0.beta → 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.
Files changed (52) hide show
  1. data/.gitignore +3 -0
  2. data/CHANGELOG +17 -3
  3. data/Gemfile +3 -0
  4. data/LICENSE +1 -1
  5. data/Manifest +5 -2
  6. data/Rakefile +13 -14
  7. data/koala.gemspec +35 -20
  8. data/lib/koala/graph_api.rb +183 -131
  9. data/lib/koala/http_services.rb +46 -55
  10. data/lib/koala/test_users.rb +21 -8
  11. data/lib/koala/uploadable_io.rb +115 -0
  12. data/lib/koala.rb +52 -84
  13. data/readme.md +19 -6
  14. data/spec/cases/api_base_spec.rb +101 -0
  15. data/spec/cases/graph_and_rest_api_spec.rb +31 -0
  16. data/spec/cases/graph_api_spec.rb +25 -0
  17. data/spec/cases/http_services/http_service_spec.rb +54 -0
  18. data/spec/cases/http_services/net_http_service_spec.rb +350 -0
  19. data/spec/cases/http_services/typhoeus_service_spec.rb +144 -0
  20. data/spec/cases/oauth_spec.rb +409 -0
  21. data/spec/cases/realtime_updates_spec.rb +184 -0
  22. data/spec/cases/rest_api_spec.rb +25 -0
  23. data/spec/{koala/test_users/test_users_tests.rb → cases/test_users_spec.rb} +46 -33
  24. data/spec/cases/uploadable_io_spec.rb +151 -0
  25. data/spec/{facebook_data.yml → fixtures/facebook_data.yml} +12 -14
  26. data/spec/{mock_facebook_responses.yml → fixtures/mock_facebook_responses.yml} +313 -306
  27. data/spec/spec_helper.rb +18 -0
  28. data/spec/support/graph_api_shared_examples.rb +424 -0
  29. data/spec/{koala → support}/live_testing_data_helper.rb +39 -42
  30. data/spec/{mock_http_service.rb → support/mock_http_service.rb} +94 -95
  31. data/spec/{koala/rest_api/rest_api_tests.rb → support/rest_api_shared_examples.rb} +43 -0
  32. data/spec/support/setup_mocks_or_live.rb +52 -0
  33. data/spec/support/uploadable_io_shared_examples.rb +76 -0
  34. metadata +107 -48
  35. data/init.rb +0 -2
  36. data/spec/koala/api_base_tests.rb +0 -102
  37. data/spec/koala/graph_and_rest_api/graph_and_rest_api_no_token_tests.rb +0 -14
  38. data/spec/koala/graph_and_rest_api/graph_and_rest_api_with_token_tests.rb +0 -16
  39. data/spec/koala/graph_api/graph_api_no_access_token_tests.rb +0 -63
  40. data/spec/koala/graph_api/graph_api_tests.rb +0 -86
  41. data/spec/koala/graph_api/graph_api_with_access_token_tests.rb +0 -154
  42. data/spec/koala/graph_api/graph_collection_tests.rb +0 -104
  43. data/spec/koala/net_http_service_tests.rb +0 -430
  44. data/spec/koala/oauth/oauth_tests.rb +0 -409
  45. data/spec/koala/realtime_updates/realtime_updates_tests.rb +0 -187
  46. data/spec/koala/rest_api/rest_api_no_access_token_tests.rb +0 -25
  47. data/spec/koala/rest_api/rest_api_with_access_token_tests.rb +0 -38
  48. data/spec/koala/typhoeus_service_tests.rb +0 -156
  49. data/spec/koala_spec.rb +0 -18
  50. data/spec/koala_spec_helper.rb +0 -70
  51. data/spec/koala_spec_without_mocks.rb +0 -19
  52. /data/spec/{koala/assets → fixtures}/beach.jpg +0 -0
@@ -1,430 +0,0 @@
1
- require 'koala/http_services'
2
- class NetHTTPServiceTests < Test::Unit::TestCase
3
- module Bear
4
- include Koala::NetHTTPService
5
- end
6
-
7
- describe "NetHTTPService module holder class Bear" do
8
- before :each do
9
- # reset the always_use_ssl parameter
10
- Bear.always_use_ssl = nil
11
- end
12
-
13
- it "should define a make_request static module method" do
14
- Bear.respond_to?(:make_request).should be_true
15
- end
16
-
17
- it "should include the Koala::HTTPService module defining common features" do
18
- Bear.included_modules.include?(Koala::HTTPService).should be_true
19
- end
20
-
21
- describe "when making a request" do
22
- before(:each) do
23
- # Setup stubs for make_request to execute without exceptions
24
- @mock_http_response = stub('Net::HTTPResponse', :code => 1)
25
- @mock_body = stub('Net::HTTPResponse body')
26
- @http_request_result = [@mock_http_response, @mock_body]
27
-
28
- # to_ary is called in Ruby 1.9 to provide backwards compatibility
29
- # with the response, body = http.get() syntax we use
30
- @mock_http_response.stub!(:to_ary).and_return(@http_request_result)
31
-
32
- @http_yield_mock = mock('Net::HTTP start yielded object')
33
-
34
- @http_yield_mock.stub(:post).and_return(@http_request_result)
35
- @http_yield_mock.stub(:get).and_return(@http_request_result)
36
-
37
- @http_mock = stub('Net::HTTP object', 'use_ssl=' => true, 'verify_mode=' => true)
38
- @http_mock.stub(:start).and_yield(@http_yield_mock)
39
-
40
- Net::HTTP.stub(:new).and_return(@http_mock)
41
- end
42
-
43
- describe "the connection" do
44
- it "should use POST if verb is not GET" do
45
- @http_yield_mock.should_receive(:post).and_return(@mock_http_response)
46
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
47
-
48
- Bear.make_request('anything', {}, 'anything')
49
- end
50
-
51
- it "should use GET if that verb is specified" do
52
- @http_yield_mock.should_receive(:get).and_return(@mock_http_response)
53
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
54
-
55
- Bear.make_request('anything', {}, 'get')
56
- end
57
-
58
- it "should add the method to the arguments if it's not get or post" do
59
- args = {}
60
- method = "telekenesis"
61
- # since the arguments get encoded later, we'll test for merge!
62
- # even though that's somewhat testing internal implementation
63
- args.should_receive(:merge!).with(:method => method)
64
-
65
- Bear.make_request('anything', args, method)
66
- end
67
- end
68
-
69
- describe "if the request has an access token" do
70
- before :each do
71
- @args = {"access_token" => "123"}
72
- end
73
-
74
- it "should use SSL" do
75
- @http_mock.should_receive('use_ssl=').with(true)
76
-
77
- Bear.make_request('anything', @args, 'anything')
78
- end
79
-
80
- it "should set the port to 443" do
81
- Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
82
-
83
- Bear.make_request('anything', @args, 'anything')
84
- end
85
- end
86
-
87
- describe "if always_use_ssl is true" do
88
- before :each do
89
- Bear.always_use_ssl = true
90
- end
91
-
92
- it "should use SSL" do
93
- @http_mock.should_receive('use_ssl=').with(true)
94
-
95
- Bear.make_request('anything', {}, 'anything')
96
- end
97
-
98
- it "should set the port to 443" do
99
- Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
100
-
101
- Bear.make_request('anything', {}, 'anything')
102
- end
103
- end
104
-
105
- describe "if the use_ssl option is provided" do
106
- it "should use SSL" do
107
- @http_mock.should_receive('use_ssl=').with(true)
108
-
109
- Bear.make_request('anything', {}, 'anything', :use_ssl => true)
110
- end
111
-
112
- it "should set the port to 443" do
113
- Net::HTTP.should_receive(:new).with(anything, 443).and_return(@http_mock)
114
-
115
- Bear.make_request('anything', {}, 'anything', :use_ssl => true)
116
- end
117
- end
118
-
119
- describe "if there's no token and always_use_ssl isn't true" do
120
- it "should not use SSL" do
121
- @http_mock.should_not_receive('use_ssl=')
122
- Bear.make_request('anything', {}, 'anything')
123
- end
124
-
125
- it "should not set the port" do
126
- Net::HTTP.should_receive(:new).with(anything, nil).and_return(@http_mock)
127
- Bear.make_request('anything', {}, 'anything')
128
- end
129
- end
130
-
131
- it "should use the graph server by default" do
132
- Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
133
- Bear.make_request('anything', {}, 'anything')
134
- end
135
-
136
- it "should use the REST server if the :rest_api option is true" do
137
- Net::HTTP.should_receive(:new).with(Koala::Facebook::REST_SERVER, anything).and_return(@http_mock)
138
- Bear.make_request('anything', {}, 'anything', :rest_api => true)
139
- end
140
-
141
- it "should turn off vertificate validaiton warnings" do
142
- @http_mock.should_receive('verify_mode=').with(OpenSSL::SSL::VERIFY_NONE)
143
-
144
- Bear.make_request('anything', {}, 'anything')
145
- end
146
-
147
- it "should start an HTTP connection" do
148
- @http_mock.should_receive(:start).and_yield(@http_yield_mock)
149
- Bear.make_request('anything', {}, 'anything')
150
- end
151
-
152
- describe "via POST" do
153
- it "should use Net::HTTP to make a POST request" do
154
- @http_yield_mock.should_receive(:post).and_return(@http_request_result)
155
-
156
- Bear.make_request('anything', {}, 'post')
157
- end
158
-
159
- it "should go to the specified path adding a / if it doesn't exist" do
160
- path = mock('Path')
161
- @http_yield_mock.should_receive(:post).with(path, anything).and_return(@http_request_result)
162
-
163
- Bear.make_request(path, {}, 'post')
164
- end
165
-
166
- it "should use encoded parameters" do
167
- args = {}
168
- params = mock('Encoded parameters')
169
- Bear.should_receive(:encode_params).with(args).and_return(params)
170
-
171
- @http_yield_mock.should_receive(:post).with(anything, params).and_return(@http_request_result)
172
-
173
- Bear.make_request('anything', args, 'post')
174
- end
175
-
176
- describe "with multipart/form-data" do
177
- before(:each) do
178
- Bear.stub(:encode_multipart_params)
179
- Bear.stub("params_require_multipart?").and_return(true)
180
-
181
- @multipart_request_stub = stub('Stub Multipart Request')
182
- Net::HTTP::Post::Multipart.stub(:new).and_return(@multipart_request_stub)
183
-
184
- @file_stub = stub('fake File', "kind_of?" => true, "path" => 'anypath.jpg')
185
-
186
- @http_yield_mock.stub(:request).with(@multipart_request_stub).and_return(@http_request_result)
187
- end
188
-
189
- it "should use multipart/form-data if any parameter is a valid file hash" do
190
- @http_yield_mock.should_receive(:request).with(@multipart_request_stub).and_return(@http_request_result)
191
-
192
- Bear.make_request('anything', {}, 'post')
193
- end
194
-
195
- it "should use the given request path for the request" do
196
- args = {"file" => @file_stub}
197
- expected_path = 'expected/path'
198
-
199
- Net::HTTP::Post::Multipart.should_receive(:new).with(expected_path, anything).and_return(@multipart_request_stub)
200
-
201
- Bear.make_request(expected_path, {}, 'post')
202
- end
203
-
204
- it "should use multipart encoded arguments for the request" do
205
- args = {"file" => @file_stub}
206
- expected_params = stub('Stub Multipart Params')
207
-
208
- Bear.should_receive(:encode_multipart_params).with(args).and_return(expected_params)
209
- Net::HTTP::Post::Multipart.should_receive(:new).with(anything, expected_params).and_return(@multipart_request_stub)
210
-
211
- Bear.make_request('anything', args, 'post')
212
- end
213
- end
214
- end
215
-
216
- describe "via GET" do
217
- it "should use Net::HTTP to make a GET request" do
218
- @http_yield_mock.should_receive(:get).and_return(@http_request_result)
219
-
220
- Bear.make_request('anything', {}, 'get')
221
- end
222
-
223
- it "should use the correct path, including arguments" do
224
- path = mock('Path')
225
- params = mock('Encoded parameters')
226
- args = {}
227
-
228
- Bear.should_receive(:encode_params).with(args).and_return(params)
229
- @http_yield_mock.should_receive(:get).with("#{path}?#{params}").and_return(@http_request_result)
230
-
231
- Bear.make_request(path, args, 'get')
232
- end
233
- end
234
-
235
- describe "the returned value" do
236
- before(:each) do
237
- @response = Bear.make_request('anything', {}, 'anything')
238
- end
239
-
240
- it "should return a Koala::Response object" do
241
- @response.class.should == Koala::Response
242
- end
243
-
244
- it "should return a Koala::Response with the right status" do
245
- @response.status.should == @mock_http_response.code
246
- end
247
-
248
- it "should reutrn a Koala::Response with the right body" do
249
- @response.body.should == @mock_body
250
- end
251
-
252
- it "should return a Koala::Response with the Net::HTTPResponse object as headers" do
253
- @response.headers.should == @mock_http_response
254
- end
255
- end # describe return value
256
- end # describe when making a request
257
-
258
- describe "when encoding parameters" do
259
- it "should return an empty string if param_hash evaluates to false" do
260
- Bear.encode_params(nil).should == ''
261
- end
262
-
263
- it "should convert values to JSON if the value is not a String" do
264
- val = 'json_value'
265
- not_a_string = 'not_a_string'
266
- not_a_string.stub(:class).and_return('NotAString')
267
- not_a_string.should_receive(:to_json).and_return(val)
268
-
269
- string = "hi"
270
-
271
- args = {
272
- not_a_string => not_a_string,
273
- string => string
274
- }
275
-
276
- result = Bear.encode_params(args)
277
- result.split('&').find do |key_and_val|
278
- key_and_val.match("#{not_a_string}=#{val}")
279
- end.should be_true
280
- end
281
-
282
- it "should escape all values" do
283
- args = Hash[*(1..4).map {|i| [i.to_s, "Value #{i}($"]}.flatten]
284
-
285
- result = Bear.encode_params(args)
286
- result.split('&').each do |key_val|
287
- key, val = key_val.split('=')
288
- val.should == CGI.escape(args[key])
289
- end
290
- end
291
-
292
- it "should convert all keys to Strings" do
293
- args = Hash[*(1..4).map {|i| [i, "val#{i}"]}.flatten]
294
-
295
- result = Bear.encode_params(args)
296
- result.split('&').each do |key_val|
297
- key, val = key_val.split('=')
298
- key.should == args.find{|key_val_arr| key_val_arr.last == val}.first.to_s
299
- end
300
- end
301
- end
302
-
303
- describe "when detecting if multipart posting is needed" do
304
- it "should be true if any parameter value requires multipart post" do
305
- valid_file_hash = stub("Stub Valid File Hash")
306
-
307
- Bear.stub!("is_valid_file_hash?").and_return(false)
308
- Bear.stub!("is_valid_file_hash?").with(valid_file_hash).and_return(true)
309
-
310
- args = {
311
- "key1" => "val",
312
- "key2" => "val",
313
- "key3" => valid_file_hash,
314
- "key4" => "val"
315
- }
316
-
317
- Bear.params_require_multipart?(args).should be_true
318
- end
319
-
320
- describe "and looking at individual values" do
321
- before(:each) do
322
- @valid_hash = {
323
- "content_type" => 1,
324
- "path" => 1
325
- }
326
- end
327
-
328
- it "should only accept hashes" do
329
- Bear.is_valid_file_hash?(@valid_hash).should be_true
330
-
331
- @valid_hash.stub!("kind_of?").with(Hash).and_return(false)
332
- Bear.is_valid_file_hash?(@valid_hash).should be_false
333
- end
334
-
335
- it "should always require a content_type key" do
336
- @valid_hash.delete("content_type")
337
- Bear.is_valid_file_hash?(@valid_hash).should be_false
338
- end
339
-
340
- it "should always require the path key" do
341
- @valid_hash.delete("path")
342
- Bear.is_valid_file_hash?(@valid_hash).should be_false
343
- end
344
-
345
- describe "with file IOs" do
346
- before :each do
347
- @stub_file = stub('Stub IO File')
348
- @valid_hash["file"] = @stub_file
349
- end
350
-
351
- it "should accept hashes with the file object that responds to read" do
352
- @stub_file.should_receive("respond_to?").with(:read).and_return(true)
353
-
354
- @valid_hash["file"] = @stub_file
355
- Bear.is_valid_file_hash?(@valid_hash).should be_true
356
- end
357
-
358
- it "should not accept hashes with a file object that does not respond to read" do
359
- @stub_file.should_receive("respond_to?").with(:read).and_return(false)
360
-
361
- @valid_hash["file"] = @stub_file
362
- Bear.is_valid_file_hash?(@valid_hash).should be_false
363
- end
364
-
365
- end
366
- end
367
- end
368
-
369
- describe "when encoding multipart/form-data params" do
370
- it "should replace valid file hashes with file objects with UploadIO objects" do
371
- file_hash_stub = {
372
- "path" => "Fake File Name",
373
- "content_type" => "Fake Content Type"
374
- }
375
-
376
- # UploadIO should be created
377
- uploadio_stub = stub("UploadIO Shell Stub")
378
- UploadIO.should_receive("new").with(file_hash_stub["path"], file_hash_stub["content_type"]).and_return(uploadio_stub)
379
-
380
- # Ruby 1.9 test compatibility
381
- content_stub = "UploadIOContent Stub"
382
- uploadio_stub.stub(:to_ary).and_return([content_stub])
383
-
384
- args = {
385
- "not_a_file" => "not a file",
386
- "file" => file_hash_stub
387
- }
388
-
389
- # Check that is_valid_file_hash is called on the file_hash_stub
390
- Bear.stub!("is_valid_file_hash?").and_return(false)
391
- Bear.should_receive("is_valid_file_hash?").with(file_hash_stub).and_return(true)
392
-
393
- result = Bear.encode_multipart_params(args)
394
-
395
- result["not_a_file"] == args["not_a_file"]
396
- result["file"] == content_stub
397
- end
398
-
399
- it "should replace valid file hashes with file objects with UploadIO objects" do
400
- file_hash_stub = {
401
- "file" => "Fake File IO",
402
- "path" => "Fake File Name",
403
- "content_type" => "Fake Content Type"
404
- }
405
-
406
- # UploadIO should be created
407
- uploadio_stub = stub("UploadIO Shell Stub")
408
- UploadIO.should_receive("new").with(file_hash_stub["file"], file_hash_stub["content_type"], file_hash_stub["path"]).and_return(uploadio_stub)
409
-
410
- # Ruby 1.9 test compatibility
411
- content_stub = "UploadIOContent Stub"
412
- uploadio_stub.stub(:to_ary).and_return([content_stub])
413
-
414
- args = {
415
- "not_a_file" => "not a file",
416
- "file" => file_hash_stub
417
- }
418
-
419
- # Check that is_valid_file_hash is called on the file_hash_stub
420
- Bear.stub!("is_valid_file_hash?").and_return(false)
421
- Bear.should_receive("is_valid_file_hash?").with(file_hash_stub).and_return(true)
422
-
423
- result = Bear.encode_multipart_params(args)
424
-
425
- result["not_a_file"] == args["not_a_file"]
426
- result["file"] == content_stub
427
- end
428
- end
429
- end
430
- end