webmock 4.3.0 → 4.4.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 +14 -0
- data/README.md +16 -0
- data/lib/webmock/json_body_hint.rb +3 -21
- data/lib/webmock/matchers/hash_argument_matcher.rb +2 -0
- data/lib/webmock/request_body_diff.rb +205 -36
- data/lib/webmock/request_execution_verifier.rb +26 -1
- data/lib/webmock/request_pattern.rb +45 -0
- data/lib/webmock/request_signature_snippet.rb +7 -17
- data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +2 -2
- data/lib/webmock/text_body_diff.rb +46 -0
- data/lib/webmock/trailing_slash_hint.rb +1 -9
- data/lib/webmock/util/upload_body.rb +12 -9
- data/lib/webmock/version.rb +1 -1
- metadata +19 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7ddc06322fc61ec3343308f1a5c74633f467978a806c8063fcc2ac29aacc4979
|
|
4
|
+
data.tar.gz: a7fcd951369a1d0cafdef12294b3ef1313c939f2f9f84dfed6bb398450e38c05
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4b30ba6991bd23b204f4ea9b662e85e977530dee690a2d9b86ae514515fca79ff07fd3475e5042a606acad5621dcc01ce6c9a760102af3cea0c47e35ca632358
|
|
7
|
+
data.tar.gz: abe4fd080b8d4bba544c7df4b45e9faf698e5dd1e421cb1351e88ba2165ec342daaec444b5ade3c0b5e329393885f8b4ae1e7764919e9f06a82e039561cb5a75
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
# 4.4.0
|
|
4
|
+
|
|
5
|
+
* Show request body differences when `assert_requested` or an RSpec request expectation fails (#825)
|
|
6
|
+
|
|
7
|
+
If your test expects `POST /orders` with `"total": 10` in the JSON body and the request omits that field, the failure message now shows `total: expected 10, but the field was missing`. Previously, you had to find the difference in the request history. WebMock shows a short diff when one distinct request matches the method and URL and its body explains the mismatch.
|
|
8
|
+
|
|
9
|
+
For multiline XML and plain text, the diff shows line numbers, with `-` for expected lines and `+` for lines actually sent. This uses the `diff-lcs` gem. Unregistered request errors previously printed JSON differences as arrays such as `["+", "total", 10]`. They now use the same wording and skip stubs with a different method or URL. When several JSON fields differ, both messages show the expected and recorded bodies if that takes no more space than listing the changes.
|
|
10
|
+
|
|
11
|
+
Thanks to [Adrian1707](https://github.com/Adrian1707) and [odinhb](https://github.com/odinhb) for the examples.
|
|
12
|
+
|
|
13
|
+
* Fix `IOError: ungetbyte failed` when matching large file uploads
|
|
14
|
+
|
|
15
|
+
With Curb, Patron or em-http-request, even calling `file.eof?` before an upload could trigger the error and leave the file at the end. WebMock now matches and records the bytes from the current position and leaves the file ready for the HTTP client to read.
|
|
16
|
+
|
|
3
17
|
# 4.3.0
|
|
4
18
|
|
|
5
19
|
* Allow real requests except to destinations listed in `disallow:` (#1058)
|
data/README.md
CHANGED
|
@@ -957,6 +957,22 @@ To make an expectation stricter, give the stub its own conditions, such as `with
|
|
|
957
957
|
|
|
958
958
|
WebMock may run a `with` block once for several identical requests and multiply the count, so keep such blocks free of side effects. To look at every request, use [`executed_requests`](#inspecting-the-requests-made).
|
|
959
959
|
|
|
960
|
+
### Reading a request body diff
|
|
961
|
+
|
|
962
|
+
If an expectation fails and one distinct request was made to the expected method and URL, WebMock can show how its body differed. For JSON, each line names the field or array position, for example `total: expected 10, got 12` or `items[3]: expected "SKU-4", got "SKU-5"`. When several values differ, WebMock shows both bodies if that takes no more space than listing the changes.
|
|
963
|
+
|
|
964
|
+
For multiline XML or plain text, use a string as the expected body. The diff shows the line numbers and the expected and recorded lines.
|
|
965
|
+
|
|
966
|
+
```diff
|
|
967
|
+
@@ -2 +2 @@
|
|
968
|
+
- <count>10</count>
|
|
969
|
+
+ <count>11</count>
|
|
970
|
+
```
|
|
971
|
+
|
|
972
|
+
If several distinct requests could explain a failed expectation, WebMock lists the request history. You also get the history when a comparison is too large or the expected body uses a partial matcher such as `hash_including`. Use `WebMock.hide_body_diff!` to always show the history for failed expectations.
|
|
973
|
+
|
|
974
|
+
For an unregistered request, the diff appears beneath each matching method and URL in the stub list. WebMock skips the diff if it would need to call your URL or query matcher again.
|
|
975
|
+
|
|
960
976
|
### Inspecting the requests made
|
|
961
977
|
|
|
962
978
|
`WebMock.executed_requests` lists every request made since the last `WebMock.reset!` or `WebMock.reset_executed_requests!`, in the order they were made, including repeats. It lets you check what your code sent with plain Ruby.
|
|
@@ -56,7 +56,7 @@ module WebMock
|
|
|
56
56
|
# The body is checked first, since that runs no user code, so URI and
|
|
57
57
|
# header matchers are only evaluated when a hint is otherwise possible.
|
|
58
58
|
def self.applies?(request_pattern, request_signature)
|
|
59
|
-
patterns, body_pattern =
|
|
59
|
+
patterns, body_pattern = RequestPattern.body_for_diagnostics(request_pattern)
|
|
60
60
|
return false unless body_pattern && body_differs_only_in_formatting?(body_pattern, request_signature)
|
|
61
61
|
|
|
62
62
|
patterns.all? { |pattern| matches_except_body?(pattern, request_signature) }
|
|
@@ -81,7 +81,7 @@ module WebMock
|
|
|
81
81
|
# method, URI and headers of all of its parts match the request. The
|
|
82
82
|
# body is parsed at most once, and only when a pattern could get the hint.
|
|
83
83
|
def self.invalid_json_hint(request_patterns, request_signature, kind)
|
|
84
|
-
candidates = request_patterns.filter_map { |request_pattern|
|
|
84
|
+
candidates = request_patterns.filter_map { |request_pattern| RequestPattern.body_for_diagnostics(request_pattern) }
|
|
85
85
|
# Method patterns run no user code, so they can rule out patterns
|
|
86
86
|
# before the body is parsed.
|
|
87
87
|
candidates.select! do |patterns, body_pattern|
|
|
@@ -98,24 +98,6 @@ module WebMock
|
|
|
98
98
|
nil
|
|
99
99
|
end
|
|
100
100
|
|
|
101
|
-
# A pattern made of parts that must all match, such as an RSpec expectation
|
|
102
|
-
# with conditions, responds to #patterns. A stub stands for its pattern.
|
|
103
|
-
def self.parts_of(request_pattern)
|
|
104
|
-
parts = request_pattern.respond_to?(:patterns) ? request_pattern.patterns : [request_pattern]
|
|
105
|
-
parts.map { |part| part.is_a?(RequestStub) ? part.request_pattern : part }
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
# The parts of the pattern and the body pattern of the only part that
|
|
109
|
-
# declares a body, or nil when none or several do, or when a part has a
|
|
110
|
-
# block given to with.
|
|
111
|
-
def self.body_pattern_of(request_pattern)
|
|
112
|
-
patterns = parts_of(request_pattern)
|
|
113
|
-
return unless patterns.all? { |pattern| pattern.is_a?(RequestPattern) && pattern.with_block.nil? }
|
|
114
|
-
|
|
115
|
-
with_body = patterns.select(&:body_pattern)
|
|
116
|
-
[patterns, with_body.first.body_pattern] if with_body.size == 1
|
|
117
|
-
end
|
|
118
|
-
|
|
119
101
|
def self.body_differs_only_in_formatting?(body_pattern, request_signature)
|
|
120
102
|
expected = body_pattern.pattern
|
|
121
103
|
actual = request_signature.body
|
|
@@ -178,7 +160,7 @@ module WebMock
|
|
|
178
160
|
rescue ::JSON::JSONError, EncodingError, WebMock::Util::Parsers::ParseError
|
|
179
161
|
nil
|
|
180
162
|
end
|
|
181
|
-
private_class_method :
|
|
163
|
+
private_class_method :body_differs_only_in_formatting?, :fails_only_on_body?,
|
|
182
164
|
:parse_error, :short_error, :matches_except_body?, :parse_json
|
|
183
165
|
end
|
|
184
166
|
end
|
|
@@ -5,6 +5,8 @@ module WebMock
|
|
|
5
5
|
# Base class for Hash matchers
|
|
6
6
|
# https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/argument_matchers.rb
|
|
7
7
|
class HashArgumentMatcher
|
|
8
|
+
attr_reader :expected
|
|
9
|
+
|
|
8
10
|
def initialize(expected)
|
|
9
11
|
@expected = Hash[WebMock::Util::HashKeysStringifier.stringify_keys!(expected, deep: true).sort]
|
|
10
12
|
end
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "hashdiff"
|
|
4
|
+
require "pp"
|
|
5
|
+
require "stringio"
|
|
6
|
+
require_relative "text_body_diff"
|
|
4
7
|
|
|
5
8
|
module WebMock
|
|
6
9
|
class RequestBodyDiff
|
|
@@ -10,20 +13,28 @@ module WebMock
|
|
|
10
13
|
@request_stub = request_stub
|
|
11
14
|
end
|
|
12
15
|
|
|
13
|
-
def
|
|
14
|
-
return
|
|
16
|
+
def formatted_for_expectation
|
|
17
|
+
return formatted unless request_signature.json_headers?
|
|
18
|
+
return if Hash === request_stub_body && request_stub_body.empty? # matches any JSON object
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
message = formatted_json(bounded: true)
|
|
21
|
+
message if message && message.bytesize <= 2000
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def formatted
|
|
25
|
+
return formatted_json if request_signature.json_headers?
|
|
26
|
+
|
|
27
|
+
type = Util::ContentType.media_type(request_signature.content_type)
|
|
28
|
+
return unless type.empty? || type.start_with?("text/") ||
|
|
29
|
+
Util::ContentType.body_format(type) == :xml
|
|
30
|
+
|
|
31
|
+
text = TextBodyDiff.between(request_stub_body, actual_body)
|
|
32
|
+
"Body diff (- expected, + actual):\n#{text}" if text
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def body_diff
|
|
36
|
+
bodies = json_bodies if request_signature.json_headers?
|
|
37
|
+
bodies ? compare_json(*bodies) : {}
|
|
27
38
|
end
|
|
28
39
|
|
|
29
40
|
attr_reader :request_signature, :request_stub
|
|
@@ -31,46 +42,204 @@ module WebMock
|
|
|
31
42
|
|
|
32
43
|
private
|
|
33
44
|
|
|
34
|
-
def
|
|
35
|
-
|
|
45
|
+
def json_bodies(bounded: false)
|
|
46
|
+
expected = request_stub_body
|
|
47
|
+
actual = actual_body
|
|
48
|
+
return unless String === actual && (Hash === expected || Array === expected || String === expected)
|
|
49
|
+
|
|
50
|
+
bounded ||= Array === expected
|
|
51
|
+
if bounded
|
|
52
|
+
return if actual.bytesize > 16_384 || (String === expected && expected.bytesize > 16_384)
|
|
53
|
+
end
|
|
54
|
+
actual = Util::Parsers::JSON.parse(actual)
|
|
55
|
+
expected = Util::Parsers::JSON.parse(expected) if String === expected
|
|
56
|
+
return if bounded && !(small_json_value?(expected) && small_json_value?(actual))
|
|
57
|
+
|
|
58
|
+
[actual, expected]
|
|
59
|
+
rescue Util::Parsers::ParseError
|
|
60
|
+
nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def compare_json(actual, expected, array_path: false)
|
|
64
|
+
# For a Ruby hash or array, 1 matches 1.0. A JSON string matches by bytes.
|
|
65
|
+
Hashdiff.diff(actual, expected, strict: String === request_stub_body, array_path: array_path) do |_path, got, wanted|
|
|
66
|
+
begin
|
|
67
|
+
true if Regexp === wanted && String === got && wanted.match?(got)
|
|
68
|
+
rescue EncodingError
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
end
|
|
36
72
|
end
|
|
37
73
|
|
|
38
|
-
def
|
|
39
|
-
|
|
74
|
+
def formatted_json(bounded: false)
|
|
75
|
+
bodies = json_bodies(bounded: bounded)
|
|
76
|
+
return unless bodies
|
|
77
|
+
changes = compare_json(*bodies, array_path: true)
|
|
78
|
+
return if changes.empty?
|
|
79
|
+
|
|
80
|
+
changes = array_changes_for_display(changes, *bodies)
|
|
81
|
+
fields = "Body diff:\n#{changes.map { |change| describe_change(*change) }.join}"
|
|
82
|
+
return fields if changes.size == 1
|
|
83
|
+
|
|
84
|
+
actual, expected = bodies
|
|
85
|
+
# The request parser produces plain JSON values. A Ruby expectation can
|
|
86
|
+
# instead contain matchers or objects with their own JSON serializers.
|
|
87
|
+
expected = json_for_display(expected, fields.length)
|
|
88
|
+
return fields unless expected
|
|
89
|
+
actual = json_for_display(actual, fields.length - expected.length)
|
|
90
|
+
return fields unless actual && actual != expected
|
|
91
|
+
|
|
92
|
+
both = "Body mismatch:\n Expected: #{expected}\n Got: #{actual}\n"
|
|
93
|
+
both.length <= fields.length ? both : fields
|
|
40
94
|
end
|
|
41
95
|
|
|
42
|
-
#
|
|
43
|
-
|
|
44
|
-
|
|
96
|
+
# Hashdiff's array edits use positions in a partially edited array.
|
|
97
|
+
# A single replacement has an unambiguous index. For a sequence of
|
|
98
|
+
# edits, use the two original arrays to decide what to show.
|
|
99
|
+
def array_changes_for_display(changes, actual, expected)
|
|
100
|
+
groups = changes.group_by do |_op, path, *|
|
|
101
|
+
index = path.index { |part| Integer === part }
|
|
102
|
+
index ? path.take(index) : path
|
|
103
|
+
end
|
|
104
|
+
groups.flat_map do |path, edits|
|
|
105
|
+
next edits if edits.size == 1
|
|
106
|
+
next edits unless edits.any? { |op, parts, *| op != "~" && Integer === parts.last }
|
|
107
|
+
removed, added = edits
|
|
108
|
+
if edits.size == 2 && removed[0] == "-" && added[0] == "+" && removed[1] == added[1]
|
|
109
|
+
[["~", removed[1], removed[2], added[2]]]
|
|
110
|
+
else
|
|
111
|
+
got = path.empty? ? actual : actual.dig(*path)
|
|
112
|
+
wanted = path.empty? ? expected : expected.dig(*path)
|
|
113
|
+
array_replacement_changes(path, got, wanted)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
45
116
|
end
|
|
46
117
|
|
|
47
|
-
def
|
|
48
|
-
|
|
118
|
+
def array_replacement_changes(path, actual, expected)
|
|
119
|
+
whole = ["~", path, actual, expected]
|
|
120
|
+
return [whole] unless Array === actual && Array === expected && actual.size == expected.size
|
|
121
|
+
|
|
122
|
+
# Use the original positions on both sides. Compare plain JSON values
|
|
123
|
+
# so formatting a failure never evaluates a matcher in the array.
|
|
124
|
+
literal = catch(:skip_json_display) { plain_json(expected, 0) {} }
|
|
125
|
+
return [whole] unless literal
|
|
49
126
|
|
|
50
|
-
|
|
127
|
+
strict = String === request_stub_body
|
|
128
|
+
positions = []
|
|
129
|
+
actual.each_index do |i|
|
|
130
|
+
equal = strict ? actual[i].eql?(literal[i]) : actual[i] == literal[i]
|
|
131
|
+
positions << ["~", path + [i], actual[i], literal[i]] unless equal
|
|
132
|
+
end
|
|
133
|
+
return [whole] if positions.empty?
|
|
134
|
+
|
|
135
|
+
length = positions.sum { |change| describe_change(*change).length }
|
|
136
|
+
length < describe_change(*whole).length ? positions : [whole]
|
|
51
137
|
end
|
|
52
138
|
|
|
53
|
-
def
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
139
|
+
def describe_change(operation, path, value, expected = nil)
|
|
140
|
+
label = +""
|
|
141
|
+
path.each do |key|
|
|
142
|
+
if Integer === key
|
|
143
|
+
label << "[#{key}]"
|
|
144
|
+
elsif String === key && key.ascii_only? && key.match?(/\A[\w-]+\z/)
|
|
145
|
+
label << "." unless label.empty?
|
|
146
|
+
label << key
|
|
147
|
+
else
|
|
148
|
+
label << "[#{display_value(key)}]"
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
kind = Integer === path.last ? "item" : "field"
|
|
152
|
+
description = case operation
|
|
153
|
+
when "~" then "expected #{display_value(expected)}, got #{display_value(value)}"
|
|
154
|
+
when "+" then "expected #{display_value(value)}, but the #{kind} was missing"
|
|
155
|
+
when "-" then "unexpected #{kind} with value #{display_value(value)}"
|
|
156
|
+
end
|
|
157
|
+
" #{label.empty? ? '(root)' : label}: #{description}\n"
|
|
57
158
|
end
|
|
58
159
|
|
|
59
|
-
def
|
|
60
|
-
|
|
160
|
+
def display_value(value)
|
|
161
|
+
json = json_for_display(value)
|
|
162
|
+
return json if json
|
|
163
|
+
# Matchers may pretty-print fragments in different encodings.
|
|
164
|
+
PP.singleline_pp(value, StringIO.new(+"")).string
|
|
61
165
|
end
|
|
62
166
|
|
|
63
|
-
|
|
64
|
-
|
|
167
|
+
# Use plain JSON values so a Ruby body pattern cannot run its own to_json.
|
|
168
|
+
# Sorting both sets of keys puts corresponding fields in the same order.
|
|
169
|
+
def json_for_display(value, limit = nil)
|
|
170
|
+
catch(:skip_json_display) do
|
|
171
|
+
plain = plain_json(value, 0) do |length|
|
|
172
|
+
if limit
|
|
173
|
+
limit -= length
|
|
174
|
+
throw :skip_json_display if limit < 0
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
::JSON.generate(plain)
|
|
178
|
+
end
|
|
179
|
+
rescue ::JSON::JSONError, EncodingError
|
|
180
|
+
nil
|
|
65
181
|
end
|
|
66
182
|
|
|
67
|
-
def
|
|
68
|
-
|
|
183
|
+
def plain_json(value, depth, &count)
|
|
184
|
+
# Match JSON.generate's nesting limit and stop cyclic Ruby patterns.
|
|
185
|
+
throw :skip_json_display if depth > 100
|
|
186
|
+
case value
|
|
187
|
+
when Hash
|
|
188
|
+
yield value.size * 2
|
|
189
|
+
value.map do |key, entry|
|
|
190
|
+
throw :skip_json_display unless String === key
|
|
191
|
+
[plain_json(key, depth + 1, &count), plain_json(entry, depth + 1, &count)]
|
|
192
|
+
end.sort.to_h
|
|
193
|
+
when Array
|
|
194
|
+
yield value.size
|
|
195
|
+
value.map { |item| plain_json(item, depth + 1, &count) }
|
|
196
|
+
when String
|
|
197
|
+
throw :skip_json_display unless value.ascii_only? || value.encoding == Encoding::UTF_8
|
|
198
|
+
yield value.length
|
|
199
|
+
String.new(value)
|
|
200
|
+
when Integer, Float, true, false, nil
|
|
201
|
+
value
|
|
202
|
+
else
|
|
203
|
+
throw :skip_json_display
|
|
204
|
+
end
|
|
205
|
+
end
|
|
69
206
|
|
|
70
|
-
|
|
207
|
+
def small_json_value?(value)
|
|
208
|
+
# Hashdiff uses LCS for arrays, including nested arrays. Limit the total
|
|
209
|
+
# number of values before starting it on the assertion path.
|
|
210
|
+
remaining = 128
|
|
211
|
+
pending = [value]
|
|
212
|
+
until pending.empty?
|
|
213
|
+
item = pending.pop
|
|
214
|
+
remaining -= 1
|
|
215
|
+
case item
|
|
216
|
+
when Hash
|
|
217
|
+
return false if item.size + pending.size > remaining
|
|
218
|
+
item.each do |key, entry|
|
|
219
|
+
return false unless String === key && key.bytesize <= 2000
|
|
220
|
+
pending << entry
|
|
221
|
+
end
|
|
222
|
+
when Array
|
|
223
|
+
return false if item.size + pending.size > remaining
|
|
224
|
+
pending.concat(item)
|
|
225
|
+
when String
|
|
226
|
+
return false unless item.instance_of?(String) && item.bytesize <= 2000
|
|
227
|
+
when Integer, Float, true, false, nil
|
|
228
|
+
# JSON scalars.
|
|
229
|
+
else
|
|
230
|
+
return false
|
|
231
|
+
end
|
|
232
|
+
end
|
|
71
233
|
true
|
|
72
|
-
|
|
73
|
-
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def request_stub_body
|
|
237
|
+
pattern = BodyPattern === request_stub ? request_stub : request_stub.request_pattern&.body_pattern
|
|
238
|
+
pattern&.pattern
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def actual_body
|
|
242
|
+
request_signature.serialized_body || request_signature.body
|
|
74
243
|
end
|
|
75
244
|
end
|
|
76
245
|
end
|
|
@@ -55,9 +55,34 @@ module WebMock
|
|
|
55
55
|
|
|
56
56
|
def failure_message_phrase(is_negated=false)
|
|
57
57
|
negation = is_negated ? "was not" : "was"
|
|
58
|
+
diff = body_diff_message if !is_negated && times_executed == 0 && WebMock.show_body_diff?
|
|
58
59
|
"The request #{Util::MessageString.utf8(request_pattern)} #{negation} expected to execute #{quantity_phrase(is_negated)}but it executed #{times(times_executed)}" +
|
|
59
60
|
(is_negated ? "" : json_body_hint + trailing_slash_hint) +
|
|
60
|
-
self.class.executed_requests_message
|
|
61
|
+
(diff || self.class.executed_requests_message)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# When a single recorded request identifies the mismatch, showing its
|
|
65
|
+
# diff is more useful than listing every unrelated request in the suite.
|
|
66
|
+
def body_diff_message
|
|
67
|
+
parts, body_pattern = RequestPattern.body_for_diagnostics(request_pattern)
|
|
68
|
+
return unless body_pattern
|
|
69
|
+
signature = nil
|
|
70
|
+
RequestRegistry.instance.requested_signatures.each do |candidate, _count|
|
|
71
|
+
next unless parts.all? { |part| part.matches_endpoint_for_diff?(candidate) }
|
|
72
|
+
return if signature
|
|
73
|
+
signature = candidate
|
|
74
|
+
end
|
|
75
|
+
return unless signature
|
|
76
|
+
|
|
77
|
+
diff = RequestBodyDiff.new(signature, body_pattern).formatted_for_expectation
|
|
78
|
+
return unless diff
|
|
79
|
+
|
|
80
|
+
# Without a body and headers, a signature prints as its method and URL.
|
|
81
|
+
recorded = parts.any?(&:headers_pattern) ? signature : RequestSignature.new(signature.method, signature.uri)
|
|
82
|
+
"\n\nRecorded #{Util::MessageString.utf8(recorded)}\n#{Util::MessageString.utf8(diff)}"
|
|
83
|
+
rescue StandardError
|
|
84
|
+
# The ordinary request history is still available if a pattern can't be displayed.
|
|
85
|
+
nil
|
|
61
86
|
end
|
|
62
87
|
|
|
63
88
|
# Only when too few requests matched: with too many, declaring the body
|
|
@@ -61,6 +61,27 @@ module WebMock
|
|
|
61
61
|
# an upload captured by that block. Changing the pattern replaces the key.
|
|
62
62
|
attr_reader :match_token
|
|
63
63
|
|
|
64
|
+
# A pattern made of parts that must all match, such as an RSpec expectation
|
|
65
|
+
# with conditions, responds to #patterns. A stub stands for its pattern.
|
|
66
|
+
def self.parts_of(pattern)
|
|
67
|
+
parts = pattern.respond_to?(:patterns) ? pattern.patterns : [pattern]
|
|
68
|
+
parts.map { |part| RequestStub === part ? part.request_pattern : part }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The parts of the pattern and the body pattern of the only part that
|
|
72
|
+
# declares a body, or nil when none or several do, or when a part has a
|
|
73
|
+
# block given to with.
|
|
74
|
+
def self.body_for_diagnostics(pattern)
|
|
75
|
+
parts = parts_of(pattern)
|
|
76
|
+
return unless parts.all? { |part| RequestPattern === part && !part.with_block }
|
|
77
|
+
sources = parts.select(&:body_pattern)
|
|
78
|
+
[parts, sources.first.body_pattern] if sources.one?
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def matches_endpoint_for_diff?(signature)
|
|
82
|
+
method_pattern.matches?(signature.method) && uri_pattern.matches_for_body_diff?(signature.uri)
|
|
83
|
+
end
|
|
84
|
+
|
|
64
85
|
def initialize(method, uri, options = {})
|
|
65
86
|
@method_pattern = MethodPattern.new(method)
|
|
66
87
|
@uri_pattern = create_uri_pattern(uri)
|
|
@@ -208,6 +229,11 @@ module WebMock
|
|
|
208
229
|
pattern_matches?(uri) && query_params_matches?(uri)
|
|
209
230
|
end
|
|
210
231
|
|
|
232
|
+
# Building an error message must not invoke a user's URI or query matcher again.
|
|
233
|
+
def matches_for_body_diff?(uri)
|
|
234
|
+
literal_query?(@query_params) && matches?(uri)
|
|
235
|
+
end
|
|
236
|
+
|
|
211
237
|
def to_s
|
|
212
238
|
str = pattern_inspect
|
|
213
239
|
str += " with query params #{@query_params.inspect}" if @query_params
|
|
@@ -232,6 +258,19 @@ module WebMock
|
|
|
232
258
|
|
|
233
259
|
private
|
|
234
260
|
|
|
261
|
+
def literal_query?(value)
|
|
262
|
+
case value
|
|
263
|
+
when Hash then value.values.all? { |item| literal_query?(item) }
|
|
264
|
+
when Array then value.all? { |item| literal_query?(item) }
|
|
265
|
+
when WebMock::Matchers::HashArgumentMatcher
|
|
266
|
+
(value.instance_of?(WebMock::Matchers::HashIncludingMatcher) ||
|
|
267
|
+
value.instance_of?(WebMock::Matchers::HashExcludingMatcher)) && literal_query?(value.expected)
|
|
268
|
+
when Regexp then value.instance_of?(Regexp)
|
|
269
|
+
when String then value.instance_of?(String)
|
|
270
|
+
else value.nil? || value.instance_of?(WebMock::Matchers::AnyArgMatcher)
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
235
274
|
def raise_any_query_with_uri_query
|
|
236
275
|
raise ArgumentError, "query: :any can't be combined with a query in the URI #{pattern_inspect}, because no request would match. Remove the query from the URI."
|
|
237
276
|
end
|
|
@@ -257,6 +296,12 @@ module WebMock
|
|
|
257
296
|
end
|
|
258
297
|
|
|
259
298
|
class URICallablePattern < URIPattern
|
|
299
|
+
ANY_URI = ->(_uri) { true }
|
|
300
|
+
|
|
301
|
+
def matches_for_body_diff?(uri)
|
|
302
|
+
@pattern.equal?(ANY_URI) && super
|
|
303
|
+
end
|
|
304
|
+
|
|
260
305
|
# A lambda can't be written as Ruby source, so it is shown in quotes, as
|
|
261
306
|
# before.
|
|
262
307
|
def to_snippet
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "pp"
|
|
4
|
-
|
|
5
3
|
module WebMock
|
|
6
4
|
class RequestSignatureSnippet
|
|
7
5
|
|
|
@@ -54,8 +52,13 @@ module WebMock
|
|
|
54
52
|
end
|
|
55
53
|
|
|
56
54
|
def add_body_diff(stub, text)
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
pattern = stub.request_pattern
|
|
56
|
+
return unless RequestPattern === pattern && pattern.body_pattern && pattern.matches_endpoint_for_diff?(request_signature)
|
|
57
|
+
diff = RequestBodyDiff.new(request_signature, stub).formatted
|
|
58
|
+
text << "\n\n#{diff}" if diff
|
|
59
|
+
rescue StandardError
|
|
60
|
+
# Formatting an optional diff must not replace the request failure.
|
|
61
|
+
nil
|
|
59
62
|
end
|
|
60
63
|
|
|
61
64
|
def add_json_body_hint(stub, text)
|
|
@@ -75,18 +78,5 @@ module WebMock
|
|
|
75
78
|
text << "\n\n#{TrailingSlashHint::STUB_HINTS[difference]}\n" if difference
|
|
76
79
|
end
|
|
77
80
|
|
|
78
|
-
def signature_stub_body_diff(stub)
|
|
79
|
-
diff = RequestBodyDiff.new(request_signature, stub).body_diff
|
|
80
|
-
diff.empty? ? "" : "Body diff:\n #{pretty_print_to_string(diff)}"
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def pretty_print_to_string(string_to_print)
|
|
84
|
-
StringIO.open("".dup) do |stream|
|
|
85
|
-
PP.pp(string_to_print, stream)
|
|
86
|
-
stream.rewind
|
|
87
|
-
stream.read
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
81
|
end
|
|
92
82
|
end
|
|
@@ -88,7 +88,7 @@ module WebMock
|
|
|
88
88
|
|
|
89
89
|
private
|
|
90
90
|
|
|
91
|
-
ANY_URI =
|
|
91
|
+
ANY_URI = URICallablePattern::ANY_URI
|
|
92
92
|
private_constant :ANY_URI
|
|
93
93
|
|
|
94
94
|
# How the description of conditions starts when nothing was given to
|
|
@@ -107,7 +107,7 @@ module WebMock
|
|
|
107
107
|
@conditions = conditions
|
|
108
108
|
end
|
|
109
109
|
|
|
110
|
-
# The parts that must all match, so
|
|
110
|
+
# The parts that must all match, so failure messages can check each of them.
|
|
111
111
|
def patterns
|
|
112
112
|
[@pattern, @conditions]
|
|
113
113
|
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module WebMock
|
|
4
|
+
module TextBodyDiff
|
|
5
|
+
MAX_CHANGES = 8
|
|
6
|
+
private_constant :MAX_CHANGES
|
|
7
|
+
|
|
8
|
+
# A diff is extra context in a failure message. Bound both the comparison
|
|
9
|
+
# and its output for large documents and uploads.
|
|
10
|
+
def self.between(expected, actual)
|
|
11
|
+
before = text_lines(expected)
|
|
12
|
+
return unless before
|
|
13
|
+
after = text_lines(actual)
|
|
14
|
+
return unless after && (before.size > 1 || after.size > 1)
|
|
15
|
+
# A line found on one side only is a change in any diff, so unrelated
|
|
16
|
+
# bodies, such as other stubs for a SOAP URL, are ruled out cheaply.
|
|
17
|
+
return if (before - after).size + (after - before).size > MAX_CHANGES
|
|
18
|
+
|
|
19
|
+
# Loaded on first use, so a suite that never diffs text doesn't load it.
|
|
20
|
+
require "diff/lcs"
|
|
21
|
+
require "diff/lcs/hunk"
|
|
22
|
+
hunks = ::Diff::LCS.diff(before, after)
|
|
23
|
+
return if hunks.empty? || hunks.sum(&:size) > MAX_CHANGES
|
|
24
|
+
|
|
25
|
+
offset = 0
|
|
26
|
+
text = hunks.map do |changes|
|
|
27
|
+
hunk = ::Diff::LCS::Hunk.new(before, after, changes, 0, offset)
|
|
28
|
+
offset = hunk.file_length_difference
|
|
29
|
+
hunk.diff(:unified) + "\n"
|
|
30
|
+
end.join
|
|
31
|
+
text if text.bytesize <= 2000
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.text_lines(body)
|
|
35
|
+
return unless String === body && body.bytesize <= 64 * 1024
|
|
36
|
+
text = body.encoding == Encoding::BINARY ? body.dup.force_encoding(Encoding::UTF_8) : body.encode(Encoding::UTF_8)
|
|
37
|
+
return unless text.valid_encoding? && !text.match?(/[[:cntrl:]&&[^\t\n\r]]|\r(?!\n)/)
|
|
38
|
+
return if text.count("\n") > 1000
|
|
39
|
+
|
|
40
|
+
text.lines(chomp: true)
|
|
41
|
+
rescue EncodingError
|
|
42
|
+
nil
|
|
43
|
+
end
|
|
44
|
+
private_class_method :text_lines
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -25,7 +25,7 @@ module WebMock
|
|
|
25
25
|
# its query are compared: the hint is about the path, and building a
|
|
26
26
|
# failure message must not run code given to with, or raise.
|
|
27
27
|
def self.difference(request_pattern, request_signature)
|
|
28
|
-
patterns = parts_of(request_pattern)
|
|
28
|
+
patterns = RequestPattern.parts_of(request_pattern)
|
|
29
29
|
return unless patterns.all? { |pattern| pattern.is_a?(RequestPattern) }
|
|
30
30
|
return unless patterns.first.uri_pattern.is_a?(URIStringPattern)
|
|
31
31
|
|
|
@@ -43,13 +43,5 @@ module WebMock
|
|
|
43
43
|
rescue StandardError
|
|
44
44
|
nil
|
|
45
45
|
end
|
|
46
|
-
|
|
47
|
-
# A pattern made of parts that must all match, such as an RSpec expectation
|
|
48
|
-
# with conditions, responds to #patterns. A stub stands for its pattern.
|
|
49
|
-
def self.parts_of(request_pattern)
|
|
50
|
-
parts = request_pattern.respond_to?(:patterns) ? request_pattern.patterns : [request_pattern]
|
|
51
|
-
parts.map { |part| part.is_a?(RequestStub) ? part.request_pattern : part }
|
|
52
|
-
end
|
|
53
|
-
private_class_method :parts_of
|
|
54
46
|
end
|
|
55
47
|
end
|
|
@@ -21,30 +21,33 @@ module WebMock
|
|
|
21
21
|
_, length = headers.find { |name, _| name.to_s.casecmp?('Content-Length') }
|
|
22
22
|
return ''.b if length && length.to_i.zero?
|
|
23
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)
|
|
24
|
+
if !gzip && source.is_a?(IO)
|
|
29
25
|
# Flush pending writes before checking the descriptor offset.
|
|
30
26
|
# sysseek rejects buffered input, which IO#pos would discard.
|
|
31
27
|
stream.read(0)
|
|
32
28
|
begin
|
|
33
29
|
position = stream.sysseek(0, IO::SEEK_CUR)
|
|
34
30
|
rescue IOError
|
|
35
|
-
|
|
31
|
+
# Save only the buffered prefix, including any pushed-back bytes.
|
|
32
|
+
# The whole upload may be too large for IO#ungetbyte to restore.
|
|
33
|
+
buffered ||= ''.b
|
|
34
|
+
buffered << stream.readpartial(16 * 1024)
|
|
35
|
+
retry
|
|
36
36
|
end
|
|
37
|
-
|
|
37
|
+
stream.ungetbyte(buffered) if buffered
|
|
38
|
+
elsif !gzip
|
|
38
39
|
position = stream.pos
|
|
39
40
|
end
|
|
40
41
|
body = ''.b
|
|
41
42
|
each_chunk(stream, length&.to_i) { |chunk| body << chunk.b }
|
|
42
43
|
body
|
|
43
44
|
ensure
|
|
44
|
-
if
|
|
45
|
-
|
|
45
|
+
if gzip && body && !body.empty?
|
|
46
|
+
# GzipReader#ungetc accepts the whole uncompressed string.
|
|
47
|
+
stream.ungetc(body)
|
|
46
48
|
elsif position
|
|
47
49
|
stream.seek(position)
|
|
50
|
+
stream.ungetbyte(buffered) if buffered
|
|
48
51
|
end
|
|
49
52
|
end
|
|
50
53
|
|
data/lib/webmock/version.rb
CHANGED
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: 4.
|
|
4
|
+
version: 4.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bartosz Blimke
|
|
@@ -37,6 +37,20 @@ dependencies:
|
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: 0.4.6
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: diff-lcs
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '1.3'
|
|
47
|
+
type: :runtime
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '1.3'
|
|
40
54
|
- !ruby/object:Gem::Dependency
|
|
41
55
|
name: hashdiff
|
|
42
56
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -341,6 +355,7 @@ files:
|
|
|
341
355
|
- lib/webmock/stub_registry.rb
|
|
342
356
|
- lib/webmock/stub_request_snippet.rb
|
|
343
357
|
- lib/webmock/test_unit.rb
|
|
358
|
+
- lib/webmock/text_body_diff.rb
|
|
344
359
|
- lib/webmock/trailing_slash_hint.rb
|
|
345
360
|
- lib/webmock/util/content_type.rb
|
|
346
361
|
- lib/webmock/util/hash_counter.rb
|
|
@@ -363,9 +378,9 @@ licenses:
|
|
|
363
378
|
- MIT
|
|
364
379
|
metadata:
|
|
365
380
|
bug_tracker_uri: https://github.com/bblimke/webmock/issues
|
|
366
|
-
changelog_uri: https://github.com/bblimke/webmock/blob/v4.
|
|
367
|
-
documentation_uri: https://www.rubydoc.info/gems/webmock/4.
|
|
368
|
-
source_code_uri: https://github.com/bblimke/webmock/tree/v4.
|
|
381
|
+
changelog_uri: https://github.com/bblimke/webmock/blob/v4.4.0/CHANGELOG.md
|
|
382
|
+
documentation_uri: https://www.rubydoc.info/gems/webmock/4.4.0
|
|
383
|
+
source_code_uri: https://github.com/bblimke/webmock/tree/v4.4.0
|
|
369
384
|
wiki_uri: https://github.com/bblimke/webmock/wiki
|
|
370
385
|
rdoc_options: []
|
|
371
386
|
require_paths:
|