webmock 3.36.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.
@@ -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.36.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.36.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.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
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: