typhoeus 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG.markdown +14 -1
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +2 -0
  5. data/LICENSE +20 -0
  6. data/README.textile +39 -5
  7. data/Rakefile +8 -5
  8. data/VERSION +1 -1
  9. data/examples/file.rb +12 -0
  10. data/examples/times.rb +40 -0
  11. data/ext/typhoeus/.gitignore +2 -1
  12. data/ext/typhoeus/native.c +1 -0
  13. data/ext/typhoeus/native.h +1 -0
  14. data/ext/typhoeus/typhoeus_easy.c +32 -7
  15. data/ext/typhoeus/typhoeus_easy.h +1 -0
  16. data/ext/typhoeus/typhoeus_form.c +59 -0
  17. data/ext/typhoeus/typhoeus_form.h +13 -0
  18. data/ext/typhoeus/typhoeus_multi.c +15 -29
  19. data/lib/typhoeus.rb +1 -0
  20. data/lib/typhoeus/easy.rb +70 -48
  21. data/lib/typhoeus/form.rb +47 -0
  22. data/lib/typhoeus/hydra.rb +40 -7
  23. data/lib/typhoeus/hydra/connect_options.rb +19 -3
  24. data/lib/typhoeus/multi.rb +7 -5
  25. data/lib/typhoeus/remote.rb +1 -1
  26. data/lib/typhoeus/remote_proxy_object.rb +2 -0
  27. data/lib/typhoeus/request.rb +15 -3
  28. data/lib/typhoeus/response.rb +16 -1
  29. data/spec/fixtures/placeholder.gif +0 -0
  30. data/spec/fixtures/placeholder.txt +1 -0
  31. data/spec/fixtures/placeholder.ukn +0 -0
  32. data/spec/servers/app.rb +9 -0
  33. data/spec/spec.opts +1 -0
  34. data/spec/spec_helper.rb +3 -0
  35. data/spec/typhoeus/easy_spec.rb +55 -6
  36. data/spec/typhoeus/filter_spec.rb +2 -2
  37. data/spec/typhoeus/form_spec.rb +106 -0
  38. data/spec/typhoeus/hydra_mock_spec.rb +1 -1
  39. data/spec/typhoeus/hydra_spec.rb +108 -38
  40. data/spec/typhoeus/multi_spec.rb +1 -1
  41. data/spec/typhoeus/normalized_header_hash_spec.rb +1 -1
  42. data/spec/typhoeus/remote_method_spec.rb +2 -2
  43. data/spec/typhoeus/remote_proxy_object_spec.rb +1 -1
  44. data/spec/typhoeus/remote_spec.rb +1 -1
  45. data/spec/typhoeus/request_spec.rb +31 -2
  46. data/spec/typhoeus/response_spec.rb +13 -1
  47. data/spec/typhoeus/utils_spec.rb +1 -1
  48. data/typhoeus.gemspec +23 -6
  49. metadata +39 -19
@@ -12,9 +12,10 @@ module Typhoeus
12
12
  #
13
13
  # @raises NetConnectNotAllowedError
14
14
  def check_allow_net_connect!(request)
15
- if !Typhoeus::Hydra.allow_net_connect? and (!Typhoeus::Hydra.ignore_localhost? or !request.localhost?)
16
- raise NetConnectNotAllowedError, "Real HTTP requests are not allowed. Unregistered request: #{request.inspect}"
17
- end
15
+ return if Typhoeus::Hydra.allow_net_connect?
16
+ return if Typhoeus::Hydra.ignore_hosts.include?(request.host_domain)
17
+
18
+ raise NetConnectNotAllowedError, "Real HTTP requests are not allowed. Unregistered request: #{request.inspect}"
18
19
  end
19
20
  private :check_allow_net_connect!
20
21
 
@@ -39,7 +40,22 @@ module Typhoeus
39
40
  def ignore_localhost?
40
41
  ignore_localhost
41
42
  end
43
+
44
+ def ignore_hosts
45
+ @ignore_hosts ||= []
46
+
47
+ if ignore_localhost?
48
+ @ignore_hosts + Typhoeus::Request::LOCALHOST_ALIASES
49
+ else
50
+ @ignore_hosts
51
+ end
52
+ end
53
+
54
+ def ignore_hosts=(hosts)
55
+ @ignore_hosts = hosts
56
+ end
42
57
  end
43
58
  end
44
59
  end
45
60
  end
61
+
@@ -3,16 +3,16 @@ module Typhoeus
3
3
  attr_reader :easy_handles
4
4
 
5
5
  def initialize
6
- reset_easy_handles
6
+ @easy_handles = []
7
7
  end
8
8
 
9
9
  def remove(easy)
10
- multi_remove_handle(easy)
10
+ multi_remove_handle(easy) if @easy_handles.include?(easy)
11
11
  end
12
12
 
13
13
  def add(easy)
14
+ raise "trying to add easy handle twice" if @easy_handles.include?(easy)
14
15
  easy.set_headers() if easy.headers.empty?
15
- @easy_handles << easy
16
16
  multi_add_handle(easy)
17
17
  end
18
18
 
@@ -27,9 +27,11 @@ module Typhoeus
27
27
  multi_cleanup
28
28
  end
29
29
 
30
- private
31
30
  def reset_easy_handles
32
- @easy_handles = []
31
+ @easy_handles.dup.each do |easy|
32
+ multi_remove_handle(easy)
33
+ yield easy if block_given?
34
+ end
33
35
  end
34
36
  end
35
37
  end
@@ -1,5 +1,5 @@
1
1
  module Typhoeus
2
- USER_AGENT = "Typhoeus - http://github.com/pauldix/typhoeus/tree/master"
2
+ USER_AGENT = "Typhoeus - http://github.com/dbalatero/typhoeus/tree/master"
3
3
 
4
4
  def self.included(base)
5
5
  base.extend ClassMethods
@@ -22,6 +22,8 @@ module Typhoeus
22
22
  unless @proxied_object
23
23
  Typhoeus.perform_easy_requests
24
24
  response = Response.new(:code => @easy.response_code,
25
+ :curl_return_code => @easy.curl_return_code,
26
+ :curl_error_message => @easy.curl_error_message,
25
27
  :headers => @easy.response_header,
26
28
  :body => @easy.response_body,
27
29
  :time => @easy.total_time_taken,
@@ -6,10 +6,12 @@ module Typhoeus
6
6
  attr_writer :headers
7
7
  attr_accessor :method, :params, :body, :connect_timeout, :timeout,
8
8
  :user_agent, :response, :cache_timeout, :follow_location,
9
- :max_redirects, :proxy, :disable_ssl_peer_verification,
9
+ :max_redirects, :proxy, :proxy_username,:proxy_password,
10
+ :disable_ssl_peer_verification,
10
11
  :ssl_cert, :ssl_cert_type, :ssl_key, :ssl_key_type,
11
12
  :ssl_key_password, :ssl_cacert, :ssl_capath, :verbose,
12
- :username, :password, :auth_method, :user_agent
13
+ :username, :password, :auth_method, :user_agent,
14
+ :proxy_auth_method, :proxy_type
13
15
 
14
16
  # Initialize a new Request
15
17
  #
@@ -52,6 +54,10 @@ module Typhoeus
52
54
  @follow_location = options[:follow_location]
53
55
  @max_redirects = options[:max_redirects]
54
56
  @proxy = options[:proxy]
57
+ @proxy_type = options[:proxy_type]
58
+ @proxy_username = options[:proxy_username]
59
+ @proxy_password = options[:proxy_password]
60
+ @proxy_auth_method = options[:proxy_auth_method]
55
61
  @disable_ssl_peer_verification = options[:disable_ssl_peer_verification]
56
62
  @ssl_cert = options[:ssl_cert]
57
63
  @ssl_cert_type = options[:ssl_cert_type]
@@ -78,8 +84,10 @@ module Typhoeus
78
84
  @handled_response = nil
79
85
  end
80
86
 
87
+ LOCALHOST_ALIASES = %w[ localhost 127.0.0.1 0.0.0.0 ]
88
+
81
89
  def localhost?
82
- %(localhost 127.0.0.1 0.0.0.0).include?(@parsed_uri.host)
90
+ LOCALHOST_ALIASES.include?(@parsed_uri.host)
83
91
  end
84
92
 
85
93
  def host
@@ -92,6 +100,10 @@ module Typhoeus
92
100
  end
93
101
  end
94
102
 
103
+ def host_domain
104
+ @parsed_uri.host
105
+ end
106
+
95
107
  def headers
96
108
  @headers["User-Agent"] = @user_agent
97
109
  @headers
@@ -4,11 +4,17 @@ module Typhoeus
4
4
  attr_reader :code, :headers, :body, :time,
5
5
  :requested_url, :requested_remote_method,
6
6
  :requested_http_method, :start_time,
7
- :effective_url
7
+ :effective_url, :start_transfer_time,
8
+ :app_connect_time, :pretransfer_time,
9
+ :connect_time, :name_lookup_time,
10
+ :curl_return_code, :curl_error_message
11
+
8
12
  attr_writer :headers_hash
9
13
 
10
14
  def initialize(params = {})
11
15
  @code = params[:code]
16
+ @curl_return_code = params[:curl_return_code]
17
+ @curl_error_message = params[:curl_error_message]
12
18
  @status_message = params[:status_message]
13
19
  @http_version = params[:http_version]
14
20
  @headers = params[:headers] || ''
@@ -17,6 +23,11 @@ module Typhoeus
17
23
  @requested_url = params[:requested_url]
18
24
  @requested_http_method = params[:requested_http_method]
19
25
  @start_time = params[:start_time]
26
+ @start_transfer_time = params[:start_transfer_time]
27
+ @app_connect_time = params[:app_connect_time]
28
+ @pretransfer_time = params[:pretransfer_time]
29
+ @connect_time = params[:connect_time]
30
+ @name_lookup_time = params[:name_lookup_time]
20
31
  @request = params[:request]
21
32
  @effective_url = params[:effective_url]
22
33
  @mock = params[:mock] || false # default
@@ -67,6 +78,10 @@ module Typhoeus
67
78
  @code != 304
68
79
  end
69
80
 
81
+ def timed_out?
82
+ curl_return_code == 28
83
+ end
84
+
70
85
  private
71
86
 
72
87
  def first_header_line
@@ -0,0 +1 @@
1
+ This file is used to test uploading.
File without changes
@@ -5,6 +5,15 @@ require 'json'
5
5
  require 'zlib'
6
6
 
7
7
  @@fail_count = 0
8
+
9
+ post '/file' do
10
+ {
11
+ 'content-type' => params[:file][:type],
12
+ 'filename' => params[:file][:filename],
13
+ 'content' => params[:file][:tempfile].read
14
+ }.to_json
15
+ end
16
+
8
17
  get '/fail/:number' do
9
18
  if @@fail_count >= params[:number].to_i
10
19
  "ok"
@@ -1,2 +1,3 @@
1
1
  --diff
2
2
  --color
3
+ --backtrace
@@ -9,3 +9,6 @@ path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
9
9
  $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
10
10
 
11
11
  require path + '/typhoeus'
12
+
13
+ Spec::Runner.configure do |config|
14
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe Typhoeus::Easy do
4
4
  describe "#supports_zlib" do
@@ -17,6 +17,42 @@ describe Typhoeus::Easy do
17
17
  end
18
18
  end
19
19
 
20
+ describe "curl errors" do
21
+ it "should provide the CURLE_OPERATION_TIMEDOUT return code when a request times out" do
22
+ e = Typhoeus::Easy.new
23
+ e.url = "http://localhost:3001/?delay=1"
24
+ e.method = :get
25
+ e.timeout = 100
26
+ e.perform
27
+ e.curl_return_code.should == 28
28
+ e.curl_error_message.should == "Timeout was reached"
29
+ e.response_code.should == 0
30
+ end
31
+
32
+ it "should provide the CURLE_COULDNT_CONNECT return code when trying to connect to a non existent port" do
33
+ e = Typhoeus::Easy.new
34
+ e.url = "http://localhost:3999"
35
+ e.method = :get
36
+ e.connect_timeout = 100
37
+ e.perform
38
+ e.curl_return_code.should == 7
39
+ e.curl_error_message.should == "Couldn't connect to server"
40
+ e.response_code.should == 0
41
+ end
42
+
43
+ it "should not return an error message on a successful easy operation" do
44
+ easy = Typhoeus::Easy.new
45
+ easy.url = "http://localhost:3002"
46
+ easy.method = :get
47
+ easy.curl_error_message.should == nil
48
+ easy.perform
49
+ easy.response_code.should == 200
50
+ easy.curl_return_code.should == 0
51
+ easy.curl_error_message.should == "No error"
52
+ end
53
+
54
+ end
55
+
20
56
  describe "options" do
21
57
  it "should not follow redirects if not instructed to" do
22
58
  e = Typhoeus::Easy.new
@@ -48,12 +84,11 @@ describe Typhoeus::Easy do
48
84
 
49
85
  it "should provide a timeout in milliseconds" do
50
86
  e = Typhoeus::Easy.new
51
- e.url = "http://localhost:3001"
87
+ e.url = "http://localhost:3001/?delay=1"
52
88
  e.method = :get
53
- e.timeout = 50
89
+ e.timeout = 10
54
90
  e.perform
55
- # this doesn't work on a mac for some reason
56
- # e.timed_out?.should == true
91
+ e.timed_out?.should == true
57
92
  end
58
93
 
59
94
  it "should allow the setting of the max redirects to follow" do
@@ -209,7 +244,21 @@ describe Typhoeus::Easy do
209
244
  easy.params = {:foo => "bar"}
210
245
  easy.perform
211
246
  easy.response_code.should == 200
212
- easy.response_body.should include("foo=bar")
247
+ easy.response_body.should include("name=\\\"foo\\\"\\r\\n\\r\\nbar")
248
+ end
249
+
250
+ it "should handle a file upload" do
251
+ easy = Typhoeus::Easy.new
252
+ easy.url = "http://localhost:3002/file"
253
+ easy.method = :post
254
+ easy.params = {:file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.txt"), "r")}
255
+ easy.perform
256
+ easy.response_code.should == 200
257
+ JSON.parse(easy.response_body).should == {
258
+ 'content-type' => 'text/plain',
259
+ 'filename' => 'placeholder.txt',
260
+ 'content' => 'This file is used to test uploading.'
261
+ }
213
262
  end
214
263
  end
215
264
 
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe Typhoeus::Filter do
4
4
  it "should take a method name and optionally take options" do
@@ -32,4 +32,4 @@ describe Typhoeus::Filter do
32
32
  filter.apply_filter?(:foo).should be_false
33
33
  end
34
34
  end
35
- end
35
+ end
@@ -0,0 +1,106 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Typhoeus::Form do
4
+ describe "#process!" do
5
+ it "should generate a valid form object" do
6
+ form = Typhoeus::Form.new({
7
+ :name => "John Smith",
8
+ :age => "29"
9
+ })
10
+ form.should_receive(:formadd_param).with("name", "John Smith")
11
+ form.should_receive(:formadd_param).with("age", "29")
12
+ form.process!
13
+ end
14
+
15
+ it "should handle params that are a hash" do
16
+ form = Typhoeus::Form.new({
17
+ :attributes => {
18
+ :eyes => "brown",
19
+ :hair => "green",
20
+ :teeth => "white"
21
+ },
22
+ :name => "John Smith",
23
+ :age => "29"
24
+ })
25
+ form.should_receive(:formadd_param).with("attributes[eyes]", "brown")
26
+ form.should_receive(:formadd_param).with("attributes[hair]", "green")
27
+ form.should_receive(:formadd_param).with("attributes[teeth]", "white")
28
+ form.should_receive(:formadd_param).with("name", "John Smith")
29
+ form.should_receive(:formadd_param).with("age", "29")
30
+ form.process!
31
+ end
32
+
33
+ it "should params that have mutliple values" do
34
+ form = Typhoeus::Form.new({
35
+ :colors => ["brown", "green", "white"],
36
+ :name => "John Smith",
37
+ :age => "29"
38
+ })
39
+ form.should_receive(:formadd_param).with("colors", "brown")
40
+ form.should_receive(:formadd_param).with("colors", "green")
41
+ form.should_receive(:formadd_param).with("colors", "white")
42
+ form.should_receive(:formadd_param).with("name", "John Smith")
43
+ form.should_receive(:formadd_param).with("age", "29")
44
+ form.process!
45
+ end
46
+
47
+ context "when a File object is a param" do
48
+ it "should handle one file" do
49
+ form = Typhoeus::Form.new(
50
+ :file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.txt"), "r")
51
+ )
52
+ form.should_receive(:formadd_file).with("file", "placeholder.txt", "text/plain", anything)
53
+ form.process!
54
+ end
55
+ it "should handle more than one file" do
56
+ form = Typhoeus::Form.new(
57
+ :text_file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.txt"), "r"),
58
+ :gif_file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.gif"), "r")
59
+ )
60
+ form.should_receive(:formadd_file).with("text_file", "placeholder.txt", "text/plain", anything)
61
+ form.should_receive(:formadd_file).with("gif_file", "placeholder.gif", "image/gif", anything)
62
+ form.process!
63
+ end
64
+ it "should default to 'application/octet-stream' if no content type can be determined" do
65
+ pending
66
+ form = Typhoeus::Form.new(
67
+ :file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.txt"), "r")
68
+ )
69
+ form.should_receive(:formadd_file).with("file", "placeholder.ukn", "application/octet-stream", anything)
70
+ form.process!
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "#to_s" do
76
+ it "should generate a valid query string" do
77
+ form = Typhoeus::Form.new({
78
+ :name => "John Smith",
79
+ :age => "29"
80
+ })
81
+ form.to_s.should == "age=29&name=John+Smith"
82
+ end
83
+
84
+ it "should handle params that are a hash" do
85
+ form = Typhoeus::Form.new({
86
+ :attributes => {
87
+ :eyes => "brown",
88
+ :hair => "green",
89
+ :teeth => "white"
90
+ },
91
+ :name => "John Smith",
92
+ :age => "29"
93
+ })
94
+ form.to_s.should == "age=29&attributes%5Beyes%5D=brown&attributes%5Bhair%5D=green&attributes%5Bteeth%5D=white&name=John+Smith"
95
+ end
96
+
97
+ it "should params that have mutliple values" do
98
+ form = Typhoeus::Form.new({
99
+ :colors => ["brown", "green", "white"],
100
+ :name => "John Smith",
101
+ :age => "29"
102
+ })
103
+ form.to_s.should == "age=29&colors=brown&colors=green&colors=white&name=John+Smith"
104
+ end
105
+ end
106
+ end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + "/../spec_helper"
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
2
 
3
3
  describe Typhoeus::HydraMock do
4
4
  it "should mark all responses as mocks" do
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  # some of these tests assume that you have some local services running.
4
4
  # ruby spec/servers/app.rb -p 3000
@@ -46,6 +46,47 @@ describe Typhoeus::Hydra do
46
46
  second.response.body.should include("second")
47
47
  end
48
48
 
49
+ it "should store the curl return codes on the reponses" do
50
+ hydra = Typhoeus::Hydra.new
51
+ first = Typhoeus::Request.new("http://localhost:3001/?delay=1", :timeout => 100)
52
+ second = Typhoeus::Request.new("http://localhost:3999", :connect_timout => 100)
53
+ hydra.queue first
54
+ hydra.queue second
55
+ hydra.run
56
+ first.response.curl_return_code == 28
57
+ second.response.curl_return_code == 7
58
+ end
59
+
60
+ it "aborts a batch of requests" do
61
+ urls = [
62
+ 'http://localhost:3000',
63
+ 'http://localhost:3001',
64
+ 'http://localhost:3002'
65
+ ]
66
+
67
+ # this will make testing easier
68
+ hydra = Typhoeus::Hydra.new( :max_concurrency => 1 )
69
+ completed = 0
70
+
71
+ 10.times {
72
+ |i|
73
+
74
+ req = Typhoeus::Request.new( urls[ i % urls.size], :params => { :cnt => i } )
75
+ req.on_complete {
76
+ |res|
77
+ completed += 1
78
+ hydra.abort if completed == 5
79
+ }
80
+
81
+ hydra.queue( req )
82
+ }
83
+
84
+ hydra.run
85
+
86
+ # technically this should be '== 6' but I don't trust it...
87
+ completed.should < 10
88
+ end
89
+
49
90
  it "has a cache_setter proc" do
50
91
  hydra = Typhoeus::Hydra.new
51
92
  hydra.cache_setter do |request|
@@ -390,37 +431,77 @@ describe Typhoeus::Hydra::ConnectOptions do
390
431
  @klass = Typhoeus::Hydra
391
432
  end
392
433
 
434
+ let!(:old_net_connect) { @klass.allow_net_connect }
435
+ let!(:old_ignore_localhost) { @klass.ignore_localhost }
436
+ let(:hydra) { @klass.new }
437
+
438
+ after(:each) do
439
+ @klass.allow_net_connect = old_net_connect
440
+ @klass.ignore_localhost = old_ignore_localhost
441
+ end
442
+
443
+ def request_for(host)
444
+ Typhoeus::Request.new("http://#{host}:3000")
445
+ end
446
+
393
447
  describe "#ignore_localhost" do
394
- it "should allow localhost requests over the wire if true" do
395
- begin
396
- old_net_connect = @klass.allow_net_connect
397
- old_ignore_localhost = @klass.ignore_localhost
448
+ context "when set to true" do
449
+ before(:each) { @klass.ignore_localhost = true }
450
+
451
+ [true, false].each do |val|
452
+ it "allows localhost requests when allow_net_connect is #{val}" do
453
+ @klass.allow_net_connect = val
454
+ expect { hydra.queue(request_for('localhost')) }.to_not raise_error
455
+ end
456
+ end
457
+ end
458
+
459
+ context "when set to false" do
460
+ before(:each) { @klass.ignore_localhost = false }
461
+
462
+ it "allows localhost requests when allow_net_connect is true" do
463
+ @klass.allow_net_connect = true
464
+ expect { hydra.queue(request_for('localhost')) }.to_not raise_error
465
+ end
466
+
467
+ it "does not allow localhost requests when allow_net_connect is false" do
398
468
  @klass.allow_net_connect = false
399
- @klass.ignore_localhost = true
469
+ expect { hydra.queue(request_for('localhost')) }.to raise_error(Typhoeus::Hydra::NetConnectNotAllowedError)
470
+ end
471
+ end
472
+ end
400
473
 
401
- req = Typhoeus::Request.new("http://localhost:3000")
402
- hydra = @klass.new
474
+ describe "#ignore_hosts" do
475
+ context 'when allow_net_connect is set to false' do
476
+ before(:each) do
477
+ @klass.ignore_localhost = false
478
+ @klass.allow_net_connect = false
479
+ end
403
480
 
404
- lambda {
405
- hydra.queue(req)
406
- }.should_not raise_error
407
- ensure
408
- @klass.allow_net_connect = old_net_connect
409
- @klass.ignore_localhost = old_ignore_localhost
481
+ Typhoeus::Request::LOCALHOST_ALIASES.each do |disallowed_host|
482
+ ignore_hosts = Typhoeus::Request::LOCALHOST_ALIASES - [disallowed_host]
483
+
484
+ context "when set to #{ignore_hosts.join(' and ')}" do
485
+ before(:each) { @klass.ignore_hosts = ignore_hosts }
486
+
487
+ it "does not allow a request to #{disallowed_host}" do
488
+ expect { hydra.queue(request_for(disallowed_host)) }.to raise_error(Typhoeus::Hydra::NetConnectNotAllowedError)
489
+ end
490
+
491
+ ignore_hosts.each do |host|
492
+ it "allows a request to #{host}" do
493
+ expect { hydra.queue(request_for(host)) }.to_not raise_error
494
+ end
495
+ end
496
+ end
410
497
  end
411
498
  end
412
499
  end
413
500
 
414
501
  describe "#allow_net_connect" do
415
502
  it "should be settable" do
416
- begin
417
- # make sure to not mess up other specs.
418
- old = @klass.allow_net_connect
419
- @klass.allow_net_connect = true
420
- @klass.allow_net_connect.should be_true
421
- ensure
422
- @klass.allow_net_connect = old
423
- end
503
+ @klass.allow_net_connect = true
504
+ @klass.allow_net_connect.should be_true
424
505
  end
425
506
 
426
507
  it "should default to true" do
@@ -428,23 +509,12 @@ describe Typhoeus::Hydra::ConnectOptions do
428
509
  end
429
510
 
430
511
  it "should raise an error if we queue a request while its false" do
431
- begin
432
- request = Typhoeus::Request.new("http://localhost:3000")
433
- old_net_connect = @klass.allow_net_connect
434
- old_ignore_localhost = @klass.ignore_localhost
512
+ @klass.allow_net_connect = false
513
+ @klass.ignore_localhost = false
435
514
 
436
- @klass.allow_net_connect = false
437
- @klass.ignore_localhost = false
438
-
439
- hydra = Typhoeus::Hydra.new
440
-
441
- lambda {
442
- hydra.queue(request)
443
- }.should raise_error(Typhoeus::Hydra::NetConnectNotAllowedError)
444
- ensure
445
- @klass.allow_net_connect = old_net_connect
446
- @klass.ignore_localhost = old_ignore_localhost
447
- end
515
+ expect {
516
+ hydra.queue(request_for('example.com'))
517
+ }.to raise_error(Typhoeus::Hydra::NetConnectNotAllowedError)
448
518
  end
449
519
  end
450
520