pauldix-typhoeus 0.0.12 → 0.0.13

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.
@@ -4,8 +4,19 @@ module Typhoeus
4
4
  def self.included(base)
5
5
  base.extend ClassMethods
6
6
  end
7
+
8
+ class MockExpectedError < StandardError; end
7
9
 
8
10
  module ClassMethods
11
+ def allow_net_connect
12
+ @allow_net_connect = true if @allow_net_connect.nil?
13
+ @allow_net_connect
14
+ end
15
+
16
+ def allow_net_connect=(value)
17
+ @allow_net_connect = value
18
+ end
19
+
9
20
  def mock(method, args = {})
10
21
  @remote_mocks ||= {}
11
22
  @remote_mocks[method] ||= {}
@@ -21,10 +32,18 @@ module Typhoeus
21
32
  def get_mock(method, url, options)
22
33
  return nil unless @remote_mocks
23
34
  if @remote_mocks.has_key? method
35
+ extra_response_args = { :requested_http_method => method,
36
+ :requested_url => url }
24
37
  if @remote_mocks[method].has_key? url
25
- get_mock_and_run_handlers(method, @remote_mocks[method][url], options)
38
+ get_mock_and_run_handlers(method,
39
+ @remote_mocks[method][url].merge(
40
+ extra_response_args),
41
+ options)
26
42
  elsif @remote_mocks[method].has_key? :catch_all
27
- get_mock_and_run_handlers(method, @remote_mocks[method][:catch_all], options)
43
+ get_mock_and_run_handlers(method,
44
+ @remote_mocks[method][:catch_all].merge(
45
+ extra_response_args),
46
+ options)
28
47
  else
29
48
  nil
30
49
  end
@@ -32,9 +51,19 @@ module Typhoeus
32
51
  nil
33
52
  end
34
53
  end
54
+
55
+ def enforce_allow_net_connect!(http_verb, url)
56
+ if !allow_net_connect
57
+ raise MockExpectedError,
58
+ "Real HTTP connections are disabled. Unregistered request: " <<
59
+ "#{http_verb.to_s.upcase} #{url}\n" <<
60
+ " Try: mock(:#{http_verb}, :url => \"#{url}\")"
61
+ end
62
+ end
35
63
 
36
64
  def get_mock_and_run_handlers(method, response_args, options)
37
- response = Response.new(response_args[:code], response_args[:headers], response_args[:body], response_args[:time])
65
+ response = Response.new(response_args)
66
+
38
67
  if response_args.has_key? :expected_body
39
68
  raise "#{method} expected body of \"#{response_args[:expected_body]}\" but received #{options[:body]}" if response_args[:expected_body] != options[:body]
40
69
  end
@@ -53,26 +82,42 @@ module Typhoeus
53
82
 
54
83
  def get(url, options = {})
55
84
  mock_object = get_mock(:get, url, options)
56
- return mock_object if mock_object
57
- remote_proxy_object(url, :get, options)
85
+ unless mock_object.nil?
86
+ mock_object
87
+ else
88
+ enforce_allow_net_connect!(:get, url)
89
+ remote_proxy_object(url, :get, options)
90
+ end
58
91
  end
59
92
 
60
93
  def post(url, options = {}, &block)
61
94
  mock_object = get_mock(:post, url, options)
62
- return mock_object if mock_object
63
- remote_proxy_object(url, :post, options)
95
+ unless mock_object.nil?
96
+ mock_object
97
+ else
98
+ enforce_allow_net_connect!(:post, url)
99
+ remote_proxy_object(url, :post, options)
100
+ end
64
101
  end
65
102
 
66
103
  def put(url, options = {}, &block)
67
104
  mock_object = get_mock(:put, url, options)
68
- return mock_object if mock_object
69
- remote_proxy_object(url, :put, options)
105
+ unless mock_object.nil?
106
+ mock_object
107
+ else
108
+ enforce_allow_net_connect!(:put, url)
109
+ remote_proxy_object(url, :put, options)
110
+ end
70
111
  end
71
112
 
72
113
  def delete(url, options = {}, &block)
73
114
  mock_object = get_mock(:delete, url, options)
74
- return mock_object if mock_object
75
- remote_proxy_object(url, :delete, options)
115
+ unless mock_object.nil?
116
+ mock_object
117
+ else
118
+ enforce_allow_net_connect!(:delete, url)
119
+ remote_proxy_object(url, :delete, options)
120
+ end
76
121
  end
77
122
 
78
123
  def remote_proxy_object(url, method, options)
@@ -92,7 +137,15 @@ module Typhoeus
92
137
  end
93
138
 
94
139
  def remote_defaults(options)
95
- @remote_defaults = options
140
+ @remote_defaults ||= {}
141
+ @remote_defaults.merge!(options)
142
+ @remote_defaults
143
+ end
144
+
145
+ # If we get subclassed, make sure that child inherits the remote defaults
146
+ # of the parent class.
147
+ def inherited(child)
148
+ child.__send__(:remote_defaults, @remote_defaults)
96
149
  end
97
150
 
98
151
  def call_remote_method(method_name, args)
@@ -176,4 +229,4 @@ module Typhoeus
176
229
  SRC
177
230
  end
178
231
  end # ClassMethods
179
- end
232
+ end
@@ -21,7 +21,12 @@ module Typhoeus
21
21
 
22
22
  unless @proxied_object
23
23
  Typhoeus.perform_easy_requests
24
- response = Response.new(@easy.response_code, @easy.response_header, @easy.response_body, @easy.total_time_taken)
24
+ response = Response.new(:code => @easy.response_code,
25
+ :headers => @easy.response_header,
26
+ :body => @easy.response_body,
27
+ :time => @easy.total_time_taken,
28
+ :requested_url => @easy.url,
29
+ :requested_http_method => @easy.method)
25
30
  if @easy.response_code >= 200 && @easy.response_code < 300
26
31
  Typhoeus.release_easy_object(@easy)
27
32
  @proxied_object = @success.nil? ? response : @success.call(response)
@@ -39,4 +44,4 @@ module Typhoeus
39
44
  @proxied_object.__send__(sym, *args, &block)
40
45
  end
41
46
  end
42
- end
47
+ end
@@ -1,12 +1,16 @@
1
1
  module Typhoeus
2
2
  class Response
3
- attr_reader :code, :headers, :body, :time
3
+ attr_reader :code, :headers, :body, :time,
4
+ :requested_url, :requested_remote_method,
5
+ :requested_http_method
4
6
 
5
- def initialize(response_code, response_headers, response_body, request_time)
6
- @code = response_code
7
- @headers = response_headers
8
- @body = response_body
9
- @time = request_time
7
+ def initialize(params = {})
8
+ @code = params[:code]
9
+ @headers = params[:headers]
10
+ @body = params[:body]
11
+ @time = params[:time]
12
+ @requested_url = params[:requested_url]
13
+ @requested_http_method = params[:requested_http_method]
10
14
  end
11
15
  end
12
- end
16
+ end
data/lib/typhoeus.rb CHANGED
@@ -12,7 +12,7 @@ require 'typhoeus/remote_proxy_object'
12
12
  require 'typhoeus/response'
13
13
 
14
14
  module Typhoeus
15
- VERSION = "0.0.12"
15
+ VERSION = "0.0.13"
16
16
 
17
17
  def self.easy_object_pool
18
18
  @easy_objects ||= []
@@ -34,6 +34,12 @@ describe Typhoeus::RemoteProxyObject do
34
34
  Typhoeus.should_receive(:perform_easy_requests).exactly(0).times
35
35
  response.code.should == 200
36
36
  end
37
+
38
+ it "should set the requested_url and requested_http_method on the response" do
39
+ response = Typhoeus::RemoteProxyObject.new(lambda {}, @easy)
40
+ response.requested_url.should == "http://localhost:3001"
41
+ response.requested_http_method.should == :get
42
+ end
37
43
 
38
44
  it "should call the on_success method with an easy object and proxy to the result of on_success" do
39
45
  klass = Class.new do
@@ -64,4 +70,4 @@ describe Typhoeus::RemoteProxyObject do
64
70
  k = Typhoeus::RemoteProxyObject.new(lambda {}, @easy, :on_failure => lambda {|e| klass.new(e)})
65
71
  k.blah.should == 0
66
72
  end
67
- end
73
+ end
@@ -14,7 +14,48 @@ describe Typhoeus do
14
14
  after(:all) do
15
15
  stop_method_server(@pid)
16
16
  end
17
-
17
+
18
+ describe "entirely disallowing HTTP connections in specs" do
19
+ describe "allow_net_connect" do
20
+ it "should default to true" do
21
+ @klass.allow_net_connect.should be_true
22
+ end
23
+
24
+ it "should be settable" do
25
+ @klass.allow_net_connect.should be_true
26
+ @klass.allow_net_connect = false
27
+ @klass.allow_net_connect.should be_false
28
+ end
29
+ end
30
+
31
+ describe "and hitting a URL that hasn't been mocked" do
32
+ it "should raise an error for any HTTP verbs" do
33
+ @klass.allow_net_connect = false
34
+
35
+ [:get, :put, :post, :delete].each do |method|
36
+ lambda {
37
+ @klass.send(method, "http://crazy_url_that_isnt_mocked.com")
38
+ }.should raise_error(Typhoeus::MockExpectedError)
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "hitting a mocked URL that returns false" do
44
+ it "should not raise a MockExpectedError" do
45
+ @klass.allow_net_connect = false
46
+ @klass.mock(:delete,
47
+ :url => "http://test.com",
48
+ :code => 500,
49
+ :body => 'ok')
50
+
51
+ lambda {
52
+ @klass.delete("http://test.com",
53
+ :on_failure => lambda { |response| false })
54
+ }.should_not raise_error
55
+ end
56
+ end
57
+ end
58
+
18
59
  describe "mocking" do
19
60
  it "should mock out GET" do
20
61
  @klass.mock(:get)
@@ -117,6 +158,23 @@ describe Typhoeus do
117
158
  @klass.mock(:get, :url => "http://localhost:3001", :body => "hi", :code => 500)
118
159
  @klass.do_stuff.should == :fail
119
160
  end
161
+
162
+ it "should allow for subclassing a class that includes Typhoeus, and merging defaults" do
163
+ class TestA
164
+ include Typhoeus
165
+ remote_defaults :on_failure => lambda { |response|
166
+ :fail
167
+ }
168
+ end
169
+
170
+ class TestB < TestA
171
+ remote_defaults :base_uri => "http://localhost"
172
+ define_remote_method :do_stuff
173
+ end
174
+
175
+ TestB.mock(:get, :url => "http://localhost", :body => "hi", :code => 500)
176
+ TestB.do_stuff.should == :fail
177
+ end
120
178
  end
121
179
 
122
180
  describe "response hash" do
@@ -421,7 +479,14 @@ describe Typhoeus do
421
479
  define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :params => {:foo => :bar}
422
480
  end
423
481
 
424
- @klass.do_stuff(:params => {:asdf => :jkl}).body.should include("QUERY_STRING=foo=bar&asdf=jkl")
482
+ result = @klass.do_stuff(:params => {:asdf => :jkl})
483
+
484
+ # Make this test more robust to hash ordering.
485
+ query_string = result.body.match(/QUERY_STRING=([^\n]+)/)
486
+ params = query_string[1].split("&")
487
+ ["asdf=jkl", "foo=bar"].each do |param|
488
+ params.should include(param)
489
+ end
425
490
  end
426
491
  end
427
492
 
@@ -539,4 +604,4 @@ describe Typhoeus do
539
604
  # end
540
605
  # end
541
606
  # end
542
- end
607
+ end
@@ -3,19 +3,29 @@ require File.dirname(__FILE__) + '/../spec_helper'
3
3
  describe Typhoeus::Response do
4
4
  describe "initialize" do
5
5
  it "should store response_code" do
6
- Typhoeus::Response.new(200, nil, nil, nil).code.should == 200
6
+ Typhoeus::Response.new(:code => 200).code.should == 200
7
7
  end
8
8
 
9
9
  it "should store response_headers" do
10
- Typhoeus::Response.new(nil, "a header!", nil, nil).headers.should == "a header!"
10
+ Typhoeus::Response.new(:headers => "a header!").headers.should == "a header!"
11
11
  end
12
12
 
13
13
  it "should store response_body" do
14
- Typhoeus::Response.new(nil, nil, "a body!", nil).body.should == "a body!"
14
+ Typhoeus::Response.new(:body => "a body!").body.should == "a body!"
15
15
  end
16
16
 
17
17
  it "should store request_time" do
18
- Typhoeus::Response.new(nil, nil, nil, 1.23).time.should == 1.23
18
+ Typhoeus::Response.new(:time => 1.23).time.should == 1.23
19
+ end
20
+
21
+ it "should store requested_url" do
22
+ response = Typhoeus::Response.new(:requested_url => "http://test.com")
23
+ response.requested_url.should == "http://test.com"
24
+ end
25
+
26
+ it "should store requested_http_method" do
27
+ response = Typhoeus::Response.new(:requested_http_method => :delete)
28
+ response.requested_http_method.should == :delete
19
29
  end
20
30
  end
21
- end
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pauldix-typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Dix
@@ -51,6 +51,7 @@ files:
51
51
  - spec/servers/method_server.rb
52
52
  has_rdoc: true
53
53
  homepage: http://github.com/pauldix/typhoeus
54
+ licenses:
54
55
  post_install_message:
55
56
  rdoc_options: []
56
57
 
@@ -72,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
73
  requirements: []
73
74
 
74
75
  rubyforge_project:
75
- rubygems_version: 1.2.0
76
+ rubygems_version: 1.3.5
76
77
  signing_key:
77
78
  specification_version: 2
78
79
  summary: A library for interacting with web services (and building SOAs) at blinding speed.