webmock 4.2.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 +30 -0
- data/README.md +44 -14
- data/lib/webmock/config.rb +1 -0
- data/lib/webmock/errors.rb +6 -1
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +2 -3
- 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/request_stub.rb +1 -1
- 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/headers.rb +13 -11
- data/lib/webmock/util/upload_body.rb +12 -9
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +14 -14
- 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,35 @@
|
|
|
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
|
+
|
|
17
|
+
# 4.3.0
|
|
18
|
+
|
|
19
|
+
* Allow real requests except to destinations listed in `disallow:` (#1058)
|
|
20
|
+
|
|
21
|
+
`WebMock.allow_net_connect!(disallow: ["billing.example.com"])` lets tests reach other hosts. When WebMock intercepts a request to the billing host, it raises `WebMock::NetConnectNotAllowedError` unless a stub matches. `disallow:` accepts the same matchers as `allow:`.
|
|
22
|
+
|
|
23
|
+
Thanks to [sandfortw](https://github.com/sandfortw) for the suggestion
|
|
24
|
+
|
|
25
|
+
* Fix `RequestStub#then` silently ignoring its block (#1037)
|
|
26
|
+
|
|
27
|
+
Previously, `stub.then { |s| s.with(query: {page: "1"}) }` ignored the block. The stub now requires `page=1`, so a request without it no longer matches. With a block, `then` returns the block's result. Without a block, it still chains responses as before.
|
|
28
|
+
|
|
29
|
+
* Fix Excon splitting stubbed chunks when `Transfer-Encoding` is `["gzip", "chunked"]`
|
|
30
|
+
|
|
31
|
+
WebMock now keeps transfer encodings in the order you specify. With `body_chunks: ["ab", "cd"]` and Excon's `chunk_size: 1`, the streaming callback now receives `"ab"` and `"cd"`. Previously it received four single bytes.
|
|
32
|
+
|
|
3
33
|
# 4.2.0
|
|
4
34
|
|
|
5
35
|
* Add support for `body_chunks:` to specify which chunks a streaming HTTP client receives from a stub (#629)
|
data/README.md
CHANGED
|
@@ -512,7 +512,7 @@ stub_request(:get, "www.example.com").to_return(raw_response_file.read)
|
|
|
512
512
|
stub_request(:any, 'www.example.net').
|
|
513
513
|
to_return { |request| {body: request.body} }
|
|
514
514
|
|
|
515
|
-
RestClient.post('www.example.net', 'abc') # ===> "abc
|
|
515
|
+
RestClient.post('www.example.net', 'abc') # ===> "abc"
|
|
516
516
|
```
|
|
517
517
|
|
|
518
518
|
### Responses dynamically evaluated from lambda
|
|
@@ -521,7 +521,7 @@ RestClient.post('www.example.net', 'abc') # ===> "abc\n"
|
|
|
521
521
|
stub_request(:any, 'www.example.net').
|
|
522
522
|
to_return(lambda { |request| {body: request.body} })
|
|
523
523
|
|
|
524
|
-
RestClient.post('www.example.net', 'abc') # ===> "abc
|
|
524
|
+
RestClient.post('www.example.net', 'abc') # ===> "abc"
|
|
525
525
|
```
|
|
526
526
|
|
|
527
527
|
### Dynamically evaluated raw responses recorded with `curl -is`
|
|
@@ -539,7 +539,7 @@ stub_request(:get, "www.example.com").
|
|
|
539
539
|
stub_request(:any, 'www.example.net').
|
|
540
540
|
to_return(body: lambda { |request| request.body })
|
|
541
541
|
|
|
542
|
-
RestClient.post('www.example.net', 'abc') # ===> "abc
|
|
542
|
+
RestClient.post('www.example.net', 'abc') # ===> "abc"
|
|
543
543
|
```
|
|
544
544
|
|
|
545
545
|
### Rack responses
|
|
@@ -607,27 +607,29 @@ WebMock is enabled, as with `Curl::Easy` requests.
|
|
|
607
607
|
```ruby
|
|
608
608
|
stub_request(:get, "www.example.com").
|
|
609
609
|
to_return({body: "abc"}, {body: "def"})
|
|
610
|
-
Net::HTTP.get('www.example.com', '/') # ===> "abc
|
|
611
|
-
Net::HTTP.get('www.example.com', '/') # ===> "def
|
|
610
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc"
|
|
611
|
+
Net::HTTP.get('www.example.com', '/') # ===> "def"
|
|
612
612
|
|
|
613
613
|
#after all responses are used the last response will be returned infinitely
|
|
614
614
|
|
|
615
|
-
Net::HTTP.get('www.example.com', '/') # ===> "def
|
|
615
|
+
Net::HTTP.get('www.example.com', '/') # ===> "def"
|
|
616
616
|
```
|
|
617
617
|
|
|
618
618
|
### Multiple responses using chained `to_return()`, `to_raise()` or `to_timeout` declarations
|
|
619
619
|
|
|
620
620
|
```ruby
|
|
621
621
|
stub_request(:get, "www.example.com").
|
|
622
|
-
to_return({body: "abc"}).then.
|
|
622
|
+
to_return({body: "abc"}).then.
|
|
623
623
|
to_return({body: "def"}).then.
|
|
624
624
|
to_raise(MyException)
|
|
625
625
|
|
|
626
|
-
Net::HTTP.get('www.example.com', '/') # ===> "abc
|
|
627
|
-
Net::HTTP.get('www.example.com', '/') # ===> "def
|
|
626
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc"
|
|
627
|
+
Net::HTTP.get('www.example.com', '/') # ===> "def"
|
|
628
628
|
Net::HTTP.get('www.example.com', '/') # ===> MyException raised
|
|
629
629
|
```
|
|
630
630
|
|
|
631
|
+
Without a block, `.then` is optional between responses. With a block, `stub.then { |s| ... }` yields the stub and returns the block's result, just like Ruby's `then`.
|
|
632
|
+
|
|
631
633
|
### Specifying number of times given response should be returned
|
|
632
634
|
|
|
633
635
|
```ruby
|
|
@@ -635,9 +637,9 @@ stub_request(:get, "www.example.com").
|
|
|
635
637
|
to_return({body: "abc"}).times(2).then.
|
|
636
638
|
to_return({body: "def"})
|
|
637
639
|
|
|
638
|
-
Net::HTTP.get('www.example.com', '/') # ===> "abc
|
|
639
|
-
Net::HTTP.get('www.example.com', '/') # ===> "abc
|
|
640
|
-
Net::HTTP.get('www.example.com', '/') # ===> "def
|
|
640
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc"
|
|
641
|
+
Net::HTTP.get('www.example.com', '/') # ===> "abc"
|
|
642
|
+
Net::HTTP.get('www.example.com', '/') # ===> "def"
|
|
641
643
|
```
|
|
642
644
|
|
|
643
645
|
### Removing unused stubs
|
|
@@ -663,6 +665,18 @@ WebMock.disable_net_connect!
|
|
|
663
665
|
Net::HTTP.get('www.something.com', '/') # ===> Failure
|
|
664
666
|
```
|
|
665
667
|
|
|
668
|
+
### Block real requests to selected hosts or URLs
|
|
669
|
+
|
|
670
|
+
```ruby
|
|
671
|
+
WebMock.allow_net_connect!(disallow: ["billing.example.com"])
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
This allows real requests to other hosts. A request to `billing.example.com` needs a matching stub or it raises `WebMock::NetConnectNotAllowedError`. Each call to `allow_net_connect!` replaces the previous list.
|
|
675
|
+
|
|
676
|
+
`disallow:` accepts the same hosts, URLs, regular expressions, and callables as `allow:`. A URL with a path matches that exact path and query, so blocking `https://api.example/charge` still allows `https://api.example/charge?id=1`. Use a host name to block every path on that host.
|
|
677
|
+
|
|
678
|
+
When Patron, Curb, or Typhoeus follows redirects inside libcurl, WebMock checks only the initial request. For direct access to an async-http connection pool, use a host rule because WebMock sees only the pool endpoint.
|
|
679
|
+
|
|
666
680
|
### External requests can be disabled while allowing localhost
|
|
667
681
|
|
|
668
682
|
```ruby
|
|
@@ -790,13 +804,13 @@ which can be passed to `WebMock.allow_net_connect!` and `WebMock.disable_net_con
|
|
|
790
804
|
WebMock.allow_net_connect!(net_http_connect_on_start: true)
|
|
791
805
|
```
|
|
792
806
|
|
|
793
|
-
|
|
807
|
+
With this setting, Net::HTTP opens a connection on `start`, even if a stub later handles the request or `disable_net_connect!` blocks it. To connect on start only for your test server, give its host name.
|
|
794
808
|
|
|
795
809
|
```ruby
|
|
796
810
|
WebMock.allow_net_connect!(net_http_connect_on_start: "www.example.com")
|
|
797
811
|
```
|
|
798
812
|
|
|
799
|
-
|
|
813
|
+
A host listed in `disallow:` prevents the connection on start. The request path is unknown at that point, so a URL with a path in `net_http_connect_on_start:` never matches. WebMock applies path-specific `disallow:` rules when the request is made.
|
|
800
814
|
|
|
801
815
|
## Setting Expectations
|
|
802
816
|
|
|
@@ -943,6 +957,22 @@ To make an expectation stricter, give the stub its own conditions, such as `with
|
|
|
943
957
|
|
|
944
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).
|
|
945
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
|
+
|
|
946
976
|
### Inspecting the requests made
|
|
947
977
|
|
|
948
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.
|
data/lib/webmock/config.rb
CHANGED
|
@@ -12,6 +12,7 @@ module WebMock
|
|
|
12
12
|
attr_accessor :allow_net_connect
|
|
13
13
|
attr_accessor :allow_localhost
|
|
14
14
|
attr_accessor :allow
|
|
15
|
+
attr_accessor :disallow
|
|
15
16
|
attr_accessor :net_http_connect_on_start
|
|
16
17
|
attr_accessor :show_stubbing_instructions
|
|
17
18
|
attr_accessor :query_values_notation
|
data/lib/webmock/errors.rb
CHANGED
|
@@ -5,8 +5,13 @@ module WebMock
|
|
|
5
5
|
class NetConnectNotAllowedError < Exception
|
|
6
6
|
def initialize(request_signature)
|
|
7
7
|
request_signature_snippet = RequestSignatureSnippet.new(request_signature)
|
|
8
|
+
reason = if Config.instance.disallow
|
|
9
|
+
"Real HTTP connections to this destination are disabled."
|
|
10
|
+
else
|
|
11
|
+
"Real HTTP connections are disabled."
|
|
12
|
+
end
|
|
8
13
|
text = [
|
|
9
|
-
"
|
|
14
|
+
"#{reason} Unregistered request: #{request_signature}",
|
|
10
15
|
request_signature_snippet.stubbing_instructions,
|
|
11
16
|
request_signature_snippet.request_stubs,
|
|
12
17
|
"="*60
|
|
@@ -68,7 +68,6 @@ if defined?(Async::HTTP)
|
|
|
68
68
|
request_signature = build_request_signature(request)
|
|
69
69
|
WebMock::RequestRegistry.instance.put(request_signature)
|
|
70
70
|
webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
71
|
-
net_connect_allowed = WebMock.net_connect_allowed?(request_signature.uri)
|
|
72
71
|
real_request = false
|
|
73
72
|
|
|
74
73
|
if webmock_response
|
|
@@ -76,11 +75,11 @@ if defined?(Async::HTTP)
|
|
|
76
75
|
raise Async::TimeoutError, 'WebMock timeout error' if webmock_response.should_timeout
|
|
77
76
|
WebMockApplication.add_webmock_response(request, webmock_response)
|
|
78
77
|
response = @webmock_client.call(request)
|
|
79
|
-
elsif net_connect_allowed
|
|
78
|
+
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
|
80
79
|
response = @network_client.call(request)
|
|
81
80
|
real_request = true
|
|
82
81
|
else
|
|
83
|
-
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
82
|
+
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
84
83
|
end
|
|
85
84
|
|
|
86
85
|
if WebMock::CallbackRegistry.any_callbacks?
|
|
@@ -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
|
data/lib/webmock/request_stub.rb
CHANGED
|
@@ -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
|
data/lib/webmock/util/headers.rb
CHANGED
|
@@ -23,21 +23,23 @@ module WebMock
|
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
#
|
|
27
|
-
# the
|
|
28
|
-
#
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
# Request matching sorts repeated values. In responses the last cookie
|
|
27
|
+
# of the same name wins, and transfer codings apply in the order given.
|
|
28
|
+
# Some suites replace normalize_headers with a one-argument method.
|
|
29
|
+
ORDERED_RESPONSE_HEADERS = %w[Set-Cookie Transfer-Encoding].freeze
|
|
30
|
+
private_constant :ORDERED_RESPONSE_HEADERS
|
|
31
|
+
|
|
32
32
|
def self.normalize_response_headers(headers)
|
|
33
33
|
normalized = normalize_headers(headers)
|
|
34
|
-
return normalized unless normalized.is_a?(Hash) && normalized[
|
|
34
|
+
return normalized unless normalized.is_a?(Hash) && ORDERED_RESPONSE_HEADERS.any? { |name| normalized[name].is_a?(Array) }
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
headers.each do |name,
|
|
38
|
-
|
|
36
|
+
ordered = {}
|
|
37
|
+
headers.each do |name, values|
|
|
38
|
+
next unless values.is_a?(Array)
|
|
39
|
+
name = normalize_name(name)
|
|
40
|
+
ordered[name] = values.map(&:to_s) if ORDERED_RESPONSE_HEADERS.include?(name) && normalized[name].is_a?(Array)
|
|
39
41
|
end
|
|
40
|
-
|
|
42
|
+
normalized.merge(ordered)
|
|
41
43
|
end
|
|
42
44
|
|
|
43
45
|
def self.sorted_headers_string(headers)
|
|
@@ -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
data/lib/webmock/webmock.rb
CHANGED
|
@@ -47,6 +47,7 @@ module WebMock
|
|
|
47
47
|
return restoring_net_connect_settings { allow_net_connect!(options); block.call } if block
|
|
48
48
|
|
|
49
49
|
Config.instance.allow_net_connect = true
|
|
50
|
+
Config.instance.disallow = options[:disallow]
|
|
50
51
|
Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
|
|
51
52
|
end
|
|
52
53
|
|
|
@@ -54,12 +55,13 @@ module WebMock
|
|
|
54
55
|
return restoring_net_connect_settings { disable_net_connect!(options); block.call } if block
|
|
55
56
|
|
|
56
57
|
Config.instance.allow_net_connect = false
|
|
58
|
+
Config.instance.disallow = nil
|
|
57
59
|
Config.instance.allow_localhost = options[:allow_localhost]
|
|
58
60
|
Config.instance.allow = options[:allow]
|
|
59
61
|
Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
|
|
60
62
|
end
|
|
61
63
|
|
|
62
|
-
NET_CONNECT_SETTINGS = %i[allow_net_connect allow_localhost allow net_http_connect_on_start].freeze
|
|
64
|
+
NET_CONNECT_SETTINGS = %i[allow_net_connect allow_localhost allow disallow net_http_connect_on_start].freeze
|
|
63
65
|
private_constant :NET_CONNECT_SETTINGS
|
|
64
66
|
|
|
65
67
|
# Runs the block, then puts back the net connect settings from before it,
|
|
@@ -92,23 +94,21 @@ module WebMock
|
|
|
92
94
|
def self.net_connect_allowed_by_settings?(uri = nil)
|
|
93
95
|
return !!Config.instance.allow_net_connect if uri.nil?
|
|
94
96
|
|
|
95
|
-
if
|
|
96
|
-
|
|
97
|
+
if Config.instance.allow_net_connect
|
|
98
|
+
disallow = Config.instance.disallow
|
|
99
|
+
!disallow || !net_connect_explicit_allowed?(disallow, WebMock::Util::URI.normalize_uri(uri))
|
|
100
|
+
else
|
|
101
|
+
uri = WebMock::Util::URI.normalize_uri(uri) if uri.is_a?(String)
|
|
102
|
+
Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri) ||
|
|
103
|
+
Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri)
|
|
97
104
|
end
|
|
98
|
-
|
|
99
|
-
!!Config.instance.allow_net_connect ||
|
|
100
|
-
( Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri) ||
|
|
101
|
-
Config.instance.allow && net_connect_explicit_allowed?(Config.instance.allow, uri) )
|
|
102
105
|
end
|
|
103
106
|
|
|
104
107
|
def self.net_http_connect_on_start?(uri)
|
|
105
|
-
allowed = Config.instance.net_http_connect_on_start
|
|
106
|
-
|
|
107
|
-
if
|
|
108
|
-
|
|
109
|
-
else
|
|
110
|
-
net_connect_explicit_allowed?(allowed, uri)
|
|
111
|
-
end
|
|
108
|
+
allowed = Config.instance.net_http_connect_on_start
|
|
109
|
+
return false unless allowed
|
|
110
|
+
return false if net_connect_explicit_allowed?(Config.instance.disallow, uri)
|
|
111
|
+
allowed.equal?(true) || net_connect_explicit_allowed?(allowed, uri)
|
|
112
112
|
end
|
|
113
113
|
|
|
114
114
|
def self.net_connect_explicit_allowed?(allowed, uri=nil)
|
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:
|