webmock 3.34.0 → 3.41.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +358 -0
- data/README.md +222 -3
- data/lib/webmock/api.rb +4 -0
- data/lib/webmock/errors.rb +1 -1
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +51 -7
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +291 -39
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +11 -3
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +23 -2
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +33 -4
- data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +4 -1
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +132 -110
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +1 -1
- data/lib/webmock/http_lib_adapters/net_http.rb +66 -3
- data/lib/webmock/http_lib_adapters/net_http_response.rb +28 -2
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +242 -40
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +65 -7
- data/lib/webmock/json_body_hint.rb +8 -5
- data/lib/webmock/request_body_diff.rb +16 -6
- data/lib/webmock/request_execution_verifier.rb +13 -2
- data/lib/webmock/request_pattern.rb +236 -36
- data/lib/webmock/request_registry.rb +75 -5
- data/lib/webmock/request_signature.rb +44 -5
- data/lib/webmock/request_signature_snippet.rb +23 -2
- data/lib/webmock/request_stub.rb +8 -4
- data/lib/webmock/response.rb +42 -3
- data/lib/webmock/stub_registry.rb +7 -6
- data/lib/webmock/stub_request_snippet.rb +9 -3
- data/lib/webmock/trailing_slash_hint.rb +55 -0
- data/lib/webmock/util/hash_counter.rb +27 -8
- data/lib/webmock/util/hash_keys_stringifier.rb +23 -1
- data/lib/webmock/util/headers.rb +17 -0
- data/lib/webmock/util/message_string.rb +28 -0
- data/lib/webmock/util/parsers/json.rb +33 -2
- data/lib/webmock/util/query_mapper.rb +56 -37
- data/lib/webmock/util/upload_body.rb +63 -0
- data/lib/webmock/util/uri.rb +21 -12
- data/lib/webmock/util/values_stringifier.rb +54 -0
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +35 -4
- data/lib/webmock.rb +3 -0
- metadata +7 -4
|
@@ -22,14 +22,20 @@ if defined?(Curl)
|
|
|
22
22
|
|
|
23
23
|
OriginalCurlEasy = Curl::Easy unless const_defined?(:OriginalCurlEasy)
|
|
24
24
|
|
|
25
|
+
OriginalCurlMulti = Curl::Multi unless const_defined?(:OriginalCurlMulti)
|
|
26
|
+
|
|
25
27
|
def self.enable!
|
|
26
28
|
Curl.send(:remove_const, :Easy)
|
|
27
29
|
Curl.send(:const_set, :Easy, Curl::WebMockCurlEasy)
|
|
30
|
+
Curl.send(:remove_const, :Multi)
|
|
31
|
+
Curl.send(:const_set, :Multi, Curl::WebMockCurlMulti)
|
|
28
32
|
end
|
|
29
33
|
|
|
30
34
|
def self.disable!
|
|
31
35
|
Curl.send(:remove_const, :Easy)
|
|
32
36
|
Curl.send(:const_set, :Easy, OriginalCurlEasy)
|
|
37
|
+
Curl.send(:remove_const, :Multi)
|
|
38
|
+
Curl.send(:const_set, :Multi, OriginalCurlMulti)
|
|
33
39
|
end
|
|
34
40
|
|
|
35
41
|
# Borrowed from Patron:
|
|
@@ -61,18 +67,130 @@ if defined?(Curl)
|
|
|
61
67
|
end
|
|
62
68
|
|
|
63
69
|
module Curl
|
|
70
|
+
class WebMockCurlMulti < Curl::Multi
|
|
71
|
+
# Intercept the native attachment boundary, retaining curb's public add,
|
|
72
|
+
# remove, request table and native event loop for allowed transfers.
|
|
73
|
+
def _add(easy)
|
|
74
|
+
# A handle of curb's own Curl::Easy class, such as a subclass defined
|
|
75
|
+
# before WebMock was loaded, one made while WebMock was disabled or a
|
|
76
|
+
# copy made with Curl::Easy#dup, makes its request without WebMock, as
|
|
77
|
+
# it always has. Easy#perform runs such handles through a multi too.
|
|
78
|
+
return super if !easy.is_a?(WebMockCurlEasy) || easy.webmock_performing_easy?
|
|
79
|
+
|
|
80
|
+
begin
|
|
81
|
+
signature, response = easy.webmock_request
|
|
82
|
+
rescue Exception # NetConnectNotAllowedError inherits directly from Exception.
|
|
83
|
+
# Older curb versions insert into requests before calling _add.
|
|
84
|
+
requests.delete(easy.object_id)
|
|
85
|
+
easy.webmock_restore_multi_url
|
|
86
|
+
raise
|
|
87
|
+
end
|
|
88
|
+
if response
|
|
89
|
+
easy.multi = self
|
|
90
|
+
webmock_pending[easy.object_id] = [easy, signature, response]
|
|
91
|
+
else
|
|
92
|
+
easy.webmock_record_multi_request(signature)
|
|
93
|
+
super
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def _remove(easy)
|
|
98
|
+
if webmock_pending.delete(easy.object_id)
|
|
99
|
+
easy.multi = nil
|
|
100
|
+
return self
|
|
101
|
+
end
|
|
102
|
+
super
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def perform(&block)
|
|
106
|
+
complete_webmock_requests
|
|
107
|
+
super do |multi|
|
|
108
|
+
begin
|
|
109
|
+
complete_webmock_requests
|
|
110
|
+
block.call(multi) if block
|
|
111
|
+
end until webmock_pending.empty?
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def webmock_pending
|
|
118
|
+
@webmock_pending ||= {}
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def complete_webmock_requests
|
|
122
|
+
until webmock_pending.empty?
|
|
123
|
+
easy, signature, response = webmock_pending.values.first
|
|
124
|
+
# Curb 1.5 takes a handle out of the multi on reset or close without
|
|
125
|
+
# calling _remove, so it no longer belongs to this multi.
|
|
126
|
+
attached = requests.key?(easy.object_id)
|
|
127
|
+
# Detach before callbacks so they can remove or requeue handles.
|
|
128
|
+
remove(easy)
|
|
129
|
+
webmock_pending.delete(easy.object_id)
|
|
130
|
+
next unless attached
|
|
131
|
+
|
|
132
|
+
if easy.webmock_follow_multi_redirect(signature, response)
|
|
133
|
+
add(easy)
|
|
134
|
+
else
|
|
135
|
+
begin
|
|
136
|
+
easy.multi = self
|
|
137
|
+
easy.webmock_complete_multi_request(signature, response)
|
|
138
|
+
ensure
|
|
139
|
+
easy.multi = nil if easy.multi == self && !requests.key?(easy.object_id)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
64
146
|
class WebMockCurlEasy < Curl::Easy
|
|
65
|
-
|
|
147
|
+
# Curb's native PostField cannot be duplicated. Its public attributes
|
|
148
|
+
# describe the field without running a content callback or opening a file.
|
|
149
|
+
MultipartField = Struct.new(:name, :content, :content_type, :local_file, :remote_file) do
|
|
150
|
+
def self.from(field)
|
|
151
|
+
new(*members.map { |attribute| field.public_send(attribute)&.to_s })
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def to_s
|
|
155
|
+
return super unless content && !local_file && !remote_file
|
|
156
|
+
Curl::PostField.content(name, content).to_s
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def initialize_copy(original)
|
|
160
|
+
super
|
|
161
|
+
each_pair { |attribute, value| self[attribute] = value.dup }
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
private_constant :MultipartField
|
|
165
|
+
|
|
166
|
+
def webmock_request
|
|
167
|
+
@webmock_method ||= :get
|
|
168
|
+
clear_webmock_response
|
|
169
|
+
webmock_clear_multi_request
|
|
66
170
|
request_signature = build_request_signature
|
|
67
|
-
|
|
171
|
+
recorded = signature_for_history(request_signature)
|
|
172
|
+
if request_signature.method == :put && @put_data.respond_to?(:read)
|
|
173
|
+
request_signature = request_signature.with_upload_body(@put_data)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(
|
|
177
|
+
request_signature, record: recorded)
|
|
178
|
+
unless webmock_response || WebMock.net_connect_allowed?(request_signature.uri)
|
|
179
|
+
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
180
|
+
end
|
|
181
|
+
[request_signature, webmock_response]
|
|
182
|
+
end
|
|
68
183
|
|
|
69
|
-
|
|
184
|
+
def curb_or_webmock
|
|
185
|
+
request_signature, webmock_response = webmock_request
|
|
186
|
+
if webmock_response
|
|
70
187
|
build_curb_response(webmock_response)
|
|
71
188
|
WebMock::CallbackRegistry.invoke_callbacks(
|
|
72
189
|
{lib: :curb}, request_signature, webmock_response)
|
|
73
190
|
invoke_curb_callbacks
|
|
74
191
|
true
|
|
75
|
-
|
|
192
|
+
else
|
|
193
|
+
@webmock_performing_easy = true
|
|
76
194
|
res = yield
|
|
77
195
|
if WebMock::CallbackRegistry.any_callbacks?
|
|
78
196
|
webmock_response = build_webmock_response
|
|
@@ -81,11 +199,113 @@ if defined?(Curl)
|
|
|
81
199
|
webmock_response)
|
|
82
200
|
end
|
|
83
201
|
res
|
|
202
|
+
end
|
|
203
|
+
ensure
|
|
204
|
+
@webmock_performing_easy = false
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def webmock_performing_easy?
|
|
208
|
+
@webmock_performing_easy
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def webmock_record_multi_request(signature)
|
|
212
|
+
@webmock_multi_request = signature
|
|
213
|
+
on_complete(&@on_complete)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def webmock_clear_multi_request
|
|
217
|
+
@webmock_multi_request = nil
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
%w[body header].each do |part|
|
|
221
|
+
define_method("on_#{part}") do |&block|
|
|
222
|
+
instance_variable_set("@on_#{part}", block)
|
|
223
|
+
if block
|
|
224
|
+
super() do |data|
|
|
225
|
+
if @webmock_multi_request
|
|
226
|
+
buffer = instance_variable_get("@webmock_#{part}") || +""
|
|
227
|
+
buffer << data
|
|
228
|
+
instance_variable_set("@webmock_#{part}", buffer)
|
|
229
|
+
end
|
|
230
|
+
block.call(data)
|
|
231
|
+
end
|
|
232
|
+
else
|
|
233
|
+
super(&block)
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def on_complete(&block)
|
|
239
|
+
@on_complete = block
|
|
240
|
+
super do |easy|
|
|
241
|
+
signature = @webmock_multi_request
|
|
242
|
+
webmock_restore_multi_url
|
|
243
|
+
webmock_clear_multi_request
|
|
244
|
+
if signature && WebMock::CallbackRegistry.any_callbacks?
|
|
245
|
+
WebMock::CallbackRegistry.invoke_callbacks(
|
|
246
|
+
{lib: :curb, real_request: true}, signature, build_webmock_response)
|
|
247
|
+
end
|
|
248
|
+
block.call(easy) if block
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def webmock_follow_multi_redirect(signature, response)
|
|
253
|
+
location = response.headers && response.headers['Location']
|
|
254
|
+
return false unless follow_location? && (300..399).include?(response.status[0]) && location
|
|
255
|
+
return false if response.should_timeout || response.exception
|
|
256
|
+
|
|
257
|
+
@webmock_multi_redirects ||= 0
|
|
258
|
+
limit = max_redirects || 50
|
|
259
|
+
if limit >= 0 && @webmock_multi_redirects >= limit
|
|
260
|
+
@webmock_last_result = 47 # CURLE_TOO_MANY_REDIRECTS
|
|
261
|
+
return false
|
|
262
|
+
end
|
|
263
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :curb}, signature, response)
|
|
264
|
+
@webmock_multi_original_url ||= url
|
|
265
|
+
@webmock_multi_redirects += 1
|
|
266
|
+
self.url = WebMock::Util::URI.heuristic_parse(url).join(location).to_s
|
|
267
|
+
true
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def webmock_restore_multi_url
|
|
271
|
+
return unless @webmock_multi_original_url
|
|
272
|
+
@last_effective_url = last_effective_url
|
|
273
|
+
self.url = @webmock_multi_original_url
|
|
274
|
+
@webmock_multi_original_url = @webmock_multi_redirects = nil
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def webmock_complete_multi_request(signature, response)
|
|
278
|
+
if response.should_timeout
|
|
279
|
+
@body_str = ""
|
|
280
|
+
@header_str = ""
|
|
281
|
+
@response_code = 0
|
|
282
|
+
@webmock_last_result = 28 # CURLE_OPERATION_TIMEDOUT
|
|
84
283
|
else
|
|
85
|
-
|
|
284
|
+
build_curb_response(response, follow_redirects: false)
|
|
285
|
+
@webmock_last_result ||= 0
|
|
86
286
|
end
|
|
287
|
+
webmock_restore_multi_url
|
|
288
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :curb}, signature, response)
|
|
289
|
+
invoke_curb_callbacks(multi: true)
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def last_result
|
|
293
|
+
@webmock_last_result || super
|
|
87
294
|
end
|
|
88
295
|
|
|
296
|
+
def clear_webmock_response
|
|
297
|
+
@body_str = @header_str = @response_code = @content_type = nil
|
|
298
|
+
@last_effective_url = @transfer_encoding = @webmock_last_result = nil
|
|
299
|
+
@webmock_body = @webmock_header = nil
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def signature_for_history(signature)
|
|
303
|
+
body = signature.body
|
|
304
|
+
return signature unless body.is_a?(Curl::PostField) || (body.is_a?(Array) && multipart_form_post?)
|
|
305
|
+
signature.with_body_values { |field| MultipartField.from(field) }
|
|
306
|
+
end
|
|
307
|
+
private :signature_for_history
|
|
308
|
+
|
|
89
309
|
def build_request_signature
|
|
90
310
|
method = @webmock_method.to_s.downcase.to_sym
|
|
91
311
|
|
|
@@ -98,12 +318,16 @@ if defined?(Curl)
|
|
|
98
318
|
when :post, :patch
|
|
99
319
|
self.post_body || @post_body
|
|
100
320
|
when :put
|
|
101
|
-
@put_data
|
|
321
|
+
if @put_data.respond_to?(:read)
|
|
322
|
+
WebMock::Util::UploadBody.read(@put_data, headers)
|
|
323
|
+
else
|
|
324
|
+
@put_data
|
|
325
|
+
end
|
|
102
326
|
else
|
|
103
327
|
nil
|
|
104
328
|
end
|
|
105
329
|
|
|
106
|
-
if
|
|
330
|
+
if @on_debug
|
|
107
331
|
@on_debug.call("Trying 127.0.0.1...\r\n", 0)
|
|
108
332
|
@on_debug.call('Connected to ' + uri.hostname + "\r\n", 0)
|
|
109
333
|
@debug_method = method.upcase
|
|
@@ -153,7 +377,7 @@ if defined?(Curl)
|
|
|
153
377
|
end
|
|
154
378
|
end
|
|
155
379
|
|
|
156
|
-
def build_curb_response(webmock_response)
|
|
380
|
+
def build_curb_response(webmock_response, follow_redirects: true)
|
|
157
381
|
raise Curl::Err::TimeoutError if webmock_response.should_timeout
|
|
158
382
|
webmock_response.raise_error_if_any
|
|
159
383
|
|
|
@@ -162,18 +386,22 @@ if defined?(Curl)
|
|
|
162
386
|
|
|
163
387
|
@header_str = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}\r\n".dup
|
|
164
388
|
|
|
165
|
-
@on_debug.call(@header_str, 1) if
|
|
389
|
+
@on_debug.call(@header_str, 1) if @on_debug
|
|
166
390
|
|
|
167
391
|
if webmock_response.headers
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
392
|
+
# Each cookie gets a line of its own, as servers send them.
|
|
393
|
+
@header_str << webmock_response.headers.flat_map do |k,v|
|
|
394
|
+
values = k == "Set-Cookie" ? Array(v) : [v.is_a?(Array) ? v.join(", ") : v]
|
|
395
|
+
values.map do |value|
|
|
396
|
+
header = "#{k}: #{value}"
|
|
397
|
+
@on_debug.call(header + "\r\n", 1) if @on_debug
|
|
398
|
+
header
|
|
399
|
+
end
|
|
172
400
|
end.join("\r\n")
|
|
173
|
-
@on_debug.call("\r\n", 1) if
|
|
401
|
+
@on_debug.call("\r\n", 1) if @on_debug
|
|
174
402
|
|
|
175
403
|
location = webmock_response.headers['Location']
|
|
176
|
-
if self.follow_location? && location
|
|
404
|
+
if follow_redirects && self.follow_location? && location
|
|
177
405
|
@last_effective_url = location
|
|
178
406
|
webmock_follow_location(location)
|
|
179
407
|
end
|
|
@@ -196,10 +424,10 @@ if defined?(Curl)
|
|
|
196
424
|
self.url = first_url
|
|
197
425
|
end
|
|
198
426
|
|
|
199
|
-
def invoke_curb_callbacks
|
|
200
|
-
@on_progress.call(0.0,1.0,0.0,1.0) if
|
|
201
|
-
self.header_str.lines.each { |header_line| @on_header.call header_line } if
|
|
202
|
-
if
|
|
427
|
+
def invoke_curb_callbacks(multi: false)
|
|
428
|
+
@on_progress.call(0.0,1.0,0.0,1.0) if @on_progress
|
|
429
|
+
self.header_str.lines.each { |header_line| @on_header.call header_line } if @on_header
|
|
430
|
+
if @on_body
|
|
203
431
|
if chunked_response?
|
|
204
432
|
self.body_str.each do |chunk|
|
|
205
433
|
@on_body.call(chunk)
|
|
@@ -208,15 +436,24 @@ if defined?(Curl)
|
|
|
208
436
|
@on_body.call(self.body_str)
|
|
209
437
|
end
|
|
210
438
|
end
|
|
211
|
-
|
|
439
|
+
completed_code = response_code
|
|
440
|
+
completed_result = last_result if multi
|
|
441
|
+
@on_complete.call(self) if @on_complete
|
|
442
|
+
|
|
443
|
+
if multi && completed_result != 0
|
|
444
|
+
@on_failure.call(self, Curl::Easy.error(completed_result)) if @on_failure
|
|
445
|
+
return
|
|
446
|
+
end
|
|
212
447
|
|
|
213
|
-
case
|
|
448
|
+
case completed_code
|
|
214
449
|
when 200..299
|
|
215
|
-
@on_success.call(self) if
|
|
450
|
+
@on_success.call(self) if @on_success
|
|
451
|
+
when 300..399
|
|
452
|
+
@on_redirect.call(self, Curl::Easy.error(0)) if multi && @on_redirect && max_redirects != 0
|
|
216
453
|
when 400..499
|
|
217
|
-
@on_missing.call(self, self.response_code) if
|
|
454
|
+
@on_missing.call(self, multi ? Curl::Easy.error(0) : self.response_code) if @on_missing
|
|
218
455
|
when 500..599
|
|
219
|
-
@on_failure.call(self, self.response_code) if
|
|
456
|
+
@on_failure.call(self, multi ? Curl::Easy.error(0) : self.response_code) if @on_failure
|
|
220
457
|
end
|
|
221
458
|
end
|
|
222
459
|
|
|
@@ -226,9 +463,9 @@ if defined?(Curl)
|
|
|
226
463
|
|
|
227
464
|
def build_webmock_response
|
|
228
465
|
status, headers =
|
|
229
|
-
WebMock::HttpLibAdapters::CurbAdapter.parse_header_string(self.header_str)
|
|
466
|
+
WebMock::HttpLibAdapters::CurbAdapter.parse_header_string((@webmock_header || self.header_str).to_s)
|
|
230
467
|
|
|
231
|
-
if
|
|
468
|
+
if @on_debug
|
|
232
469
|
http_response = ["HTTP/1.0 #{@debug_method} #{@debug_path}"]
|
|
233
470
|
headers.each do |name, value|
|
|
234
471
|
http_response << "#{name}: #{value}"
|
|
@@ -240,7 +477,7 @@ if defined?(Curl)
|
|
|
240
477
|
|
|
241
478
|
webmock_response = WebMock::Response.new
|
|
242
479
|
webmock_response.status = [self.response_code, status]
|
|
243
|
-
webmock_response.body = self.body_str
|
|
480
|
+
webmock_response.body = @webmock_body || self.body_str
|
|
244
481
|
webmock_response.headers = headers
|
|
245
482
|
webmock_response
|
|
246
483
|
end
|
|
@@ -270,10 +507,19 @@ if defined?(Curl)
|
|
|
270
507
|
end
|
|
271
508
|
end
|
|
272
509
|
|
|
273
|
-
def http_put
|
|
510
|
+
def http_put(data = nil)
|
|
511
|
+
# put_data= can supply the upcoming request's body until perform resets the method.
|
|
512
|
+
data = @put_data if data.nil? && @webmock_method == :put
|
|
274
513
|
@webmock_method = :put
|
|
275
|
-
@put_data = data
|
|
276
|
-
super
|
|
514
|
+
@put_data = data
|
|
515
|
+
return super('') if data.nil?
|
|
516
|
+
return super(data) if data.is_a?(String) || data.respond_to?(:read) || multipart_form_post?
|
|
517
|
+
|
|
518
|
+
# Curb joins other positional arguments as it would form fields.
|
|
519
|
+
@put_data = [data].join('&')
|
|
520
|
+
super(@put_data)
|
|
521
|
+
ensure
|
|
522
|
+
@put_data = nil
|
|
277
523
|
end
|
|
278
524
|
alias put http_put
|
|
279
525
|
|
|
@@ -293,10 +539,12 @@ if defined?(Curl)
|
|
|
293
539
|
@post_body = nil
|
|
294
540
|
end
|
|
295
541
|
|
|
296
|
-
def put_data=
|
|
542
|
+
def put_data=(data)
|
|
543
|
+
body = data.respond_to?(:read) ? data : data&.to_s
|
|
544
|
+
super(body)
|
|
297
545
|
@webmock_method = :put
|
|
298
|
-
@put_data =
|
|
299
|
-
|
|
546
|
+
@put_data = body
|
|
547
|
+
data
|
|
300
548
|
end
|
|
301
549
|
|
|
302
550
|
def post_body= data
|
|
@@ -345,7 +593,7 @@ if defined?(Curl)
|
|
|
345
593
|
@content_type ||= super
|
|
346
594
|
end
|
|
347
595
|
|
|
348
|
-
%w[ success failure missing
|
|
596
|
+
%w[ success failure missing redirect progress debug ].each do |callback|
|
|
349
597
|
class_eval <<-METHOD, __FILE__, __LINE__
|
|
350
598
|
def on_#{callback} &block
|
|
351
599
|
@on_#{callback} = block
|
|
@@ -359,11 +607,15 @@ if defined?(Curl)
|
|
|
359
607
|
end
|
|
360
608
|
|
|
361
609
|
def reset
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
610
|
+
@put_data = nil
|
|
611
|
+
clear_webmock_response
|
|
612
|
+
webmock_clear_multi_request
|
|
613
|
+
reset_webmock_method
|
|
614
|
+
@webmock_multi_original_url = @webmock_multi_redirects = nil
|
|
615
|
+
# Curb's reset drops every callback, so a stubbed request, or a real one
|
|
616
|
+
# WebMock records, doesn't run the ones set before it either.
|
|
617
|
+
@on_success = @on_failure = @on_missing = @on_redirect = @on_complete = nil
|
|
618
|
+
@on_body = @on_header = @on_progress = @on_debug = nil
|
|
367
619
|
super
|
|
368
620
|
end
|
|
369
621
|
end
|
|
@@ -84,6 +84,10 @@ if defined?(EventMachine::HttpClient)
|
|
|
84
84
|
|
|
85
85
|
class WebMockHttpClient < EventMachine::HttpClient
|
|
86
86
|
include HttpEncoding
|
|
87
|
+
# A YAML or Marshal dump names the cookie jar EventMachine::HttpClient::CookieJar,
|
|
88
|
+
# which is this class while WebMock is enabled, and loading a dump looks
|
|
89
|
+
# the constant up without the superclass.
|
|
90
|
+
CookieJar = superclass.const_get(:CookieJar, false) if superclass.const_defined?(:CookieJar, false)
|
|
87
91
|
|
|
88
92
|
def uri
|
|
89
93
|
@req.uri
|
|
@@ -107,8 +111,6 @@ if defined?(EventMachine::HttpClient)
|
|
|
107
111
|
end
|
|
108
112
|
|
|
109
113
|
def send_request(head, body)
|
|
110
|
-
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
|
|
111
|
-
|
|
112
114
|
if stubbed_webmock_response
|
|
113
115
|
WebMock::CallbackRegistry.invoke_callbacks({lib: :em_http_request}, request_signature, stubbed_webmock_response)
|
|
114
116
|
@uri ||= nil
|
|
@@ -144,7 +146,13 @@ if defined?(EventMachine::HttpClient)
|
|
|
144
146
|
|
|
145
147
|
def stubbed_webmock_response
|
|
146
148
|
unless defined?(@stubbed_webmock_response)
|
|
147
|
-
|
|
149
|
+
signature = request_signature
|
|
150
|
+
recorded = if signature.body.respond_to?(:read) && signature.body.respond_to?(:eof?)
|
|
151
|
+
signature.with_body_values { |stream| WebMock::Util::UploadBody.read(stream, signature.headers) }
|
|
152
|
+
else
|
|
153
|
+
signature
|
|
154
|
+
end
|
|
155
|
+
@stubbed_webmock_response = WebMock::StubRegistry.instance.response_for_request(signature, record: recorded)
|
|
148
156
|
end
|
|
149
157
|
|
|
150
158
|
@stubbed_webmock_response
|
|
@@ -53,7 +53,7 @@ if defined?(Excon)
|
|
|
53
53
|
|
|
54
54
|
def self.handle_request(params)
|
|
55
55
|
mock_request = self.build_request params.dup
|
|
56
|
-
WebMock::RequestRegistry.instance.
|
|
56
|
+
WebMock::RequestRegistry.instance.put(mock_request)
|
|
57
57
|
|
|
58
58
|
if mock_response = WebMock::StubRegistry.instance.response_for_request(mock_request)
|
|
59
59
|
self.perform_callbacks(mock_request, mock_response, real_request: false)
|
|
@@ -129,18 +129,39 @@ if defined?(Excon)
|
|
|
129
129
|
def self.real_response(mock)
|
|
130
130
|
raise Excon::Errors::Timeout if mock.should_timeout
|
|
131
131
|
mock.raise_error_if_any
|
|
132
|
+
headers = mock.headers || {}
|
|
133
|
+
# Copies, so changes to a response's cookies can't reach the stub.
|
|
134
|
+
cookies = Array(headers["Set-Cookie"]).map(&:dup)
|
|
135
|
+
# Excon joins repeated headers, and lists each cookie in :cookies.
|
|
136
|
+
headers = headers.merge("Set-Cookie" => join_cookies(cookies)) if cookies.size > 1
|
|
132
137
|
{
|
|
133
138
|
body: mock.body,
|
|
134
139
|
status: mock.status[0].to_i,
|
|
135
140
|
reason_phrase: mock.status[1],
|
|
136
|
-
headers:
|
|
141
|
+
headers: headers,
|
|
142
|
+
cookies: cookies
|
|
137
143
|
}
|
|
138
144
|
end
|
|
139
145
|
|
|
146
|
+
# Joined as Excon joins them, or as bytes when they are in
|
|
147
|
+
# incompatible encodings, as a server sends them.
|
|
148
|
+
def self.join_cookies(cookies)
|
|
149
|
+
cookies.join(", ")
|
|
150
|
+
rescue Encoding::CompatibilityError
|
|
151
|
+
cookies.map(&:b).join(", ".b)
|
|
152
|
+
end
|
|
153
|
+
|
|
140
154
|
def self.mock_response(real)
|
|
141
155
|
mock = WebMock::Response.new
|
|
142
156
|
mock.status = [real.status, real.reason_phrase]
|
|
143
157
|
mock.headers = real.headers
|
|
158
|
+
# Excon joins the cookies of a response in its header, so callbacks,
|
|
159
|
+
# such as VCR's, get each one from the list Excon keeps.
|
|
160
|
+
# Copies, so a callback that changes one doesn't change the response.
|
|
161
|
+
cookies = real.data[:cookies]
|
|
162
|
+
if cookies && cookies.size > 1
|
|
163
|
+
mock.headers = real.headers.to_h.reject { |name, _| name.casecmp?("Set-Cookie") }.merge("Set-Cookie" => cookies.map(&:dup))
|
|
164
|
+
end
|
|
144
165
|
mock.body = real.body.dup
|
|
145
166
|
mock
|
|
146
167
|
end
|
|
@@ -29,7 +29,7 @@ module HTTP
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def register_request(signature)
|
|
32
|
-
::WebMock::RequestRegistry.instance.
|
|
32
|
+
::WebMock::RequestRegistry.instance.put(signature)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
def replay
|
|
@@ -37,14 +37,43 @@ module HTTP
|
|
|
37
37
|
|
|
38
38
|
return unless webmock_response
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
features = @options.features.values
|
|
41
|
+
response = nil
|
|
42
|
+
exchange = ->(_request) { response = replay_response(features, webmock_response) }
|
|
43
|
+
# HTTP.rb 6 instruments a request in around_request, inside the
|
|
44
|
+
# HTTP::Client#perform a stub skips, so the instrumentation runs around
|
|
45
|
+
# a stubbed exchange too. Other features that run around the exchange,
|
|
46
|
+
# such as caching and digest_auth, don't run for a stub. The stub's
|
|
47
|
+
# response is returned whatever an instrumenter returns, and also when
|
|
48
|
+
# one doesn't yield, as before.
|
|
49
|
+
instrumentation(features).reverse.reduce(exchange) do |inner, feature|
|
|
50
|
+
->(request) { feature.around_request(request) { |r| inner.call(r) } }
|
|
51
|
+
end.call(@request)
|
|
52
|
+
response || replay_response(features, webmock_response)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def instrumentation(features)
|
|
56
|
+
return [] unless defined?(::HTTP::Features::Instrumentation) && ::HTTP::Feature.method_defined?(:around_request)
|
|
57
|
+
|
|
58
|
+
features.select { |feature| feature.is_a?(::HTTP::Features::Instrumentation) }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def replay_response(features, webmock_response)
|
|
62
|
+
# HTTP::Client calls on_error for an error while sending, which for a
|
|
63
|
+
# stub is its timeout or error, but not for one raised while wrapping
|
|
64
|
+
# the response, as the raise_error feature does for a 500.
|
|
65
|
+
begin
|
|
66
|
+
raise_timeout_error if webmock_response.should_timeout
|
|
67
|
+
webmock_response.raise_error_if_any
|
|
68
|
+
rescue ::HTTP::Error => e
|
|
69
|
+
features.each { |feature| feature.on_error(@request, e) if feature.respond_to?(:on_error) }
|
|
70
|
+
raise
|
|
71
|
+
end
|
|
42
72
|
|
|
43
73
|
invoke_callbacks(webmock_response, real_request: false)
|
|
44
74
|
response = ::HTTP::Response.from_webmock @request, webmock_response, request_signature
|
|
45
75
|
|
|
46
76
|
# HTTP.rb 6.0+ wraps responses in reverse feature order
|
|
47
|
-
features = @options.features.values
|
|
48
77
|
features = features.reverse if HTTP::VERSION >= "6.0.0"
|
|
49
78
|
features.each { |feature| response = feature.wrap_response(response) }
|
|
50
79
|
response
|
|
@@ -6,7 +6,10 @@ rescue LoadError
|
|
|
6
6
|
# HTTP gem not found
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
# Other libraries define a top-level HTTP with a VERSION too, and
|
|
10
|
+
# `require "http"` can load one of them, so look for the HTTP::Client
|
|
11
|
+
# this adapter patches as well.
|
|
12
|
+
if defined?(HTTP::VERSION) && defined?(HTTP::Client)
|
|
10
13
|
WebMock::VersionChecker.new("HTTP Gem", HTTP::VERSION, "0.6.0").check_version!
|
|
11
14
|
|
|
12
15
|
module WebMock
|