webmock 3.34.0 → 3.41.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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +358 -0
  3. data/README.md +222 -3
  4. data/lib/webmock/api.rb +4 -0
  5. data/lib/webmock/errors.rb +1 -1
  6. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +51 -7
  7. data/lib/webmock/http_lib_adapters/curb_adapter.rb +291 -39
  8. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +11 -3
  9. data/lib/webmock/http_lib_adapters/excon_adapter.rb +23 -2
  10. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +33 -4
  11. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +4 -1
  12. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +132 -110
  13. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +1 -1
  14. data/lib/webmock/http_lib_adapters/net_http.rb +66 -3
  15. data/lib/webmock/http_lib_adapters/net_http_response.rb +28 -2
  16. data/lib/webmock/http_lib_adapters/patron_adapter.rb +242 -40
  17. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +65 -7
  18. data/lib/webmock/json_body_hint.rb +8 -5
  19. data/lib/webmock/request_body_diff.rb +16 -6
  20. data/lib/webmock/request_execution_verifier.rb +13 -2
  21. data/lib/webmock/request_pattern.rb +236 -36
  22. data/lib/webmock/request_registry.rb +75 -5
  23. data/lib/webmock/request_signature.rb +44 -5
  24. data/lib/webmock/request_signature_snippet.rb +23 -2
  25. data/lib/webmock/request_stub.rb +8 -4
  26. data/lib/webmock/response.rb +42 -3
  27. data/lib/webmock/stub_registry.rb +7 -6
  28. data/lib/webmock/stub_request_snippet.rb +9 -3
  29. data/lib/webmock/trailing_slash_hint.rb +55 -0
  30. data/lib/webmock/util/hash_counter.rb +27 -8
  31. data/lib/webmock/util/hash_keys_stringifier.rb +23 -1
  32. data/lib/webmock/util/headers.rb +17 -0
  33. data/lib/webmock/util/message_string.rb +28 -0
  34. data/lib/webmock/util/parsers/json.rb +33 -2
  35. data/lib/webmock/util/query_mapper.rb +56 -37
  36. data/lib/webmock/util/upload_body.rb +63 -0
  37. data/lib/webmock/util/uri.rb +21 -12
  38. data/lib/webmock/util/values_stringifier.rb +54 -0
  39. data/lib/webmock/version.rb +1 -1
  40. data/lib/webmock/webmock.rb +35 -4
  41. data/lib/webmock.rb +3 -0
  42. metadata +7 -4
@@ -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.34.0' unless defined?(::WebMock::VERSION)
4
+ VERSION = '3.41.0' unless defined?(::WebMock::VERSION)
5
5
  end
@@ -43,24 +43,53 @@ 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!
61
82
  end
62
83
 
63
84
  def self.net_connect_allowed?(uri = nil)
85
+ net_connect_allowed_by_settings?(uri)
86
+ end
87
+
88
+ # Whether WebMock's own settings allow a real connection. VCR, for one,
89
+ # redefines net_connect_allowed? to allow every request and decides each
90
+ # one in a global stub, so a connection that no stub sees, such as one
91
+ # taken from an async-http connection pool, is checked with this.
92
+ def self.net_connect_allowed_by_settings?(uri = nil)
64
93
  return !!Config.instance.allow_net_connect if uri.nil?
65
94
 
66
95
  if uri.is_a?(String)
@@ -90,8 +119,10 @@ module WebMock
90
119
  allowed.match?(uri.to_s) ||
91
120
  (uri.port == uri.default_port && allowed.match?(uri.omit(:port).to_s))
92
121
  when String
93
- allowed == uri.host ||
94
- allowed == "#{uri.host}:#{uri.port}" ||
122
+ # Host names are case insensitive. casecmp folds ASCII letters only,
123
+ # unlike casecmp?, which would also equate "straße" and "strasse".
124
+ (uri.host && allowed.casecmp(uri.host) == 0) ||
125
+ allowed.casecmp("#{uri.host}:#{uri.port}") == 0 ||
95
126
  net_connect_allowed_url?(allowed, uri)
96
127
  else
97
128
  if allowed.respond_to?(:call)
data/lib/webmock.rb CHANGED
@@ -15,6 +15,8 @@ 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'
19
+ require_relative 'webmock/util/message_string'
18
20
  require_relative 'webmock/util/hash_keys_stringifier'
19
21
  require_relative 'webmock/util/values_stringifier'
20
22
  require_relative 'webmock/util/parsers/json'
@@ -38,6 +40,7 @@ require_relative 'webmock/stub_request_snippet'
38
40
  require_relative 'webmock/request_signature_snippet'
39
41
  require_relative 'webmock/request_body_diff'
40
42
  require_relative 'webmock/json_body_hint'
43
+ require_relative 'webmock/trailing_slash_hint'
41
44
 
42
45
  require_relative 'webmock/assertion_failure'
43
46
  require_relative 'webmock/request_execution_verifier'
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.34.0
4
+ version: 3.41.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -341,15 +341,18 @@ files:
341
341
  - lib/webmock/stub_registry.rb
342
342
  - lib/webmock/stub_request_snippet.rb
343
343
  - lib/webmock/test_unit.rb
344
+ - lib/webmock/trailing_slash_hint.rb
344
345
  - lib/webmock/util/content_type.rb
345
346
  - lib/webmock/util/hash_counter.rb
346
347
  - lib/webmock/util/hash_keys_stringifier.rb
347
348
  - lib/webmock/util/hash_validator.rb
348
349
  - lib/webmock/util/headers.rb
350
+ - lib/webmock/util/message_string.rb
349
351
  - lib/webmock/util/parsers/json.rb
350
352
  - lib/webmock/util/parsers/parse_error.rb
351
353
  - lib/webmock/util/parsers/xml.rb
352
354
  - lib/webmock/util/query_mapper.rb
355
+ - lib/webmock/util/upload_body.rb
353
356
  - lib/webmock/util/uri.rb
354
357
  - lib/webmock/util/values_stringifier.rb
355
358
  - lib/webmock/util/version_checker.rb
@@ -360,9 +363,9 @@ licenses:
360
363
  - MIT
361
364
  metadata:
362
365
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
363
- changelog_uri: https://github.com/bblimke/webmock/blob/v3.34.0/CHANGELOG.md
364
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.34.0
365
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.34.0
366
+ changelog_uri: https://github.com/bblimke/webmock/blob/v3.41.0/CHANGELOG.md
367
+ documentation_uri: https://www.rubydoc.info/gems/webmock/3.41.0
368
+ source_code_uri: https://github.com/bblimke/webmock/tree/v3.41.0
366
369
  wiki_uri: https://github.com/bblimke/webmock/wiki
367
370
  rdoc_options: []
368
371
  require_paths: