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
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'stringio'
|
|
4
|
+
|
|
3
5
|
begin
|
|
4
6
|
require 'patron'
|
|
5
7
|
rescue LoadError
|
|
@@ -14,35 +16,142 @@ if defined?(::Patron::Session)
|
|
|
14
16
|
|
|
15
17
|
OriginalPatronSession = ::Patron::Session unless const_defined?(:OriginalPatronSession)
|
|
16
18
|
|
|
19
|
+
# Patron 1.0 moved Response.new to keyword arguments, see https://github.com/toland/patron/issues/209
|
|
20
|
+
PATRON_RESPONSE_TAKES_KEYWORDS =
|
|
21
|
+
::Patron::Response.instance_method(:initialize).parameters.include?([:keyreq, :url])
|
|
22
|
+
|
|
17
23
|
class WebMockPatronSession < ::Patron::Session
|
|
18
24
|
def handle_request(req)
|
|
19
|
-
request_signature =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
WebMock::HttpLibAdapters::PatronAdapter.
|
|
26
|
-
handle_file_name(req, webmock_response)
|
|
27
|
-
res = WebMock::HttpLibAdapters::PatronAdapter.
|
|
28
|
-
build_patron_response(webmock_response, default_response_charset)
|
|
29
|
-
WebMock::CallbackRegistry.invoke_callbacks(
|
|
30
|
-
{lib: :patron}, request_signature, webmock_response)
|
|
25
|
+
request_signature, webmock_response = PatronAdapter.register_request(req)
|
|
26
|
+
|
|
27
|
+
if webmock_response
|
|
28
|
+
PatronAdapter.handle_file_name(req, webmock_response)
|
|
29
|
+
res = PatronAdapter.build_patron_response(webmock_response, default_response_charset)
|
|
30
|
+
PatronAdapter.invoke_callbacks(request_signature, webmock_response)
|
|
31
31
|
res
|
|
32
|
-
|
|
32
|
+
else
|
|
33
33
|
res = super
|
|
34
|
-
|
|
35
|
-
webmock_response = WebMock::HttpLibAdapters::PatronAdapter.
|
|
36
|
-
build_webmock_response(res)
|
|
37
|
-
WebMock::CallbackRegistry.invoke_callbacks(
|
|
38
|
-
{lib: :patron, real_request: true}, request_signature,
|
|
39
|
-
webmock_response)
|
|
40
|
-
end
|
|
34
|
+
PatronAdapter.invoke_real_request_callbacks(request_signature, res)
|
|
41
35
|
res
|
|
42
|
-
else
|
|
43
|
-
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
44
36
|
end
|
|
45
37
|
end
|
|
38
|
+
|
|
39
|
+
# Patron 1.1 sends #get_file and streamed requests past
|
|
40
|
+
# #handle_request
|
|
41
|
+
if ::Patron::Session.public_method_defined?(:stream)
|
|
42
|
+
def stream(action, url, headers = {}, **options, &block)
|
|
43
|
+
return super unless block
|
|
44
|
+
|
|
45
|
+
req = build_request(action, url, headers, **options)
|
|
46
|
+
request_signature, webmock_response, recorded_body = PatronAdapter.register_request(req)
|
|
47
|
+
|
|
48
|
+
if webmock_response
|
|
49
|
+
PatronAdapter.stream_stubbed_response(webmock_response, request_signature,
|
|
50
|
+
default_response_charset, recorded_body.to_s.bytesize, &block)
|
|
51
|
+
else
|
|
52
|
+
# Callbacks run once the head has arrived, so they also run
|
|
53
|
+
# when the block leaves before the end of the body
|
|
54
|
+
super(action, url, headers, **options) do |stream|
|
|
55
|
+
PatronAdapter.invoke_real_request_callbacks(request_signature, stream)
|
|
56
|
+
yield stream
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
# Called by #get_file, and by #request for a GET with a file
|
|
64
|
+
def download(req, path)
|
|
65
|
+
request_signature, webmock_response = PatronAdapter.register_request(req)
|
|
66
|
+
|
|
67
|
+
if webmock_response
|
|
68
|
+
# Like Patron, with the body in the file only
|
|
69
|
+
res = PatronAdapter.build_patron_response(webmock_response, default_response_charset, with_body: false)
|
|
70
|
+
File.open(path, "wb") { |f| f.write(webmock_response.body) }
|
|
71
|
+
PatronAdapter.invoke_callbacks(request_signature, webmock_response)
|
|
72
|
+
res
|
|
73
|
+
else
|
|
74
|
+
res = super
|
|
75
|
+
PatronAdapter.invoke_real_request_callbacks(request_signature, res)
|
|
76
|
+
res
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Stands in for the Patron::ResponseStream that Patron::Session#stream
|
|
83
|
+
# yields, reading a stubbed body
|
|
84
|
+
class StubbedResponseStream
|
|
85
|
+
CHUNK_SIZE = 64 * 1024
|
|
86
|
+
|
|
87
|
+
HEAD_READERS = [:url, :status, :status_line, :redirect_count, :headers,
|
|
88
|
+
:charset, :ok?, :error?, :content_length, :content_type].freeze
|
|
89
|
+
|
|
90
|
+
HEAD_READERS.each do |name|
|
|
91
|
+
define_method(name) { @head.public_send(name) }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def initialize(head, body)
|
|
95
|
+
@head = head
|
|
96
|
+
@body = body
|
|
97
|
+
@closed = false
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def read(length = nil, outbuf = nil)
|
|
101
|
+
ensure_open!
|
|
102
|
+
@body.read(length, outbuf)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def readpartial(maxlen, outbuf = nil)
|
|
106
|
+
ensure_open!
|
|
107
|
+
@body.readpartial(maxlen, outbuf)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def each
|
|
111
|
+
return enum_for(:each) unless block_given?
|
|
112
|
+
ensure_open!
|
|
113
|
+
while (chunk = @body.read(CHUNK_SIZE))
|
|
114
|
+
yield chunk
|
|
115
|
+
end
|
|
116
|
+
self
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def each_line(&block)
|
|
120
|
+
return enum_for(:each_line) unless block
|
|
121
|
+
ensure_open!
|
|
122
|
+
@body.each_line(&block)
|
|
123
|
+
self
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def drain!
|
|
127
|
+
ensure_open!
|
|
128
|
+
@body.read.bytesize
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def eof?
|
|
132
|
+
ensure_open!
|
|
133
|
+
@body.eof?
|
|
134
|
+
end
|
|
135
|
+
alias eof eof?
|
|
136
|
+
|
|
137
|
+
def close
|
|
138
|
+
@closed = true
|
|
139
|
+
nil
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def closed?
|
|
143
|
+
@closed
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def inspect
|
|
147
|
+
"#<#{self.class.name} @status_line='#{status_line}'#{' (closed)' if @closed}>"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
private
|
|
151
|
+
|
|
152
|
+
def ensure_open!
|
|
153
|
+
raise IOError, "closed stream" if @closed
|
|
154
|
+
end
|
|
46
155
|
end
|
|
47
156
|
|
|
48
157
|
def self.enable!
|
|
@@ -72,19 +181,36 @@ if defined?(::Patron::Session)
|
|
|
72
181
|
uri.path = uri.normalized_path.gsub("[^:]//","/")
|
|
73
182
|
|
|
74
183
|
if [:put, :post, :patch].include?(req.action)
|
|
75
|
-
|
|
76
|
-
|
|
184
|
+
data = req.upload_data
|
|
185
|
+
path = req.file_name || (data.to_path if data.respond_to?(:to_path))
|
|
186
|
+
if path
|
|
187
|
+
if !File.exist?(path) || !File.readable?(path)
|
|
77
188
|
raise ArgumentError.new("Unable to open specified file.")
|
|
78
189
|
end
|
|
79
|
-
request_body = File.
|
|
80
|
-
|
|
81
|
-
|
|
190
|
+
request_body = if !File.file?(path)
|
|
191
|
+
# Leave a named pipe or device for Patron to open.
|
|
192
|
+
path.to_s
|
|
193
|
+
elsif req.file_name
|
|
194
|
+
# Keep the encoding used by existing post_file and put_file stubs.
|
|
195
|
+
File.read(path)
|
|
196
|
+
else
|
|
197
|
+
File.binread(path)
|
|
198
|
+
end
|
|
199
|
+
elsif data.respond_to?(:read) && OriginalPatronSession.public_method_defined?(:stream)
|
|
200
|
+
# Patron 1.1 added streaming uploads and Session#stream together.
|
|
201
|
+
request_body = WebMock::Util::UploadBody.read(data, req.headers)
|
|
202
|
+
elsif data
|
|
203
|
+
# A multipart post keeps its form fields as a Hash. The copy
|
|
204
|
+
# lets stubs and callbacks still receive the caller's Hash.
|
|
205
|
+
request_body = data.is_a?(Hash) ? data.dup : data.to_s
|
|
82
206
|
else
|
|
83
207
|
raise ArgumentError.new("Must provide either data or a filename when doing a PUT or POST")
|
|
84
208
|
end
|
|
85
209
|
end
|
|
86
210
|
|
|
87
|
-
|
|
211
|
+
# A copy, so the Authorization header added below for matching is
|
|
212
|
+
# not sent with a real request
|
|
213
|
+
headers = req.headers.dup
|
|
88
214
|
|
|
89
215
|
if req.credentials
|
|
90
216
|
headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(req.credentials)
|
|
@@ -99,30 +225,106 @@ if defined?(::Patron::Session)
|
|
|
99
225
|
request_signature
|
|
100
226
|
end
|
|
101
227
|
|
|
102
|
-
def self.build_patron_response(webmock_response, default_response_charset)
|
|
228
|
+
def self.build_patron_response(webmock_response, default_response_charset, with_body: true)
|
|
103
229
|
raise ::Patron::TimeoutError if webmock_response.should_timeout
|
|
104
230
|
webmock_response.raise_error_if_any
|
|
105
231
|
|
|
232
|
+
header_data = build_header_data(webmock_response)
|
|
233
|
+
|
|
234
|
+
if PATRON_RESPONSE_TAKES_KEYWORDS
|
|
235
|
+
::Patron::Response.new(
|
|
236
|
+
url: "".dup,
|
|
237
|
+
status: webmock_response.status[0],
|
|
238
|
+
redirect_count: 0,
|
|
239
|
+
header_data: header_data,
|
|
240
|
+
body: with_body ? webmock_response.body.dup : nil,
|
|
241
|
+
default_charset: default_response_charset
|
|
242
|
+
)
|
|
243
|
+
else
|
|
244
|
+
::Patron::Response.new(
|
|
245
|
+
"".dup,
|
|
246
|
+
webmock_response.status[0],
|
|
247
|
+
0,
|
|
248
|
+
header_data,
|
|
249
|
+
webmock_response.body.dup,
|
|
250
|
+
default_response_charset
|
|
251
|
+
)
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# Records the request and returns its signature with the stubbed
|
|
256
|
+
# response, which is nil when a real request is allowed
|
|
257
|
+
def self.register_request(req)
|
|
258
|
+
recorded = build_request_signature(req)
|
|
259
|
+
request_signature = recorded
|
|
260
|
+
if recorded.body && !req.file_name && req.upload_data && !req.upload_data.equal?(recorded.body)
|
|
261
|
+
request_signature = recorded.with_upload_body(req.upload_data)
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(
|
|
265
|
+
request_signature, record: recorded)
|
|
266
|
+
if webmock_response.nil? && !WebMock.net_connect_allowed?(request_signature.uri)
|
|
267
|
+
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
[request_signature, webmock_response, recorded.body]
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def self.invoke_callbacks(request_signature, webmock_response)
|
|
274
|
+
WebMock::CallbackRegistry.invoke_callbacks(
|
|
275
|
+
{lib: :patron}, request_signature, webmock_response)
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def self.invoke_real_request_callbacks(request_signature, patron_response)
|
|
279
|
+
return unless WebMock::CallbackRegistry.any_callbacks?
|
|
280
|
+
|
|
281
|
+
WebMock::CallbackRegistry.invoke_callbacks(
|
|
282
|
+
{lib: :patron, real_request: true}, request_signature,
|
|
283
|
+
build_webmock_response(patron_response))
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
# Callbacks run before the block, so they also run when it leaves
|
|
287
|
+
# before the end of the body
|
|
288
|
+
def self.stream_stubbed_response(webmock_response, request_signature, default_response_charset, uploaded_bytes)
|
|
289
|
+
head = build_patron_response(webmock_response, default_response_charset, with_body: false)
|
|
290
|
+
invoke_callbacks(request_signature, webmock_response)
|
|
291
|
+
# Kept out of the stream, so the stream has only the methods of
|
|
292
|
+
# Patron::ResponseStream
|
|
293
|
+
body = StringIO.new(webmock_response.body.to_s.b)
|
|
294
|
+
stream = StubbedResponseStream.new(head, body)
|
|
295
|
+
begin
|
|
296
|
+
yield stream
|
|
297
|
+
ensure
|
|
298
|
+
stream.close
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
::Patron::TransferStats.new(
|
|
302
|
+
url: head.url,
|
|
303
|
+
status: head.status,
|
|
304
|
+
redirect_count: head.redirect_count,
|
|
305
|
+
header_data: build_header_data(webmock_response),
|
|
306
|
+
downloaded_bytes: body.pos,
|
|
307
|
+
uploaded_bytes: uploaded_bytes,
|
|
308
|
+
timings: {},
|
|
309
|
+
complete: body.eof?,
|
|
310
|
+
default_charset: default_response_charset
|
|
311
|
+
)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def self.build_header_data(webmock_response)
|
|
106
315
|
header_fields = (webmock_response.headers || []).map { |(k, vs)| Array(vs).map { |v| "#{k}: #{v}" } }.flatten
|
|
107
316
|
status_line = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}"
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
::Patron::Response.new(
|
|
111
|
-
"".dup,
|
|
112
|
-
webmock_response.status[0],
|
|
113
|
-
0,
|
|
114
|
-
header_data,
|
|
115
|
-
webmock_response.body.dup,
|
|
116
|
-
default_response_charset
|
|
117
|
-
)
|
|
317
|
+
([status_line] + header_fields).join("\r\n")
|
|
118
318
|
end
|
|
119
319
|
|
|
320
|
+
# Takes a Patron::Response, or the Patron::ResponseStream of a streamed
|
|
321
|
+
# request, whose body is not read here
|
|
120
322
|
def self.build_webmock_response(patron_response)
|
|
121
323
|
webmock_response = WebMock::Response.new
|
|
122
324
|
reason = patron_response.status_line.
|
|
123
325
|
scan(%r(\AHTTP/(\d+(?:\.\d+)?)\s+(\d\d\d)\s*([^\r\n]+)?))[0][2]
|
|
124
326
|
webmock_response.status = [patron_response.status, reason]
|
|
125
|
-
webmock_response.body = patron_response.body
|
|
327
|
+
webmock_response.body = patron_response.body if patron_response.respond_to?(:body)
|
|
126
328
|
webmock_response.headers = patron_response.headers
|
|
127
329
|
webmock_response
|
|
128
330
|
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
begin
|
|
4
4
|
require 'typhoeus'
|
|
5
|
+
require 'pathname'
|
|
5
6
|
rescue LoadError
|
|
6
7
|
# typhoeus not found
|
|
7
8
|
end
|
|
@@ -62,12 +63,19 @@ if defined?(Typhoeus)
|
|
|
62
63
|
headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(req.options[:userpwd])
|
|
63
64
|
end
|
|
64
65
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
# libcurl sends the cookie option as a Cookie header, unless the
|
|
67
|
+
# request already has one. With libcurl's cookie engine on, it adds
|
|
68
|
+
# the cookies it keeps to that header, which can't be known here.
|
|
69
|
+
cookie = req.options[:cookie]
|
|
70
|
+
cookie_engine = %i[cookiefile cookiejar cookielist].any? { |option| req.options[option] }
|
|
71
|
+
if cookie && !cookie_engine && (headers || {}).keys.none? { |name| name.to_s.casecmp?('Cookie') }
|
|
72
|
+
headers = (headers || {}).merge('Cookie' => cookie)
|
|
69
73
|
end
|
|
70
74
|
|
|
75
|
+
body = req.options[:body]
|
|
76
|
+
form = body.is_a?(Hash)
|
|
77
|
+
body = WebMock::Util::QueryMapper.values_to_query(body) if form
|
|
78
|
+
|
|
71
79
|
request_signature = WebMock::RequestSignature.new(
|
|
72
80
|
req.options[:method] || :get,
|
|
73
81
|
uri.to_s,
|
|
@@ -75,11 +83,53 @@ if defined?(Typhoeus)
|
|
|
75
83
|
headers: headers
|
|
76
84
|
)
|
|
77
85
|
|
|
86
|
+
add_form_content_type(request_signature, req) if form
|
|
78
87
|
req.instance_variable_set(:@__webmock_request_signature, request_signature)
|
|
79
88
|
|
|
80
89
|
request_signature
|
|
81
90
|
end
|
|
82
91
|
|
|
92
|
+
# libcurl adds this header when Ethon sends a Hash body as a URL
|
|
93
|
+
# encoded form: for a POST, even with an empty form, and for other
|
|
94
|
+
# methods with a form that isn't empty, except PUT, which Ethon
|
|
95
|
+
# uploads, and HEAD, which sends no body. A multipart form, with a
|
|
96
|
+
# file or the multipart option, is sent with a boundary instead, which
|
|
97
|
+
# isn't recorded. Ethon's own form decides whether a form is empty or
|
|
98
|
+
# multipart, since that differs between Ethon versions. A Content-Type
|
|
99
|
+
# given as content_type counts as set, since WebMock reads header
|
|
100
|
+
# names that way, though libcurl then sends both headers. With the
|
|
101
|
+
# nobody option, or when Ethon can't read the form, such as for a
|
|
102
|
+
# file without a path, no header is recorded, as before.
|
|
103
|
+
def self.add_form_content_type(signature, req)
|
|
104
|
+
headers = signature.headers || {}
|
|
105
|
+
return if headers.key?('Content-Type') || req.options[:nobody] || !defined?(::Ethon::Easy::Form)
|
|
106
|
+
|
|
107
|
+
form = ::Ethon::Easy::Form.new(nil, req.options[:body], req.options[:multipart])
|
|
108
|
+
if form.empty?
|
|
109
|
+
return unless signature.method == :post
|
|
110
|
+
else
|
|
111
|
+
return if [:put, :head].include?(signature.method)
|
|
112
|
+
form.params_encoding = req.options.fetch(:params_encoding, :typhoeus)
|
|
113
|
+
return if form.multipart?
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
signature.headers = headers.merge('Content-Type' => 'application/x-www-form-urlencoded')
|
|
117
|
+
rescue StandardError
|
|
118
|
+
nil
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def self.signature_for_history(signature)
|
|
122
|
+
return signature unless signature.body.is_a?(Array)
|
|
123
|
+
|
|
124
|
+
# Ethon uploads file parts by path and stringifies non-scalar form values.
|
|
125
|
+
signature.with_body_values do |value|
|
|
126
|
+
case value
|
|
127
|
+
when File, Tempfile then Pathname.new(value.path)
|
|
128
|
+
when String, Numeric, Symbol, TrueClass, FalseClass, NilClass then value
|
|
129
|
+
else value.to_s
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
83
133
|
|
|
84
134
|
def self.build_webmock_response(typhoeus_response)
|
|
85
135
|
webmock_response = WebMock::Response.new
|
|
@@ -163,7 +213,12 @@ if defined?(Typhoeus)
|
|
|
163
213
|
|
|
164
214
|
AFTER_REQUEST_CALLBACK = Proc.new do |response|
|
|
165
215
|
request = response.request
|
|
166
|
-
|
|
216
|
+
# Another before callback, such as VCR's Typhoeus hook, can answer a
|
|
217
|
+
# request before WebMock's runs, and then WebMock has nothing to
|
|
218
|
+
# report. The signature is taken once, so a request that runs again
|
|
219
|
+
# isn't reported with the one from its earlier run.
|
|
220
|
+
next unless request.instance_variable_defined?(:@__webmock_request_signature)
|
|
221
|
+
request_signature = request.remove_instance_variable(:@__webmock_request_signature)
|
|
167
222
|
webmock_response =
|
|
168
223
|
::WebMock::HttpLibAdapters::TyphoeusAdapter.
|
|
169
224
|
build_webmock_response(response)
|
|
@@ -192,9 +247,9 @@ if defined?(Typhoeus)
|
|
|
192
247
|
request_signature = ::WebMock::HttpLibAdapters::TyphoeusAdapter.build_request_signature(request, url)
|
|
193
248
|
request.block_connection = false;
|
|
194
249
|
|
|
195
|
-
::WebMock::
|
|
250
|
+
recorded = ::WebMock::HttpLibAdapters::TyphoeusAdapter.signature_for_history(request_signature)
|
|
196
251
|
|
|
197
|
-
if webmock_response = ::WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
252
|
+
if webmock_response = ::WebMock::StubRegistry.instance.response_for_request(request_signature, record: recorded)
|
|
198
253
|
response = ::WebMock::HttpLibAdapters::TyphoeusAdapter.generate_typhoeus_response(webmock_response, url)
|
|
199
254
|
if request.respond_to?(:on_headers)
|
|
200
255
|
request.execute_headers_callbacks(response)
|
|
@@ -207,6 +262,9 @@ if defined?(Typhoeus)
|
|
|
207
262
|
webmock_response.raise_error_if_any
|
|
208
263
|
res = false
|
|
209
264
|
elsif !WebMock.net_connect_allowed?(request_signature.uri)
|
|
265
|
+
# The request doesn't complete, so the callback that takes the
|
|
266
|
+
# signature off it doesn't run.
|
|
267
|
+
request.remove_instance_variable(:@__webmock_request_signature)
|
|
210
268
|
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
211
269
|
end
|
|
212
270
|
end
|
|
@@ -54,11 +54,14 @@ module WebMock
|
|
|
54
54
|
return false unless expected.is_a?(String) && actual.is_a?(String) && expected != actual
|
|
55
55
|
return false unless request_signature.json_headers?
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
# The advice is to declare the body as JSON.parse(json_string), so
|
|
58
|
+
# the stub's string must parse with JSON.parse itself. The request
|
|
59
|
+
# body is read as WebMock reads it for matching.
|
|
60
|
+
expected_json = parse_json { ::JSON.parse(expected) }
|
|
58
61
|
return false unless expected_json.is_a?(Hash) || expected_json.is_a?(Array)
|
|
59
62
|
|
|
60
63
|
# eql? compares nested values strictly, so 1 and 1.0 are different.
|
|
61
|
-
expected_json.eql?(parse_json(actual))
|
|
64
|
+
expected_json.eql?(parse_json { WebMock::Util::Parsers::JSON.parse(actual) })
|
|
62
65
|
end
|
|
63
66
|
|
|
64
67
|
def self.matches_except_body?(request_pattern, request_signature)
|
|
@@ -67,9 +70,9 @@ module WebMock
|
|
|
67
70
|
(request_pattern.headers_pattern.nil? || request_pattern.headers_pattern.matches?(request_signature.headers))
|
|
68
71
|
end
|
|
69
72
|
|
|
70
|
-
def self.parse_json
|
|
71
|
-
|
|
72
|
-
rescue ::JSON::JSONError, EncodingError
|
|
73
|
+
def self.parse_json
|
|
74
|
+
yield
|
|
75
|
+
rescue ::JSON::JSONError, EncodingError, WebMock::Util::Parsers::ParseError
|
|
73
76
|
nil
|
|
74
77
|
end
|
|
75
78
|
private_class_method :parts_of, :body_differs_only_in_formatting?, :matches_except_body?, :parse_json
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "hashdiff"
|
|
4
|
-
require "json"
|
|
5
4
|
|
|
6
5
|
module WebMock
|
|
7
6
|
class RequestBodyDiff
|
|
@@ -14,7 +13,17 @@ module WebMock
|
|
|
14
13
|
def body_diff
|
|
15
14
|
return {} unless request_signature_diffable? && request_stub_diffable?
|
|
16
15
|
|
|
17
|
-
Hashdiff.diff(request_signature_body_hash, request_stub_body_hash)
|
|
16
|
+
Hashdiff.diff(request_signature_body_hash, request_stub_body_hash) do |_path, actual, expected|
|
|
17
|
+
# A regexp that matches the request's value is not a difference.
|
|
18
|
+
# Anything else, including matchers and lambdas, which building a
|
|
19
|
+
# message must not run, is left to Hashdiff by returning nil.
|
|
20
|
+
begin
|
|
21
|
+
true if expected.is_a?(Regexp) && actual.is_a?(String) && expected.match?(actual)
|
|
22
|
+
rescue EncodingError
|
|
23
|
+
# A regexp and a value in incompatible encodings don't match.
|
|
24
|
+
nil
|
|
25
|
+
end
|
|
26
|
+
end
|
|
18
27
|
end
|
|
19
28
|
|
|
20
29
|
attr_reader :request_signature, :request_stub
|
|
@@ -30,14 +39,15 @@ module WebMock
|
|
|
30
39
|
request_stub_body.is_a?(Hash) || request_stub_parseable_json?
|
|
31
40
|
end
|
|
32
41
|
|
|
42
|
+
# Bodies are read as WebMock reads them for matching.
|
|
33
43
|
def request_signature_body_hash
|
|
34
|
-
JSON.parse(request_signature.body)
|
|
44
|
+
WebMock::Util::Parsers::JSON.parse(request_signature.body)
|
|
35
45
|
end
|
|
36
46
|
|
|
37
47
|
def request_stub_body_hash
|
|
38
48
|
return request_stub_body if request_stub_body.is_a?(Hash)
|
|
39
49
|
|
|
40
|
-
JSON.parse(request_stub_body)
|
|
50
|
+
WebMock::Util::Parsers::JSON.parse(request_stub_body)
|
|
41
51
|
end
|
|
42
52
|
|
|
43
53
|
def request_stub_body
|
|
@@ -57,9 +67,9 @@ module WebMock
|
|
|
57
67
|
def parseable_json?(body_pattern)
|
|
58
68
|
return false unless body_pattern.is_a?(String)
|
|
59
69
|
|
|
60
|
-
JSON.parse(body_pattern)
|
|
70
|
+
WebMock::Util::Parsers::JSON.parse(body_pattern)
|
|
61
71
|
true
|
|
62
|
-
rescue
|
|
72
|
+
rescue WebMock::Util::Parsers::ParseError
|
|
63
73
|
false
|
|
64
74
|
end
|
|
65
75
|
end
|
|
@@ -55,8 +55,8 @@ module WebMock
|
|
|
55
55
|
|
|
56
56
|
def failure_message_phrase(is_negated=false)
|
|
57
57
|
negation = is_negated ? "was not" : "was"
|
|
58
|
-
"The request #{request_pattern} #{negation} expected to execute #{quantity_phrase(is_negated)}but it executed #{times(times_executed)}" +
|
|
59
|
-
(is_negated ? "" : json_body_hint) +
|
|
58
|
+
"The request #{Util::MessageString.utf8(request_pattern)} #{negation} expected to execute #{quantity_phrase(is_negated)}but it executed #{times(times_executed)}" +
|
|
59
|
+
(is_negated ? "" : json_body_hint + trailing_slash_hint) +
|
|
60
60
|
self.class.executed_requests_message
|
|
61
61
|
end
|
|
62
62
|
|
|
@@ -71,6 +71,17 @@ module WebMock
|
|
|
71
71
|
requests.empty? ? "" : "\n\n#{JsonBodyHint::EXPECTATION_HINT}"
|
|
72
72
|
end
|
|
73
73
|
|
|
74
|
+
# Only when too few requests matched, as for json_body_hint.
|
|
75
|
+
def trailing_slash_hint
|
|
76
|
+
return "" unless too_few_requests_executed?
|
|
77
|
+
|
|
78
|
+
RequestRegistry.instance.requested_signatures.each do |request_signature, _|
|
|
79
|
+
difference = TrailingSlashHint.difference(request_pattern, request_signature)
|
|
80
|
+
return "\n\n#{TrailingSlashHint::EXPECTATION_HINTS[difference]}" if difference
|
|
81
|
+
end
|
|
82
|
+
""
|
|
83
|
+
end
|
|
84
|
+
|
|
74
85
|
def too_few_requests_executed?
|
|
75
86
|
if @at_least_times_executed
|
|
76
87
|
times_executed < @at_least_times_executed
|