webmock 3.26.2 → 3.34.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.
@@ -52,13 +52,22 @@ module WebMock
52
52
  self
53
53
  end
54
54
 
55
+ # Counts only the requests that also match the given conditions, which
56
+ # take the same options and block as RequestPattern#with. Like
57
+ # RequestPattern#with, calling it again adds to the conditions. The stub
58
+ # or request pattern under test is not changed.
59
+ def with(options = {}, &block)
60
+ (@conditions ||= RequestPattern.new(:any, ANY_URI)).with(options, &block)
61
+ self
62
+ end
63
+
55
64
  def matches?(request_pattern)
56
- @request_execution_verifier.request_pattern = request_pattern
65
+ @request_execution_verifier.request_pattern = with_conditions(request_pattern)
57
66
  @request_execution_verifier.matches?
58
67
  end
59
68
 
60
69
  def does_not_match?(request_pattern)
61
- @request_execution_verifier.request_pattern = request_pattern
70
+ @request_execution_verifier.request_pattern = with_conditions(request_pattern)
62
71
  @request_execution_verifier.does_not_match?
63
72
  end
64
73
 
@@ -76,5 +85,41 @@ module WebMock
76
85
 
77
86
  # RSpec 2 compatibility:
78
87
  alias_method :negative_failure_message, :failure_message_when_negated
88
+
89
+ private
90
+
91
+ ANY_URI = ->(_uri) { true }
92
+ private_constant :ANY_URI
93
+
94
+ # How the description of conditions starts when nothing was given to
95
+ # #with. What was given follows it, as in ' with body "abc"'.
96
+ EMPTY_CONDITIONS = RequestPattern.new(:any, ANY_URI).to_s.freeze
97
+ private_constant :EMPTY_CONDITIONS
98
+
99
+ def with_conditions(request_pattern)
100
+ @conditions ? PatternWithConditions.new(request_pattern, @conditions) : request_pattern
101
+ end
102
+
103
+ # Matches a request when both the pattern and the conditions match it.
104
+ class PatternWithConditions
105
+ def initialize(pattern, conditions)
106
+ @pattern = pattern
107
+ @conditions = conditions
108
+ end
109
+
110
+ # The parts that must all match, so JsonBodyHint can check each of them.
111
+ def patterns
112
+ [@pattern, @conditions]
113
+ end
114
+
115
+ def matches?(request_signature)
116
+ @pattern.matches?(request_signature) && @conditions.matches?(request_signature)
117
+ end
118
+
119
+ def to_s
120
+ "#{@pattern} (only requests#{@conditions.to_s.delete_prefix(EMPTY_CONDITIONS)})"
121
+ end
122
+ end
123
+ private_constant :PatternWithConditions
79
124
  end
80
125
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WebMock
4
+ module Util
5
+ module ContentType
6
+ BODY_FORMATS = {
7
+ 'text/xml' => :xml,
8
+ 'application/xml' => :xml,
9
+ 'application/json' => :json,
10
+ 'text/json' => :json,
11
+ 'application/javascript' => :json,
12
+ 'text/javascript' => :json,
13
+ 'text/html' => :html,
14
+ 'application/x-yaml' => :yaml,
15
+ 'text/yaml' => :yaml,
16
+ 'text/plain' => :plain
17
+ }
18
+
19
+ def self.media_type(content_type)
20
+ content_type.to_s.scrub.split(';', 2).first.to_s.strip.downcase
21
+ end
22
+
23
+ def self.url_encoded?(content_type)
24
+ media_type(content_type) == 'application/x-www-form-urlencoded'
25
+ end
26
+
27
+ def self.body_format(content_type)
28
+ type = media_type(content_type)
29
+
30
+ BODY_FORMATS.fetch(type) do
31
+ case type
32
+ when %r{\A[^/\s]+/[^/\s]+\+json\z} then :json
33
+ when %r{\A[^/\s]+/[^/\s]+\+xml\z} then :xml
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -58,8 +58,8 @@ module WebMock
58
58
 
59
59
  def self.strip_default_port_from_uri_string(uri_string)
60
60
  case uri_string
61
- when %r{^http://} then uri_string.sub(%r{:80(/|$)}, '\1')
62
- when %r{^https://} then uri_string.sub(%r{:443(/|$)}, '\1')
61
+ when %r{^http://} then uri_string.sub(%r{\A(http://[^/?#]+):80(?=[/?#]|\z)}, '\1')
62
+ when %r{^https://} then uri_string.sub(%r{\A(https://[^/?#]+):443(?=[/?#]|\z)}, '\1')
63
63
  else uri_string
64
64
  end
65
65
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WebMock
4
- VERSION = '3.26.2' unless defined?(::WebMock::VERSION)
4
+ VERSION = '3.34.0' unless defined?(::WebMock::VERSION)
5
5
  end
@@ -87,14 +87,12 @@ module WebMock
87
87
  when Array
88
88
  allowed.any? { |allowed_item| net_connect_explicit_allowed?(allowed_item, uri) }
89
89
  when Regexp
90
- (uri.to_s =~ allowed) != nil ||
91
- (uri.omit(:port).to_s =~ allowed) != nil && uri.port == uri.default_port
90
+ allowed.match?(uri.to_s) ||
91
+ (uri.port == uri.default_port && allowed.match?(uri.omit(:port).to_s))
92
92
  when String
93
- allowed == uri.to_s ||
94
93
  allowed == uri.host ||
95
94
  allowed == "#{uri.host}:#{uri.port}" ||
96
- allowed == "#{uri.scheme}://#{uri.host}:#{uri.port}" ||
97
- allowed == "#{uri.scheme}://#{uri.host}" && uri.port == uri.default_port
95
+ net_connect_allowed_url?(allowed, uri)
98
96
  else
99
97
  if allowed.respond_to?(:call)
100
98
  allowed.call(uri)
@@ -102,6 +100,28 @@ module WebMock
102
100
  end
103
101
  end
104
102
 
103
+ def self.net_connect_allowed_url?(allowed, uri)
104
+ return false unless allowed.match?(%r{\Ahttps?://}i)
105
+
106
+ # Normalization turns an empty path into "/", so check the original URL.
107
+ allowed_uri = Addressable::URI.parse(allowed)
108
+ allow_any_path = allowed_uri.path.empty? && allowed_uri.query.nil?
109
+
110
+ # Connection URIs have no request path to match yet.
111
+ return false if uri.path.empty? && !allowed_uri.path.empty?
112
+
113
+ allowed_uri = WebMock::Util::URI.normalize_uri(allowed_uri)
114
+ request_uri = WebMock::Util::URI.normalize_uri(uri)
115
+
116
+ return false unless allowed_uri.origin == request_uri.origin
117
+ return true if allow_any_path
118
+
119
+ allowed_uri.path == request_uri.path && allowed_uri.query == request_uri.query
120
+ rescue Addressable::URI::InvalidURIError
121
+ false
122
+ end
123
+ private_class_method :net_connect_allowed_url?
124
+
105
125
  def self.show_body_diff!
106
126
  Config.instance.show_body_diff = true
107
127
  end
data/lib/webmock.rb CHANGED
@@ -13,6 +13,7 @@ require_relative 'webmock/errors'
13
13
  require_relative 'webmock/util/query_mapper'
14
14
  require_relative 'webmock/util/uri'
15
15
  require_relative 'webmock/util/headers'
16
+ require_relative 'webmock/util/content_type'
16
17
  require_relative 'webmock/util/hash_counter'
17
18
  require_relative 'webmock/util/hash_keys_stringifier'
18
19
  require_relative 'webmock/util/values_stringifier'
@@ -36,6 +37,7 @@ require_relative 'webmock/rack_response'
36
37
  require_relative 'webmock/stub_request_snippet'
37
38
  require_relative 'webmock/request_signature_snippet'
38
39
  require_relative 'webmock/request_body_diff'
40
+ require_relative 'webmock/json_body_hint'
39
41
 
40
42
  require_relative 'webmock/assertion_failure'
41
43
  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.26.2
4
+ version: 3.34.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartosz Blimke
@@ -318,6 +318,7 @@ files:
318
318
  - lib/webmock/http_lib_adapters/net_http_response.rb
319
319
  - lib/webmock/http_lib_adapters/patron_adapter.rb
320
320
  - lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb
321
+ - lib/webmock/json_body_hint.rb
321
322
  - lib/webmock/matchers/any_arg_matcher.rb
322
323
  - lib/webmock/matchers/hash_argument_matcher.rb
323
324
  - lib/webmock/matchers/hash_excluding_matcher.rb
@@ -340,6 +341,7 @@ files:
340
341
  - lib/webmock/stub_registry.rb
341
342
  - lib/webmock/stub_request_snippet.rb
342
343
  - lib/webmock/test_unit.rb
344
+ - lib/webmock/util/content_type.rb
343
345
  - lib/webmock/util/hash_counter.rb
344
346
  - lib/webmock/util/hash_keys_stringifier.rb
345
347
  - lib/webmock/util/hash_validator.rb
@@ -358,9 +360,9 @@ licenses:
358
360
  - MIT
359
361
  metadata:
360
362
  bug_tracker_uri: https://github.com/bblimke/webmock/issues
361
- changelog_uri: https://github.com/bblimke/webmock/blob/v3.26.2/CHANGELOG.md
362
- documentation_uri: https://www.rubydoc.info/gems/webmock/3.26.2
363
- source_code_uri: https://github.com/bblimke/webmock/tree/v3.26.2
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
364
366
  wiki_uri: https://github.com/bblimke/webmock/wiki
365
367
  rdoc_options: []
366
368
  require_paths:
@@ -376,7 +378,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
376
378
  - !ruby/object:Gem::Version
377
379
  version: '0'
378
380
  requirements: []
379
- rubygems_version: 4.1.0.dev
381
+ rubygems_version: 4.0.20
380
382
  specification_version: 4
381
383
  summary: Library for stubbing HTTP requests in Ruby.
382
384
  test_files: []