webmock 3.42.0 → 4.0.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 +52 -1
- data/README.md +5 -3
- data/lib/webmock/callback_registry.rb +14 -7
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +1 -7
- data/lib/webmock/http_lib_adapters/net_http.rb +19 -8
- data/lib/webmock/json_body_hint.rb +111 -7
- data/lib/webmock/request_execution_verifier.rb +6 -4
- data/lib/webmock/request_pattern.rb +10 -65
- data/lib/webmock/request_signature_snippet.rb +10 -2
- data/lib/webmock/request_stub.rb +24 -3
- data/lib/webmock/util/parsers/json.rb +6 -68
- data/lib/webmock/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4c4c96d7db983ae7dfbdf251c8d0a146a64761b0716cd8b1f2cde3ba5d523146
|
|
4
|
+
data.tar.gz: 685b2118f07a46a1bcd2f02cb96d24d035087cae2504f45a7927b6673d27ccfd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cb1a375f97aab7f5d298dedbcb12561537b82e9484d0bf4f64338e768142cbeb6f90f21b29ac6323a283a788294c126702ba6e4e5d20a4973540ab1a8a07a808
|
|
7
|
+
data.tar.gz: 2e42da579d5100fbcb2633838726795cccf41e1459d96d4cba4e6da566d876c0583cfa364e579b54bf2858ef363b8871c1f60bdba7493863760c716b76f84040
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,56 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
# 4.0.0
|
|
4
|
+
|
|
5
|
+
4.0 requires Ruby 3.0 or later. Its other changes fix behaviour that was wrong in 3.x, so a spec fails only if it passed because of one of them. A typical test suite needs no changes: a stub such as `stub_request(:get, url).to_return(body: "ok")` works as before, a request that sends `{id: 1}.to_json` still matches `with(body: {id: 1})`, and VCR records and replays as before.
|
|
6
|
+
|
|
7
|
+
## Breaking change
|
|
8
|
+
|
|
9
|
+
* **Ruby 3.0 or later, and crack 0.4.6 or later.** On Ruby 2.6 and 2.7, Bundler keeps installing WebMock 3.x. On JRuby, 4.0 needs JRuby 9.4 or later. If your Gemfile pins crack below 0.4.6, loosen the pin.
|
|
10
|
+
|
|
11
|
+
## Fixes that can fail a spec
|
|
12
|
+
|
|
13
|
+
* **Stubs that didn't do what they said now fail where they are declared.** A bare `to_return` crashed on the first matching request, `to_raise(:timeout)` returned a 200 instead of raising, and `to_return(status: 404) { ... }` dropped the status. These now raise `ArgumentError`.
|
|
14
|
+
|
|
15
|
+
* **Request bodies that aren't valid JSON no longer match a hash pattern.** A request that sent `{a: 1}` as JSON matched `with(body: {a: 1})`, although a real server would reject it. JSON from `to_json` isn't affected. Before upgrading, run your specs on 3.42.0 and fix any JSON body warnings it prints.
|
|
16
|
+
|
|
17
|
+
* **Real Net::HTTP requests behave as without WebMock.** This only matters if your specs make real requests through Net::HTTP that aren't stubbed or recorded with VCR. The block given to `request` now gets the response while Net::HTTP reads it, instead of after WebMock has read all of it.
|
|
18
|
+
|
|
19
|
+
Statuses such as `:ok` still print the deprecation warning added in 3.39.0, rather than raising. Use a number, such as `200`.
|
|
20
|
+
|
|
21
|
+
## If a spec fails
|
|
22
|
+
|
|
23
|
+
### Stub declarations
|
|
24
|
+
|
|
25
|
+
* For an empty 200 response, leave off a bare `to_return`, or give it a response, such as `to_return(status: 200)`. `to_return` and `to_return_json` with no arguments, an empty list or empty `**options` raise `ArgumentError`, and so does a `nil` response, which raised `NoMethodError` before.
|
|
26
|
+
* Give `to_raise` an exception class, an exception or a message, such as `to_raise(StandardError)`. Use `to_timeout` for a timeout.
|
|
27
|
+
* Give `to_return` either responses or a block, such as `to_return { |request| {status: 404, body: "x"} }`.
|
|
28
|
+
|
|
29
|
+
The same checks apply to `and_return`, `and_return_json` and `and_raise`.
|
|
30
|
+
|
|
31
|
+
Thanks to [Daniel Pepper](https://github.com/dpep)
|
|
32
|
+
|
|
33
|
+
### JSON request bodies (#432)
|
|
34
|
+
|
|
35
|
+
3.41.0 started reading JSON request bodies with `JSON.parse`, and fell back to WebMock's old JSON parser with a deprecation warning. 4.0 drops the old parser, so a body declared as a hash, an array, `hash_including` or `hash_excluding` no longer matches:
|
|
36
|
+
|
|
37
|
+
* a request body that isn't valid JSON, such as `{a: 1}` or `{'a': 1}`. Send `{a: 1}.to_json` instead.
|
|
38
|
+
* a value the old parser read differently. The body `{"amount":1e+20}` matches `{amount: 1e20}`, not `{amount: "1e+20"}`.
|
|
39
|
+
|
|
40
|
+
A lambda or matcher in a body pattern now gets only the value `JSON.parse` returns, such as the Float `1.0e+20`. One that calls a String method on it, such as `start_with?`, raises `NoMethodError`, which fails the request even when another stub would match. 3.42.0 warns about such a lambda only when the old reading matched.
|
|
41
|
+
|
|
42
|
+
When the only reason a stub doesn't match is that the request body isn't valid JSON, the failure message now says so and quotes the parser's error, unless body diffs are hidden with `WebMock.hide_body_diff!`.
|
|
43
|
+
|
|
44
|
+
### Real Net::HTTP requests (#742)
|
|
45
|
+
|
|
46
|
+
Previously, with real requests allowed, WebMock read the whole response before calling the block given to `request`. So `response.read_body { |chunk| ... }` got the body in one piece, and `response.decode_content = true` in the block left a gzip body compressed. Now the block gets the response while Net::HTTP reads it, as without WebMock, unless an `after_request` callback for Net::HTTP, such as VCR's, needs the whole body first. A callback registered with `except: [:net_http]` doesn't.
|
|
47
|
+
|
|
48
|
+
Code that relied on WebMock reading the body first can now fail, as it does without WebMock:
|
|
49
|
+
|
|
50
|
+
* Another request on the same connection, sent inside the block or after breaking out of it before the body is read, can fail. Send it after the block returns, or on another connection.
|
|
51
|
+
* `read_body` with a block, called after a request made with a block has returned, raises `IOError`. Call it inside the block.
|
|
52
|
+
* When the connection drops while the body is read, the net-http versions in Ruby 3.4 and later raise instead of retrying. That includes `http.get` without a block of your own, since it passes one to `request`. The net-http versions in Ruby 3.3 and earlier retry a GET and run the block again, even when the block raised `IOError`.
|
|
53
|
+
|
|
3
54
|
# 3.42.0
|
|
4
55
|
|
|
5
56
|
* Fix VCR failing to replay responses recorded with the `auto_inflate` feature of the `http` gem (#1154)
|
|
@@ -44,7 +95,7 @@ WebMock now parses JSON request bodies with Ruby's `JSON.parse`, instead of its
|
|
|
44
95
|
|
|
45
96
|
* Name a future major version in the deprecation warning for statuses such as `:ok`
|
|
46
97
|
|
|
47
|
-
The deprecation warning for a status such as `:ok`, added in 3.
|
|
98
|
+
The deprecation warning for a status such as `:ok`, added in 3.39.0, said that WebMock 4.0 would raise `ArgumentError` for it. 4.0 changes how JSON request bodies are read and doesn't change statuses yet, so the warning now names a future major version.
|
|
48
99
|
|
|
49
100
|
# 3.40.0
|
|
50
101
|
|
data/README.md
CHANGED
|
@@ -38,15 +38,13 @@ Supported HTTP libraries
|
|
|
38
38
|
|
|
39
39
|
Supported Ruby Interpreters
|
|
40
40
|
---------------------------
|
|
41
|
-
* MRI 2.6
|
|
42
|
-
* MRI 2.7
|
|
43
41
|
* MRI 3.0
|
|
44
42
|
* MRI 3.1
|
|
45
43
|
* MRI 3.2
|
|
46
44
|
* MRI 3.3
|
|
47
45
|
* MRI 3.4
|
|
48
46
|
* MRI 4.0
|
|
49
|
-
* JRuby
|
|
47
|
+
* JRuby 9.4 or later
|
|
50
48
|
|
|
51
49
|
## Installation
|
|
52
50
|
|
|
@@ -1160,6 +1158,10 @@ WebMock.after_request(except: [:patron],
|
|
|
1160
1158
|
end
|
|
1161
1159
|
```
|
|
1162
1160
|
|
|
1161
|
+
#### Callbacks and Net::HTTP streaming
|
|
1162
|
+
|
|
1163
|
+
A callback that runs for real Net::HTTP requests makes WebMock read the whole response body before it calls the block given to `request`, so the callback gets all of it. Without such a callback, the block gets the response while Net::HTTP reads it, as without WebMock, so it can read the body in chunks. WebMock decides this when the request is sent, so a callback registered while a request with a block is in flight doesn't get that response. Callbacks stay registered across tests, so one added in an earlier test turns streaming off in later tests too. To keep a callback away from Net::HTTP, register it with `except: [:net_http]`.
|
|
1164
|
+
|
|
1163
1165
|
#### Removing callbacks
|
|
1164
1166
|
|
|
1165
1167
|
Callbacks stay registered after `WebMock.reset!`, so a callback added in one test keeps running in later ones. To remove all callbacks, call:
|
|
@@ -15,16 +15,23 @@ module WebMock
|
|
|
15
15
|
def self.invoke_callbacks(options, request_signature, response)
|
|
16
16
|
return if @@callbacks.empty?
|
|
17
17
|
CallbackRegistry.callbacks.each do |callback|
|
|
18
|
-
|
|
19
|
-
real_only = callback[:options][:real_requests_only]
|
|
20
|
-
unless except && except.include?(options[:lib])
|
|
21
|
-
if !real_only || options[:real_request]
|
|
22
|
-
callback[:block].call(request_signature, response)
|
|
23
|
-
end
|
|
24
|
-
end
|
|
18
|
+
callback[:block].call(request_signature, response) if runs_for?(callback, options)
|
|
25
19
|
end
|
|
26
20
|
end
|
|
27
21
|
|
|
22
|
+
# Whether invoke_callbacks would run any callback for a request with
|
|
23
|
+
# these options, given each callback's except and real_requests_only.
|
|
24
|
+
def self.any_callbacks_for?(options)
|
|
25
|
+
@@callbacks.any? { |callback| runs_for?(callback, options) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.runs_for?(callback, options)
|
|
29
|
+
except = callback[:options][:except]
|
|
30
|
+
real_only = callback[:options][:real_requests_only]
|
|
31
|
+
!(except && except.include?(options[:lib])) && (!real_only || options[:real_request])
|
|
32
|
+
end
|
|
33
|
+
private_class_method :runs_for?
|
|
34
|
+
|
|
28
35
|
def self.reset
|
|
29
36
|
@@callbacks = []
|
|
30
37
|
end
|
|
@@ -7,13 +7,7 @@ rescue LoadError
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
if defined?(Curl)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
# Curl::Easy#perform raises "cannot define finalizer for TrueClass" on Ruby 2.6
|
|
13
|
-
# in curb 1.3.0 through 1.3.5. Fixed in curb 1.3.6. See https://github.com/taf2/curb/issues/478
|
|
14
|
-
unsupported_curb_versions += %w[1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5] if RUBY_VERSION < '2.7'
|
|
15
|
-
|
|
16
|
-
WebMock::VersionChecker.new('Curb', Curl::CURB_VERSION, '0.7.16', nil, unsupported_curb_versions).check_version!
|
|
10
|
+
WebMock::VersionChecker.new('Curb', Curl::CURB_VERSION, '0.7.16', nil, ['0.8.7']).check_version!
|
|
17
11
|
|
|
18
12
|
module WebMock
|
|
19
13
|
module HttpLibAdapters
|
|
@@ -111,11 +111,21 @@ module WebMock
|
|
|
111
111
|
build_net_http_response(webmock_response, request.uri, &block)
|
|
112
112
|
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
|
113
113
|
check_right_http_connection
|
|
114
|
+
callback_options = {lib: :net_http, real_request: true}
|
|
115
|
+
# Without callbacks for this request, nothing needs the body
|
|
116
|
+
# before the block, so the block gets the response while
|
|
117
|
+
# Net::HTTP reads it, as without WebMock, and can stream the
|
|
118
|
+
# body or set decode_content. With callbacks, such as VCR's,
|
|
119
|
+
# the body is read first, so they get all of it.
|
|
120
|
+
stream = block && !WebMock::CallbackRegistry.any_callbacks_for?(callback_options)
|
|
114
121
|
after_request = lambda do |response|
|
|
115
|
-
|
|
122
|
+
# Checked again, so that callbacks registered while the
|
|
123
|
+
# request was made get the response too, as before. A request
|
|
124
|
+
# that streams to its block doesn't come here, so callbacks
|
|
125
|
+
# registered after it was sent don't get its response.
|
|
126
|
+
if WebMock::CallbackRegistry.any_callbacks_for?(callback_options)
|
|
116
127
|
webmock_response = build_webmock_response(response)
|
|
117
|
-
WebMock::CallbackRegistry.invoke_callbacks(
|
|
118
|
-
{lib: :net_http, real_request: true}, request_signature, webmock_response)
|
|
128
|
+
WebMock::CallbackRegistry.invoke_callbacks(callback_options, request_signature, webmock_response)
|
|
119
129
|
end
|
|
120
130
|
response.extend Net::WebMockHTTPResponse
|
|
121
131
|
# Net::HTTP has read the body and applied its encoding already
|
|
@@ -123,16 +133,17 @@ module WebMock
|
|
|
123
133
|
block.call response if block
|
|
124
134
|
response
|
|
125
135
|
end
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
136
|
+
real_request = lambda {
|
|
137
|
+
next super(request, nil, &block) if stream
|
|
138
|
+
|
|
139
|
+
after_request.call(super(request, nil, &nil))
|
|
129
140
|
}
|
|
130
141
|
if started?
|
|
131
142
|
ensure_actual_connection
|
|
132
|
-
|
|
143
|
+
real_request.call
|
|
133
144
|
else
|
|
134
145
|
start_with_connect {
|
|
135
|
-
|
|
146
|
+
real_request.call
|
|
136
147
|
}
|
|
137
148
|
end
|
|
138
149
|
else
|
|
@@ -3,10 +3,21 @@
|
|
|
3
3
|
require "json"
|
|
4
4
|
|
|
5
5
|
module WebMock
|
|
6
|
+
# Hints in failure messages about JSON request bodies.
|
|
7
|
+
#
|
|
6
8
|
# A body declared as a string must match the request body exactly, which for
|
|
7
9
|
# JSON includes key order and whitespace. When a request fails to match only
|
|
8
10
|
# because of that, failure messages suggest declaring the body as a hash.
|
|
9
11
|
# See #485.
|
|
12
|
+
#
|
|
13
|
+
# A body declared as a hash, an array, hash_including or hash_excluding is
|
|
14
|
+
# matched against the body as JSON.parse reads it, unless it matches any
|
|
15
|
+
# body, as {} does, while WebMock 3.x also read some invalid JSON, such as
|
|
16
|
+
# {a: 1}. When a request fails to match only because its body isn't valid
|
|
17
|
+
# JSON, failure messages say so.
|
|
18
|
+
#
|
|
19
|
+
# A hint is extra, so an error while working one out, for example from a
|
|
20
|
+
# header pattern, means no hint rather than a broken failure message.
|
|
10
21
|
module JsonBodyHint
|
|
11
22
|
ADVICE = "A body declared as a string must match exactly. To match it as JSON, " \
|
|
12
23
|
"declare it as a hash, for example with(body: JSON.parse(json_string))."
|
|
@@ -17,6 +28,20 @@ module WebMock
|
|
|
17
28
|
EXPECTATION_HINT = "Hint: a request was made with the same JSON body as expected, " \
|
|
18
29
|
"but formatted differently, for example with keys in another order. #{ADVICE}"
|
|
19
30
|
|
|
31
|
+
INVALID_JSON_ADVICE = "WebMock 4.0 and later read JSON request bodies only with JSON.parse, " \
|
|
32
|
+
"while earlier versions also matched some bodies that aren't valid JSON, such as {a: 1}."
|
|
33
|
+
|
|
34
|
+
BYTE_ORDER_MARK = [0xEF, 0xBB, 0xBF].pack("C*").freeze
|
|
35
|
+
private_constant :BYTE_ORDER_MARK
|
|
36
|
+
|
|
37
|
+
# %s is the parser's error.
|
|
38
|
+
INVALID_JSON_HINTS = {
|
|
39
|
+
stub: "Hint: the request body isn't valid JSON (%s), so a stub above with a body declared as " \
|
|
40
|
+
"a hash, an array, hash_including or hash_excluding can't match it. #{INVALID_JSON_ADVICE}",
|
|
41
|
+
expectation: "Hint: a request was made with a body that isn't valid JSON (%s), " \
|
|
42
|
+
"so it can't match the expected body. #{INVALID_JSON_ADVICE}"
|
|
43
|
+
}.freeze
|
|
44
|
+
|
|
20
45
|
# True when one of the patterns declares its body as a JSON object or array
|
|
21
46
|
# string, the request has a JSON content type and a body that parses to the
|
|
22
47
|
# same JSON but is written differently, and the method, URI and headers of
|
|
@@ -31,14 +56,46 @@ module WebMock
|
|
|
31
56
|
# The body is checked first, since that runs no user code, so URI and
|
|
32
57
|
# header matchers are only evaluated when a hint is otherwise possible.
|
|
33
58
|
def self.applies?(request_pattern, request_signature)
|
|
34
|
-
patterns =
|
|
35
|
-
return false unless
|
|
36
|
-
|
|
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)
|
|
59
|
+
patterns, body_pattern = body_pattern_of(request_pattern)
|
|
60
|
+
return false unless body_pattern && body_differs_only_in_formatting?(body_pattern, request_signature)
|
|
40
61
|
|
|
41
62
|
patterns.all? { |pattern| matches_except_body?(pattern, request_signature) }
|
|
63
|
+
rescue StandardError
|
|
64
|
+
false
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The hint for an expected request that didn't match: that the JSON is
|
|
68
|
+
# formatted differently, or that the body isn't valid JSON. At most one of
|
|
69
|
+
# them fits, since one needs a body declared as a string and the other a
|
|
70
|
+
# hash, an array, hash_including or hash_excluding.
|
|
71
|
+
def self.expectation_hint(request_pattern, request_signature)
|
|
72
|
+
return EXPECTATION_HINT if applies?(request_pattern, request_signature)
|
|
73
|
+
|
|
74
|
+
invalid_json_hint([request_pattern], request_signature, :expectation)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The hint, for kind :stub or :expectation, when the request body isn't
|
|
78
|
+
# valid JSON and that is the only reason one of the patterns doesn't
|
|
79
|
+
# match: it declares its body as a hash, an array, hash_including or
|
|
80
|
+
# hash_excluding, which doesn't match a body that doesn't parse, and the
|
|
81
|
+
# method, URI and headers of all of its parts match the request. The
|
|
82
|
+
# body is parsed at most once, and only when a pattern could get the hint.
|
|
83
|
+
def self.invalid_json_hint(request_patterns, request_signature, kind)
|
|
84
|
+
candidates = request_patterns.filter_map { |request_pattern| body_pattern_of(request_pattern) }
|
|
85
|
+
# Method patterns run no user code, so they can rule out patterns
|
|
86
|
+
# before the body is parsed.
|
|
87
|
+
candidates.select! do |patterns, body_pattern|
|
|
88
|
+
body_pattern.parses_body? && patterns.all? { |pattern| pattern.method_pattern.matches?(request_signature.method) }
|
|
89
|
+
end
|
|
90
|
+
return if candidates.empty? || !request_signature.json_headers?
|
|
91
|
+
|
|
92
|
+
# The body that a hash or array pattern reads, as in BodyPattern#matches?.
|
|
93
|
+
error = parse_error(request_signature.serialized_body || request_signature.body)
|
|
94
|
+
return unless error && candidates.any? { |patterns, body_pattern| fails_only_on_body?(patterns, body_pattern, request_signature) }
|
|
95
|
+
|
|
96
|
+
format(INVALID_JSON_HINTS.fetch(kind), short_error(error))
|
|
97
|
+
rescue StandardError
|
|
98
|
+
nil
|
|
42
99
|
end
|
|
43
100
|
|
|
44
101
|
# A pattern made of parts that must all match, such as an RSpec expectation
|
|
@@ -48,6 +105,17 @@ module WebMock
|
|
|
48
105
|
parts.map { |part| part.is_a?(RequestStub) ? part.request_pattern : part }
|
|
49
106
|
end
|
|
50
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
|
+
|
|
51
119
|
def self.body_differs_only_in_formatting?(body_pattern, request_signature)
|
|
52
120
|
expected = body_pattern.pattern
|
|
53
121
|
actual = request_signature.body
|
|
@@ -64,6 +132,41 @@ module WebMock
|
|
|
64
132
|
expected_json.eql?(parse_json { WebMock::Util::Parsers::JSON.parse(actual) })
|
|
65
133
|
end
|
|
66
134
|
|
|
135
|
+
# A body that doesn't parse reaches the body pattern as no body, so it is
|
|
136
|
+
# given none here: the body isn't parsed again, and no user code runs, as
|
|
137
|
+
# the pattern has nothing to compare its values with. Some patterns, such
|
|
138
|
+
# as hash_including({}), match any body, so they get no hint. A pattern
|
|
139
|
+
# that raises, for example a header regexp in an encoding that doesn't
|
|
140
|
+
# fit the request header, gets no hint, while the others still can.
|
|
141
|
+
def self.fails_only_on_body?(patterns, body_pattern, request_signature)
|
|
142
|
+
!body_pattern.matches?(nil, request_signature.content_type) &&
|
|
143
|
+
patterns.all? { |pattern| matches_except_body?(pattern, request_signature) }
|
|
144
|
+
rescue StandardError
|
|
145
|
+
false
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# The parser's error, or nil when the body parses, is blank after a byte
|
|
149
|
+
# order mark, which the parser drops, or is nested deeper than WebMock
|
|
150
|
+
# reads. None of those is what the hint is about.
|
|
151
|
+
def self.parse_error(body)
|
|
152
|
+
return unless body.is_a?(String) && !body.b.delete_prefix(BYTE_ORDER_MARK).match?(/\A[ \t\r\n]*\z/)
|
|
153
|
+
|
|
154
|
+
WebMock::Util::Parsers::JSON.parse(body)
|
|
155
|
+
nil
|
|
156
|
+
rescue WebMock::Util::Parsers::ParseError => error
|
|
157
|
+
error.message unless error.cause.is_a?(::JSON::NestingError)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Older json versions quote the rest of the body, json 2.5 starts with a
|
|
161
|
+
# line of its C source, such as "809: ", and json 2.8 and 2.9 cut the
|
|
162
|
+
# quote at 32 bytes, which can leave part of a character at the end.
|
|
163
|
+
def self.short_error(message)
|
|
164
|
+
message = message.delete_prefix(WebMock::Util::Parsers::JSON::ERROR_PREFIX)
|
|
165
|
+
cut = message.length > 1000
|
|
166
|
+
message = WebMock::Util::MessageString.utf8(message[0, 1000].scrub("")).gsub(/\s+/, " ").sub(/\A\d+: /, "").strip
|
|
167
|
+
cut || message.length > 100 ? "#{message[0, 100]}..." : message
|
|
168
|
+
end
|
|
169
|
+
|
|
67
170
|
def self.matches_except_body?(request_pattern, request_signature)
|
|
68
171
|
request_pattern.method_pattern.matches?(request_signature.method) &&
|
|
69
172
|
request_pattern.uri_pattern.matches?(request_signature.uri) &&
|
|
@@ -75,6 +178,7 @@ module WebMock
|
|
|
75
178
|
rescue ::JSON::JSONError, EncodingError, WebMock::Util::Parsers::ParseError
|
|
76
179
|
nil
|
|
77
180
|
end
|
|
78
|
-
private_class_method :parts_of, :body_differs_only_in_formatting?, :
|
|
181
|
+
private_class_method :parts_of, :body_pattern_of, :body_differs_only_in_formatting?, :fails_only_on_body?,
|
|
182
|
+
:parse_error, :short_error, :matches_except_body?, :parse_json
|
|
79
183
|
end
|
|
80
184
|
end
|
|
@@ -61,14 +61,16 @@ module WebMock
|
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
# Only when too few requests matched: with too many, declaring the body
|
|
64
|
-
# as a hash would match even more of them
|
|
64
|
+
# as a hash would match even more of them, and a request whose body isn't
|
|
65
|
+
# valid JSON isn't why there are too many.
|
|
65
66
|
def json_body_hint
|
|
66
67
|
return "" unless WebMock.show_body_diff? && too_few_requests_executed?
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
JsonBodyHint.
|
|
69
|
+
RequestRegistry.instance.requested_signatures.each do |request_signature, _|
|
|
70
|
+
hint = JsonBodyHint.expectation_hint(request_pattern, request_signature)
|
|
71
|
+
return "\n\n#{hint}" if hint
|
|
70
72
|
end
|
|
71
|
-
|
|
73
|
+
""
|
|
72
74
|
end
|
|
73
75
|
|
|
74
76
|
# Only when too few requests matched, as for json_body_hint.
|
|
@@ -423,6 +423,13 @@ module WebMock
|
|
|
423
423
|
@pattern = pattern.is_a?(Hash) ? normalize_hash(pattern) : webmock_hash_matcher(pattern)
|
|
424
424
|
end
|
|
425
425
|
|
|
426
|
+
# True for a hash, an array, hash_including or hash_excluding, which
|
|
427
|
+
# match the body as parsed for its content type, such as JSON, unless
|
|
428
|
+
# they match any body, as {} does.
|
|
429
|
+
def parses_body?
|
|
430
|
+
@pattern.is_a?(Hash) || @pattern.is_a?(Array) || @pattern.is_a?(WebMock::Matchers::HashArgumentMatcher)
|
|
431
|
+
end
|
|
432
|
+
|
|
426
433
|
def matches?(body, content_type = "", serialized_body = nil)
|
|
427
434
|
# Checked on the raw value, so body patterns used with mixed case
|
|
428
435
|
# multipart content types keep working as in earlier versions.
|
|
@@ -437,11 +444,11 @@ module WebMock
|
|
|
437
444
|
|
|
438
445
|
if (@pattern).is_a?(Hash)
|
|
439
446
|
return true if @pattern.empty?
|
|
440
|
-
|
|
447
|
+
matching_body_hashes?(body_as_hash(body, content_type), @pattern, content_type)
|
|
441
448
|
elsif (@pattern).is_a?(Array)
|
|
442
|
-
|
|
449
|
+
matching_body_array?(body_as_hash(body, content_type), @pattern, content_type)
|
|
443
450
|
elsif (@pattern).is_a?(WebMock::Matchers::HashArgumentMatcher)
|
|
444
|
-
|
|
451
|
+
@pattern == body_as_hash(body, content_type)
|
|
445
452
|
else
|
|
446
453
|
empty_string?(@pattern) && empty_string?(body) ||
|
|
447
454
|
@pattern == body ||
|
|
@@ -455,68 +462,6 @@ module WebMock
|
|
|
455
462
|
|
|
456
463
|
private
|
|
457
464
|
|
|
458
|
-
WARNED_LEGACY_JSON = {}
|
|
459
|
-
private_constant :WARNED_LEGACY_JSON
|
|
460
|
-
|
|
461
|
-
# WebMock 3.40.0 and earlier read JSON bodies with a YAML based parser,
|
|
462
|
-
# which reads some bodies that aren't valid JSON and some values
|
|
463
|
-
# differently from the json gem. When the pattern doesn't match the json
|
|
464
|
-
# gem's reading and the old parser reads the body differently, the old
|
|
465
|
-
# reading decides, as in 3.40.0, with a warning once for each pattern
|
|
466
|
-
# when it matches.
|
|
467
|
-
def matching_parsed_body?(body, content_type)
|
|
468
|
-
parsed = body_as_hash(body, content_type)
|
|
469
|
-
begin
|
|
470
|
-
return true if yield(parsed)
|
|
471
|
-
rescue SignalException, SystemExit
|
|
472
|
-
raise
|
|
473
|
-
rescue Exception => error
|
|
474
|
-
# A value in the pattern, such as a lambda or an RSpec expectation,
|
|
475
|
-
# may expect what the old parser reads, so the old reading decides.
|
|
476
|
-
end
|
|
477
|
-
|
|
478
|
-
json = body && WebMock::Util::ContentType.body_format(content_type) == :json
|
|
479
|
-
legacy = legacy_json(body) if json
|
|
480
|
-
if !json || legacy.eql?(parsed)
|
|
481
|
-
raise error if error
|
|
482
|
-
return false
|
|
483
|
-
end
|
|
484
|
-
|
|
485
|
-
# As in 3.40.0, including an error the pattern raises for it.
|
|
486
|
-
matched = yield(legacy)
|
|
487
|
-
warn_legacy_json(body, parsed) if matched
|
|
488
|
-
matched
|
|
489
|
-
end
|
|
490
|
-
|
|
491
|
-
def legacy_json(body)
|
|
492
|
-
WebMock::Util::Parsers::JSON.parse_legacy(body)
|
|
493
|
-
rescue WebMock::Util::Parsers::ParseError
|
|
494
|
-
nil
|
|
495
|
-
end
|
|
496
|
-
|
|
497
|
-
def warn_legacy_json(body, parsed)
|
|
498
|
-
pattern = to_s
|
|
499
|
-
# Without object addresses, so equal patterns, such as ones with a
|
|
500
|
-
# lambda built in each example, warn once.
|
|
501
|
-
key = pattern.gsub(/#<([\w:]+?):0x\h+/, '#<\\1').hash
|
|
502
|
-
return if WARNED_LEGACY_JSON[key]
|
|
503
|
-
WARNED_LEGACY_JSON[key] = true
|
|
504
|
-
advice =
|
|
505
|
-
if parsed.nil?
|
|
506
|
-
"Send valid JSON, for example from to_json, and write the pattern for what JSON.parse returns."
|
|
507
|
-
else
|
|
508
|
-
"Write the pattern for what JSON.parse returns: #{shorten(parsed.inspect)}."
|
|
509
|
-
end
|
|
510
|
-
WebMock::Deprecation.warning(
|
|
511
|
-
"the body pattern #{shorten(pattern)} matched the request body #{shorten(body).inspect} " \
|
|
512
|
-
"only as WebMock's old JSON parser reads it. WebMock 4.0 drops that parser, so it won't match. #{advice}"
|
|
513
|
-
)
|
|
514
|
-
end
|
|
515
|
-
|
|
516
|
-
def shorten(text)
|
|
517
|
-
text.length > 100 ? "#{text[0, 100]}..." : text
|
|
518
|
-
end
|
|
519
|
-
|
|
520
465
|
def body_as_hash(body, content_type)
|
|
521
466
|
return if body.nil?
|
|
522
467
|
|
|
@@ -24,10 +24,11 @@ module WebMock
|
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
def request_stubs
|
|
27
|
-
|
|
27
|
+
stubs = WebMock::StubRegistry.instance.request_stubs
|
|
28
|
+
return if stubs.empty?
|
|
28
29
|
|
|
29
30
|
text = "registered request stubs:\n".dup
|
|
30
|
-
|
|
31
|
+
stubs.each do |stub|
|
|
31
32
|
text << "\n#{WebMock::Util::MessageString.utf8(WebMock::StubRequestSnippet.new(stub).to_s(false))}"
|
|
32
33
|
if WebMock.show_body_diff?
|
|
33
34
|
add_body_diff(stub, text)
|
|
@@ -35,6 +36,7 @@ module WebMock
|
|
|
35
36
|
end
|
|
36
37
|
add_trailing_slash_hint(stub, text)
|
|
37
38
|
end
|
|
39
|
+
add_invalid_json_hint(stubs, text) if WebMock.show_body_diff?
|
|
38
40
|
text
|
|
39
41
|
end
|
|
40
42
|
|
|
@@ -62,6 +64,12 @@ module WebMock
|
|
|
62
64
|
text << "\n\n#{JsonBodyHint::STUB_HINT}\n"
|
|
63
65
|
end
|
|
64
66
|
|
|
67
|
+
# Once after the stubs, since it is about the request rather than a stub.
|
|
68
|
+
def add_invalid_json_hint(stubs, text)
|
|
69
|
+
hint = JsonBodyHint.invalid_json_hint(stubs, request_signature, :stub)
|
|
70
|
+
text << "\n\n#{hint}\n" if hint
|
|
71
|
+
end
|
|
72
|
+
|
|
65
73
|
def add_trailing_slash_hint(stub, text)
|
|
66
74
|
difference = TrailingSlashHint.difference(stub.request_pattern, request_signature)
|
|
67
75
|
text << "\n\n#{TrailingSlashHint::STUB_HINTS[difference]}\n" if difference
|
data/lib/webmock/request_stub.rb
CHANGED
|
@@ -18,9 +18,14 @@ module WebMock
|
|
|
18
18
|
|
|
19
19
|
def to_return(*response_hashes, &block)
|
|
20
20
|
if block
|
|
21
|
+
raise ArgumentError, '#to_return does not accept both responses and a block' unless response_hashes.empty?
|
|
22
|
+
|
|
21
23
|
@responses_sequences << ResponsesSequence.new([ResponseFactory.response_for(block)])
|
|
22
24
|
else
|
|
23
|
-
|
|
25
|
+
response_hashes = [*response_hashes].flatten
|
|
26
|
+
require_args!(response_hashes, :to_return, 'response')
|
|
27
|
+
|
|
28
|
+
@responses_sequences << ResponsesSequence.new(response_hashes.map {|r| ResponseFactory.response_for(r)})
|
|
24
29
|
end
|
|
25
30
|
self
|
|
26
31
|
end
|
|
@@ -29,7 +34,10 @@ module WebMock
|
|
|
29
34
|
def to_return_json(*response_hashes)
|
|
30
35
|
raise ArgumentError, '#to_return_json does not support passing a block' if block_given?
|
|
31
36
|
|
|
32
|
-
|
|
37
|
+
response_hashes = [*response_hashes].flatten
|
|
38
|
+
require_args!(response_hashes, :to_return_json, 'response')
|
|
39
|
+
|
|
40
|
+
json_response_hashes = response_hashes.map do |resp_h|
|
|
33
41
|
headers, body = resp_h.values_at(:headers, :body)
|
|
34
42
|
|
|
35
43
|
json_body = if body.respond_to?(:call)
|
|
@@ -60,7 +68,15 @@ module WebMock
|
|
|
60
68
|
end
|
|
61
69
|
|
|
62
70
|
def to_raise(*exceptions)
|
|
63
|
-
|
|
71
|
+
exceptions = [*exceptions].flatten
|
|
72
|
+
require_args!(exceptions, :to_raise, 'exception')
|
|
73
|
+
exceptions.each do |exception|
|
|
74
|
+
next if exception.is_a?(String) || exception.is_a?(Exception) || (exception.is_a?(Class) && exception <= Exception)
|
|
75
|
+
|
|
76
|
+
raise ArgumentError, "#{exception.inspect} is not a valid exception for #to_raise"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
@responses_sequences << ResponsesSequence.new(exceptions.map {|e|
|
|
64
80
|
ResponseFactory.response_for(exception: e)
|
|
65
81
|
})
|
|
66
82
|
self
|
|
@@ -134,6 +150,11 @@ module WebMock
|
|
|
134
150
|
|
|
135
151
|
private
|
|
136
152
|
|
|
153
|
+
def require_args!(values, method, noun)
|
|
154
|
+
raise ArgumentError, "##{method} requires at least one #{noun}" if values.empty?
|
|
155
|
+
raise ArgumentError, "nil is not a valid #{noun} for ##{method}" if values.any?(&:nil?)
|
|
156
|
+
end
|
|
157
|
+
|
|
137
158
|
def json_response_body(body)
|
|
138
159
|
case body
|
|
139
160
|
when String, IO, Pathname
|
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# parse_legacy is a copy of https://github.com/jnunemaker/crack/blob/master/lib/crack/json.rb
|
|
4
|
-
# with date parsing removed
|
|
5
|
-
# Copyright (c) 2004-2008 David Heinemeier Hansson
|
|
6
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
-
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
9
|
-
|
|
10
3
|
require "json"
|
|
11
4
|
require_relative "parse_error"
|
|
12
5
|
|
|
@@ -18,23 +11,15 @@ module WebMock
|
|
|
18
11
|
# gem version to parse in a thread's stack.
|
|
19
12
|
MAX_NESTING = 1000
|
|
20
13
|
|
|
14
|
+
# Starts the message of every ParseError raised here.
|
|
15
|
+
ERROR_PREFIX = "Invalid JSON string: "
|
|
16
|
+
|
|
21
17
|
def self.parse(json)
|
|
22
18
|
# JSON texts may start with a byte order mark, which parsers may ignore.
|
|
23
19
|
json = to_utf8(json).delete_prefix("\uFEFF")
|
|
24
20
|
::JSON.parse(json, allow_duplicate_key: true, max_nesting: MAX_NESTING)
|
|
25
21
|
rescue ::JSON::JSONError, EncodingError => e
|
|
26
|
-
raise ParseError, "
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# The YAML based parser WebMock used up to 3.40.0. It reads some
|
|
30
|
-
# bodies that aren't valid JSON, such as {a: 1}, and some values
|
|
31
|
-
# differently from the json gem, such as 1e+20 as a String.
|
|
32
|
-
# WebMock 4.0 drops it.
|
|
33
|
-
def self.parse_legacy(json)
|
|
34
|
-
yaml = unescape(convert_json_to_yaml(json))
|
|
35
|
-
YAML.load(yaml)
|
|
36
|
-
rescue ArgumentError, EncodingError, Psych::Exception => e
|
|
37
|
-
raise ParseError, "Invalid JSON string: #{yaml}, Error: #{e.inspect}"
|
|
22
|
+
raise ParseError, "#{ERROR_PREFIX}#{e.message}"
|
|
38
23
|
end
|
|
39
24
|
|
|
40
25
|
# The json gem reads bytes that aren't valid in their encoding, so
|
|
@@ -45,59 +30,12 @@ module WebMock
|
|
|
45
30
|
if json.encoding == Encoding::BINARY || json.encoding == Encoding::US_ASCII
|
|
46
31
|
json = json.dup.force_encoding(Encoding::UTF_8)
|
|
47
32
|
end
|
|
48
|
-
raise ParseError, "
|
|
33
|
+
raise ParseError, "#{ERROR_PREFIX}it isn't valid #{json.encoding}" unless json.valid_encoding?
|
|
49
34
|
|
|
50
35
|
json.encoding == Encoding::UTF_8 ? json : json.encode(Encoding::UTF_8)
|
|
51
36
|
end
|
|
52
37
|
private_class_method :to_utf8
|
|
53
|
-
|
|
54
|
-
protected
|
|
55
|
-
|
|
56
|
-
def self.unescape(str)
|
|
57
|
-
str.gsub(/\\u([0-9a-f]{4})/) { [$1.hex].pack("U") }
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
# Ensure that ":" and "," are always followed by a space
|
|
61
|
-
def self.convert_json_to_yaml(json) #:nodoc:
|
|
62
|
-
scanner, quoting, marks, times = StringScanner.new(json), false, [], []
|
|
63
|
-
while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
|
|
64
|
-
case char = scanner[1]
|
|
65
|
-
when '"', "'"
|
|
66
|
-
if !quoting
|
|
67
|
-
quoting = char
|
|
68
|
-
elsif quoting == char
|
|
69
|
-
quoting = false
|
|
70
|
-
end
|
|
71
|
-
when ":",","
|
|
72
|
-
marks << scanner.pos - 1 unless quoting
|
|
73
|
-
when "\\"
|
|
74
|
-
scanner.skip(/\\/)
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
if marks.empty?
|
|
79
|
-
json.gsub(/\\\//, '/')
|
|
80
|
-
else
|
|
81
|
-
left_pos = [-1].push(*marks)
|
|
82
|
-
right_pos = marks << json.bytesize
|
|
83
|
-
output = []
|
|
84
|
-
|
|
85
|
-
left_pos.each_with_index do |left, i|
|
|
86
|
-
if json.respond_to?(:byteslice)
|
|
87
|
-
output << json.byteslice(left.succ..right_pos[i])
|
|
88
|
-
else
|
|
89
|
-
output << json[left.succ..right_pos[i]]
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
output = output * " "
|
|
94
|
-
|
|
95
|
-
times.each { |i| output[i-1] = ' ' }
|
|
96
|
-
output.gsub!(/\\\//, '/')
|
|
97
|
-
output
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
38
|
end
|
|
101
39
|
end
|
|
102
40
|
end
|
|
103
|
-
end
|
|
41
|
+
end
|
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
|
+
version: 4.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bartosz Blimke
|
|
@@ -29,14 +29,14 @@ dependencies:
|
|
|
29
29
|
requirements:
|
|
30
30
|
- - ">="
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: 0.
|
|
32
|
+
version: 0.4.6
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: 0.
|
|
39
|
+
version: 0.4.6
|
|
40
40
|
- !ruby/object:Gem::Dependency
|
|
41
41
|
name: hashdiff
|
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -363,9 +363,9 @@ licenses:
|
|
|
363
363
|
- MIT
|
|
364
364
|
metadata:
|
|
365
365
|
bug_tracker_uri: https://github.com/bblimke/webmock/issues
|
|
366
|
-
changelog_uri: https://github.com/bblimke/webmock/blob/
|
|
367
|
-
documentation_uri: https://www.rubydoc.info/gems/webmock/
|
|
368
|
-
source_code_uri: https://github.com/bblimke/webmock/tree/
|
|
366
|
+
changelog_uri: https://github.com/bblimke/webmock/blob/v4.0.0/CHANGELOG.md
|
|
367
|
+
documentation_uri: https://www.rubydoc.info/gems/webmock/4.0.0
|
|
368
|
+
source_code_uri: https://github.com/bblimke/webmock/tree/v4.0.0
|
|
369
369
|
wiki_uri: https://github.com/bblimke/webmock/wiki
|
|
370
370
|
rdoc_options: []
|
|
371
371
|
require_paths:
|
|
@@ -374,7 +374,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
374
374
|
requirements:
|
|
375
375
|
- - ">="
|
|
376
376
|
- !ruby/object:Gem::Version
|
|
377
|
-
version: '
|
|
377
|
+
version: '3.0'
|
|
378
378
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
379
379
|
requirements:
|
|
380
380
|
- - ">="
|