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
|
@@ -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
|
|
@@ -58,10 +87,68 @@ module HTTP
|
|
|
58
87
|
def perform
|
|
59
88
|
return unless ::WebMock.net_connect_allowed?(request_signature.uri)
|
|
60
89
|
response = @perform.call
|
|
61
|
-
invoke_callbacks(response
|
|
90
|
+
invoke_callbacks(webmock_response_for(response), real_request: true)
|
|
62
91
|
response
|
|
63
92
|
end
|
|
64
93
|
|
|
94
|
+
# The response that callbacks get for a real one. HTTP.rb's auto_inflate
|
|
95
|
+
# feature inflates the body and keeps the headers of the compressed one.
|
|
96
|
+
# WebMock replays a stub through the features, so a stub returning these
|
|
97
|
+
# headers with the inflated body, such as a VCR cassette, would be
|
|
98
|
+
# inflated again. Callbacks get headers that describe the inflated body.
|
|
99
|
+
def webmock_response_for(response)
|
|
100
|
+
return response.to_webmock unless ::WebMock::CallbackRegistry.any_callbacks? && auto_inflate?
|
|
101
|
+
|
|
102
|
+
inflated = false
|
|
103
|
+
webmock_response = response.to_webmock { |body| inflated = reads_from_inflater?(body) }
|
|
104
|
+
describe_inflated_body(webmock_response) if inflated
|
|
105
|
+
webmock_response
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Whether the client inflates with HTTP.rb's own feature. A subclass may
|
|
109
|
+
# do anything with the headers and the body, so its responses are left
|
|
110
|
+
# as they are.
|
|
111
|
+
def auto_inflate?
|
|
112
|
+
return false unless defined?(::HTTP::Features::AutoInflate)
|
|
113
|
+
|
|
114
|
+
features = @options.features.values.grep(::HTTP::Features::AutoInflate)
|
|
115
|
+
features.any? && features.all? { |feature| exactly?(::HTTP::Features::AutoInflate, feature) }
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Only evidence counts, so that callbacks never get other headers for a
|
|
119
|
+
# body that is still compressed. HTTP.rb shows inflation only in the
|
|
120
|
+
# stream the body reads from, which its logging feature may wrap. With
|
|
121
|
+
# HTTP.rb 6's caching feature the body is a String, which shows nothing.
|
|
122
|
+
# A subclass may do anything with what it reads, so it shows nothing too.
|
|
123
|
+
def reads_from_inflater?(body)
|
|
124
|
+
return false unless defined?(::HTTP::Response::Inflater) && exactly?(::HTTP::Response::Body, body)
|
|
125
|
+
|
|
126
|
+
stream = body.instance_variable_get(:@stream)
|
|
127
|
+
if defined?(::HTTP::Features::Logging::BodyLogger) && exactly?(::HTTP::Features::Logging::BodyLogger, stream)
|
|
128
|
+
stream = stream.instance_variable_get(:@stream)
|
|
129
|
+
end
|
|
130
|
+
exactly?(::HTTP::Response::Inflater, stream)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
CLASS_OF = ::Kernel.instance_method(:class)
|
|
134
|
+
private_constant :CLASS_OF
|
|
135
|
+
|
|
136
|
+
# Asks Ruby and not the object, which may say anything of itself.
|
|
137
|
+
def exactly?(klass, object)
|
|
138
|
+
klass.equal?(CLASS_OF.bind(object).call)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# No Content-Encoding, as for a body Net::HTTP inflates, and its own
|
|
142
|
+
# length. A response with an empty body, such as one to HEAD, keeps its
|
|
143
|
+
# headers: they tell of a body that isn't there to be inflated again.
|
|
144
|
+
def describe_inflated_body(webmock_response)
|
|
145
|
+
return if webmock_response.body.empty?
|
|
146
|
+
|
|
147
|
+
headers = webmock_response.headers
|
|
148
|
+
headers.delete("Content-Encoding")
|
|
149
|
+
headers["Content-Length"] = webmock_response.body.bytesize.to_s if headers.key?("Content-Length")
|
|
150
|
+
end
|
|
151
|
+
|
|
65
152
|
def halt
|
|
66
153
|
raise ::WebMock::NetConnectNotAllowedError.new request_signature
|
|
67
154
|
end
|
|
@@ -9,6 +9,7 @@ rescue LoadError
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
if defined?(::HTTPClient)
|
|
12
|
+
require 'delegate'
|
|
12
13
|
|
|
13
14
|
module WebMock
|
|
14
15
|
module HttpLibAdapters
|
|
@@ -45,84 +46,109 @@ if defined?(::HTTPClient)
|
|
|
45
46
|
end
|
|
46
47
|
|
|
47
48
|
module WebMockHTTPClients
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
# HTTPClient publishes headers before it writes the body to the caller's
|
|
50
|
+
# pipe. Capture the chunks upstream so callbacks never read from that pipe.
|
|
51
|
+
class RecordingSession < SimpleDelegator
|
|
52
|
+
def initialize(session, body)
|
|
53
|
+
super(session)
|
|
54
|
+
@body = body
|
|
55
|
+
end
|
|
52
56
|
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
def get_body
|
|
58
|
+
__getobj__.get_body do |chunk|
|
|
59
|
+
@body << chunk.b
|
|
60
|
+
yield chunk
|
|
61
|
+
end
|
|
62
|
+
end
|
|
55
63
|
end
|
|
56
64
|
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
class RecordingSessionManager < Struct.new(:manager, :body)
|
|
66
|
+
def query(req, proxy)
|
|
67
|
+
RecordingSession.new(manager.query(req, proxy), body)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def keep(session)
|
|
71
|
+
manager.keep(session.__getobj__)
|
|
72
|
+
end
|
|
59
73
|
end
|
|
60
74
|
|
|
61
|
-
|
|
62
|
-
|
|
75
|
+
class RecordingConnection
|
|
76
|
+
attr_reader :response, :body
|
|
63
77
|
|
|
64
|
-
|
|
78
|
+
def initialize(connection)
|
|
79
|
+
@connection = connection
|
|
80
|
+
@body = ''.b
|
|
81
|
+
end
|
|
65
82
|
|
|
66
|
-
|
|
83
|
+
def push(response)
|
|
84
|
+
@response = response
|
|
85
|
+
# HTTPClient's loopback responses bypass the session entirely.
|
|
86
|
+
@body.replace(response.content.string) if response.content.is_a?(StringIO)
|
|
87
|
+
@connection.push(response)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
67
90
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
91
|
+
class ResponseFilter < Struct.new(:filter)
|
|
92
|
+
def filter_request(_req)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def filter_response(req, response)
|
|
96
|
+
filter.filter_response(req, response)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
private_constant :RecordingConnection, :RecordingSessionManager, :RecordingSession, :ResponseFilter
|
|
100
|
+
|
|
101
|
+
def do_get_block(req, proxy, conn, &block)
|
|
102
|
+
filters = apply_request_filters(req, proxy)
|
|
103
|
+
request_signature = build_request_signature(req)
|
|
104
|
+
WebMock::RequestRegistry.instance.put(request_signature)
|
|
105
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
106
|
+
if webmock_response
|
|
107
|
+
response = build_httpclient_response(webmock_response, false, req.header, &block)
|
|
108
|
+
filters.each { |filter| filter.filter_response(req, response) }
|
|
109
|
+
conn.push(response)
|
|
75
110
|
WebMock::CallbackRegistry.invoke_callbacks(
|
|
76
111
|
{lib: :httpclient}, request_signature, webmock_response)
|
|
77
|
-
|
|
112
|
+
# Return the response, as HTTPClient#do_get_block does. Libraries that
|
|
113
|
+
# wrap the method to trace requests read its result.
|
|
114
|
+
response
|
|
78
115
|
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
|
79
|
-
|
|
80
|
-
webmock_responses.delete(request_signature)
|
|
81
|
-
|
|
82
|
-
res = if stream
|
|
83
|
-
do_get_stream_without_webmock(req, proxy, conn, &block)
|
|
84
|
-
elsif block
|
|
85
|
-
body = ''
|
|
86
|
-
do_get_block_without_webmock(req, proxy, conn) do |http_res, chunk|
|
|
87
|
-
if chunk && chunk.bytesize > 0
|
|
88
|
-
body += chunk
|
|
89
|
-
block.call(http_res, chunk)
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
else
|
|
93
|
-
do_get_block_without_webmock(req, proxy, conn)
|
|
94
|
-
end
|
|
95
|
-
res = conn.pop
|
|
96
|
-
conn.push(res)
|
|
97
|
-
if WebMock::CallbackRegistry.any_callbacks?
|
|
98
|
-
webmock_response = build_webmock_response(res, body)
|
|
99
|
-
WebMock::CallbackRegistry.invoke_callbacks(
|
|
100
|
-
{lib: :httpclient, real_request: true}, request_signature,
|
|
101
|
-
webmock_response)
|
|
102
|
-
end
|
|
103
|
-
res
|
|
116
|
+
clone(freeze: false).send_prepared_sync_request(req, proxy, conn, filters, request_signature, &block)
|
|
104
117
|
else
|
|
105
118
|
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
106
119
|
end
|
|
107
120
|
end
|
|
108
121
|
|
|
109
122
|
def do_request_async(method, uri, query, body, extheader)
|
|
110
|
-
clear_thread_variables
|
|
111
123
|
req = create_request(method, uri, query, body, extheader)
|
|
124
|
+
proxy = no_proxy?(uri) ? nil : @proxy
|
|
125
|
+
filters = apply_request_filters(req, proxy)
|
|
112
126
|
request_signature = build_request_signature(req)
|
|
113
|
-
|
|
127
|
+
WebMock::RequestRegistry.instance.put(request_signature)
|
|
114
128
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
async_conn_for_stubbed_request(req, request_signature, webmock_response)
|
|
118
|
-
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
|
119
|
-
conn = super
|
|
120
|
-
conn.async_thread[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES]
|
|
121
|
-
conn.async_thread[WEBMOCK_HTTPCLIENT_RESPONSES] = Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES]
|
|
122
|
-
conn
|
|
123
|
-
else
|
|
129
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
130
|
+
unless webmock_response || WebMock.net_connect_allowed?(request_signature.uri)
|
|
124
131
|
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
125
132
|
end
|
|
133
|
+
|
|
134
|
+
# Keep the prepared request, including any headers its filters assigned.
|
|
135
|
+
# HTTPClient's do_request_async would create a second request in the worker.
|
|
136
|
+
conn = ::HTTPClient::Connection.new
|
|
137
|
+
conn.async_thread = Thread.new do
|
|
138
|
+
begin
|
|
139
|
+
if webmock_response
|
|
140
|
+
response = build_httpclient_response(webmock_response, true, req.header)
|
|
141
|
+
filters.each { |filter| filter.filter_response(req, response) }
|
|
142
|
+
conn.push(response)
|
|
143
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :httpclient}, request_signature, webmock_response)
|
|
144
|
+
else
|
|
145
|
+
clone(freeze: false).send_prepared_async_request(req, proxy, conn, filters, request_signature)
|
|
146
|
+
end
|
|
147
|
+
rescue Exception => e
|
|
148
|
+
conn.push e
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
conn
|
|
126
152
|
end
|
|
127
153
|
|
|
128
154
|
def build_httpclient_response(webmock_response, stream = false, req_header = nil, &block)
|
|
@@ -167,11 +193,7 @@ if defined?(::HTTPClient)
|
|
|
167
193
|
webmock_response
|
|
168
194
|
end
|
|
169
195
|
|
|
170
|
-
def build_request_signature(req
|
|
171
|
-
@request_filter.each do |filter|
|
|
172
|
-
filter.filter_request(req)
|
|
173
|
-
end
|
|
174
|
-
|
|
196
|
+
def build_request_signature(req)
|
|
175
197
|
uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
|
|
176
198
|
if req.header.request_query
|
|
177
199
|
# HTTPClient sends the query argument after the query in the URI
|
|
@@ -187,38 +209,65 @@ if defined?(::HTTPClient)
|
|
|
187
209
|
end
|
|
188
210
|
headers = headers_from_session(uri).merge(headers)
|
|
189
211
|
|
|
190
|
-
|
|
212
|
+
WebMock::RequestSignature.new(
|
|
191
213
|
req.header.request_method.downcase.to_sym,
|
|
192
214
|
uri.to_s,
|
|
193
215
|
body: req.http_body.dump,
|
|
194
216
|
headers: headers
|
|
195
217
|
)
|
|
196
|
-
|
|
197
|
-
# reuse a previous identical signature object if we stored one for later use
|
|
198
|
-
if reuse_existing && previous_signature = previous_signature_for(signature)
|
|
199
|
-
return previous_signature
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
signature
|
|
203
218
|
end
|
|
204
219
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
220
|
+
protected
|
|
221
|
+
|
|
222
|
+
# Only the per-request copy changes its filter list. It shares the client's
|
|
223
|
+
# sessions and lets HTTPClient run the response filters at the normal time.
|
|
224
|
+
def send_prepared_sync_request(req, proxy, conn, filters, request_signature, &block)
|
|
225
|
+
@request_filter = filters.map { |filter| ResponseFilter.new(filter) }
|
|
226
|
+
if block
|
|
227
|
+
body = ''
|
|
228
|
+
do_get_block_without_webmock(req, proxy, conn) do |http_res, chunk|
|
|
229
|
+
if chunk && chunk.bytesize > 0
|
|
230
|
+
body += chunk
|
|
231
|
+
block.call(http_res, chunk)
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
else
|
|
235
|
+
do_get_block_without_webmock(req, proxy, conn)
|
|
236
|
+
end
|
|
237
|
+
res = conn.pop
|
|
238
|
+
conn.push(res)
|
|
239
|
+
if WebMock::CallbackRegistry.any_callbacks?
|
|
240
|
+
webmock_response = build_webmock_response(res, body)
|
|
241
|
+
WebMock::CallbackRegistry.invoke_callbacks(
|
|
242
|
+
{lib: :httpclient, real_request: true}, request_signature,
|
|
243
|
+
webmock_response)
|
|
208
244
|
end
|
|
245
|
+
res
|
|
209
246
|
end
|
|
210
247
|
|
|
211
|
-
def
|
|
212
|
-
|
|
213
|
-
|
|
248
|
+
def send_prepared_async_request(req, proxy, conn, filters, request_signature)
|
|
249
|
+
@request_filter = filters.map { |filter| ResponseFilter.new(filter) }
|
|
250
|
+
if WebMock::CallbackRegistry.any_callbacks?
|
|
251
|
+
recorder = RecordingConnection.new(conn)
|
|
252
|
+
@session_manager = RecordingSessionManager.new(@session_manager, recorder.body)
|
|
253
|
+
end
|
|
214
254
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
255
|
+
protect_keep_alive_disconnected { do_get_stream(req, proxy, recorder || conn) }
|
|
256
|
+
if recorder
|
|
257
|
+
response = build_webmock_response(recorder.response, recorder.body)
|
|
258
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :httpclient, real_request: true}, request_signature, response)
|
|
259
|
+
end
|
|
218
260
|
end
|
|
219
261
|
|
|
220
262
|
private
|
|
221
263
|
|
|
264
|
+
def apply_request_filters(req, proxy)
|
|
265
|
+
# Recent HTTPClient versions select the applicable proxy filters here.
|
|
266
|
+
return filter_request(req, proxy) if respond_to?(:filter_request, true)
|
|
267
|
+
|
|
268
|
+
@request_filter.each { |filter| filter.filter_request(req) }
|
|
269
|
+
end
|
|
270
|
+
|
|
222
271
|
# some of the headers sent by HTTPClient are derived from
|
|
223
272
|
# the client session
|
|
224
273
|
def headers_from_session(uri)
|
|
@@ -229,40 +278,10 @@ if defined?(::HTTPClient)
|
|
|
229
278
|
hdrs
|
|
230
279
|
end
|
|
231
280
|
end
|
|
232
|
-
|
|
233
|
-
def clear_thread_variables
|
|
234
|
-
Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = nil
|
|
235
|
-
Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] = nil
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
# The async thread must not go through do_get, which would rebuild the request signature.
|
|
239
|
-
# Rebuilding calls headers_from_session again, and the session sets a Date header with
|
|
240
|
-
# 1 second resolution, so a signature built later can differ from the one the response was
|
|
241
|
-
# cached under, which makes the thread evaluate the stub registry again and consume
|
|
242
|
-
# an extra response from the stub sequence. The thread only delivers what the caller
|
|
243
|
-
# already evaluated, mirroring do_get's stubbed branch.
|
|
244
|
-
def async_conn_for_stubbed_request(req, request_signature, webmock_response)
|
|
245
|
-
conn = HTTPClient::Connection.new
|
|
246
|
-
conn.async_thread = Thread.new do
|
|
247
|
-
begin
|
|
248
|
-
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
|
|
249
|
-
response = build_httpclient_response(webmock_response, true, req.header)
|
|
250
|
-
@request_filter.each do |filter|
|
|
251
|
-
filter.filter_response(req, response)
|
|
252
|
-
end
|
|
253
|
-
conn.push(response)
|
|
254
|
-
WebMock::CallbackRegistry.invoke_callbacks({lib: :httpclient}, request_signature, webmock_response)
|
|
255
|
-
rescue Exception => e
|
|
256
|
-
conn.push e
|
|
257
|
-
end
|
|
258
|
-
end
|
|
259
|
-
conn
|
|
260
|
-
end
|
|
261
281
|
end
|
|
262
282
|
|
|
263
283
|
class WebMockHTTPClient < HTTPClient
|
|
264
284
|
alias_method :do_get_block_without_webmock, :do_get_block
|
|
265
|
-
alias_method :do_get_stream_without_webmock, :do_get_stream
|
|
266
285
|
|
|
267
286
|
include WebMockHTTPClients
|
|
268
287
|
end
|
|
@@ -270,7 +289,6 @@ if defined?(::HTTPClient)
|
|
|
270
289
|
if defined? ::JSONClient
|
|
271
290
|
class WebMockJSONClient < JSONClient
|
|
272
291
|
alias_method :do_get_block_without_webmock, :do_get_block
|
|
273
|
-
alias_method :do_get_stream_without_webmock, :do_get_stream
|
|
274
292
|
|
|
275
293
|
include WebMockHTTPClients
|
|
276
294
|
end
|
|
@@ -54,7 +54,7 @@ if defined?(Manticore)
|
|
|
54
54
|
|
|
55
55
|
def response_object_for(request, context, &block)
|
|
56
56
|
request_signature = generate_webmock_request_signature(request, context)
|
|
57
|
-
WebMock::RequestRegistry.instance.
|
|
57
|
+
WebMock::RequestRegistry.instance.put(request_signature)
|
|
58
58
|
|
|
59
59
|
if webmock_response = registered_response_for(request_signature)
|
|
60
60
|
webmock_response.raise_error_if_any
|
|
@@ -100,7 +100,7 @@ module WebMock
|
|
|
100
100
|
|
|
101
101
|
request_signature = WebMock::NetHTTPUtility.request_signature_from_request(self, request, body)
|
|
102
102
|
|
|
103
|
-
WebMock::RequestRegistry.instance.
|
|
103
|
+
WebMock::RequestRegistry.instance.put(request_signature)
|
|
104
104
|
|
|
105
105
|
if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
106
106
|
# Close the real connection left by an earlier request, if any.
|
|
@@ -111,11 +111,21 @@ module WebMock
|
|
|
111
111
|
build_net_http_response(webmock_response, request.uri, &block)
|
|
112
112
|
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
|
113
113
|
check_right_http_connection
|
|
114
|
+
callback_options = {lib: :net_http, real_request: true}
|
|
115
|
+
# Without callbacks for this request, nothing needs the body
|
|
116
|
+
# before the block, so the block gets the response while
|
|
117
|
+
# Net::HTTP reads it, as without WebMock, and can stream the
|
|
118
|
+
# body or set decode_content. With callbacks, such as VCR's,
|
|
119
|
+
# the body is read first, so they get all of it.
|
|
120
|
+
stream = block && !WebMock::CallbackRegistry.any_callbacks_for?(callback_options)
|
|
114
121
|
after_request = lambda do |response|
|
|
115
|
-
|
|
122
|
+
# Checked again, so that callbacks registered while the
|
|
123
|
+
# request was made get the response too, as before. A request
|
|
124
|
+
# that streams to its block doesn't come here, so callbacks
|
|
125
|
+
# registered after it was sent don't get its response.
|
|
126
|
+
if WebMock::CallbackRegistry.any_callbacks_for?(callback_options)
|
|
116
127
|
webmock_response = build_webmock_response(response)
|
|
117
|
-
WebMock::CallbackRegistry.invoke_callbacks(
|
|
118
|
-
{lib: :net_http, real_request: true}, request_signature, webmock_response)
|
|
128
|
+
WebMock::CallbackRegistry.invoke_callbacks(callback_options, request_signature, webmock_response)
|
|
119
129
|
end
|
|
120
130
|
response.extend Net::WebMockHTTPResponse
|
|
121
131
|
# Net::HTTP has read the body and applied its encoding already
|
|
@@ -123,16 +133,17 @@ module WebMock
|
|
|
123
133
|
block.call response if block
|
|
124
134
|
response
|
|
125
135
|
end
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
136
|
+
real_request = lambda {
|
|
137
|
+
next super(request, nil, &block) if stream
|
|
138
|
+
|
|
139
|
+
after_request.call(super(request, nil, &nil))
|
|
129
140
|
}
|
|
130
141
|
if started?
|
|
131
142
|
ensure_actual_connection
|
|
132
|
-
|
|
143
|
+
real_request.call
|
|
133
144
|
else
|
|
134
145
|
start_with_connect {
|
|
135
|
-
|
|
146
|
+
real_request.call
|
|
136
147
|
}
|
|
137
148
|
end
|
|
138
149
|
else
|
|
@@ -295,6 +306,8 @@ module WebMock
|
|
|
295
306
|
end
|
|
296
307
|
|
|
297
308
|
path = WebMock::Util::URI.heuristic_parse(path).request_uri if path =~ /^http/
|
|
309
|
+
# OPTIONS * (asterisk form) has no path; a URI can only hold it as /*
|
|
310
|
+
path = "/*" if path == "*" && request.method == "OPTIONS"
|
|
298
311
|
|
|
299
312
|
uri = get_uri(net_http, path)
|
|
300
313
|
method = request.method.downcase.to_sym
|
|
@@ -302,10 +315,19 @@ module WebMock
|
|
|
302
315
|
headers = Hash[*request.to_hash.map {|k,v| [k, v]}.inject([]) {|r,x| r + x}]
|
|
303
316
|
|
|
304
317
|
if request.body_stream
|
|
305
|
-
body = request.body_stream
|
|
318
|
+
body = read_body_stream(request.body_stream)
|
|
306
319
|
request.body_stream = nil
|
|
307
320
|
end
|
|
308
321
|
|
|
322
|
+
# A form set with #set_form is encoded by Net::HTTP when it sends the
|
|
323
|
+
# request. The request is left as it is, so it is sent as without
|
|
324
|
+
# WebMock and encodes its form again when sent again, and the
|
|
325
|
+
# signature gets the body it sends.
|
|
326
|
+
form = request.instance_variable_get(:@body_data)
|
|
327
|
+
if form && body.nil? && request.body.nil?
|
|
328
|
+
return WebMock::RequestSignature.new(method, uri, body: form_body(request, form), headers: headers)
|
|
329
|
+
end
|
|
330
|
+
|
|
309
331
|
if body != nil && body.respond_to?(:read)
|
|
310
332
|
request.set_body_internal body.read
|
|
311
333
|
else
|
|
@@ -315,6 +337,52 @@ module WebMock
|
|
|
315
337
|
WebMock::RequestSignature.new(method, uri, body: request.body, headers: headers)
|
|
316
338
|
end
|
|
317
339
|
|
|
340
|
+
# Net::HTTP reads a body stream with IO.copy_stream, which always passes
|
|
341
|
+
# a length, so some streams only accept #read with one. Such a stream is
|
|
342
|
+
# read in chunks. A stream that answers #read only through method_missing
|
|
343
|
+
# is read without a length, as before.
|
|
344
|
+
def self.read_body_stream(stream)
|
|
345
|
+
arity = begin
|
|
346
|
+
stream.method(:read).arity
|
|
347
|
+
rescue NameError
|
|
348
|
+
-1
|
|
349
|
+
end
|
|
350
|
+
return stream.read if arity == 0 || arity == -1
|
|
351
|
+
|
|
352
|
+
body = String.new
|
|
353
|
+
# As IO.copy_stream reads it: with a buffer, unless read takes only a
|
|
354
|
+
# length, and until it returns nil or an empty string, or raises
|
|
355
|
+
# EOFError.
|
|
356
|
+
buffer = String.new
|
|
357
|
+
begin
|
|
358
|
+
loop do
|
|
359
|
+
chunk = arity == 1 ? stream.read(16 * 1024) : stream.read(16 * 1024, buffer)
|
|
360
|
+
break if chunk.nil? || chunk.empty?
|
|
361
|
+
|
|
362
|
+
begin
|
|
363
|
+
body << chunk
|
|
364
|
+
rescue Encoding::CompatibilityError
|
|
365
|
+
# Chunks in incompatible encodings are joined as bytes.
|
|
366
|
+
body.force_encoding(Encoding::BINARY) << chunk.b
|
|
367
|
+
end
|
|
368
|
+
end
|
|
369
|
+
rescue EOFError
|
|
370
|
+
end
|
|
371
|
+
body
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
# The body Net::HTTPGenericRequest#send_request_with_body_data sends for
|
|
375
|
+
# a URL encoded form given as a Hash or an Array of pairs, which Net::HTTP
|
|
376
|
+
# encodes again when it sends the request. A multipart form, or a form
|
|
377
|
+
# given as another Enumerable, which may be read only once, gets no body,
|
|
378
|
+
# as before.
|
|
379
|
+
def self.form_body(request, form)
|
|
380
|
+
return unless form.is_a?(Hash) || form.is_a?(Array)
|
|
381
|
+
return if request.content_type =~ /\Amultipart\/form-data\z/i
|
|
382
|
+
|
|
383
|
+
URI.encode_www_form(form)
|
|
384
|
+
end
|
|
385
|
+
|
|
318
386
|
def self.get_uri(net_http, path = nil)
|
|
319
387
|
protocol = net_http.use_ssl? ? "https" : "http"
|
|
320
388
|
|
|
@@ -43,11 +43,11 @@ if defined?(::Patron::Session)
|
|
|
43
43
|
return super unless block
|
|
44
44
|
|
|
45
45
|
req = build_request(action, url, headers, **options)
|
|
46
|
-
request_signature, webmock_response = PatronAdapter.register_request(req)
|
|
46
|
+
request_signature, webmock_response, recorded_body = PatronAdapter.register_request(req)
|
|
47
47
|
|
|
48
48
|
if webmock_response
|
|
49
49
|
PatronAdapter.stream_stubbed_response(webmock_response, request_signature,
|
|
50
|
-
default_response_charset, &block)
|
|
50
|
+
default_response_charset, recorded_body.to_s.bytesize, &block)
|
|
51
51
|
else
|
|
52
52
|
# Callbacks run once the head has arrived, so they also run
|
|
53
53
|
# when the block leaves before the end of the body
|
|
@@ -181,13 +181,28 @@ if defined?(::Patron::Session)
|
|
|
181
181
|
uri.path = uri.normalized_path.gsub("[^:]//","/")
|
|
182
182
|
|
|
183
183
|
if [:put, :post, :patch].include?(req.action)
|
|
184
|
-
|
|
185
|
-
|
|
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)
|
|
186
188
|
raise ArgumentError.new("Unable to open specified file.")
|
|
187
189
|
end
|
|
188
|
-
request_body = File.
|
|
189
|
-
|
|
190
|
-
|
|
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
|
|
191
206
|
else
|
|
192
207
|
raise ArgumentError.new("Must provide either data or a filename when doing a PUT or POST")
|
|
193
208
|
end
|
|
@@ -240,15 +255,19 @@ if defined?(::Patron::Session)
|
|
|
240
255
|
# Records the request and returns its signature with the stubbed
|
|
241
256
|
# response, which is nil when a real request is allowed
|
|
242
257
|
def self.register_request(req)
|
|
243
|
-
|
|
244
|
-
|
|
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
|
|
245
263
|
|
|
246
|
-
webmock_response = WebMock::StubRegistry.instance.response_for_request(
|
|
264
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(
|
|
265
|
+
request_signature, record: recorded)
|
|
247
266
|
if webmock_response.nil? && !WebMock.net_connect_allowed?(request_signature.uri)
|
|
248
267
|
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
249
268
|
end
|
|
250
269
|
|
|
251
|
-
[request_signature, webmock_response]
|
|
270
|
+
[request_signature, webmock_response, recorded.body]
|
|
252
271
|
end
|
|
253
272
|
|
|
254
273
|
def self.invoke_callbacks(request_signature, webmock_response)
|
|
@@ -266,7 +285,7 @@ if defined?(::Patron::Session)
|
|
|
266
285
|
|
|
267
286
|
# Callbacks run before the block, so they also run when it leaves
|
|
268
287
|
# before the end of the body
|
|
269
|
-
def self.stream_stubbed_response(webmock_response, request_signature, default_response_charset)
|
|
288
|
+
def self.stream_stubbed_response(webmock_response, request_signature, default_response_charset, uploaded_bytes)
|
|
270
289
|
head = build_patron_response(webmock_response, default_response_charset, with_body: false)
|
|
271
290
|
invoke_callbacks(request_signature, webmock_response)
|
|
272
291
|
# Kept out of the stream, so the stream has only the methods of
|
|
@@ -285,7 +304,7 @@ if defined?(::Patron::Session)
|
|
|
285
304
|
redirect_count: head.redirect_count,
|
|
286
305
|
header_data: build_header_data(webmock_response),
|
|
287
306
|
downloaded_bytes: body.pos,
|
|
288
|
-
uploaded_bytes:
|
|
307
|
+
uploaded_bytes: uploaded_bytes,
|
|
289
308
|
timings: {},
|
|
290
309
|
complete: body.eof?,
|
|
291
310
|
default_charset: default_response_charset
|