webmock 3.35.0 → 3.36.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: acb33845b6ff4fbb890271cbed06608a6095c823ce57a9be5ca2121090ff117f
4
- data.tar.gz: f3bf9d36ba38af4e134016cf4d941589d271e75cb06d2dc314781d8f1ffe341e
3
+ metadata.gz: 7ac78ad5e14ee19d53223a19af699b7b22b18ba06fdfcd58837daa4810df48e3
4
+ data.tar.gz: 18bea0ee19e26728813e849777d377cb048ffc7da4f2927901cd80d86e579b90
5
5
  SHA512:
6
- metadata.gz: e99abdb908ce47c4bba164cfa574a2b94f54e042f7bd55f1ba4f64bf88b43f3c721c7a7e39f8870c7f0d3ecdc139ee79d7c14de0c201ca0937de8355ded288c7
7
- data.tar.gz: 9a75e24decc112843f7e89ee47e68fbf312bec7d23590d0b17c1661295d5b3c2b690cc4800757e1e2c2d62b58990ddd217f492c6e095209c282ed6107d732367
6
+ metadata.gz: 91d74df9d7351cad99a7b51b162f3d2beb2e89e76969d570d650cbabcf3d3123f2a2d098c00f73af1e45983f4757f2693d894b5875d3ec941d57918c19304a3b
7
+ data.tar.gz: f8d50c967121c7e843dcf6d0ea52ad467359cb023ae72c928151bd9cb1ff71188a46cd258470357a02156bb51682486e621726679eb48b1ac86ad2b0f60b5a77
data/CHANGELOG.md CHANGED
@@ -1,5 +1,51 @@
1
1
  # Changelog
2
2
 
3
+ # 3.36.0
4
+
5
+ * Fix matching of indexed arrays nested inside arrays of hashes (#1144)
6
+
7
+ Previously, WebMock read indexed arrays nested inside an array of hashes, such as `a[][b][0][c]=1`, as hashes with `"0"`, `"1"` keys. WebMock writes that form itself, so `with(query: {"a" => [{"b" => [{"c" => "1"}]}]})` with a regexp URI did not match `?a[][b][][c]=1`, and a Typhoeus request with that hash as its body did not match a stub with the same hash.
8
+
9
+ If a stub uses the old shape, such as `{"a" => [{"b" => {"0" => {"c" => "1"}}}]}`, for example one copied from WebMock's suggested stub, use arrays instead.
10
+
11
+ Thanks to [Hamza Tarar](https://github.com/ihamzatarar)
12
+
13
+ * Fix matching of arrays of hashes that repeat a nested key
14
+
15
+ Previously, WebMock put a nested key repeated under `[]` into the first hash of the array, so `items[][address][city]=X&items[][address][city]=Y` was read as one item with the city `Y`. A stub listing both items did not match, and a stub for `X` and `Y` also matched a request for `Z` and `Y`. Rails and Faraday encode arrays of nested hashes this way.
16
+
17
+ Such keys now go into a new hash, as in Rack. If a stub lists such an array as one item, for example one copied from WebMock's suggested stub, list every item.
18
+
19
+ Queries that mix a value and nested keys under the same name, such as `a[][b]=1&a[][b][c]=2` or `a[c]=1&a[c][]=2`, no longer raise `IndexError` or join the values into one string.
20
+
21
+ * Apply Net::HTTP `response_body_encoding` to stubbed response bodies (#1138)
22
+
23
+ Previously, stubbed Net::HTTP responses ignored `Net::HTTP#response_body_encoding=` and `Net::HTTPResponse#body_encoding=`. A body stubbed as binary, for example from a raw response file, stayed `ASCII-8BIT` even with `http.response_body_encoding = true` and a `Content-Type` of `text/plain; charset=utf-8`.
24
+
25
+ Stubbed bodies now get the encoding real responses get: with `true`, the charset from the `Content-Type` header or an HTML `<meta>` tag, and otherwise the encoding the setting names. Without the setting, a body keeps the encoding it was stubbed with.
26
+
27
+ Thanks to [Yang Bai](https://github.com/ybai08)
28
+
29
+ * Give each stubbed Net::HTTP response its own copy of the body
30
+
31
+ Previously, a stubbed Net::HTTP response returned the stubbed body string itself. A body stubbed with a frozen string, such as a literal in a file with `# frozen_string_literal: true`, made `response.body.force_encoding("UTF-8")` raise `FrozenError`, which a real response never does. Changing the body in place, for example with `response.body << "!"`, changed it for later requests to the same stub too.
32
+
33
+ * Keep the size of async-http responses to HEAD requests (#1143)
34
+
35
+ async-http gives the size of a response to a HEAD request as `response.body.length`, not as a header. Previously, a response stubbed with `stub_request(:head, url).to_return(headers: {"Content-Length" => "42"})` ignored the `Content-Length`: its body was `nil` over HTTP/1 and had a length of 0 over HTTP/2. A real response lost its size, with a `nil` body, when a `WebMock.after_request` callback was registered, for example while VCR was recording, and the recorded response had no `Content-Length`.
36
+
37
+ A stubbed response now takes its size from the stubbed `Content-Length`, or from the stubbed body without one, as before. A real response keeps its size when a callback is registered, and the recorded response has it as its `Content-Length`.
38
+
39
+ Thanks to [Andrew K. Fader](https://github.com/andrewfader)
40
+
41
+ * Keep the URI query of HTTPClient requests that pass a query argument (#1145)
42
+
43
+ Previously, WebMock dropped the query in the URI when an HTTPClient request also passed the query argument. `HTTPClient.new.get("http://example.com/?a=1", {"b" => "2"})` requests `/?a=1&b=2`, but WebMock saw `/?b=2`, and with `{}` as the query argument it saw no query at all. A stub or `a_request` expectation could match a URL that was never requested.
44
+
45
+ If a stub or expectation for such a request leaves out the query in the URI, add it, for example `stub_request(:get, "http://example.com/?a=1&b=2")`. Record VCR cassettes with such requests again.
46
+
47
+ Thanks to [AmeenKhan12345](https://github.com/AmeenKhan12345)
48
+
3
49
  # 3.35.0
4
50
 
5
51
  * Fix `get_file` with Patron 1.1 (#1147)
@@ -129,19 +129,32 @@ if defined?(Async::HTTP)
129
129
  end
130
130
 
131
131
  def build_webmock_response(response)
132
- body = response.read
133
- response.body = ::Protocol::HTTP::Body::Buffered.wrap(body)
132
+ # async-http takes the Content-Length out of the headers of a response
133
+ # to a HEAD request and gives it a body that only holds that size.
134
+ # Keep that body, and record the size as the Content-Length sent.
135
+ if head_body?(response.body)
136
+ content_length = response.body.length
137
+ else
138
+ body = response.read
139
+ response.body = ::Protocol::HTTP::Body::Buffered.wrap(body)
140
+ end
134
141
 
135
142
  webmock_response = WebMock::Response.new
136
143
  webmock_response.status = [
137
144
  response.status,
138
145
  WebMock::HttpLibAdapters::AsyncHttpClientAdapter.status_descriptions[response.status]
139
146
  ]
140
- webmock_response.headers = build_webmock_response_headers(response)
147
+ headers = build_webmock_response_headers(response)
148
+ headers['content-length'] = [content_length.to_s] if content_length
149
+ webmock_response.headers = headers
141
150
  webmock_response.body = body
142
151
  webmock_response
143
152
  end
144
153
 
154
+ def head_body?(body)
155
+ defined?(::Protocol::HTTP::Body::Head) && body.is_a?(::Protocol::HTTP::Body::Head)
156
+ end
157
+
145
158
  def build_webmock_response_headers(response)
146
159
  response.headers.each.each_with_object({}) do |(k, v), o|
147
160
  o[k] ||= []
@@ -215,7 +228,8 @@ if defined?(Async::HTTP)
215
228
  def call(request)
216
229
  request.read
217
230
  webmock_response = get_webmock_response(request)
218
- build_response(webmock_response)
231
+ body = request.head? ? head_body(webmock_response) : webmock_response.body
232
+ build_response(webmock_response, body)
219
233
  end
220
234
 
221
235
  def add_webmock_response(request, webmock_response)
@@ -235,7 +249,7 @@ if defined?(Async::HTTP)
235
249
  @webmock_responses ||= {}
236
250
  end
237
251
 
238
- def build_response(webmock_response)
252
+ def build_response(webmock_response, body)
239
253
  headers = (webmock_response.headers || {}).each_with_object([]) do |(k, value), o|
240
254
  next if FRAMING_HEADERS.include?(k.downcase)
241
255
 
@@ -247,9 +261,22 @@ if defined?(Async::HTTP)
247
261
  ::Protocol::HTTP::Response[
248
262
  webmock_response.status[0],
249
263
  headers,
250
- webmock_response.body
264
+ body
251
265
  ]
252
266
  end
267
+
268
+ # async-http answers a HEAD request with the size of the body as the
269
+ # Content-Length, and leaves the body out. A stub's own Content-Length
270
+ # is the size of the body it stands for, so it takes precedence.
271
+ def head_body(webmock_response)
272
+ content_length = Array(webmock_response.headers&.[]('Content-Length')).first.to_s
273
+
274
+ if content_length.match?(/\A\d+\z/) && defined?(::Protocol::HTTP::Body::Head)
275
+ ::Protocol::HTTP::Body::Head.new(content_length.to_i)
276
+ else
277
+ webmock_response.body
278
+ end
279
+ end
253
280
  end
254
281
  end
255
282
  end
@@ -173,7 +173,11 @@ if defined?(::HTTPClient)
173
173
  end
174
174
 
175
175
  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
176
+ if req.header.request_query
177
+ # HTTPClient sends the query argument after the query in the URI
178
+ query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query, notation: WebMock::Config.instance.query_values_notation)
179
+ uri.query = [uri.query, query].reject { |part| part.to_s.empty? }.join('&')
180
+ end
177
181
  uri.port = req.header.request_uri.port
178
182
 
179
183
  headers = req.header.all.inject({}) do |hdrs, header|
@@ -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
@@ -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
 
@@ -65,7 +65,7 @@ module WebMock::Util
65
65
  if options[:notation] == :flat_array
66
66
  accumulator << [key, value]
67
67
  else
68
- accumulator[key] = value.kind_of?(Hash) ? dehash(value) : value
68
+ accumulator[key] = dehash_value(value)
69
69
  end
70
70
  accumulator
71
71
  end
@@ -125,44 +125,34 @@ module WebMock::Util
125
125
  def fill_accumulator_for_subscript(accumulator, key, value)
126
126
  current_node = accumulator
127
127
  subkeys = key.split(/(?=\[[^\[\]]+)/)
128
- subkeys[0..-2].each do |subkey|
128
+ subkeys[0..-2].each_with_index do |subkey, index|
129
129
  node = subkey =~ /\[\]\z/ ? [] : {}
130
+ current_node = array_element_for(current_node, subkeys[index..-1]) if current_node.is_a? Array
130
131
  subkey = subkey.gsub(/[\[\]]/, '')
131
- if current_node.is_a? Array
132
- container = current_node.find { |n| n.is_a?(Hash) && n.has_key?(subkey) }
133
- if container
134
- current_node = container[subkey]
135
- else
136
- current_node << {subkey => node}
137
- current_node = node
138
- end
139
- else
140
- current_node[subkey] = node unless current_node[subkey]
141
- current_node = current_node[subkey]
142
- end
132
+ current_node[subkey] = node unless current_node[subkey]
133
+ current_node = current_node[subkey]
143
134
  end
144
135
  last_key = subkeys.last
145
136
  array_value = !!(last_key =~ /\[\]$/)
146
137
  last_key = last_key.gsub(/[\[\]]/, '')
147
138
  if current_node.is_a? Array
148
- last_container = current_node.select { |n| n.is_a?(Hash) }.last
149
- if last_container && !last_container.has_key?(last_key)
150
- if array_value
151
- last_container[last_key] ||= []
152
- last_container[last_key] << value
153
- else
154
- last_container[last_key] = value
155
- end
139
+ # Unlike in Rack, a final key that the last hash already has starts
140
+ # a new hash even with [], so a[][b][]=1&a[][b][]=2 makes two hashes
141
+ last = current_node.last
142
+ current_node << {} unless last.is_a?(Hash) && !last.key?(last_key)
143
+ if array_value
144
+ (current_node.last[last_key] ||= []) << value
156
145
  else
157
- if array_value
158
- current_node << {last_key => [value]}
159
- else
160
- current_node << {last_key => value}
161
- end
146
+ current_node.last[last_key] = value
162
147
  end
163
148
  else
164
149
  if array_value
165
- current_node[last_key] ||= []
150
+ if current_node.is_a?(Hash)
151
+ current_node[last_key] = [current_node[last_key]].compact unless current_node[last_key].is_a?(Array)
152
+ else
153
+ # A value is in the way, as in x=5&x[5][]=1, so do what older versions did
154
+ current_node[last_key] ||= []
155
+ end
166
156
  current_node[last_key] << value unless value.nil?
167
157
  else
168
158
  current_node[last_key] = value
@@ -170,6 +160,30 @@ module WebMock::Util
170
160
  end
171
161
  end
172
162
 
163
+ # Returns the hash in an array of hashes that a nested key goes into.
164
+ # As in Rack, that is the last hash, unless the rest of the key is
165
+ # already set there, so a[][b][c]=1&a[][b][c]=2 makes two hashes.
166
+ def array_element_for(array, subkeys)
167
+ last = array.last
168
+ return last if last.is_a?(Hash) && key_path_free?(last, subkeys)
169
+ array << {}
170
+ array.last
171
+ end
172
+
173
+ # Whether the rest of a key fits into the hash without replacing a
174
+ # value. A part ending in [] fits while it holds an array.
175
+ def key_path_free?(hash, subkeys)
176
+ subkeys.each_with_index do |subkey, index|
177
+ key = subkey.gsub(/[\[\]]/, '')
178
+ return true unless hash.key?(key)
179
+ value = hash[key]
180
+ return value.is_a?(Array) if subkey.end_with?('[]')
181
+ return false if index == subkeys.size - 1 || !value.is_a?(Hash)
182
+ hash = value
183
+ end
184
+ false
185
+ end
186
+
173
187
  ##
174
188
  # Sets the query component for this URI from a Hash object.
175
189
  # This method produces a query string using the :subscript notation.
@@ -219,11 +233,7 @@ module WebMock::Util
219
233
  end
220
234
 
221
235
  def dehash(hash)
222
- hash.each do |(key, value)|
223
- if value.is_a?(::Hash)
224
- hash[key] = self.dehash(value)
225
- end
226
- end
236
+ hash.transform_values! { |value| dehash_value(value) }
227
237
  if hash != {} && hash.keys.all? { |key| key =~ /\A\d+\z/ }
228
238
  # By number, then by text, so "01" and "1" keep a fixed order
229
239
  hash.sort_by { |key, _| [key.to_i, key] }.map(&:last)
@@ -232,6 +242,16 @@ module WebMock::Util
232
242
  end
233
243
  end
234
244
 
245
+ # A query such as a[][b][0][c]=1 produces an array whose element holds
246
+ # {"0" => ...}, so the numeric keys have to be collapsed inside arrays too.
247
+ def dehash_value(value)
248
+ case value
249
+ when ::Hash then dehash(value)
250
+ when ::Array then value.map { |element| dehash_value(element) }
251
+ else value
252
+ end
253
+ end
254
+
235
255
  ##
236
256
  # Joins and converts parent and value into a properly encoded and
237
257
  # ordered URL query.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebMock
4
- VERSION = '3.35.0' unless defined?(::WebMock::VERSION)
4
+ VERSION = '3.36.0' unless defined?(::WebMock::VERSION)
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.35.0
4
+ version: 3.36.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -360,9 +360,9 @@ licenses:
360
360
  - MIT
361
361
  metadata:
362
362
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
363
- changelog_uri: https://github.com/bblimke/webmock/blob/v3.35.0/CHANGELOG.md
364
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.35.0
365
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.35.0
363
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.36.0/CHANGELOG.md
364
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.36.0
365
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.36.0
366
366
  wiki_uri: https://github.com/bblimke/webmock/wiki
367
367
  rdoc_options: []
368
368
  require_paths: