pauldix-typhoeus 0.0.14 → 0.0.15
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.
- data/lib/typhoeus/remote.rb +64 -6
- data/lib/typhoeus/remote_method.rb +2 -1
- data/lib/typhoeus.rb +1 -1
- data/spec/typhoeus/remote_spec.rb +78 -2
- metadata +1 -1
data/lib/typhoeus/remote.rb
CHANGED
@@ -99,7 +99,51 @@ module Typhoeus
|
|
99
99
|
raise MockExpectedError, message
|
100
100
|
end
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
|
+
def check_expected_headers!(response_args, options)
|
104
|
+
missing_headers = {}
|
105
|
+
|
106
|
+
response_args[:expected_headers].each do |key, value|
|
107
|
+
if options[:headers].nil?
|
108
|
+
missing_headers[key] = [value, nil]
|
109
|
+
elsif ((options[:headers][key] && value != :anything) &&
|
110
|
+
options[:headers][key] != value)
|
111
|
+
|
112
|
+
missing_headers[key] = [value, options[:headers][key]]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
unless missing_headers.empty?
|
117
|
+
raise headers_error_summary(response_args, options, missing_headers, 'expected')
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def check_unexpected_headers!(response_args, options)
|
122
|
+
bad_headers = {}
|
123
|
+
response_args[:unexpected_headers].each do |key, value|
|
124
|
+
if (options[:headers][key] && value == :anything) ||
|
125
|
+
(options[:headers][key] == value)
|
126
|
+
bad_headers[key] = [value, options[:headers][key]]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
unless bad_headers.empty?
|
131
|
+
raise headers_error_summary(response_args, options, bad_headers, 'did not expect')
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def headers_error_summary(response_args, options, missing_headers, lead_in)
|
136
|
+
error = "#{lead_in} the following headers: #{response_args[:expected_headers].inspect}, but received: #{options[:headers].inspect}\n\n"
|
137
|
+
error << "Differences:\n"
|
138
|
+
error << "------------\n"
|
139
|
+
missing_headers.each do |key, values|
|
140
|
+
error << " - #{key}: #{lead_in} #{values[0].inspect}, got #{values[1].inspect}\n"
|
141
|
+
end
|
142
|
+
|
143
|
+
error
|
144
|
+
end
|
145
|
+
private :headers_error_summary
|
146
|
+
|
103
147
|
def get_mock_and_run_handlers(method, response_args, options)
|
104
148
|
response = Response.new(response_args)
|
105
149
|
|
@@ -108,24 +152,29 @@ module Typhoeus
|
|
108
152
|
end
|
109
153
|
|
110
154
|
if response_args.has_key? :expected_headers
|
111
|
-
|
155
|
+
check_expected_headers!(response_args, options)
|
112
156
|
end
|
113
|
-
|
157
|
+
|
158
|
+
if response_args.has_key? :unexpected_headers
|
159
|
+
check_unexpected_headers!(response_args, options)
|
160
|
+
end
|
161
|
+
|
114
162
|
if response.code >= 200 && response.code < 300 && options.has_key?(:on_success)
|
115
163
|
response = options[:on_success].call(response)
|
116
164
|
elsif options.has_key?(:on_failure)
|
117
165
|
response = options[:on_failure].call(response)
|
118
166
|
end
|
119
|
-
|
167
|
+
|
168
|
+
encode_nil_response(response)
|
120
169
|
end
|
121
|
-
|
170
|
+
|
122
171
|
[:get, :post, :put, :delete].each do |method|
|
123
172
|
line = __LINE__ + 2 # get any errors on the correct line num
|
124
173
|
code = <<-SRC
|
125
174
|
def #{method.to_s}(url, options = {})
|
126
175
|
mock_object = get_mock(:#{method.to_s}, url, options)
|
127
176
|
unless mock_object.nil?
|
128
|
-
mock_object
|
177
|
+
decode_nil_response(mock_object)
|
129
178
|
else
|
130
179
|
enforce_allow_net_connect!(:#{method.to_s}, url, options[:params])
|
131
180
|
remote_proxy_object(url, :#{method.to_s}, options)
|
@@ -243,5 +292,14 @@ module Typhoeus
|
|
243
292
|
end
|
244
293
|
SRC
|
245
294
|
end
|
295
|
+
|
296
|
+
private
|
297
|
+
def encode_nil_response(response)
|
298
|
+
response == nil ? :__nil__ : response
|
299
|
+
end
|
300
|
+
|
301
|
+
def decode_nil_response(response)
|
302
|
+
response == :__nil__ ? nil : response
|
303
|
+
end
|
246
304
|
end # ClassMethods
|
247
305
|
end
|
@@ -12,6 +12,7 @@ module Typhoeus
|
|
12
12
|
@cache_responses = options.delete(:cache_responses)
|
13
13
|
@memoize_responses = options.delete(:memoize_responses) || @cache_responses
|
14
14
|
@cache_ttl = @cache_responses == true ? 0 : @cache_responses
|
15
|
+
@keys = nil
|
15
16
|
|
16
17
|
clear_cache
|
17
18
|
end
|
@@ -104,4 +105,4 @@ module Typhoeus
|
|
104
105
|
end
|
105
106
|
end
|
106
107
|
end
|
107
|
-
end
|
108
|
+
end
|
data/lib/typhoeus.rb
CHANGED
@@ -54,6 +54,20 @@ describe Typhoeus do
|
|
54
54
|
}.should_not raise_error
|
55
55
|
end
|
56
56
|
end
|
57
|
+
|
58
|
+
describe "handlers" do
|
59
|
+
it "should be able to return nil as part of their block" do
|
60
|
+
@klass.allow_net_connect = false
|
61
|
+
url = 'http://api.mysite.com/v1/stuff.json'
|
62
|
+
@klass.mock(:get,
|
63
|
+
:url => url,
|
64
|
+
:body => '',
|
65
|
+
:code => 500)
|
66
|
+
result = @klass.get(url,
|
67
|
+
:on_failure => lambda { |response| nil })
|
68
|
+
result.should be_nil
|
69
|
+
end
|
70
|
+
end
|
57
71
|
end
|
58
72
|
|
59
73
|
describe "mocking" do
|
@@ -138,13 +152,68 @@ describe Typhoeus do
|
|
138
152
|
}.should raise_error
|
139
153
|
end
|
140
154
|
end
|
141
|
-
|
155
|
+
|
156
|
+
describe "check_expected_headers!" do
|
157
|
+
before(:each) do
|
158
|
+
@header_klass = Class.new do
|
159
|
+
include Typhoeus
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should match a header with :anything" do
|
164
|
+
lambda {
|
165
|
+
@header_klass.check_expected_headers!(
|
166
|
+
{ :expected_headers => { 'Content-Type' => :anything } },
|
167
|
+
{ :headers => { 'Content-Type' => 'text/xml' } }
|
168
|
+
)
|
169
|
+
}.should_not raise_error
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should enforce exact matching" do
|
173
|
+
lambda {
|
174
|
+
@header_klass.check_expected_headers!(
|
175
|
+
{ :expected_headers => { 'Content-Type' => 'text/html' } },
|
176
|
+
{ :headers => { 'Content-Type' => 'text/xml' } }
|
177
|
+
)
|
178
|
+
}.should raise_error
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
describe "check_unexpected_headers!" do
|
183
|
+
before(:each) do
|
184
|
+
@header_klass = Class.new do
|
185
|
+
include Typhoeus
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should match a header with :anything" do
|
190
|
+
lambda {
|
191
|
+
@header_klass.check_unexpected_headers!(
|
192
|
+
{ :unexpected_headers => { 'Content-Type' => :anything } },
|
193
|
+
{ :headers => { "Content-Type" => "text/xml" } }
|
194
|
+
)
|
195
|
+
}.should raise_error
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should not match if a header is different from the expected value" do
|
199
|
+
lambda {
|
200
|
+
@header_klass.check_unexpected_headers!(
|
201
|
+
{ :unexpected_headers => { 'Content-Type' => 'text/html' } },
|
202
|
+
{ :headers => { "Content-Type" => "text/xml" } }
|
203
|
+
)
|
204
|
+
}.should_not raise_error
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
142
208
|
describe "request header expectations" do
|
143
209
|
before(:all) do
|
144
210
|
@header_klass = Class.new do
|
145
211
|
include Typhoeus
|
146
212
|
end
|
147
|
-
@header_klass.mock(:get,
|
213
|
+
@header_klass.mock(:get,
|
214
|
+
:url => "http://asdf",
|
215
|
+
:expected_headers => {"If-None-Match" => "\"lkjsd90823\""},
|
216
|
+
:unexpected_headers => { 'Content-Type' => "text/xml" })
|
148
217
|
end
|
149
218
|
|
150
219
|
it "should take expected request headers" do
|
@@ -156,6 +225,13 @@ describe Typhoeus do
|
|
156
225
|
@header_klass.get("http://asdf")
|
157
226
|
}.should raise_error
|
158
227
|
end
|
228
|
+
|
229
|
+
it "should raise if an unexpected header shows up" do
|
230
|
+
lambda {
|
231
|
+
@header_klass.get("http://asdf",
|
232
|
+
:headers => { "Content-Type" => "text/xml" })
|
233
|
+
}.should raise_error
|
234
|
+
end
|
159
235
|
end
|
160
236
|
|
161
237
|
describe "remote methods" do
|