webmock 3.35.0 → 3.38.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.
@@ -18,10 +18,32 @@ module WebMock
18
18
  [k,v]
19
19
  }.inject([]) {|r,x| r + x}]
20
20
  else
21
- arg
21
+ options[:deep] ? with_string_keys(arg) : arg
22
22
  end
23
23
  end
24
24
 
25
+ # RSpec's hash_including and hash_excluding nested in a pattern keep
26
+ # their keys as given, so they get string keys here, as do hashes in
27
+ # array_including and array_excluding. They stay RSpec matchers, which
28
+ # compare nested values the way RSpec does. A matcher without keys to
29
+ # change is kept as it is.
30
+ def self.with_string_keys(arg)
31
+ # Defined in request_pattern.rb, which this file can be loaded without.
32
+ return arg unless defined?(WebMock::RSpecMatcherDetector)
33
+
34
+ name = if WebMock::RSpecMatcherDetector.rSpecHashMatcher?(arg)
35
+ :@expected
36
+ elsif WebMock::RSpecMatcherDetector.rSpecArrayMatcher?(arg)
37
+ arg.instance_variable_defined?(:@expected) ? :@expected : :@unexpected
38
+ end
39
+ return arg unless name
40
+
41
+ expected = arg.instance_variable_get(name)
42
+ converted = stringify_keys!(expected, deep: true)
43
+ converted == expected ? arg : arg.class.new(converted)
44
+ end
45
+ private_class_method :with_string_keys
46
+
25
47
  end
26
48
  end
27
49
  end
@@ -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.
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+
5
+ module WebMock
6
+ module Util
7
+ module UploadBody
8
+ def self.read(stream, headers)
9
+ # Only inspect readers whose buffering and pushback semantics we know.
10
+ # A wrapper can delegate ungetbyte to a file while transforming read.
11
+ read_owner = stream.method(:read).owner
12
+ if defined?(Tempfile) && stream.is_a?(Tempfile) && read_owner == Tempfile.instance_method(:read).owner
13
+ stream = stream.to_io
14
+ read_owner = stream.method(:read).owner
15
+ end
16
+ gzip = defined?(Zlib::GzipReader) && read_owner == Zlib::GzipReader
17
+ source = gzip ? stream.to_io : stream
18
+ return stream.to_s unless (read_owner == IO || read_owner == StringIO || gzip) &&
19
+ (source.is_a?(StringIO) || (source.is_a?(IO) && source.stat.file?))
20
+
21
+ _, length = headers.find { |name, _| name.to_s.casecmp?('Content-Length') }
22
+ return ''.b if length && length.to_i.zero?
23
+
24
+ if gzip
25
+ # Rewinding a gzip reader loses its pushed-back, uncompressed bytes.
26
+ # GzipReader#ungetc accepts the whole string, including binary data.
27
+ pushback = :ungetc
28
+ elsif source.is_a?(IO)
29
+ # Flush pending writes before checking the descriptor offset.
30
+ # sysseek rejects buffered input, which IO#pos would discard.
31
+ stream.read(0)
32
+ begin
33
+ position = stream.sysseek(0, IO::SEEK_CUR)
34
+ rescue IOError
35
+ pushback = :ungetbyte
36
+ end
37
+ else
38
+ position = stream.pos
39
+ end
40
+ body = ''.b
41
+ each_chunk(stream, length&.to_i) { |chunk| body << chunk.b }
42
+ body
43
+ ensure
44
+ if pushback && body && !body.empty?
45
+ stream.public_send(pushback, body)
46
+ elsif position
47
+ stream.seek(position)
48
+ end
49
+ end
50
+
51
+ def self.each_chunk(stream, length)
52
+ while length.nil? || length > 0
53
+ size = length ? [length, 64 * 1024].min : 64 * 1024
54
+ chunk = stream.read(size)
55
+ break if chunk.nil? || chunk.empty?
56
+ yield chunk
57
+ length -= chunk.bytesize if length
58
+ end
59
+ end
60
+ private_class_method :each_chunk
61
+ end
62
+ end
63
+ end
@@ -13,16 +13,20 @@ module WebMock
13
13
  hash[key] = Addressable::URI.heuristic_parse(key)
14
14
  end
15
15
 
16
- NORMALIZED_URIS = Hash.new do |hash, uri|
17
- normalized_uri = WebMock::Util::URI.heuristic_parse(uri)
18
- if normalized_uri.query_values
19
- sorted_query_values = sort_query_values(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query, notation: Config.instance.query_values_notation) || {})
20
- normalized_uri.query = WebMock::Util::QueryMapper.values_to_query(sorted_query_values, notation: WebMock::Config.instance.query_values_notation)
16
+ # Keyed by query notation, then by URI, so changing
17
+ # Config.instance.query_values_notation applies to URIs seen before.
18
+ NORMALIZED_URIS = Hash.new do |by_notation, notation|
19
+ by_notation[notation] = Hash.new do |hash, uri|
20
+ normalized_uri = WebMock::Util::URI.heuristic_parse(uri)
21
+ if normalized_uri.query_values
22
+ sorted_query_values = sort_query_values(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query, notation: notation) || {})
23
+ normalized_uri.query = WebMock::Util::QueryMapper.values_to_query(sorted_query_values, notation: notation)
24
+ end
25
+ normalized_uri = normalized_uri.normalize #normalize! is slower
26
+ normalized_uri.query = normalized_uri.query.gsub("+", "%2B") if normalized_uri.query
27
+ normalized_uri.port = normalized_uri.inferred_port unless normalized_uri.port
28
+ hash[uri] = normalized_uri
21
29
  end
22
- normalized_uri = normalized_uri.normalize #normalize! is slower
23
- normalized_uri.query = normalized_uri.query.gsub("+", "%2B") if normalized_uri.query
24
- normalized_uri.port = normalized_uri.inferred_port unless normalized_uri.port
25
- hash[uri] = normalized_uri
26
30
  end
27
31
 
28
32
  def self.heuristic_parse(uri)
@@ -32,7 +36,7 @@ module WebMock
32
36
  def self.normalize_uri(uri)
33
37
  return uri if uri.is_a?(Regexp)
34
38
  uri = 'http://' + uri unless uri.match('^https?://') if uri.is_a?(String)
35
- NORMALIZED_URIS[uri].dup
39
+ NORMALIZED_URIS[Config.instance.query_values_notation][uri].dup
36
40
  end
37
41
 
38
42
  def self.variations_of_uri_as_strings(uri_object, only_with_scheme: false)
@@ -76,8 +80,13 @@ module WebMock
76
80
  private
77
81
 
78
82
  def self.sort_query_values(query_values)
79
- sorted_query_values = query_values.sort
80
- query_values.is_a?(Hash) ? Hash[*sorted_query_values.inject([]) { |values, pair| values + pair}] : sorted_query_values
83
+ if query_values.is_a?(Hash)
84
+ Hash[*query_values.sort.inject([]) { |values, pair| values + pair}]
85
+ else
86
+ # Pairs from :flat_array. A key without "=" has a nil value, which
87
+ # can't be compared with a string, so it comes first.
88
+ query_values.sort_by { |key, value| value.nil? ? [key, 0, ""] : [key, 1, value] }
89
+ end
81
90
  end
82
91
 
83
92
  def self.uris_with_inferred_port_and_without(uris)
@@ -19,4 +19,58 @@ class WebMock::Util::ValuesStringifier
19
19
  value
20
20
  end
21
21
  end
22
+
23
+ # Like stringify_values, for query patterns: also converts the values of
24
+ # nested hash_including and hash_excluding matchers. Strings are kept as
25
+ # given, so a String subclass keeps its own ===.
26
+ def self.stringify_query_values(value)
27
+ case value
28
+ when WebMock::Matchers::HashArgumentMatcher
29
+ # A copy, so a subclass keeps its own constructor and state.
30
+ value.dup.tap do |matcher|
31
+ matcher.instance_variable_set(:@expected, stringify_query_values(value.instance_variable_get(:@expected)))
32
+ end
33
+ when Hash
34
+ Hash[value.map { |k, v| [k, stringify_query_values(v)] }]
35
+ when Array
36
+ value.map { |v| stringify_query_values(v) }
37
+ when String
38
+ value
39
+ else
40
+ if WebMock::RSpecMatcherDetector.rSpecHashMatcher?(value)
41
+ expected = stringify_query_values(value.instance_variable_get(:@expected))
42
+ NestedRSpecMatcher.new(value.class.new(expected))
43
+ else
44
+ stringify_values(value)
45
+ end
46
+ end
47
+ end
48
+
49
+ # Keeps RSpec's comparison of nested values, such as an array of regexps,
50
+ # and adds ==, which a query Hash uses to compare its values.
51
+ class NestedRSpecMatcher
52
+ def initialize(matcher)
53
+ @matcher = matcher
54
+ end
55
+
56
+ def ===(actual)
57
+ @matcher === actual
58
+ end
59
+ alias == ===
60
+
61
+ def inspect
62
+ @matcher.description
63
+ end
64
+ end
65
+
66
+ # Whether a query pattern holds a matcher, so it can't be written into
67
+ # a URL.
68
+ def self.includes_query_matcher?(value)
69
+ case value
70
+ when Hash then value.each_value.any? { |v| includes_query_matcher?(v) }
71
+ when Array then value.any? { |v| includes_query_matcher?(v) }
72
+ when WebMock::Matchers::HashArgumentMatcher, NestedRSpecMatcher then true
73
+ else false
74
+ end
75
+ end
22
76
  end
@@ -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.38.0' unless defined?(::WebMock::VERSION)
5
5
  end
@@ -43,18 +43,39 @@ module WebMock
43
43
  end
44
44
  end
45
45
 
46
- def self.allow_net_connect!(options = {})
46
+ def self.allow_net_connect!(options = {}, &block)
47
+ return restoring_net_connect_settings { allow_net_connect!(options); block.call } if block
48
+
47
49
  Config.instance.allow_net_connect = true
48
50
  Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
49
51
  end
50
52
 
51
- def self.disable_net_connect!(options = {})
53
+ def self.disable_net_connect!(options = {}, &block)
54
+ return restoring_net_connect_settings { disable_net_connect!(options); block.call } if block
55
+
52
56
  Config.instance.allow_net_connect = false
53
57
  Config.instance.allow_localhost = options[:allow_localhost]
54
58
  Config.instance.allow = options[:allow]
55
59
  Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
56
60
  end
57
61
 
62
+ NET_CONNECT_SETTINGS = %i[allow_net_connect allow_localhost allow net_http_connect_on_start].freeze
63
+ private_constant :NET_CONNECT_SETTINGS
64
+
65
+ # Runs the block, then puts back the net connect settings from before it,
66
+ # even if the block raises. Settings assigned inside the block are undone.
67
+ # An allow list changed in place stays changed, and blocks overlapping in
68
+ # different threads can restore each other's settings in the wrong order.
69
+ def self.restoring_net_connect_settings
70
+ saved = NET_CONNECT_SETTINGS.map { |name| [name, Config.instance.public_send(name)] }
71
+ begin
72
+ yield
73
+ ensure
74
+ saved.each { |name, value| Config.instance.public_send(:"#{name}=", value) }
75
+ end
76
+ end
77
+ private_class_method :restoring_net_connect_settings
78
+
58
79
  class << self
59
80
  alias :enable_net_connect! :allow_net_connect!
60
81
  alias :disallow_net_connect! :disable_net_connect!
@@ -90,8 +111,10 @@ module WebMock
90
111
  allowed.match?(uri.to_s) ||
91
112
  (uri.port == uri.default_port && allowed.match?(uri.omit(:port).to_s))
92
113
  when String
93
- allowed == uri.host ||
94
- allowed == "#{uri.host}:#{uri.port}" ||
114
+ # Host names are case insensitive. casecmp folds ASCII letters only,
115
+ # unlike casecmp?, which would also equate "straße" and "strasse".
116
+ (uri.host && allowed.casecmp(uri.host) == 0) ||
117
+ allowed.casecmp("#{uri.host}:#{uri.port}") == 0 ||
95
118
  net_connect_allowed_url?(allowed, uri)
96
119
  else
97
120
  if allowed.respond_to?(:call)
data/lib/webmock.rb CHANGED
@@ -15,6 +15,7 @@ require_relative 'webmock/util/uri'
15
15
  require_relative 'webmock/util/headers'
16
16
  require_relative 'webmock/util/content_type'
17
17
  require_relative 'webmock/util/hash_counter'
18
+ require_relative 'webmock/util/upload_body'
18
19
  require_relative 'webmock/util/hash_keys_stringifier'
19
20
  require_relative 'webmock/util/values_stringifier'
20
21
  require_relative 'webmock/util/parsers/json'
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.38.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -350,6 +350,7 @@ files:
350
350
  - lib/webmock/util/parsers/parse_error.rb
351
351
  - lib/webmock/util/parsers/xml.rb
352
352
  - lib/webmock/util/query_mapper.rb
353
+ - lib/webmock/util/upload_body.rb
353
354
  - lib/webmock/util/uri.rb
354
355
  - lib/webmock/util/values_stringifier.rb
355
356
  - lib/webmock/util/version_checker.rb
@@ -360,9 +361,9 @@ licenses:
360
361
  - MIT
361
362
  metadata:
362
363
  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
364
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.38.0/CHANGELOG.md
365
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.38.0
366
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.38.0
366
367
  wiki_uri: https://github.com/bblimke/webmock/wiki
367
368
  rdoc_options: []
368
369
  require_paths: