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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +358 -0
  3. data/README.md +222 -3
  4. data/lib/webmock/api.rb +4 -0
  5. data/lib/webmock/errors.rb +1 -1
  6. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +51 -7
  7. data/lib/webmock/http_lib_adapters/curb_adapter.rb +291 -39
  8. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +11 -3
  9. data/lib/webmock/http_lib_adapters/excon_adapter.rb +23 -2
  10. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +33 -4
  11. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +4 -1
  12. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +132 -110
  13. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +1 -1
  14. data/lib/webmock/http_lib_adapters/net_http.rb +66 -3
  15. data/lib/webmock/http_lib_adapters/net_http_response.rb +28 -2
  16. data/lib/webmock/http_lib_adapters/patron_adapter.rb +242 -40
  17. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +65 -7
  18. data/lib/webmock/json_body_hint.rb +8 -5
  19. data/lib/webmock/request_body_diff.rb +16 -6
  20. data/lib/webmock/request_execution_verifier.rb +13 -2
  21. data/lib/webmock/request_pattern.rb +236 -36
  22. data/lib/webmock/request_registry.rb +75 -5
  23. data/lib/webmock/request_signature.rb +44 -5
  24. data/lib/webmock/request_signature_snippet.rb +23 -2
  25. data/lib/webmock/request_stub.rb +8 -4
  26. data/lib/webmock/response.rb +42 -3
  27. data/lib/webmock/stub_registry.rb +7 -6
  28. data/lib/webmock/stub_request_snippet.rb +9 -3
  29. data/lib/webmock/trailing_slash_hint.rb +55 -0
  30. data/lib/webmock/util/hash_counter.rb +27 -8
  31. data/lib/webmock/util/hash_keys_stringifier.rb +23 -1
  32. data/lib/webmock/util/headers.rb +17 -0
  33. data/lib/webmock/util/message_string.rb +28 -0
  34. data/lib/webmock/util/parsers/json.rb +33 -2
  35. data/lib/webmock/util/query_mapper.rb +56 -37
  36. data/lib/webmock/util/upload_body.rb +63 -0
  37. data/lib/webmock/util/uri.rb +21 -12
  38. data/lib/webmock/util/values_stringifier.rb +54 -0
  39. data/lib/webmock/version.rb +1 -1
  40. data/lib/webmock/webmock.rb +35 -4
  41. data/lib/webmock.rb +3 -0
  42. metadata +7 -4
@@ -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
- WEBMOCK_HTTPCLIENT_RESPONSES = :webmock_httpclient_responses
49
- WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES = :webmock_httpclient_request_signatures
50
-
51
- REQUEST_RESPONSE_LOCK = Mutex.new
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
- def do_get_block(req, proxy, conn, &block)
54
- do_get(req, proxy, conn, false, &block)
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
- def do_get_stream(req, proxy, conn, &block)
58
- do_get(req, proxy, conn, true, &block)
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
- def do_get(req, proxy, conn, stream = false, &block)
62
- clear_thread_variables unless conn.async_thread
75
+ class RecordingConnection
76
+ attr_reader :response, :body
63
77
 
64
- request_signature = build_request_signature(req, :reuse_existing)
78
+ def initialize(connection)
79
+ @connection = connection
80
+ @body = ''.b
81
+ end
65
82
 
66
- WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
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
- if webmock_responses[request_signature]
69
- webmock_response = webmock_responses.delete(request_signature)
70
- response = build_httpclient_response(webmock_response, stream, req.header, &block)
71
- @request_filter.each do |filter|
72
- filter.filter_response(req, response)
73
- end
74
- res = conn.push(response)
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
- res
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
- # in case there is a nil entry in the hash...
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
- webmock_request_signatures << request_signature
127
+ WebMock::RequestRegistry.instance.put(request_signature)
114
128
 
115
- if webmock_responses[request_signature]
116
- webmock_response = webmock_responses.delete(request_signature)
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,13 +193,13 @@ if defined?(::HTTPClient)
167
193
  webmock_response
168
194
  end
169
195
 
170
- def build_request_signature(req, reuse_existing = false)
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
- uri.query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query, notation: WebMock::Config.instance.query_values_notation) if req.header.request_query
198
+ if req.header.request_query
199
+ # HTTPClient sends the query argument after the query in the URI
200
+ query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query, notation: WebMock::Config.instance.query_values_notation)
201
+ uri.query = [uri.query, query].reject { |part| part.to_s.empty? }.join('&')
202
+ end
177
203
  uri.port = req.header.request_uri.port
178
204
 
179
205
  headers = req.header.all.inject({}) do |hdrs, header|
@@ -183,38 +209,65 @@ if defined?(::HTTPClient)
183
209
  end
184
210
  headers = headers_from_session(uri).merge(headers)
185
211
 
186
- signature = WebMock::RequestSignature.new(
212
+ WebMock::RequestSignature.new(
187
213
  req.header.request_method.downcase.to_sym,
188
214
  uri.to_s,
189
215
  body: req.http_body.dump,
190
216
  headers: headers
191
217
  )
192
-
193
- # reuse a previous identical signature object if we stored one for later use
194
- if reuse_existing && previous_signature = previous_signature_for(signature)
195
- return previous_signature
196
- end
197
-
198
- signature
199
218
  end
200
219
 
201
- def webmock_responses
202
- Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] ||= Hash.new do |hash, request_signature|
203
- hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
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)
204
244
  end
245
+ res
205
246
  end
206
247
 
207
- def webmock_request_signatures
208
- Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] ||= []
209
- end
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
210
254
 
211
- def previous_signature_for(signature)
212
- return nil unless index = webmock_request_signatures.index(signature)
213
- webmock_request_signatures.delete_at(index)
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
214
260
  end
215
261
 
216
262
  private
217
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
+
218
271
  # some of the headers sent by HTTPClient are derived from
219
272
  # the client session
220
273
  def headers_from_session(uri)
@@ -225,40 +278,10 @@ if defined?(::HTTPClient)
225
278
  hdrs
226
279
  end
227
280
  end
228
-
229
- def clear_thread_variables
230
- Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = nil
231
- Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] = nil
232
- end
233
-
234
- # The async thread must not go through do_get, which would rebuild the request signature.
235
- # Rebuilding calls headers_from_session again, and the session sets a Date header with
236
- # 1 second resolution, so a signature built later can differ from the one the response was
237
- # cached under, which makes the thread evaluate the stub registry again and consume
238
- # an extra response from the stub sequence. The thread only delivers what the caller
239
- # already evaluated, mirroring do_get's stubbed branch.
240
- def async_conn_for_stubbed_request(req, request_signature, webmock_response)
241
- conn = HTTPClient::Connection.new
242
- conn.async_thread = Thread.new do
243
- begin
244
- WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
245
- response = build_httpclient_response(webmock_response, true, req.header)
246
- @request_filter.each do |filter|
247
- filter.filter_response(req, response)
248
- end
249
- conn.push(response)
250
- WebMock::CallbackRegistry.invoke_callbacks({lib: :httpclient}, request_signature, webmock_response)
251
- rescue Exception => e
252
- conn.push e
253
- end
254
- end
255
- conn
256
- end
257
281
  end
258
282
 
259
283
  class WebMockHTTPClient < HTTPClient
260
284
  alias_method :do_get_block_without_webmock, :do_get_block
261
- alias_method :do_get_stream_without_webmock, :do_get_stream
262
285
 
263
286
  include WebMockHTTPClients
264
287
  end
@@ -266,7 +289,6 @@ if defined?(::HTTPClient)
266
289
  if defined? ::JSONClient
267
290
  class WebMockJSONClient < JSONClient
268
291
  alias_method :do_get_block_without_webmock, :do_get_block
269
- alias_method :do_get_stream_without_webmock, :do_get_stream
270
292
 
271
293
  include WebMockHTTPClients
272
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.requested_signatures.put(request_signature)
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.requested_signatures.put(request_signature)
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.
@@ -118,6 +118,8 @@ module WebMock
118
118
  {lib: :net_http, real_request: true}, request_signature, webmock_response)
119
119
  end
120
120
  response.extend Net::WebMockHTTPResponse
121
+ # Net::HTTP has read the body and applied its encoding already
122
+ response.instance_variable_set(:@__body_encoding_applied, true)
121
123
  block.call response if block
122
124
  response
123
125
  end
@@ -181,7 +183,9 @@ module WebMock
181
183
  body = webmock_response.body
182
184
  body = nil if webmock_response.status[0].to_s == '204'
183
185
 
184
- response.instance_variable_set(:@body, body)
186
+ # A copy, so that changing the response body, for example its
187
+ # encoding, does not change the stubbed body
188
+ response.instance_variable_set(:@body, body&.dup)
185
189
  webmock_response.headers.to_a.each do |name, values|
186
190
  values = [values] unless values.is_a?(Array)
187
191
  values.each do |value|
@@ -192,6 +196,7 @@ module WebMock
192
196
  response.instance_variable_set(:@read, true)
193
197
 
194
198
  response.uri = request_uri
199
+ response.body_encoding = response_body_encoding if respond_to?(:response_body_encoding)
195
200
 
196
201
  response.extend Net::WebMockHTTPResponse
197
202
 
@@ -202,6 +207,7 @@ module WebMock
202
207
  webmock_response.raise_error_if_any
203
208
 
204
209
  yield response if block_given?
210
+ response.send(:webmock_apply_body_encoding)
205
211
 
206
212
  response
207
213
  end
@@ -289,6 +295,8 @@ module WebMock
289
295
  end
290
296
 
291
297
  path = WebMock::Util::URI.heuristic_parse(path).request_uri if path =~ /^http/
298
+ # OPTIONS * (asterisk form) has no path; a URI can only hold it as /*
299
+ path = "/*" if path == "*" && request.method == "OPTIONS"
292
300
 
293
301
  uri = get_uri(net_http, path)
294
302
  method = request.method.downcase.to_sym
@@ -296,10 +304,19 @@ module WebMock
296
304
  headers = Hash[*request.to_hash.map {|k,v| [k, v]}.inject([]) {|r,x| r + x}]
297
305
 
298
306
  if request.body_stream
299
- body = request.body_stream.read
307
+ body = read_body_stream(request.body_stream)
300
308
  request.body_stream = nil
301
309
  end
302
310
 
311
+ # A form set with #set_form is encoded by Net::HTTP when it sends the
312
+ # request. The request is left as it is, so it is sent as without
313
+ # WebMock and encodes its form again when sent again, and the
314
+ # signature gets the body it sends.
315
+ form = request.instance_variable_get(:@body_data)
316
+ if form && body.nil? && request.body.nil?
317
+ return WebMock::RequestSignature.new(method, uri, body: form_body(request, form), headers: headers)
318
+ end
319
+
303
320
  if body != nil && body.respond_to?(:read)
304
321
  request.set_body_internal body.read
305
322
  else
@@ -309,6 +326,52 @@ module WebMock
309
326
  WebMock::RequestSignature.new(method, uri, body: request.body, headers: headers)
310
327
  end
311
328
 
329
+ # Net::HTTP reads a body stream with IO.copy_stream, which always passes
330
+ # a length, so some streams only accept #read with one. Such a stream is
331
+ # read in chunks. A stream that answers #read only through method_missing
332
+ # is read without a length, as before.
333
+ def self.read_body_stream(stream)
334
+ arity = begin
335
+ stream.method(:read).arity
336
+ rescue NameError
337
+ -1
338
+ end
339
+ return stream.read if arity == 0 || arity == -1
340
+
341
+ body = String.new
342
+ # As IO.copy_stream reads it: with a buffer, unless read takes only a
343
+ # length, and until it returns nil or an empty string, or raises
344
+ # EOFError.
345
+ buffer = String.new
346
+ begin
347
+ loop do
348
+ chunk = arity == 1 ? stream.read(16 * 1024) : stream.read(16 * 1024, buffer)
349
+ break if chunk.nil? || chunk.empty?
350
+
351
+ begin
352
+ body << chunk
353
+ rescue Encoding::CompatibilityError
354
+ # Chunks in incompatible encodings are joined as bytes.
355
+ body.force_encoding(Encoding::BINARY) << chunk.b
356
+ end
357
+ end
358
+ rescue EOFError
359
+ end
360
+ body
361
+ end
362
+
363
+ # The body Net::HTTPGenericRequest#send_request_with_body_data sends for
364
+ # a URL encoded form given as a Hash or an Array of pairs, which Net::HTTP
365
+ # encodes again when it sends the request. A multipart form, or a form
366
+ # given as another Enumerable, which may be read only once, gets no body,
367
+ # as before.
368
+ def self.form_body(request, form)
369
+ return unless form.is_a?(Hash) || form.is_a?(Array)
370
+ return if request.content_type =~ /\Amultipart\/form-data\z/i
371
+
372
+ URI.encode_www_form(form)
373
+ end
374
+
312
375
  def self.get_uri(net_http, path = nil)
313
376
  protocol = net_http.use_ssl? ? "https" : "http"
314
377
 
@@ -20,17 +20,43 @@ module Net
20
20
  if !(defined?(@__read_body_previously_called).nil?) && @__read_body_previously_called
21
21
  return super
22
22
  end
23
- return @body if dest.nil? && block.nil?
23
+ if dest.nil? && block.nil?
24
+ webmock_apply_body_encoding
25
+ return @body
26
+ end
24
27
  raise ArgumentError.new("both arg and block given for HTTP method") if dest && block
25
28
  return nil if @body.nil?
26
29
 
27
30
  dest ||= ::Net::ReadAdapter.new(block)
28
- dest << @body.dup
31
+ dest << @body
29
32
  @body = dest
33
+ webmock_apply_body_encoding
34
+ @body
30
35
  ensure
31
36
  # allow subsequent calls to #read_body to proceed as normal, without our hack...
32
37
  @__read_body_previously_called = true
33
38
  end
39
+
40
+ private
41
+
42
+ # Net::HTTPResponse#read_body applies the encoding set with #body_encoding=
43
+ # (or Net::HTTP#response_body_encoding=) once, to the body it reads, which
44
+ # happens in the request block or right after it. A stubbed body is set
45
+ # without reading, so apply the encoding the first time the body is read
46
+ # or when the request block returns, and ignore later changes to it.
47
+ def webmock_apply_body_encoding
48
+ return if defined?(@__body_encoding_applied) && @__body_encoding_applied
49
+ @__body_encoding_applied = true
50
+
51
+ encoding = defined?(@body_encoding) && @body_encoding
52
+ # A frozen body was assigned in the request block, which Net::HTTP
53
+ # would replace with the body it reads, so leave it as it is
54
+ return unless encoding && @body.is_a?(String) && !@body.frozen?
55
+
56
+ # Net::HTTP detects the encoding from the bytes it read
57
+ encoding = detect_encoding(@body.b) unless encoding.is_a?(Encoding)
58
+ @body.force_encoding(encoding) if encoding
59
+ end
34
60
  end
35
61
  end
36
62