koala 1.1.0rc2 → 1.1.0rc3

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,47 +35,47 @@ describe "Koala::HTTPService" do
35
35
  end
36
36
  end
37
37
 
38
- describe "ca_file accessor" do
39
- it "should be added" do
40
- Bear.methods.collect {|m| m.to_sym}.should include(:ca_file)
41
- Bear.methods.collect {|m| m.to_sym}.should include(:ca_file=)
42
- end
43
- end
44
-
45
- describe "ca_path accessor" do
46
- it "should be added" do
47
- Bear.methods.collect {|m| m.to_sym}.should include(:ca_path)
48
- Bear.methods.collect {|m| m.to_sym}.should include(:ca_path=)
49
- end
50
- end
51
-
52
38
  describe "server" do
53
- describe "without options[:beta]" do
54
- it "should return the rest server if options[:rest_api]" do
39
+ describe "with no options" do
40
+ it "returns the REST server if options[:rest_api]" do
55
41
  Bear.server(:rest_api => true).should == Koala::Facebook::REST_SERVER
56
42
  end
57
43
 
58
- it "should return the rest server if !options[:rest_api]" do
44
+ it "returns the graph server if !options[:rest_api]" do
59
45
  Bear.server(:rest_api => false).should == Koala::Facebook::GRAPH_SERVER
60
46
  Bear.server({}).should == Koala::Facebook::GRAPH_SERVER
61
47
  end
62
48
  end
63
-
64
- describe "without options[:beta]" do
49
+
50
+ describe "with options[:beta]" do
65
51
  before :each do
66
52
  @options = {:beta => true}
67
53
  end
68
54
 
69
- it "should return the rest server if options[:rest_api]" do
55
+ it "returns the beta REST server if options[:rest_api]" do
56
+ server = Bear.server(@options.merge(:rest_api => true))
57
+ server.should =~ Regexp.new("beta.#{Koala::Facebook::REST_SERVER}")
58
+ end
59
+
60
+ it "returns the beta rest server if !options[:rest_api]" do
61
+ server = Bear.server(@options)
62
+ server.should =~ Regexp.new("beta.#{Koala::Facebook::GRAPH_SERVER}")
63
+ end
64
+ end
65
+
66
+ describe "with options[:video]" do
67
+ before :each do
68
+ @options = {:video => true}
69
+ end
70
+
71
+ it "should return the REST video server if options[:rest_api]" do
70
72
  server = Bear.server(@options.merge(:rest_api => true))
71
- server.should =~ Regexp.new(Koala::Facebook::REST_SERVER)
72
- server.should =~ /beta\./
73
+ server.should =~ Regexp.new(Koala::Facebook::REST_SERVER.gsub(/\.facebook/, "-video.facebook"))
73
74
  end
74
75
 
75
- it "should return the rest server if !options[:rest_api]" do
76
- server = Bear.server(:beta => true)
77
- server.should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER)
78
- server.should =~ /beta\./
76
+ it "should return the graph video server if !options[:rest_api]" do
77
+ server = Bear.server(@options)
78
+ server.should =~ Regexp.new(Koala::Facebook::GRAPH_SERVER.gsub(/\.facebook/, "-video.facebook"))
79
79
  end
80
80
  end
81
81
  end
@@ -89,7 +89,7 @@ describe "Koala::HTTPService" do
89
89
  val = 'json_value'
90
90
  not_a_string = 'not_a_string'
91
91
  not_a_string.stub(:is_a?).and_return(false)
92
- not_a_string.should_receive(:to_json).and_return(val)
92
+ MultiJson.should_receive(:encode).with(not_a_string).and_return(val)
93
93
 
94
94
  string = "hi"
95
95
 
@@ -9,6 +9,21 @@ describe "NetHTTPService module holder class Horse" do
9
9
  Horse.always_use_ssl = Horse.proxy = Horse.timeout = nil
10
10
  end
11
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
+
12
27
  it "should define a make_request static module method" do
13
28
  Horse.respond_to?(:make_request).should be_true
14
29
  end
@@ -20,23 +35,19 @@ describe "NetHTTPService module holder class Horse" do
20
35
  describe "when making a request" do
21
36
  before(:each) do
22
37
  # Setup stubs for make_request to execute without exceptions
23
- @mock_http_response = stub('Net::HTTPResponse', :code => 1)
24
38
  @mock_body = stub('Net::HTTPResponse body')
25
- @http_request_result = [@mock_http_response, @mock_body]
26
-
27
- # to_ary is called in Ruby 1.9 to provide backwards compatibility
28
- # with the response, body = http.get() syntax we use
29
- @mock_http_response.stub!(:to_ary).and_return(@http_request_result)
39
+ @mock_http_response = stub('Net::HTTPResponse', :code => 1, :body => @mock_body)
30
40
 
31
41
  @http_yield_mock = mock('Net::HTTP start yielded object')
32
42
 
33
- @http_yield_mock.stub(:post).and_return(@http_request_result)
34
- @http_yield_mock.stub(:get).and_return(@http_request_result)
43
+ @http_yield_mock.stub(:post).and_return(@mock_http_response)
44
+ @http_yield_mock.stub(:get).and_return(@mock_http_response)
35
45
 
36
46
  @http_mock = stub('Net::HTTP object', 'use_ssl=' => true, 'verify_mode=' => true)
37
47
  @http_mock.stub(:start).and_yield(@http_yield_mock)
38
48
  @http_mock.stub(:ca_path=)
39
- @http_mock.stub(:ca_file=)
49
+ @http_mock.stub(:ca_file=)
50
+ @http_mock.stub(:verify_mode=)
40
51
 
41
52
  Net::HTTP.stub(:new).and_return(@http_mock)
42
53
  end
@@ -84,7 +95,7 @@ describe "NetHTTPService module holder class Horse" do
84
95
  Horse.make_request('anything', @args, 'anything')
85
96
  end
86
97
  end
87
-
98
+
88
99
  describe "if always_use_ssl is true" do
89
100
  before :each do
90
101
  Horse.always_use_ssl = true
@@ -128,7 +139,7 @@ describe "NetHTTPService module holder class Horse" do
128
139
  Horse.make_request('anything', {}, 'anything')
129
140
  end
130
141
  end
131
-
142
+
132
143
  describe "proxy options" do
133
144
  before :each do
134
145
  Horse.proxy = "http://defaultproxy"
@@ -141,19 +152,19 @@ describe "NetHTTPService module holder class Horse" do
141
152
  Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything, "passedproxy", 80, nil, nil).and_return(@http_mock)
142
153
  Horse.make_request('anything', {} , 'anything', {:proxy => "http://passedproxy"})
143
154
  end
144
-
155
+
145
156
  it "should use default proxy if default is provided and NO proxy option passed" do
146
157
  Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything, "defaultproxy", 80, nil, nil).and_return(@http_mock)
147
158
  Horse.make_request('anything', {} , 'anything', {})
148
159
  end
149
-
160
+
150
161
  it "should NOT use a proxy if default is NOT provided and NO proxy option passed" do
151
162
  Horse.proxy = nil
152
163
  Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
153
164
  Horse.make_request('anything', {} , 'anything', {})
154
165
  end
155
166
  end
156
-
167
+
157
168
  describe "timeout options" do
158
169
  before :each do
159
170
  Horse.timeout = 20 # seconds
@@ -167,13 +178,13 @@ describe "NetHTTPService module holder class Horse" do
167
178
  @http_mock.should_receive('read_timeout=').with(10)
168
179
  Horse.make_request('anything', {} , 'anything', {:timeout => 10})
169
180
  end
170
-
181
+
171
182
  it "should use default timout if default is provided and NO timeout option passed" do
172
183
  @http_mock.should_receive('open_timeout=').with(20)
173
184
  @http_mock.should_receive('read_timeout=').with(20)
174
185
  Horse.make_request('anything', {} , 'anything', {})
175
186
  end
176
-
187
+
177
188
  it "should NOT use a timeout if default is NOT provided and NO timeout option passed" do
178
189
  Horse.timeout = nil # seconds
179
190
  @http_mock.should_not_receive('open_timeout=')
@@ -181,115 +192,176 @@ describe "NetHTTPService module holder class Horse" do
181
192
  Horse.make_request('anything', {} , 'anything', {})
182
193
  end
183
194
  end
184
-
195
+
185
196
  describe "ca_file options" do
186
197
  after :each do
187
198
  Horse.always_use_ssl = nil
188
199
  Horse.ca_file = nil
189
200
  end
190
-
201
+
191
202
  it "should not use a ca_file if the request is not via SSL" do
192
- Horse.always_use_ssl = false
203
+ Horse.always_use_ssl = false
193
204
  @http_mock.should_not_receive(:ca_file=)
194
205
  Horse.make_request('anything', {} , 'anything', {:ca_file => '/no/file'})
195
206
  end
196
-
207
+
197
208
  describe "when via SSL" do
198
209
  before :each do
199
210
  Horse.always_use_ssl = true
200
-
201
211
  @global_ca_file_path = '/global/ca/file/path'
202
- File.stub(:exists?).and_return(true)
203
212
  end
204
213
 
205
- it "should not use a default ca_file if the default ca_file does not exist" do
206
- Horse.ca_file = @global_ca_file_path
207
-
208
- File.should_receive(:exists?).with(@global_ca_file_path).and_return(false)
209
- Horse.should_not_receive(:ca_file=).with(@global_ca_file_path)
210
-
211
- Horse.make_request('anything', {} , 'anything', {})
212
- end
213
-
214
- it "should use passed ca_file options if provided" do
215
- given_ca_file = '/ca/file'
216
-
217
- Horse.ca_file = @global_ca_file_path
218
- @http_mock.should_not_receive(:ca_file=).with(@global_ca_file_path)
219
- @http_mock.should_receive(:ca_file=).with(given_ca_file)
220
-
221
- Horse.make_request('anything', {} , 'anything', {:ca_file => given_ca_file})
222
- end
223
-
224
- it "should use default ca_file if default is provided and NO ca_file option is passed" do
225
- Horse.ca_file = @global_ca_file_path
226
- @http_mock.should_receive(:ca_file=).with(@global_ca_file_path)
227
-
228
- Horse.make_request('anything', {} , 'anything', {})
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
229
226
  end
230
-
231
- it "should NOT use a ca_file if default is NOT provided and NO ca_file option is passed" do
232
- @http_mock.should_not_receive(:ca_file=)
233
-
234
- Horse.make_request('anything', {} , 'anything', {})
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
235
255
  end
236
256
  end
237
257
  end
238
-
258
+
239
259
  describe "ca_path options" do
240
260
  after :each do
241
261
  Horse.always_use_ssl = nil
242
262
  Horse.ca_path = nil
243
263
  end
244
-
264
+
245
265
  it "should not use a ca_path if the request is not via SSL" do
246
- Horse.always_use_ssl = false
266
+ Horse.always_use_ssl = false
247
267
  @http_mock.should_not_receive('ca_path=')
248
268
  Horse.make_request('anything', {} , 'anything', {:ca_file => '/no/file'})
249
269
  end
250
-
270
+
251
271
  describe "when via SSL" do
252
272
  before :each do
253
273
  Horse.always_use_ssl = true
254
-
255
274
  @global_ca_path = '/global/ca/path'
256
- Dir.stub(:exists?).and_return(true)
257
275
  end
258
276
 
259
- it "should not use a default ca_path if the default ca_path does not exist" do
260
- Horse.ca_path = @global_ca_path
261
-
262
- Dir.should_receive(:exists?).with(@global_ca_path).and_return(false)
263
- Horse.should_not_receive(:ca_path=).with(@global_ca_path)
264
-
265
- Horse.make_request('anything', {} , 'anything', {})
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
266
318
  end
267
-
268
- it "should use passed ca_path options if provided" do
269
- given_ca_path = '/ca/path'
270
-
271
- Horse.ca_path = @global_ca_path
272
- @http_mock.should_not_receive(:ca_ath=).with(@global_ca_path)
273
- @http_mock.should_receive(:ca_path=).with(given_ca_path)
274
-
275
- Horse.make_request('anything', {} , 'anything', {:ca_path => given_ca_path})
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
276
337
  end
277
-
278
- it "should use default ca_path if default is provided and NO ca_path option is passed" do
279
- Horse.ca_path = @global_ca_path
280
- @http_mock.should_receive(:ca_path=).with(@global_ca_path)
281
-
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)
282
348
  Horse.make_request('anything', {} , 'anything', {})
283
349
  end
284
-
285
- it "should NOT use a ca_path if default is NOT provided and NO ca_path option is passed" do
286
- @http_mock.should_not_receive(:ca_path=)
287
-
288
- Horse.make_request('anything', {} , 'anything', {})
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', {})
289
361
  end
290
362
  end
291
- end
292
-
363
+ end
364
+
293
365
  it "should use the graph server by default" do
294
366
  Net::HTTP.should_receive(:new).with(Koala::Facebook::GRAPH_SERVER, anything).and_return(@http_mock)
295
367
  Horse.make_request('anything', {}, 'anything')
@@ -300,17 +372,11 @@ describe "NetHTTPService module holder class Horse" do
300
372
  Horse.make_request('anything', {}, 'anything', :rest_api => true)
301
373
  end
302
374
 
303
- it "no longer sets verify_mode to no verification" do
304
- @http_mock.should_not_receive('verify_mode=')
305
-
306
- Horse.make_request('anything', {}, 'anything')
307
- end
308
-
309
375
  it "should start an HTTP connection" do
310
376
  @http_mock.should_receive(:start).and_yield(@http_yield_mock)
311
377
  Horse.make_request('anything', {}, 'anything')
312
378
  end
313
-
379
+
314
380
  it 'creates a HTTP Proxy object when options contain a proxy' do
315
381
  Net::HTTP.should_receive(:new).with(anything, anything, 'proxy', 1234, 'user', 'pass').and_return(@http_mock)
316
382
  Horse.make_request('anything', {}, 'anything', {:proxy => 'http://user:pass@proxy:1234'})
@@ -324,14 +390,14 @@ describe "NetHTTPService module holder class Horse" do
324
390
 
325
391
  describe "via POST" do
326
392
  it "should use Net::HTTP to make a POST request" do
327
- @http_yield_mock.should_receive(:post).and_return(@http_request_result)
393
+ @http_yield_mock.should_receive(:post).and_return(@mock_http_response)
328
394
 
329
395
  Horse.make_request('anything', {}, 'post')
330
396
  end
331
397
 
332
398
  it "should go to the specified path adding a / if it doesn't exist" do
333
399
  path = mock('Path')
334
- @http_yield_mock.should_receive(:post).with(path, anything).and_return(@http_request_result)
400
+ @http_yield_mock.should_receive(:post).with(path, anything).and_return(@mock_http_response)
335
401
 
336
402
  Horse.make_request(path, {}, 'post')
337
403
  end
@@ -341,7 +407,7 @@ describe "NetHTTPService module holder class Horse" do
341
407
  params = mock('Encoded parameters')
342
408
  Horse.should_receive(:encode_params).with(args).and_return(params)
343
409
 
344
- @http_yield_mock.should_receive(:post).with(anything, params).and_return(@http_request_result)
410
+ @http_yield_mock.should_receive(:post).with(anything, params).and_return(@mock_http_response)
345
411
 
346
412
  Horse.make_request('anything', args, 'post')
347
413
  end
@@ -356,11 +422,11 @@ describe "NetHTTPService module holder class Horse" do
356
422
 
357
423
  @file_stub = stub('fake File', "kind_of?" => true, "path" => 'anypath.jpg')
358
424
 
359
- @http_yield_mock.stub(:request).with(@multipart_request_stub).and_return(@http_request_result)
425
+ @http_yield_mock.stub(:request).with(@multipart_request_stub).and_return(@mock_http_response)
360
426
  end
361
427
 
362
428
  it "should use multipart/form-data if any parameter is a valid file hash" do
363
- @http_yield_mock.should_receive(:request).with(@multipart_request_stub).and_return(@http_request_result)
429
+ @http_yield_mock.should_receive(:request).with(@multipart_request_stub).and_return(@mock_http_response)
364
430
 
365
431
  Horse.make_request('anything', {}, 'post')
366
432
  end
@@ -388,7 +454,7 @@ describe "NetHTTPService module holder class Horse" do
388
454
 
389
455
  describe "via GET" do
390
456
  it "should use Net::HTTP to make a GET request" do
391
- @http_yield_mock.should_receive(:get).and_return(@http_request_result)
457
+ @http_yield_mock.should_receive(:get).and_return(@mock_http_response)
392
458
 
393
459
  Horse.make_request('anything', {}, 'get')
394
460
  end
@@ -399,7 +465,7 @@ describe "NetHTTPService module holder class Horse" do
399
465
  args = {}
400
466
 
401
467
  Horse.should_receive(:encode_params).with(args).and_return(params)
402
- @http_yield_mock.should_receive(:get).with("#{path}?#{params}").and_return(@http_request_result)
468
+ @http_yield_mock.should_receive(:get).with("#{path}?#{params}").and_return(@mock_http_response)
403
469
 
404
470
  Horse.make_request(path, args, 'get')
405
471
  end
@@ -442,11 +508,11 @@ describe "NetHTTPService module holder class Horse" do
442
508
 
443
509
  Horse.params_require_multipart?(args).should be_true
444
510
  end
445
-
511
+
446
512
  describe "when encoding multipart/form-data params" do
447
513
  it "should replace Koala::UploadableIO values with UploadIO values" do
448
514
  upload_io = UploadIO.new(__FILE__, "fake type")
449
-
515
+
450
516
  uploadable_io = stub('Koala::UploadableIO')
451
517
  uploadable_io.should_receive(:kind_of?).with(Koala::UploadableIO).and_return(true)
452
518
  uploadable_io.should_receive(:to_upload_io).and_return(upload_io)
@@ -454,13 +520,13 @@ describe "NetHTTPService module holder class Horse" do
454
520
  "not_a_file" => "not a file",
455
521
  "file" => uploadable_io
456
522
  }
457
-
523
+
458
524
  result = Horse.encode_multipart_params(args)
459
525
 
460
526
  result["not_a_file"] == args["not_a_file"]
461
527
  result["file"] == upload_io
462
528
  end
463
529
  end
464
-
530
+
465
531
  end
466
532
  end