pauldix-typhoeus 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/typhoeus/remote.rb +61 -46
- data/lib/typhoeus.rb +1 -1
- data/spec/typhoeus/remote_spec.rb +16 -0
- metadata +1 -1
data/lib/typhoeus/remote.rb
CHANGED
@@ -26,7 +26,39 @@ module Typhoeus
|
|
26
26
|
args[:time] ||= 0
|
27
27
|
url = args.delete(:url)
|
28
28
|
url ||= :catch_all
|
29
|
-
|
29
|
+
params = args.delete(:params)
|
30
|
+
|
31
|
+
key = mock_key_for(url, params)
|
32
|
+
|
33
|
+
@remote_mocks[method][key] = args
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns a key for a given URL and passed in
|
37
|
+
# set of Typhoeus options to be used to store/retrieve
|
38
|
+
# a corresponding mock.
|
39
|
+
def mock_key_for(url, params = nil)
|
40
|
+
if url == :catch_all
|
41
|
+
url
|
42
|
+
else
|
43
|
+
key = url
|
44
|
+
if params and !params.empty?
|
45
|
+
key += flatten_and_sort_hash(params).to_s
|
46
|
+
end
|
47
|
+
key
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def flatten_and_sort_hash(params)
|
52
|
+
params = params.dup
|
53
|
+
|
54
|
+
# Flatten any sub-hashes to a single string.
|
55
|
+
params.keys.each do |key|
|
56
|
+
if params[key].is_a?(Hash)
|
57
|
+
params[key] = params[key].sort_by { |k, v| k.to_s.downcase }.to_s
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
params.sort_by { |k, v| k.to_s.downcase }
|
30
62
|
end
|
31
63
|
|
32
64
|
def get_mock(method, url, options)
|
@@ -34,9 +66,10 @@ module Typhoeus
|
|
34
66
|
if @remote_mocks.has_key? method
|
35
67
|
extra_response_args = { :requested_http_method => method,
|
36
68
|
:requested_url => url }
|
37
|
-
|
69
|
+
mock_key = mock_key_for(url, options[:params])
|
70
|
+
if @remote_mocks[method].has_key? mock_key
|
38
71
|
get_mock_and_run_handlers(method,
|
39
|
-
@remote_mocks[method][
|
72
|
+
@remote_mocks[method][mock_key].merge(
|
40
73
|
extra_response_args),
|
41
74
|
options)
|
42
75
|
elsif @remote_mocks[method].has_key? :catch_all
|
@@ -52,12 +85,18 @@ module Typhoeus
|
|
52
85
|
end
|
53
86
|
end
|
54
87
|
|
55
|
-
def enforce_allow_net_connect!(http_verb, url)
|
88
|
+
def enforce_allow_net_connect!(http_verb, url, params = nil)
|
56
89
|
if !allow_net_connect
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
90
|
+
message = "Real HTTP connections are disabled. Unregistered request: " <<
|
91
|
+
"#{http_verb.to_s.upcase} #{url}\n" <<
|
92
|
+
" Try: mock(:#{http_verb}, :url => \"#{url}\""
|
93
|
+
if params
|
94
|
+
message << ",\n :params => #{params.inspect}"
|
95
|
+
end
|
96
|
+
|
97
|
+
message << ")"
|
98
|
+
|
99
|
+
raise MockExpectedError, message
|
61
100
|
end
|
62
101
|
end
|
63
102
|
|
@@ -80,44 +119,20 @@ module Typhoeus
|
|
80
119
|
response
|
81
120
|
end
|
82
121
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
else
|
98
|
-
enforce_allow_net_connect!(:post, url)
|
99
|
-
remote_proxy_object(url, :post, options)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def put(url, options = {}, &block)
|
104
|
-
mock_object = get_mock(:put, url, 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
|
111
|
-
end
|
112
|
-
|
113
|
-
def delete(url, options = {}, &block)
|
114
|
-
mock_object = get_mock(:delete, url, 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
|
122
|
+
[:get, :post, :put, :delete].each do |method|
|
123
|
+
line = __LINE__ + 2 # get any errors on the correct line num
|
124
|
+
code = <<-SRC
|
125
|
+
def #{method.to_s}(url, options = {})
|
126
|
+
mock_object = get_mock(:#{method.to_s}, url, options)
|
127
|
+
unless mock_object.nil?
|
128
|
+
mock_object
|
129
|
+
else
|
130
|
+
enforce_allow_net_connect!(:#{method.to_s}, url, options[:params])
|
131
|
+
remote_proxy_object(url, :#{method.to_s}, options)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
SRC
|
135
|
+
module_eval(code, "./lib/typhoeus/remote.rb", line)
|
121
136
|
end
|
122
137
|
|
123
138
|
def remote_proxy_object(url, method, options)
|
data/lib/typhoeus.rb
CHANGED
@@ -103,6 +103,22 @@ describe Typhoeus do
|
|
103
103
|
|
104
104
|
@klass.get("http://localhost:234234234").code.should == 0
|
105
105
|
end
|
106
|
+
|
107
|
+
it "should be able to mock using specific params as well" do
|
108
|
+
@klass.allow_net_connect = false
|
109
|
+
@klass.mock(:get, :url => "http://stuff")
|
110
|
+
|
111
|
+
lambda {
|
112
|
+
@klass.get("http://stuff", :params => { :a => 'test' })
|
113
|
+
}.should raise_error(Typhoeus::MockExpectedError)
|
114
|
+
|
115
|
+
@klass.mock(:get,
|
116
|
+
:url => "http://stuff",
|
117
|
+
:params => { :a => 'test' })
|
118
|
+
lambda {
|
119
|
+
@klass.get("http://stuff", :params => { :a => 'test' })
|
120
|
+
}.should_not raise_error(Typhoeus::MockExpectedError)
|
121
|
+
end
|
106
122
|
|
107
123
|
describe "request body expectations" do
|
108
124
|
before(:all) do
|