webmock 3.36.0 → 4.0.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 +335 -0
- data/README.md +227 -6
- data/lib/webmock/api.rb +4 -0
- data/lib/webmock/callback_registry.rb +14 -7
- data/lib/webmock/errors.rb +1 -1
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +18 -1
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +292 -46
- 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/response.rb +3 -0
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +92 -5
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +127 -109
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +1 -1
- data/lib/webmock/http_lib_adapters/net_http.rb +78 -10
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +32 -13
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +65 -7
- data/lib/webmock/json_body_hint.rb +119 -12
- data/lib/webmock/request_body_diff.rb +16 -6
- data/lib/webmock/request_execution_verifier.rb +19 -6
- data/lib/webmock/request_pattern.rb +178 -33
- data/lib/webmock/request_registry.rb +75 -5
- data/lib/webmock/request_signature.rb +44 -5
- data/lib/webmock/request_signature_snippet.rb +33 -4
- data/lib/webmock/request_stub.rb +32 -7
- 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 +23 -54
- 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 +10 -7
|
@@ -7,13 +7,7 @@ rescue LoadError
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
if defined?(Curl)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
# Curl::Easy#perform raises "cannot define finalizer for TrueClass" on Ruby 2.6
|
|
13
|
-
# in curb 1.3.0 through 1.3.5. Fixed in curb 1.3.6. See https://github.com/taf2/curb/issues/478
|
|
14
|
-
unsupported_curb_versions += %w[1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5] if RUBY_VERSION < '2.7'
|
|
15
|
-
|
|
16
|
-
WebMock::VersionChecker.new('Curb', Curl::CURB_VERSION, '0.7.16', nil, unsupported_curb_versions).check_version!
|
|
10
|
+
WebMock::VersionChecker.new('Curb', Curl::CURB_VERSION, '0.7.16', nil, ['0.8.7']).check_version!
|
|
17
11
|
|
|
18
12
|
module WebMock
|
|
19
13
|
module HttpLibAdapters
|
|
@@ -22,14 +16,20 @@ if defined?(Curl)
|
|
|
22
16
|
|
|
23
17
|
OriginalCurlEasy = Curl::Easy unless const_defined?(:OriginalCurlEasy)
|
|
24
18
|
|
|
19
|
+
OriginalCurlMulti = Curl::Multi unless const_defined?(:OriginalCurlMulti)
|
|
20
|
+
|
|
25
21
|
def self.enable!
|
|
26
22
|
Curl.send(:remove_const, :Easy)
|
|
27
23
|
Curl.send(:const_set, :Easy, Curl::WebMockCurlEasy)
|
|
24
|
+
Curl.send(:remove_const, :Multi)
|
|
25
|
+
Curl.send(:const_set, :Multi, Curl::WebMockCurlMulti)
|
|
28
26
|
end
|
|
29
27
|
|
|
30
28
|
def self.disable!
|
|
31
29
|
Curl.send(:remove_const, :Easy)
|
|
32
30
|
Curl.send(:const_set, :Easy, OriginalCurlEasy)
|
|
31
|
+
Curl.send(:remove_const, :Multi)
|
|
32
|
+
Curl.send(:const_set, :Multi, OriginalCurlMulti)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
# Borrowed from Patron:
|
|
@@ -61,18 +61,130 @@ if defined?(Curl)
|
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
module Curl
|
|
64
|
+
class WebMockCurlMulti < Curl::Multi
|
|
65
|
+
# Intercept the native attachment boundary, retaining curb's public add,
|
|
66
|
+
# remove, request table and native event loop for allowed transfers.
|
|
67
|
+
def _add(easy)
|
|
68
|
+
# A handle of curb's own Curl::Easy class, such as a subclass defined
|
|
69
|
+
# before WebMock was loaded, one made while WebMock was disabled or a
|
|
70
|
+
# copy made with Curl::Easy#dup, makes its request without WebMock, as
|
|
71
|
+
# it always has. Easy#perform runs such handles through a multi too.
|
|
72
|
+
return super if !easy.is_a?(WebMockCurlEasy) || easy.webmock_performing_easy?
|
|
73
|
+
|
|
74
|
+
begin
|
|
75
|
+
signature, response = easy.webmock_request
|
|
76
|
+
rescue Exception # NetConnectNotAllowedError inherits directly from Exception.
|
|
77
|
+
# Older curb versions insert into requests before calling _add.
|
|
78
|
+
requests.delete(easy.object_id)
|
|
79
|
+
easy.webmock_restore_multi_url
|
|
80
|
+
raise
|
|
81
|
+
end
|
|
82
|
+
if response
|
|
83
|
+
easy.multi = self
|
|
84
|
+
webmock_pending[easy.object_id] = [easy, signature, response]
|
|
85
|
+
else
|
|
86
|
+
easy.webmock_record_multi_request(signature)
|
|
87
|
+
super
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def _remove(easy)
|
|
92
|
+
if webmock_pending.delete(easy.object_id)
|
|
93
|
+
easy.multi = nil
|
|
94
|
+
return self
|
|
95
|
+
end
|
|
96
|
+
super
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def perform(&block)
|
|
100
|
+
complete_webmock_requests
|
|
101
|
+
super do |multi|
|
|
102
|
+
begin
|
|
103
|
+
complete_webmock_requests
|
|
104
|
+
block.call(multi) if block
|
|
105
|
+
end until webmock_pending.empty?
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
private
|
|
110
|
+
|
|
111
|
+
def webmock_pending
|
|
112
|
+
@webmock_pending ||= {}
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def complete_webmock_requests
|
|
116
|
+
until webmock_pending.empty?
|
|
117
|
+
easy, signature, response = webmock_pending.values.first
|
|
118
|
+
# Curb 1.5 takes a handle out of the multi on reset or close without
|
|
119
|
+
# calling _remove, so it no longer belongs to this multi.
|
|
120
|
+
attached = requests.key?(easy.object_id)
|
|
121
|
+
# Detach before callbacks so they can remove or requeue handles.
|
|
122
|
+
remove(easy)
|
|
123
|
+
webmock_pending.delete(easy.object_id)
|
|
124
|
+
next unless attached
|
|
125
|
+
|
|
126
|
+
if easy.webmock_follow_multi_redirect(signature, response)
|
|
127
|
+
add(easy)
|
|
128
|
+
else
|
|
129
|
+
begin
|
|
130
|
+
easy.multi = self
|
|
131
|
+
easy.webmock_complete_multi_request(signature, response)
|
|
132
|
+
ensure
|
|
133
|
+
easy.multi = nil if easy.multi == self && !requests.key?(easy.object_id)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
64
140
|
class WebMockCurlEasy < Curl::Easy
|
|
65
|
-
|
|
141
|
+
# Curb's native PostField cannot be duplicated. Its public attributes
|
|
142
|
+
# describe the field without running a content callback or opening a file.
|
|
143
|
+
MultipartField = Struct.new(:name, :content, :content_type, :local_file, :remote_file) do
|
|
144
|
+
def self.from(field)
|
|
145
|
+
new(*members.map { |attribute| field.public_send(attribute)&.to_s })
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def to_s
|
|
149
|
+
return super unless content && !local_file && !remote_file
|
|
150
|
+
Curl::PostField.content(name, content).to_s
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def initialize_copy(original)
|
|
154
|
+
super
|
|
155
|
+
each_pair { |attribute, value| self[attribute] = value.dup }
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
private_constant :MultipartField
|
|
159
|
+
|
|
160
|
+
def webmock_request
|
|
161
|
+
@webmock_method ||= :get
|
|
162
|
+
clear_webmock_response
|
|
163
|
+
webmock_clear_multi_request
|
|
66
164
|
request_signature = build_request_signature
|
|
67
|
-
|
|
165
|
+
recorded = signature_for_history(request_signature)
|
|
166
|
+
if request_signature.method == :put && @put_data.respond_to?(:read)
|
|
167
|
+
request_signature = request_signature.with_upload_body(@put_data)
|
|
168
|
+
end
|
|
68
169
|
|
|
69
|
-
|
|
170
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(
|
|
171
|
+
request_signature, record: recorded)
|
|
172
|
+
unless webmock_response || WebMock.net_connect_allowed?(request_signature.uri)
|
|
173
|
+
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
174
|
+
end
|
|
175
|
+
[request_signature, webmock_response]
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def curb_or_webmock
|
|
179
|
+
request_signature, webmock_response = webmock_request
|
|
180
|
+
if webmock_response
|
|
70
181
|
build_curb_response(webmock_response)
|
|
71
182
|
WebMock::CallbackRegistry.invoke_callbacks(
|
|
72
183
|
{lib: :curb}, request_signature, webmock_response)
|
|
73
184
|
invoke_curb_callbacks
|
|
74
185
|
true
|
|
75
|
-
|
|
186
|
+
else
|
|
187
|
+
@webmock_performing_easy = true
|
|
76
188
|
res = yield
|
|
77
189
|
if WebMock::CallbackRegistry.any_callbacks?
|
|
78
190
|
webmock_response = build_webmock_response
|
|
@@ -81,11 +193,113 @@ if defined?(Curl)
|
|
|
81
193
|
webmock_response)
|
|
82
194
|
end
|
|
83
195
|
res
|
|
196
|
+
end
|
|
197
|
+
ensure
|
|
198
|
+
@webmock_performing_easy = false
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def webmock_performing_easy?
|
|
202
|
+
@webmock_performing_easy
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def webmock_record_multi_request(signature)
|
|
206
|
+
@webmock_multi_request = signature
|
|
207
|
+
on_complete(&@on_complete)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def webmock_clear_multi_request
|
|
211
|
+
@webmock_multi_request = nil
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
%w[body header].each do |part|
|
|
215
|
+
define_method("on_#{part}") do |&block|
|
|
216
|
+
instance_variable_set("@on_#{part}", block)
|
|
217
|
+
if block
|
|
218
|
+
super() do |data|
|
|
219
|
+
if @webmock_multi_request
|
|
220
|
+
buffer = instance_variable_get("@webmock_#{part}") || +""
|
|
221
|
+
buffer << data
|
|
222
|
+
instance_variable_set("@webmock_#{part}", buffer)
|
|
223
|
+
end
|
|
224
|
+
block.call(data)
|
|
225
|
+
end
|
|
226
|
+
else
|
|
227
|
+
super(&block)
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def on_complete(&block)
|
|
233
|
+
@on_complete = block
|
|
234
|
+
super do |easy|
|
|
235
|
+
signature = @webmock_multi_request
|
|
236
|
+
webmock_restore_multi_url
|
|
237
|
+
webmock_clear_multi_request
|
|
238
|
+
if signature && WebMock::CallbackRegistry.any_callbacks?
|
|
239
|
+
WebMock::CallbackRegistry.invoke_callbacks(
|
|
240
|
+
{lib: :curb, real_request: true}, signature, build_webmock_response)
|
|
241
|
+
end
|
|
242
|
+
block.call(easy) if block
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def webmock_follow_multi_redirect(signature, response)
|
|
247
|
+
location = response.headers && response.headers['Location']
|
|
248
|
+
return false unless follow_location? && (300..399).include?(response.status[0]) && location
|
|
249
|
+
return false if response.should_timeout || response.exception
|
|
250
|
+
|
|
251
|
+
@webmock_multi_redirects ||= 0
|
|
252
|
+
limit = max_redirects || 50
|
|
253
|
+
if limit >= 0 && @webmock_multi_redirects >= limit
|
|
254
|
+
@webmock_last_result = 47 # CURLE_TOO_MANY_REDIRECTS
|
|
255
|
+
return false
|
|
256
|
+
end
|
|
257
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :curb}, signature, response)
|
|
258
|
+
@webmock_multi_original_url ||= url
|
|
259
|
+
@webmock_multi_redirects += 1
|
|
260
|
+
self.url = WebMock::Util::URI.heuristic_parse(url).join(location).to_s
|
|
261
|
+
true
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
def webmock_restore_multi_url
|
|
265
|
+
return unless @webmock_multi_original_url
|
|
266
|
+
@last_effective_url = last_effective_url
|
|
267
|
+
self.url = @webmock_multi_original_url
|
|
268
|
+
@webmock_multi_original_url = @webmock_multi_redirects = nil
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def webmock_complete_multi_request(signature, response)
|
|
272
|
+
if response.should_timeout
|
|
273
|
+
@body_str = ""
|
|
274
|
+
@header_str = ""
|
|
275
|
+
@response_code = 0
|
|
276
|
+
@webmock_last_result = 28 # CURLE_OPERATION_TIMEDOUT
|
|
84
277
|
else
|
|
85
|
-
|
|
278
|
+
build_curb_response(response, follow_redirects: false)
|
|
279
|
+
@webmock_last_result ||= 0
|
|
86
280
|
end
|
|
281
|
+
webmock_restore_multi_url
|
|
282
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :curb}, signature, response)
|
|
283
|
+
invoke_curb_callbacks(multi: true)
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def last_result
|
|
287
|
+
@webmock_last_result || super
|
|
87
288
|
end
|
|
88
289
|
|
|
290
|
+
def clear_webmock_response
|
|
291
|
+
@body_str = @header_str = @response_code = @content_type = nil
|
|
292
|
+
@last_effective_url = @transfer_encoding = @webmock_last_result = nil
|
|
293
|
+
@webmock_body = @webmock_header = nil
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def signature_for_history(signature)
|
|
297
|
+
body = signature.body
|
|
298
|
+
return signature unless body.is_a?(Curl::PostField) || (body.is_a?(Array) && multipart_form_post?)
|
|
299
|
+
signature.with_body_values { |field| MultipartField.from(field) }
|
|
300
|
+
end
|
|
301
|
+
private :signature_for_history
|
|
302
|
+
|
|
89
303
|
def build_request_signature
|
|
90
304
|
method = @webmock_method.to_s.downcase.to_sym
|
|
91
305
|
|
|
@@ -98,12 +312,16 @@ if defined?(Curl)
|
|
|
98
312
|
when :post, :patch
|
|
99
313
|
self.post_body || @post_body
|
|
100
314
|
when :put
|
|
101
|
-
@put_data
|
|
315
|
+
if @put_data.respond_to?(:read)
|
|
316
|
+
WebMock::Util::UploadBody.read(@put_data, headers)
|
|
317
|
+
else
|
|
318
|
+
@put_data
|
|
319
|
+
end
|
|
102
320
|
else
|
|
103
321
|
nil
|
|
104
322
|
end
|
|
105
323
|
|
|
106
|
-
if
|
|
324
|
+
if @on_debug
|
|
107
325
|
@on_debug.call("Trying 127.0.0.1...\r\n", 0)
|
|
108
326
|
@on_debug.call('Connected to ' + uri.hostname + "\r\n", 0)
|
|
109
327
|
@debug_method = method.upcase
|
|
@@ -153,7 +371,7 @@ if defined?(Curl)
|
|
|
153
371
|
end
|
|
154
372
|
end
|
|
155
373
|
|
|
156
|
-
def build_curb_response(webmock_response)
|
|
374
|
+
def build_curb_response(webmock_response, follow_redirects: true)
|
|
157
375
|
raise Curl::Err::TimeoutError if webmock_response.should_timeout
|
|
158
376
|
webmock_response.raise_error_if_any
|
|
159
377
|
|
|
@@ -162,18 +380,22 @@ if defined?(Curl)
|
|
|
162
380
|
|
|
163
381
|
@header_str = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}\r\n".dup
|
|
164
382
|
|
|
165
|
-
@on_debug.call(@header_str, 1) if
|
|
383
|
+
@on_debug.call(@header_str, 1) if @on_debug
|
|
166
384
|
|
|
167
385
|
if webmock_response.headers
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
386
|
+
# Each cookie gets a line of its own, as servers send them.
|
|
387
|
+
@header_str << webmock_response.headers.flat_map do |k,v|
|
|
388
|
+
values = k == "Set-Cookie" ? Array(v) : [v.is_a?(Array) ? v.join(", ") : v]
|
|
389
|
+
values.map do |value|
|
|
390
|
+
header = "#{k}: #{value}"
|
|
391
|
+
@on_debug.call(header + "\r\n", 1) if @on_debug
|
|
392
|
+
header
|
|
393
|
+
end
|
|
172
394
|
end.join("\r\n")
|
|
173
|
-
@on_debug.call("\r\n", 1) if
|
|
395
|
+
@on_debug.call("\r\n", 1) if @on_debug
|
|
174
396
|
|
|
175
397
|
location = webmock_response.headers['Location']
|
|
176
|
-
if self.follow_location? && location
|
|
398
|
+
if follow_redirects && self.follow_location? && location
|
|
177
399
|
@last_effective_url = location
|
|
178
400
|
webmock_follow_location(location)
|
|
179
401
|
end
|
|
@@ -196,10 +418,10 @@ if defined?(Curl)
|
|
|
196
418
|
self.url = first_url
|
|
197
419
|
end
|
|
198
420
|
|
|
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
|
|
421
|
+
def invoke_curb_callbacks(multi: false)
|
|
422
|
+
@on_progress.call(0.0,1.0,0.0,1.0) if @on_progress
|
|
423
|
+
self.header_str.lines.each { |header_line| @on_header.call header_line } if @on_header
|
|
424
|
+
if @on_body
|
|
203
425
|
if chunked_response?
|
|
204
426
|
self.body_str.each do |chunk|
|
|
205
427
|
@on_body.call(chunk)
|
|
@@ -208,15 +430,24 @@ if defined?(Curl)
|
|
|
208
430
|
@on_body.call(self.body_str)
|
|
209
431
|
end
|
|
210
432
|
end
|
|
211
|
-
|
|
433
|
+
completed_code = response_code
|
|
434
|
+
completed_result = last_result if multi
|
|
435
|
+
@on_complete.call(self) if @on_complete
|
|
436
|
+
|
|
437
|
+
if multi && completed_result != 0
|
|
438
|
+
@on_failure.call(self, Curl::Easy.error(completed_result)) if @on_failure
|
|
439
|
+
return
|
|
440
|
+
end
|
|
212
441
|
|
|
213
|
-
case
|
|
442
|
+
case completed_code
|
|
214
443
|
when 200..299
|
|
215
|
-
@on_success.call(self) if
|
|
444
|
+
@on_success.call(self) if @on_success
|
|
445
|
+
when 300..399
|
|
446
|
+
@on_redirect.call(self, Curl::Easy.error(0)) if multi && @on_redirect && max_redirects != 0
|
|
216
447
|
when 400..499
|
|
217
|
-
@on_missing.call(self, self.response_code) if
|
|
448
|
+
@on_missing.call(self, multi ? Curl::Easy.error(0) : self.response_code) if @on_missing
|
|
218
449
|
when 500..599
|
|
219
|
-
@on_failure.call(self, self.response_code) if
|
|
450
|
+
@on_failure.call(self, multi ? Curl::Easy.error(0) : self.response_code) if @on_failure
|
|
220
451
|
end
|
|
221
452
|
end
|
|
222
453
|
|
|
@@ -226,9 +457,9 @@ if defined?(Curl)
|
|
|
226
457
|
|
|
227
458
|
def build_webmock_response
|
|
228
459
|
status, headers =
|
|
229
|
-
WebMock::HttpLibAdapters::CurbAdapter.parse_header_string(self.header_str)
|
|
460
|
+
WebMock::HttpLibAdapters::CurbAdapter.parse_header_string((@webmock_header || self.header_str).to_s)
|
|
230
461
|
|
|
231
|
-
if
|
|
462
|
+
if @on_debug
|
|
232
463
|
http_response = ["HTTP/1.0 #{@debug_method} #{@debug_path}"]
|
|
233
464
|
headers.each do |name, value|
|
|
234
465
|
http_response << "#{name}: #{value}"
|
|
@@ -240,7 +471,7 @@ if defined?(Curl)
|
|
|
240
471
|
|
|
241
472
|
webmock_response = WebMock::Response.new
|
|
242
473
|
webmock_response.status = [self.response_code, status]
|
|
243
|
-
webmock_response.body = self.body_str
|
|
474
|
+
webmock_response.body = @webmock_body || self.body_str
|
|
244
475
|
webmock_response.headers = headers
|
|
245
476
|
webmock_response
|
|
246
477
|
end
|
|
@@ -270,10 +501,19 @@ if defined?(Curl)
|
|
|
270
501
|
end
|
|
271
502
|
end
|
|
272
503
|
|
|
273
|
-
def http_put
|
|
504
|
+
def http_put(data = nil)
|
|
505
|
+
# put_data= can supply the upcoming request's body until perform resets the method.
|
|
506
|
+
data = @put_data if data.nil? && @webmock_method == :put
|
|
274
507
|
@webmock_method = :put
|
|
275
|
-
@put_data = data
|
|
276
|
-
super
|
|
508
|
+
@put_data = data
|
|
509
|
+
return super('') if data.nil?
|
|
510
|
+
return super(data) if data.is_a?(String) || data.respond_to?(:read) || multipart_form_post?
|
|
511
|
+
|
|
512
|
+
# Curb joins other positional arguments as it would form fields.
|
|
513
|
+
@put_data = [data].join('&')
|
|
514
|
+
super(@put_data)
|
|
515
|
+
ensure
|
|
516
|
+
@put_data = nil
|
|
277
517
|
end
|
|
278
518
|
alias put http_put
|
|
279
519
|
|
|
@@ -293,10 +533,12 @@ if defined?(Curl)
|
|
|
293
533
|
@post_body = nil
|
|
294
534
|
end
|
|
295
535
|
|
|
296
|
-
def put_data=
|
|
536
|
+
def put_data=(data)
|
|
537
|
+
body = data.respond_to?(:read) ? data : data&.to_s
|
|
538
|
+
super(body)
|
|
297
539
|
@webmock_method = :put
|
|
298
|
-
@put_data =
|
|
299
|
-
|
|
540
|
+
@put_data = body
|
|
541
|
+
data
|
|
300
542
|
end
|
|
301
543
|
|
|
302
544
|
def post_body= data
|
|
@@ -345,7 +587,7 @@ if defined?(Curl)
|
|
|
345
587
|
@content_type ||= super
|
|
346
588
|
end
|
|
347
589
|
|
|
348
|
-
%w[ success failure missing
|
|
590
|
+
%w[ success failure missing redirect progress debug ].each do |callback|
|
|
349
591
|
class_eval <<-METHOD, __FILE__, __LINE__
|
|
350
592
|
def on_#{callback} &block
|
|
351
593
|
@on_#{callback} = block
|
|
@@ -359,11 +601,15 @@ if defined?(Curl)
|
|
|
359
601
|
end
|
|
360
602
|
|
|
361
603
|
def reset
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
604
|
+
@put_data = nil
|
|
605
|
+
clear_webmock_response
|
|
606
|
+
webmock_clear_multi_request
|
|
607
|
+
reset_webmock_method
|
|
608
|
+
@webmock_multi_original_url = @webmock_multi_redirects = nil
|
|
609
|
+
# Curb's reset drops every callback, so a stubbed request, or a real one
|
|
610
|
+
# WebMock records, doesn't run the ones set before it either.
|
|
611
|
+
@on_success = @on_failure = @on_missing = @on_redirect = @on_complete = nil
|
|
612
|
+
@on_body = @on_header = @on_progress = @on_debug = nil
|
|
367
613
|
super
|
|
368
614
|
end
|
|
369
615
|
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
|
|
@@ -7,6 +7,9 @@ module HTTP
|
|
|
7
7
|
|
|
8
8
|
webmock_response.status = [status.to_i, reason]
|
|
9
9
|
|
|
10
|
+
# The caller gets the body before it is read, to see what it reads from.
|
|
11
|
+
body = self.body
|
|
12
|
+
yield body if block_given?
|
|
10
13
|
webmock_response.body = body.to_s
|
|
11
14
|
# This call is used to reset the body of the response to enable it to be streamed if necessary.
|
|
12
15
|
# The `body.to_s` call above reads the body, which allows WebMock to trigger any registered callbacks.
|