itrigga-net_helper 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,139 +1,186 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper' )
2
2
 
3
3
  describe "Itrigga::NetHelper" do
4
- before(:each) do
5
- @mock_response = mock("response")
6
- @mock_response.stub!(:body).and_return("body")
4
+ before do
5
+ @default_http_params = {
6
+ :timeout => 5,
7
+ :retries_on_timeout => 5,
8
+ :max_redirects => 3
9
+ }
10
+
7
11
  end
8
12
 
9
- describe "typhoeus_present?" do
10
- context "when Typhoeus is loaded" do
11
- before(:each) do
12
- Itrigga::NetHelper.send(:const_set, "Typhoeus", Proc.new {})
13
- end
14
-
15
- it "should return true" do
16
- Itrigga::NetHelper.typhoeus_present?.should == true
17
- end
13
+ describe "available_http_engines" do
14
+ it "should return the constants defined in the Itrigga::NetHelper module (ie the classes)" do
15
+ Itrigga::NetHelper.should_receive(:constants)
16
+ Itrigga::NetHelper.available_http_engines
18
17
  end
19
18
 
20
- context "when Typhoeus is not loaded" do
21
- before(:each) do
22
- Itrigga::NetHelper.send(:remove_const, "Typhoeus")
19
+ it "should return the correct classes" do
20
+ Itrigga::NetHelper.available_http_engines.sort.should == ["RestClient","Typhoeus"].sort
21
+ end
22
+ end
23
+
24
+
25
+ describe "default_http_engine" do
26
+ it "should be rest client" do
27
+ Itrigga::NetHelper.default_http_engine.should == "RestClient"
28
+ end
29
+ end
30
+
31
+
32
+ describe "do_get" do
33
+ before do
34
+ Itrigga::NetHelper.stub!(:get).and_return("result")
35
+ end
36
+
37
+ context "using defaults" do
38
+ it "should call get changing the params into a hash" do
39
+ Itrigga::NetHelper.should_receive(:get).with(hash_including({:url => "blart"}.merge(@default_http_params)))
40
+ Itrigga::NetHelper.do_get "blart"
23
41
  end
24
-
25
- it "should return false" do
26
- Itrigga::NetHelper.typhoeus_present?.should == false
42
+ end
43
+
44
+ context "overriding defaults" do
45
+ it "should call get changing the params into a hash" do
46
+ Itrigga::NetHelper.should_receive(:get).with(hash_including(:url => "blart",:timeout => 1, :retries_on_timeout => 2, :max_redirects => 42))
47
+ Itrigga::NetHelper.do_get "blart", 1, 2, 42
27
48
  end
28
49
  end
29
50
 
51
+ it "should return the result from get" do
52
+ Itrigga::NetHelper.do_get("blart").should == "result"
53
+ end
30
54
  end
31
-
32
- describe "do_get" do
33
- before(:each) do
34
- Itrigga::NetHelper.stub!(:typhoeus_present?).and_return(false)
55
+
56
+
57
+ describe "get" do
58
+ before do
59
+ Itrigga::NetHelper.stub!(:get_engine).and_return(@engine = mock("HTTPEngine"))
60
+ @engine.stub!(:get).and_return("result")
61
+ Itrigga::NetHelper.stub!(:with_timeout).and_yield
62
+ @default_http_params = @default_http_params.merge({:headers => {}})
63
+ @default_http_params_with_url = {:url => "blart"}.merge(@default_http_params)
35
64
  end
36
65
 
37
-
38
- it "should get the HTTP response" do
39
- Itrigga::NetHelper.stub!(:handle_response).and_return("")
40
- Net::HTTP.should_receive( :get_response ).with( URI.parse("http://some.com") ).and_return(@mock_response)
41
- Itrigga::NetHelper.do_get( "http://some.com" )
66
+ it "should raise error if no :url option given" do
67
+ lambda { Itrigga::NetHelper.get }.should raise_error(ArgumentError,":url is required")
68
+ end
69
+
70
+ it "should call get_engine with merged params" do
71
+ Itrigga::NetHelper.should_receive(:get_engine).with(hash_including(@default_http_params_with_url))
72
+ Itrigga::NetHelper.get :url => "blart"
73
+ end
74
+
75
+ it "should call with_timeout" do
76
+ Itrigga::NetHelper.should_receive(:with_timeout).with(@default_http_params_with_url).and_yield
77
+ Itrigga::NetHelper.get :url => "blart"
78
+ end
79
+
80
+ it "should call get in the http engine in the block" do
81
+ @engine.should_receive(:get).with(@default_http_params_with_url).and_return("result")
82
+ Itrigga::NetHelper.get :url => "blart"
42
83
  end
43
84
 
44
- it "should call handle_response with the response" do
45
- Net::HTTP.stub!( :get_response ).with( URI.parse("http://some.com") ).and_return(@mock_response)
46
- Itrigga::NetHelper.should_receive(:handle_response).and_return("")
47
- Itrigga::NetHelper.do_get( "http://some.com" )
85
+ it "should return the result of the block" do
86
+ Itrigga::NetHelper.get(:url => "blart").should == "result"
48
87
  end
88
+ end
49
89
 
90
+
91
+ describe "with_timeout" do
92
+ before do
93
+ @block = Proc.new{ 6 + 6 }
94
+ end
50
95
 
51
- describe "when the GET takes longer than time_out seconds" do
52
- before(:each) do
53
- Itrigga::NetHelper.stub!(:get_with_timeout).and_raise( TimeoutError.new("expected timeout") )
96
+ context "when SystemTimer is available" do
97
+ before do
98
+ silence_warnings { Object.const_set(:SystemTimer, @system_timer = mock("SystemTimer")) }
99
+ @system_timer.stub!(:timeout_after).and_yield
100
+ end
101
+ after do
102
+ silence_warnings { Object.send(:remove_const, :SystemTimer) }
54
103
  end
55
104
 
56
- describe "when it has retried < retries_on_timeout times" do
57
- it "should retry" do
58
- Itrigga::NetHelper.should_receive(:get_with_timeout).exactly(3).times
59
- # NOTE: the important expectation is the above one!
60
- lambda {
61
- Itrigga::NetHelper.do_get( "http://some.com", 1, 2 )
62
- }.should raise_error( IOError )
63
- end
105
+ it "should call timeout_after" do
106
+ @system_timer.should_receive(:timeout_after).with(42).and_yield
107
+ Itrigga::NetHelper.with_timeout({:timeout => 42}, &@block)
64
108
  end
65
109
 
66
- describe "when it has retried retries_on_timeout times" do
67
- it "should raise an IOError" do
68
- lambda {
69
- Itrigga::NetHelper.do_get( "http://some.com", 0, 2 )
70
- }.should raise_error( IOError )
71
- end
110
+ it "should return the result from the block" do
111
+ Itrigga::NetHelper.with_timeout({:timeout => 42}, &@block).should == 12
72
112
  end
73
113
 
114
+ it "should not call timeout" do
115
+ Itrigga::NetHelper.should_not_receive(:timeout)
116
+ Itrigga::NetHelper.with_timeout({:timeout => 42}, &@block)
117
+ end
74
118
  end
75
- end
76
-
77
- describe "handle_response" do
78
119
 
79
- describe "when the response is OK" do
80
- before(:each) do
81
- @mock_response.stub!(:is_a?).with(String).and_return false
82
- @mock_response.stub!(:is_a?).with(Net::HTTPSuccess).and_return true
120
+ context "when SystemTimer is not available" do
121
+ it "should call timeout" do
122
+ Itrigga::NetHelper.should_receive(:timeout).with(42).and_yield
123
+ Itrigga::NetHelper.with_timeout({:timeout => 42}, &@block)
83
124
  end
84
125
 
85
- it "should return the response body" do
86
- Itrigga::NetHelper.handle_response("http://some.com", @mock_response).should == "body"
126
+ it "should return the result from the block" do
127
+ Itrigga::NetHelper.with_timeout({:timeout => 42}, &@block).should == 12
87
128
  end
88
129
  end
89
130
 
90
- describe "when the response is a redirect" do
91
- before(:each) do
92
- @mock_response.stub!(:is_a?).with(String).and_return false
93
- @mock_response.stub!(:is_a?).with(Net::HTTPSuccess).and_return false
94
- @mock_response.stub!(:is_a?).with(Net::HTTPRedirection).and_return true
95
- @mock_response.stub!(:[]).with('location').and_return("http://some.redirect")
131
+ context "when an error is thrown" do
132
+ it "should retry 3 times then raise an IOError" do
133
+ Itrigga::NetHelper.should_receive(:timeout).exactly(4).times.and_raise(TimeoutError)
134
+ lambda { Itrigga::NetHelper.with_timeout({:timeout => 42, :retries_on_timeout => 3}, &@block) }.should raise_error(IOError)
96
135
  end
97
136
 
98
- describe "when redirects > 0" do
99
- it "should recurse" do
100
- Itrigga::NetHelper.should_receive(:do_get)
101
- Itrigga::NetHelper.handle_response("http://some.com", @mock_response)
102
- end
103
-
104
- it "should pass the location field from the response as the url, and the same timeout" do
105
- Itrigga::NetHelper.should_receive(:do_get).with( "http://some.redirect", 23, anything )
106
- Itrigga::NetHelper.handle_response( "http://some.com", @mock_response, 23)
107
- end
108
-
109
- it "should pass redirects - 1 as the redirects param" do
110
- Itrigga::NetHelper.should_receive(:do_get).with( anything, anything, 15 )
111
- Itrigga::NetHelper.handle_response( "http://some.com", @mock_response, 23, 16)
112
- end
113
-
114
- describe "when the response location is relative" do
115
- before(:each) do
116
- @mock_response.stub!(:[]).with('location').and_return("/some/relative/path?param=567")
117
- end
118
-
119
- it "should pass on the absolute url" do
120
- Itrigga::NetHelper.should_receive(:do_get).with( "http://some.com/some/relative/path?param=567", anything, anything )
121
- Itrigga::NetHelper.handle_response( "http://some.com/some_other/path.xml?id=123", @mock_response, 23, 16)
122
- end
123
- end
137
+ it "should not handle other errors" do
138
+ Itrigga::NetHelper.stub!(:timeout).and_raise "Funkiness Alert!"
139
+ lambda { Itrigga::NetHelper.with_timeout({:timeout => 42, :retries_on_timeout => 3}, &@block) }.should raise_error
140
+ end
141
+ end
142
+ end
143
+
144
+
145
+ describe "get_engine" do
146
+ context "when the ITNH_HTTP_ENGINE is defined" do
147
+ before do
148
+ silence_warnings {
149
+ Object.const_set(:ITNH_HTTP_ENGINE, "Typhoeus")
150
+ Object.const_set(:Typhoeus, "Typhoeus")
151
+ }
152
+ end
153
+ after do
154
+ silence_warnings {
155
+ Object.send(:remove_const, :ITNH_HTTP_ENGINE)
156
+ Object.send(:remove_const, :Typhoeus)
157
+ }
124
158
  end
125
159
 
126
- describe "when redirects = 0" do
127
- it "should raise an IOError" do
128
- lambda {
129
- Itrigga::NetHelper.handle_response( "http://some.com", @mock_response, 23, 0)
130
- }.should raise_error( IOError )
131
- end
160
+ it "should return the Typhoeus class" do
161
+ Itrigga::NetHelper.get_engine.should == Itrigga::NetHelper::Typhoeus
162
+ end
163
+
164
+ it "should respect overrides" do
165
+ Itrigga::NetHelper.get_engine(:http_engine => "RestClient").should == Itrigga::NetHelper::RestClient
166
+ end
167
+ end
168
+
169
+ context "with no Typhoeus defined" do
170
+ it "should not use Typhoeus even when told to" do
171
+ Object.send(:remove_const, :Typhoeus) rescue nil #only remove it if its present
172
+ Itrigga::NetHelper.get_engine(:http_engine => "Typhoeus").should == Itrigga::NetHelper::RestClient # because Typhoeus is not defined (ie not available)
173
+
132
174
  end
133
175
  end
134
176
 
177
+ it "should use the default engine if given an invalid option" do
178
+ Itrigga::NetHelper.get_engine(:http_engine => "blart and flange").should == Itrigga::NetHelper::RestClient
179
+ end
180
+
135
181
  end
136
182
 
183
+
137
184
  describe "query_string" do
138
185
  it "should return a string" do
139
186
  Itrigga::NetHelper.query_string( 'blart' => 'flange' ).should be_a(String)
@@ -172,6 +219,9 @@ describe "Itrigga::NetHelper" do
172
219
  end
173
220
  end
174
221
 
222
+
223
+
224
+
175
225
  describe "discover_favicon_url" do
176
226
  before(:each) do
177
227
  @url = "http://blart.com"
@@ -189,9 +239,6 @@ describe "Itrigga::NetHelper" do
189
239
  Itrigga::NetHelper.discover_favicon_url(@url)
190
240
  end
191
241
 
192
-
193
-
194
-
195
242
  context "each discovered href" do
196
243
  context "when the get is a success" do
197
244
  before(:each) do
@@ -209,6 +256,9 @@ describe "Itrigga::NetHelper" do
209
256
  end
210
257
 
211
258
  end
259
+
260
+
261
+
212
262
  describe "discover_icon_hrefs" do
213
263
  before(:each) do
214
264
  @html = <<-HTML
@@ -252,145 +302,7 @@ describe "Itrigga::NetHelper" do
252
302
 
253
303
  end
254
304
 
255
- describe "raw_get" do
256
- before(:each) do
257
- Itrigga::NetHelper.stub!(:get_with_auth).and_return( 'body' )
258
- Itrigga::NetHelper.stub!(:establish_session_if_needed)
259
- Itrigga::NetHelper.stub!(:typhoeus_present?).and_return(false)
260
- @mock_session = mock("http session")
261
- @mock_session.stub!(:request_get).and_return( @mock_response = mock('request_get response') )
262
- @mock_response.stub!(:body).and_return('response body')
263
- @mock_parsed_url = mock("parsed url")
264
- @mock_parsed_url.stub!(:host).and_return("host")
265
- @mock_parsed_url.stub!(:path).and_return("path")
266
- @mock_parsed_url.stub!(:port).and_return("port")
267
-
268
- @mock_response = mock('response')
269
- @mock_response.stub!(:is_a?).with(Net::HTTPRedirection).and_return(false)
270
- @mock_response.stub!(:body).and_return('cheese')
271
- end
272
-
273
- it "should call establish_session_if_needed with the given opts" do
274
- Itrigga::NetHelper.should_receive(:establish_session_if_needed).with(hash_including(:blart=>'flange'))
275
- Itrigga::NetHelper.raw_get(:blart=>'flange', :http_session=>@mock_session, :parsed_url=>@mock_parsed_url)
276
- end
277
-
278
- context "when given a :username" do
279
- it "should call get_with_auth with the given options" do
280
- Itrigga::NetHelper.should_receive(:get_with_auth).with(hash_including(:username=>'dave')).and_return(@mock_response)
281
- Itrigga::NetHelper.raw_get(:username=>'dave')
282
- end
283
-
284
- it "should return the result of get_with_auth" do
285
- Itrigga::NetHelper.stub!(:get_with_auth).with(hash_including(:username=>'dave')).and_return(@mock_response)
286
- Itrigga::NetHelper.raw_get(:username=>'dave').should == 'cheese'
287
- end
288
-
289
- context "when response is a redirection" do
290
- before(:each) do
291
- @mock_response.stub!(:is_a?).with(Net::HTTPRedirection).and_return(true, false)
292
- @mock_response.stub!(:[]).with('location').and_return("http://cheesefish.com")
293
- URI.stub!(:parse).with("http://cheesefish.com").and_return(@mock_parsed_url = mock('parsed url'))
294
- end
295
-
296
- it "should follow the redirect" do
297
- URI.should_receive(:parse).with("http://cheesefish.com").and_return(@mock_parsed_url = mock('parsed url'))
298
- Itrigga::NetHelper.should_receive(:get_with_auth).twice.and_return(@mock_response)
299
- Itrigga::NetHelper.raw_get(:username=>'dave')
300
- end
301
- end
302
- end
303
-
304
- context "when not given a :username" do
305
- it "should call request_get on the http session with the :parsed_url's path and :headers" do
306
- @mock_session.should_receive(:request_get).with( 'path', {:donkey=>'dipstick'}).and_return(@mock_response)
307
- Itrigga::NetHelper.raw_get( :parsed_url=>@mock_parsed_url, :http_session=>@mock_session, :headers=>{:donkey=>'dipstick'} )
308
- end
309
-
310
- it "should return the result of calling body on the request_get's response" do
311
- Itrigga::NetHelper.raw_get(:parsed_url=>@mock_parsed_url, :http_session=>@mock_session).should == 'response body'
312
- end
313
- end
314
-
315
- end
316
-
317
- describe "get_with_auth" do
318
- before(:each) do
319
- Itrigga::NetHelper.stub!(:establish_session_if_needed)
320
- Itrigga::NetHelper.stub!(:typhoeus_present).and_return(false)
321
- @mock_get = mock('get')
322
- @mock_get.stub!(:basic_auth)
323
- Net::HTTP::Get.stub!(:new).and_return(@mock_get)
324
- @mock_parsed_url = mock("parsed url")
325
- @mock_parsed_url.stub!(:host).and_return("host")
326
- @mock_parsed_url.stub!(:path).and_return("path")
327
- @mock_parsed_url.stub!(:port).and_return("port")
328
- @mock_session = mock("http session")
329
- @mock_session.stub!(:request).and_return("request response")
330
- end
331
-
332
- it "should call establish_session_if_needed with the given opts" do
333
- Itrigga::NetHelper.should_receive(:establish_session_if_needed).with(hash_including(:blart=>'flange'))
334
- Itrigga::NetHelper.get_with_auth(:blart=>'flange', :parsed_url=>@mock_parsed_url, :http_session=>@mock_session)
335
- end
336
-
337
- it "should create a new Net::HTTP::Get with the path of the :parsed_url" do
338
- Net::HTTP::Get.should_receive(:new).with('path').and_return(@mock_get)
339
- Itrigga::NetHelper.get_with_auth(:blart=>'flange', :parsed_url=>@mock_parsed_url, :http_session=>@mock_session)
340
- end
341
-
342
- it "should call basic_auth on the get, passing :username and :password from the given params" do
343
- @mock_get.should_receive(:basic_auth).with( 'username', 'password' )
344
- Itrigga::NetHelper.get_with_auth(:username=>'username', :password=>'password', :parsed_url=>@mock_parsed_url, :http_session=>@mock_session)
345
- end
346
-
347
- it "should call request on the http_session with the get, and any given :headers" do
348
- @mock_session.should_receive(:request).with(@mock_get, {:header1=>'value1'})
349
- Itrigga::NetHelper.get_with_auth(:http_session=>@mock_session, :headers=>{:header1=>'value1'}, :parsed_url=>@mock_parsed_url, :http_session=>@mock_session )
350
- end
351
-
352
- it "should return the result of the request" do
353
- @mock_session.should_receive(:request).with(@mock_get, {:header1=>'value1'}).and_return("cheese")
354
- Itrigga::NetHelper.get_with_auth(:http_session=>@mock_session, :headers=>{:header1=>'value1'}, :parsed_url=>@mock_parsed_url, :http_session=>@mock_session ).should == "cheese"
355
- end
356
- end
357
305
 
358
- describe "establish_session_if_needed" do
359
- before(:each) do
360
- @mock_parsed_url = mock("parsed url")
361
- @mock_parsed_url.stub!(:host).and_return("host")
362
- @mock_parsed_url.stub!(:port).and_return("port")
363
- @mock_parsed_url.stub!(:scheme).and_return("scheme")
364
- @mock_session = mock('http session')
365
- @mock_session.stub!(:use_ssl=)
366
- Net::HTTP.stub!(:new).and_return(@mock_session)
367
- end
368
-
369
- context "When not given a :parsed_url" do
370
- it "should set :parsed_url to URI.parse(:url)" do
371
- URI.should_receive(:parse).with("URL").and_return(@mock_parsed_url)
372
- Itrigga::NetHelper.establish_session_if_needed(:url=>"URL")
373
- end
374
- end
375
-
376
- context "When not given a :http_session" do
377
- it "should set :http_session to a new Net::HTTP with the parsed_url's host and port" do
378
- Net::HTTP.should_receive(:new).with("host", "port").and_return(@mock_session)
379
- Itrigga::NetHelper.establish_session_if_needed(:parsed_url=>@mock_parsed_url)
380
- end
381
- end
382
-
383
- context "when the :parsed_url's scheme is https" do
384
- before(:each) do
385
- @mock_parsed_url.stub!(:scheme).and_return("https")
386
- end
387
-
388
- it "should set use_ssl=true on the http session" do
389
- @mock_session.should_receive(:use_ssl=).with(true)
390
- Itrigga::NetHelper.establish_session_if_needed(:parsed_url=>@mock_parsed_url)
391
- end
392
- end
393
- end
394
306
 
395
307
  describe "shorten_url" do
396
308
  before do
@@ -423,6 +335,9 @@ describe "Itrigga::NetHelper" do
423
335
  end
424
336
  end
425
337
 
338
+
339
+
340
+
426
341
  describe "format_url_shortener_api_url" do
427
342
  before do
428
343
  @raw_url = "http://very_long_url_goes_here.com"
@@ -478,8 +393,24 @@ describe "Itrigga::NetHelper" do
478
393
  opts = {:target => "display_name", :file => "a_file_name", :target_path => "/path/goes/here/to/file.xml", :host=>@host}
479
394
  Itrigga::NetHelper.should_receive(:'`').with("scp -P123 -i key_file_path a_file_name user@hostname:/path/goes/here/to/file.xml")
480
395
  Itrigga::NetHelper.transfer_file_scp(opts)
396
+ end
397
+ end
398
+
399
+
400
+
401
+ describe "ip_of" do
402
+ before do
403
+ IPSocket.stub!(:getaddress).and_return("ip_address")
404
+ end
405
+
406
+ it "should call getaddress" do
407
+ IPSocket.should_receive(:getaddress).with("server").and_return("ip_address")
408
+ Itrigga::NetHelper.ip_of("server")
481
409
  end
482
410
 
411
+ it "should return the address" do
412
+ Itrigga::NetHelper.ip_of("server").should == "ip_address"
413
+ end
483
414
  end
484
415
 
485
416
  end