webmock 3.21.2 → 3.23.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ad4b861d9b998186e2b85f1da5e35dce4451d13d16822cf35f42d52fbce40de0
4
- data.tar.gz: e7395eb84a1688a56f6916b4a58992cf1a88bcee695eb2e7f015260d33d3c677
3
+ metadata.gz: 8a2d1add2b84d303329a722f0c7e9643f237949d0dc471ea1678021324cd41b4
4
+ data.tar.gz: e00640f1b7664959922011ae2f7b922811d94be4fb1bea90a988f3cca83486bc
5
5
  SHA512:
6
- metadata.gz: 1599e16d961cf7a5a87fe2ac9030cd33ccb5d20d645832e317a36cbad45e4d4836cd506016d0f5a5a5ee69c502e7993ed8a33c6fce080b9a40aa5626f2a212b5
7
- data.tar.gz: 044b1c3bc0ec98535f4949649984b33475390a1f5b4213abba21a61c357d93173088214c3e5dd4ef943ac21800485cd078ad5ef2bc722c7563c9fa8b5fd80ea5
6
+ metadata.gz: 87f797196dedde96873a3740234d33f799745df144953ccfc36403ca6fbe4bd239f5f05818eb7cec25730823c2f99f83a1f42b79cf977b5672d720ed02bb3495
7
+ data.tar.gz: af708a4202382fb1a55c90a7f2c14b1a9d9b277bc8c4ee898dd31b712162bee4e9fb1fb6dd2931c1e39123eee8f15545db9fcd198ea5751f746be7eabfad33eb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ # 3.23.0
4
+
5
+ * Fixed HTTP.rb adapter to support streaming real responses when WebMock is enabled.
6
+
7
+ Thanks to [Viacheslav Nepomniashchikh](https://github.com/aka-nez) for reporting and investigating this [issue](https://github.com/bblimke/webmock/issues/1017).
8
+
9
+ # 3.22.0
10
+
11
+ * Addressed an issue in the HTTPClient adapter where memoized stubbed responses and memoized request_signatures were incorrectly persisted between subsequent requests ([#1019](https://github.com/bblimke/webmock/issues/1019)). The implementation of a more robust thread-safety solution by [Tom Beauvais](https://github.com/tbeauvais) in [PR #300](https://github.com/bblimke/webmock/pull/300) not only resolved the memoization problem but also enhanced the overall thread safety of the adapter. This update ensures that stubbed responses and request signatures are correctly isolated to individual requests, improving both consistency and thread safety.
12
+
3
13
  # 3.21.2
4
14
 
5
15
  * Corrected type checking in `WebMock::Response#assert_valid_body!` to accurately recognize `Hash` objects. Additionally, improved the clarity of the error message for unsupported body types, guiding users towards proper usage.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  WebMock
2
2
  =======
3
3
  [![Gem Version](https://badge.fury.io/rb/webmock.svg)](http://badge.fury.io/rb/webmock)
4
- [![Build Status](https://github.com/bblimke/webmock/workflows/CI/badge.svg?branch=master)](https://github.com/bblimke/webmock/actions)
4
+ [![Build Status](https://github.com/bblimke/webmock/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/bblimke/webmock/actions)
5
5
  [![Code Climate](https://codeclimate.com/github/bblimke/webmock/badges/gpa.svg)](https://codeclimate.com/github/bblimke/webmock)
6
6
  [![Mentioned in Awesome Ruby](https://awesome.re/mentioned-badge.svg)](https://github.com/markets/awesome-ruby)
7
7
 
@@ -6,9 +6,19 @@ module HTTP
6
6
  webmock_response = ::WebMock::Response.new
7
7
 
8
8
  webmock_response.status = [status.to_i, reason]
9
+
9
10
  webmock_response.body = body.to_s
10
- webmock_response.headers = headers.to_h
11
+ # This call is used to reset the body of the response to enable it to be streamed if necessary.
12
+ # The `body.to_s` call above reads the body, which allows WebMock to trigger any registered callbacks.
13
+ # However, once the body is read to_s, it cannot be streamed again and attempting to do so
14
+ # will raise a "HTTP::StateError: body has already been consumed" error.
15
+ # To avoid this error, we replace the original body with a new one.
16
+ # The new body has its @stream attribute set to new Streamer, instead of the original Connection.
17
+ # Unfortunately, it's not possible to reset the original body to its initial streaming state.
18
+ # Therefore, this replacement is the best workaround currently available.
19
+ reset_body_to_allow_it_to_be_streamed!(webmock_response)
11
20
 
21
+ webmock_response.headers = headers.to_h
12
22
  webmock_response
13
23
  end
14
24
 
@@ -21,16 +31,7 @@ module HTTP
21
31
  # HTTP.rb 3.0+ uses a keyword argument to pass the encoding, but 1.x
22
32
  # and 2.x use a positional argument, and 0.x don't support supplying
23
33
  # the encoding.
24
- body = if HTTP::VERSION < "1.0.0"
25
- Body.new(Streamer.new(webmock_response.body))
26
- elsif HTTP::VERSION < "3.0.0"
27
- Body.new(Streamer.new(webmock_response.body), webmock_response.body.encoding)
28
- else
29
- Body.new(
30
- Streamer.new(webmock_response.body, encoding: webmock_response.body.encoding),
31
- encoding: webmock_response.body.encoding
32
- )
33
- end
34
+ body = build_http_rb_response_body_from_webmock_response(webmock_response)
34
35
 
35
36
  return new(status, "1.1", headers, body, uri) if HTTP::VERSION < "1.0.0"
36
37
 
@@ -54,7 +55,18 @@ module HTTP
54
55
  })
55
56
  end
56
57
 
57
- private
58
+ def build_http_rb_response_body_from_webmock_response(webmock_response)
59
+ if HTTP::VERSION < "1.0.0"
60
+ Body.new(Streamer.new(webmock_response.body))
61
+ elsif HTTP::VERSION < "3.0.0"
62
+ Body.new(Streamer.new(webmock_response.body), webmock_response.body.encoding)
63
+ else
64
+ Body.new(
65
+ Streamer.new(webmock_response.body, encoding: webmock_response.body.encoding),
66
+ encoding: webmock_response.body.encoding
67
+ )
68
+ end
69
+ end
58
70
 
59
71
  def normalize_uri(uri)
60
72
  return unless uri
@@ -65,5 +77,11 @@ module HTTP
65
77
  uri
66
78
  end
67
79
  end
80
+
81
+ private
82
+
83
+ def reset_body_to_allow_it_to_be_streamed!(webmock_response)
84
+ @body = self.class.build_http_rb_response_body_from_webmock_response(webmock_response)
85
+ end
68
86
  end
69
87
  end
@@ -31,9 +31,9 @@ if defined?(HTTP) && defined?(HTTP::VERSION)
31
31
  end
32
32
  end
33
33
 
34
- require "webmock/http_lib_adapters/http_rb/client"
35
- require "webmock/http_lib_adapters/http_rb/request"
36
- require "webmock/http_lib_adapters/http_rb/response"
37
- require "webmock/http_lib_adapters/http_rb/streamer"
38
- require "webmock/http_lib_adapters/http_rb/webmock"
34
+ require_relative "http_rb/client"
35
+ require_relative "http_rb/request"
36
+ require_relative "http_rb/response"
37
+ require_relative "http_rb/streamer"
38
+ require_relative "http_rb/webmock"
39
39
  end
@@ -45,6 +45,8 @@ if defined?(::HTTPClient)
45
45
  end
46
46
 
47
47
  module WebMockHTTPClients
48
+ WEBMOCK_HTTPCLIENT_RESPONSES = :webmock_httpclient_responses
49
+ WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES = :webmock_httpclient_request_signatures
48
50
 
49
51
  REQUEST_RESPONSE_LOCK = Mutex.new
50
52
 
@@ -57,12 +59,14 @@ if defined?(::HTTPClient)
57
59
  end
58
60
 
59
61
  def do_get(req, proxy, conn, stream = false, &block)
62
+ clear_thread_variables unless conn.async_thread
63
+
60
64
  request_signature = build_request_signature(req, :reuse_existing)
61
65
 
62
66
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
63
67
 
64
68
  if webmock_responses[request_signature]
65
- webmock_response = synchronize_request_response { webmock_responses.delete(request_signature) }
69
+ webmock_response = webmock_responses.delete(request_signature)
66
70
  response = build_httpclient_response(webmock_response, stream, req.header, &block)
67
71
  @request_filter.each do |filter|
68
72
  filter.filter_response(req, response)
@@ -73,7 +77,7 @@ if defined?(::HTTPClient)
73
77
  res
74
78
  elsif WebMock.net_connect_allowed?(request_signature.uri)
75
79
  # in case there is a nil entry in the hash...
76
- synchronize_request_response { webmock_responses.delete(request_signature) }
80
+ webmock_responses.delete(request_signature)
77
81
 
78
82
  res = if stream
79
83
  do_get_stream_without_webmock(req, proxy, conn, &block)
@@ -103,12 +107,16 @@ if defined?(::HTTPClient)
103
107
  end
104
108
 
105
109
  def do_request_async(method, uri, query, body, extheader)
110
+ clear_thread_variables
106
111
  req = create_request(method, uri, query, body, extheader)
107
112
  request_signature = build_request_signature(req)
108
- synchronize_request_response { webmock_request_signatures << request_signature }
113
+ webmock_request_signatures << request_signature
109
114
 
110
115
  if webmock_responses[request_signature] || WebMock.net_connect_allowed?(request_signature.uri)
111
- super
116
+ conn = super
117
+ conn.async_thread[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES]
118
+ conn.async_thread[WEBMOCK_HTTPCLIENT_RESPONSES] = Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES]
119
+ conn
112
120
  else
113
121
  raise WebMock::NetConnectNotAllowedError.new(request_signature)
114
122
  end
@@ -188,22 +196,18 @@ if defined?(::HTTPClient)
188
196
  end
189
197
 
190
198
  def webmock_responses
191
- @webmock_responses ||= Hash.new do |hash, request_signature|
192
- synchronize_request_response do
193
- hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
194
- end
199
+ Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] ||= Hash.new do |hash, request_signature|
200
+ hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
195
201
  end
196
202
  end
197
203
 
198
204
  def webmock_request_signatures
199
- @webmock_request_signatures ||= []
205
+ Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] ||= []
200
206
  end
201
207
 
202
208
  def previous_signature_for(signature)
203
- synchronize_request_response do
204
- return nil unless index = webmock_request_signatures.index(signature)
205
- webmock_request_signatures.delete_at(index)
206
- end
209
+ return nil unless index = webmock_request_signatures.index(signature)
210
+ webmock_request_signatures.delete_at(index)
207
211
  end
208
212
 
209
213
  private
@@ -219,14 +223,9 @@ if defined?(::HTTPClient)
219
223
  end
220
224
  end
221
225
 
222
- def synchronize_request_response
223
- if REQUEST_RESPONSE_LOCK.owned?
224
- yield
225
- else
226
- REQUEST_RESPONSE_LOCK.synchronize do
227
- yield
228
- end
229
- end
226
+ def clear_thread_variables
227
+ Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = nil
228
+ Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] = nil
230
229
  end
231
230
  end
232
231
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebMock
4
- VERSION = '3.21.2' unless defined?(::WebMock::VERSION)
4
+ VERSION = '3.23.0' unless defined?(::WebMock::VERSION)
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.21.2
4
+ version: 3.23.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-20 00:00:00.000000000 Z
11
+ date: 2024-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -371,9 +371,9 @@ licenses:
371
371
  - MIT
372
372
  metadata:
373
373
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
374
- changelog_uri: https://github.com/bblimke/webmock/blob/v3.21.2/CHANGELOG.md
375
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.21.2
376
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.21.2
374
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.23.0/CHANGELOG.md
375
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.23.0
376
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.23.0
377
377
  wiki_uri: https://github.com/bblimke/webmock/wiki
378
378
  post_install_message:
379
379
  rdoc_options: []