webmock 3.29.0 → 3.30.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 +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +25 -2
- data/lib/webmock/json_body_hint.rb +34 -10
- data/lib/webmock/request_pattern.rb +1 -1
- data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +47 -2
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +25 -5
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0be0b6d1129e7014a36a5dbe7a7e77d3a25852d1668f54c455e0be96cd2d1287
|
|
4
|
+
data.tar.gz: db8c3267c3fbf2eef3b69f4b72764a82983e589a6508c9d0be8d2cd53047d484
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ad6692051819395acd28a095acb54762b81b379b9d75115f0572351f2693973558978b24392f2086bb2dd6bb3969730881efb0d2dc54210440c9114bc2c867b6
|
|
7
|
+
data.tar.gz: d4c877e82c741f3319e66d803421eb0fb52f3d1a798465bc19c8b55e52d00fc57fca89cd5eb82a52f88d9f0223c2784adcb7110838405a42f05e9e5c26f53561
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
# 3.30.0
|
|
4
|
+
|
|
5
|
+
* Allow narrowing an RSpec expectation on a stub with `with`, as in `expect(stub).to have_been_requested.once.with(body: "abc")` (#1128)
|
|
6
|
+
|
|
7
|
+
The expectation counts only the requests that match both the stub and the conditions given to `with`. The stub itself does not change.
|
|
8
|
+
|
|
9
|
+
Thanks to [Sébastien S.](https://github.com/inkstak)
|
|
10
|
+
|
|
11
|
+
* Show the JSON body hint for expectations narrowed with `with` too, and no longer show it when a block is given to the stub or expectation, since checking would mean calling the block
|
|
12
|
+
|
|
13
|
+
* Allow a full URL with a path in `disable_net_connect!(allow: ...)`, as in `allow: "https://github.com/mozilla/geckodriver/releases/latest"` (#977)
|
|
14
|
+
|
|
15
|
+
URLs are compared the same way as in stubs, so the default port, letter case in the host, and the order and encoding of query parameters don't matter. A URL without a path or query allows every path on that scheme, host and port; a trailing slash restricts requests to `/` with no query string. Credentials in either URL are ignored.
|
|
16
|
+
|
|
17
|
+
Thanks to [Henry Blyth](https://github.com/henrahmagix)
|
|
18
|
+
|
|
3
19
|
# 3.29.0
|
|
4
20
|
|
|
5
21
|
* Show a body diff for JSON requests whose content type has a `+json` suffix, such as `application/vnd.api+json`, and for the other content types WebMock already matches as JSON (#734)
|
data/README.md
CHANGED
|
@@ -573,6 +573,21 @@ RestClient.get('www.example.org', '/') # ===> Failure
|
|
|
573
573
|
RestClient.get('www.example.org:8080', '/') # ===> Allowed
|
|
574
574
|
```
|
|
575
575
|
|
|
576
|
+
With a `String` specifying a full URL:
|
|
577
|
+
|
|
578
|
+
```ruby
|
|
579
|
+
WebMock.disable_net_connect!(allow: 'https://www.example.org/health')
|
|
580
|
+
|
|
581
|
+
RestClient.get('https://www.example.org/health') # ===> Allowed
|
|
582
|
+
RestClient.get('https://www.example.org/health?full=1') # ===> Failure
|
|
583
|
+
RestClient.get('https://www.example.org/health/db') # ===> Failure
|
|
584
|
+
RestClient.get('https://www.example.org/') # ===> Failure
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
A URL with a path allows only that path and query string. URLs are compared the same way as in stubs, so the default port, letter case in the host, and the order and encoding of query parameters don't matter.
|
|
588
|
+
|
|
589
|
+
A URL without a path or query, such as `https://www.example.org`, allows every path on that scheme, host and port. Adding the trailing slash (`https://www.example.org/`) allows only the root path `/` with no query string. To allow every path under a prefix, use a `Regexp`.
|
|
590
|
+
|
|
576
591
|
With a `Regexp` matching the URI:
|
|
577
592
|
|
|
578
593
|
```ruby
|
|
@@ -637,6 +652,8 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`. At
|
|
|
637
652
|
WebMock.allow_net_connect!(net_http_connect_on_start: "www.example.com")
|
|
638
653
|
```
|
|
639
654
|
|
|
655
|
+
The path is not known when the connection is made, so a URL with a path given here never matches.
|
|
656
|
+
|
|
640
657
|
## Setting Expectations
|
|
641
658
|
|
|
642
659
|
### Setting expectations in Test::Unit
|
|
@@ -751,11 +768,17 @@ expect(a_request(:post, "www.example.com").
|
|
|
751
768
|
### Setting expectations in RSpec on the stub
|
|
752
769
|
|
|
753
770
|
```ruby
|
|
754
|
-
stub = stub_request(:
|
|
771
|
+
stub = stub_request(:post, "www.example.com")
|
|
772
|
+
|
|
755
773
|
# ... make requests ...
|
|
756
|
-
|
|
774
|
+
|
|
775
|
+
expect(stub).to have_been_requested.twice
|
|
776
|
+
expect(stub).to have_been_requested.once.with(body: {"a" => [1, 2, 3]})
|
|
777
|
+
expect(stub).to have_been_requested.once.with(body: {"a" => [4, 5, 6]})
|
|
757
778
|
```
|
|
758
779
|
|
|
780
|
+
Options and a block given to `with` take the same form as in `stub_request(...).with`. The expectation then counts only the requests that match both the stub and those conditions. The stub itself does not change.
|
|
781
|
+
|
|
759
782
|
## Clearing stubs and request history
|
|
760
783
|
|
|
761
784
|
If you want to reset all current stubs and history of requests use `WebMock.reset!`
|
|
@@ -17,24 +17,48 @@ module WebMock
|
|
|
17
17
|
EXPECTATION_HINT = "Hint: a request was made with the same JSON body as expected, " \
|
|
18
18
|
"but formatted differently, for example with keys in another order. #{ADVICE}"
|
|
19
19
|
|
|
20
|
-
# True when the
|
|
21
|
-
# the request has a JSON content type and a body that parses to the
|
|
22
|
-
# JSON but is written differently, and the
|
|
23
|
-
# match the request.
|
|
20
|
+
# True when one of the patterns declares its body as a JSON object or array
|
|
21
|
+
# string, the request has a JSON content type and a body that parses to the
|
|
22
|
+
# same JSON but is written differently, and the method, URI and headers of
|
|
23
|
+
# every pattern match the request. Any other patterns the request has to
|
|
24
|
+
# match, such as the conditions of an RSpec expectation, must not declare
|
|
25
|
+
# a body of their own.
|
|
26
|
+
#
|
|
27
|
+
# Building a failure message must not call a block given to with, which
|
|
28
|
+
# could raise and replace the failure, or repeat side effects. So there is
|
|
29
|
+
# no hint when any of the patterns has one.
|
|
30
|
+
#
|
|
31
|
+
# The body is checked first, since that runs no user code, so URI and
|
|
32
|
+
# header matchers are only evaluated when a hint is otherwise possible.
|
|
24
33
|
def self.applies?(request_pattern, request_signature)
|
|
25
|
-
|
|
34
|
+
patterns = parts_of(request_pattern)
|
|
35
|
+
return false unless patterns.all? { |pattern| pattern.is_a?(RequestPattern) && pattern.with_block.nil? }
|
|
26
36
|
|
|
27
|
-
|
|
37
|
+
with_body = patterns.select(&:body_pattern)
|
|
38
|
+
return false unless with_body.size == 1
|
|
39
|
+
return false unless body_differs_only_in_formatting?(with_body.first.body_pattern, request_signature)
|
|
40
|
+
|
|
41
|
+
patterns.all? { |pattern| matches_except_body?(pattern, request_signature) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# A pattern made of parts that must all match, such as an RSpec expectation
|
|
45
|
+
# with conditions, responds to #patterns. A stub stands for its pattern.
|
|
46
|
+
def self.parts_of(request_pattern)
|
|
47
|
+
parts = request_pattern.respond_to?(:patterns) ? request_pattern.patterns : [request_pattern]
|
|
48
|
+
parts.map { |part| part.is_a?(RequestStub) ? part.request_pattern : part }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.body_differs_only_in_formatting?(body_pattern, request_signature)
|
|
52
|
+
expected = body_pattern.pattern
|
|
28
53
|
actual = request_signature.body
|
|
29
54
|
return false unless expected.is_a?(String) && actual.is_a?(String) && expected != actual
|
|
30
55
|
return false unless request_signature.json_headers?
|
|
31
56
|
|
|
32
57
|
expected_json = parse_json(expected)
|
|
33
58
|
return false unless expected_json.is_a?(Hash) || expected_json.is_a?(Array)
|
|
34
|
-
# eql? compares nested values strictly, so 1 and 1.0 are different.
|
|
35
|
-
return false unless expected_json.eql?(parse_json(actual))
|
|
36
59
|
|
|
37
|
-
|
|
60
|
+
# eql? compares nested values strictly, so 1 and 1.0 are different.
|
|
61
|
+
expected_json.eql?(parse_json(actual))
|
|
38
62
|
end
|
|
39
63
|
|
|
40
64
|
def self.matches_except_body?(request_pattern, request_signature)
|
|
@@ -48,6 +72,6 @@ module WebMock
|
|
|
48
72
|
rescue ::JSON::JSONError, EncodingError
|
|
49
73
|
nil
|
|
50
74
|
end
|
|
51
|
-
private_class_method :matches_except_body?, :parse_json
|
|
75
|
+
private_class_method :parts_of, :body_differs_only_in_formatting?, :matches_except_body?, :parse_json
|
|
52
76
|
end
|
|
53
77
|
end
|
|
@@ -14,7 +14,7 @@ module WebMock
|
|
|
14
14
|
|
|
15
15
|
class RequestPattern
|
|
16
16
|
|
|
17
|
-
attr_reader :method_pattern, :uri_pattern, :body_pattern, :headers_pattern
|
|
17
|
+
attr_reader :method_pattern, :uri_pattern, :body_pattern, :headers_pattern, :with_block
|
|
18
18
|
|
|
19
19
|
def initialize(method, uri, options = {})
|
|
20
20
|
@method_pattern = MethodPattern.new(method)
|
|
@@ -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
|
data/lib/webmock/version.rb
CHANGED
data/lib/webmock/webmock.rb
CHANGED
|
@@ -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
|
|
91
|
-
(uri.
|
|
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
|
|
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
|
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.
|
|
4
|
+
version: 3.30.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.
|
|
364
|
-
documentation_uri: https://www.rubydoc.info/gems/webmock/3.
|
|
365
|
-
source_code_uri: https://github.com/bblimke/webmock/tree/v3.
|
|
363
|
+
changelog_uri: https://github.com/bblimke/webmock/blob/v3.30.0/CHANGELOG.md
|
|
364
|
+
documentation_uri: https://www.rubydoc.info/gems/webmock/3.30.0
|
|
365
|
+
source_code_uri: https://github.com/bblimke/webmock/tree/v3.30.0
|
|
366
366
|
wiki_uri: https://github.com/bblimke/webmock/wiki
|
|
367
367
|
rdoc_options: []
|
|
368
368
|
require_paths:
|