webmock 3.20.0 → 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: ffda5916fee8f80d1d0a725b7de314652a588658189bb28ad7a984551d406a3a
4
- data.tar.gz: f2aa5c1f78094f72f9c7310dfbb3e866be2acc6910bd3c3a1a4e562744e0fa65
3
+ metadata.gz: 8a2d1add2b84d303329a722f0c7e9643f237949d0dc471ea1678021324cd41b4
4
+ data.tar.gz: e00640f1b7664959922011ae2f7b922811d94be4fb1bea90a988f3cca83486bc
5
5
  SHA512:
6
- metadata.gz: e3fbfb98e4a6d090abd630ccfea78f5b06038e3f6d4683bd1f651f6c8b07b31698f71f9588df1a8d90066ab0d38a088a604204b4f710bfd9249235a9f2664453
7
- data.tar.gz: ffcaf10bc3cd523f6c12c2209fc4e48fb34813e520789ca78ee0cb65b3285f7bf1534ef36faafbd61a49d82ca547f2ecfb3ea74b33b5ba3b4294493f0683942d
6
+ metadata.gz: 87f797196dedde96873a3740234d33f799745df144953ccfc36403ca6fbe4bd239f5f05818eb7cec25730823c2f99f83a1f42b79cf977b5672d720ed02bb3495
7
+ data.tar.gz: af708a4202382fb1a55c90a7f2c14b1a9d9b277bc8c4ee898dd31b712162bee4e9fb1fb6dd2931c1e39123eee8f15545db9fcd198ea5751f746be7eabfad33eb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,37 @@
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
+
13
+ # 3.21.2
14
+
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.
16
+
17
+ Thanks to [Jake Robb](https://github.com/jakerobb) for reporting.
18
+
19
+ # 3.21.1
20
+
21
+ * The stubbed Net::HTTPResponse#uri now returns request.uri, aligning it with the behavior of an actual Net::HTTPResponse.
22
+
23
+ Thanks to [Abe Voelker](https://github.com/abevoelker) for reporting and to [Victor Maslov](https://github.com/Nakilon) and [Gio Lodi](https://github.com/mokagio) for the suggested solution.
24
+
25
+ # 3.21.0
26
+
27
+ * Don't use deprecated Rack::VERSION for Rack >= 3
28
+
29
+ Thanks to [Étienne Barrié](https://github.com/etiennebarrie)
30
+
31
+ * Updated HTTPClient adapter, to build request signature using the URI after filters have been applied.
32
+
33
+ Thanks to [Matt Brown](https://github.com/mattbnz)
34
+
3
35
  # 3.20.0
4
36
 
5
37
  * Optimised EmHttpRequestAdapter performance.
@@ -178,7 +210,7 @@
178
210
 
179
211
  * Fixed async-http adapter to only considered requests as real if they are real.
180
212
 
181
- Thanks to Thanks to [Tony Schneider](https://github.com/tonywok) and [Samuel Williams](https://github.com/ioquatix)
213
+ Thanks to [Tony Schneider](https://github.com/tonywok) and [Samuel Williams](https://github.com/ioquatix)
182
214
 
183
215
  # 3.11.2
184
216
 
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
 
@@ -606,7 +606,11 @@ which can be passed to `WebMock.allow_net_connect!` and `WebMock.disable_net_con
606
606
  WebMock.allow_net_connect!(net_http_connect_on_start: true)
607
607
  ```
608
608
 
609
- This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
609
+ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`. At the time of connection being made there is no information about the request or URL yet, therefore WebMock is not able to decide whether to stub a request or not and all connections are allowed. To enable connections only to a specific domain (e.g. your test server) use:
610
+
611
+ ```ruby
612
+ WebMock.allow_net_connect!(net_http_connect_on_start: "www.example.com")
613
+ ```
610
614
 
611
615
  ## Setting Expectations
612
616
 
@@ -1193,6 +1197,11 @@ People who submitted patches and new features or suggested improvements. Many th
1193
1197
  * Earlopain
1194
1198
  * James Brown
1195
1199
  * Kazuhiro NISHIYAMA
1200
+ * Étienne Barrié
1201
+ * Matt Brown
1202
+ * Victor Maslov
1203
+ * Gio Lodi
1204
+ * Ryan Brooks
1196
1205
 
1197
1206
  For a full list of contributors you can visit the
1198
1207
  [contributors](https://github.com/bblimke/webmock/contributors) page.
@@ -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
@@ -157,14 +165,14 @@ if defined?(::HTTPClient)
157
165
  end
158
166
 
159
167
  def build_request_signature(req, reuse_existing = false)
160
- uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
161
- uri.query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query, notation: WebMock::Config.instance.query_values_notation) if req.header.request_query
162
- uri.port = req.header.request_uri.port
163
-
164
168
  @request_filter.each do |filter|
165
169
  filter.filter_request(req)
166
170
  end
167
171
 
172
+ uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
173
+ uri.query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query, notation: WebMock::Config.instance.query_values_notation) if req.header.request_query
174
+ uri.port = req.header.request_uri.port
175
+
168
176
  headers = req.header.all.inject({}) do |hdrs, header|
169
177
  hdrs[header[0]] ||= []
170
178
  hdrs[header[0]] << header[1]
@@ -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
 
@@ -77,7 +77,7 @@ module WebMock
77
77
  @socket = Net::HTTP.socket_type.new
78
78
  WebMock::CallbackRegistry.invoke_callbacks(
79
79
  {lib: :net_http}, request_signature, webmock_response)
80
- build_net_http_response(webmock_response, &block)
80
+ build_net_http_response(webmock_response, request.uri, &block)
81
81
  elsif WebMock.net_connect_allowed?(request_signature.uri)
82
82
  check_right_http_connection
83
83
  after_request = lambda do |response|
@@ -144,7 +144,7 @@ module WebMock
144
144
  end
145
145
  end
146
146
 
147
- def build_net_http_response(webmock_response, &block)
147
+ def build_net_http_response(webmock_response, request_uri, &block)
148
148
  response = Net::HTTPResponse.send(:response_class, webmock_response.status[0].to_s).new("1.0", webmock_response.status[0].to_s, webmock_response.status[1])
149
149
  body = webmock_response.body
150
150
  body = nil if webmock_response.status[0].to_s == '204'
@@ -159,6 +159,8 @@ module WebMock
159
159
 
160
160
  response.instance_variable_set(:@read, true)
161
161
 
162
+ response.uri = request_uri
163
+
162
164
  response.extend Net::WebMockHTTPResponse
163
165
 
164
166
  if webmock_response.should_timeout
@@ -47,7 +47,9 @@ module WebMock
47
47
  # Rack-specific variables
48
48
  env['rack.input'] = StringIO.new(body)
49
49
  env['rack.errors'] = $stderr
50
- env['rack.version'] = Rack::VERSION
50
+ if !Rack.const_defined?(:RELEASE) || Rack::RELEASE < "3"
51
+ env['rack.version'] = Rack::VERSION
52
+ end
51
53
  env['rack.url_scheme'] = uri.scheme
52
54
  env['rack.run_once'] = true
53
55
  env['rack.session'] = session
@@ -117,11 +117,11 @@ module WebMock
117
117
  return if @body.nil?
118
118
  return if valid_types.any? { |c| @body.is_a?(c) }
119
119
 
120
- if @body.class.is_a?(Hash)
121
- raise InvalidBody, "must be one of: #{valid_types}, but you've used a #{@body.class}' instead." \
122
- "\n What shall we encode it to? try calling .to_json .to_xml instead on the hash instead, or otherwise convert it to a string."
120
+ if @body.is_a?(Hash)
121
+ raise InvalidBody, "must be one of: #{valid_types}, but you've used a #{@body.class}. " \
122
+ "Please convert it by calling .to_json .to_xml, or otherwise convert it to a string."
123
123
  else
124
- raise InvalidBody, "must be one of: #{valid_types}. '#{@body.class}' given"
124
+ raise InvalidBody, "must be one of: #{valid_types}. '#{@body.class}' given."
125
125
  end
126
126
  end
127
127
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebMock
4
- VERSION = '3.20.0' 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.20.0
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-06 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.20.0/CHANGELOG.md
375
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.20.0
376
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.20.0
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: []
@@ -390,7 +390,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
390
390
  - !ruby/object:Gem::Version
391
391
  version: '0'
392
392
  requirements: []
393
- rubygems_version: 3.5.3
393
+ rubygems_version: 3.5.6
394
394
  signing_key:
395
395
  specification_version: 4
396
396
  summary: Library for stubbing HTTP requests in Ruby.