webmock 3.37.0 → 3.38.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 +96 -0
- data/README.md +141 -4
- data/lib/webmock/http_lib_adapters/net_http.rb +2 -0
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +32 -4
- data/lib/webmock/request_pattern.rb +111 -29
- data/lib/webmock/request_stub.rb +8 -4
- data/lib/webmock/util/hash_keys_stringifier.rb +23 -1
- data/lib/webmock/util/uri.rb +21 -12
- data/lib/webmock/util/values_stringifier.rb +54 -0
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +27 -4
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fe7cd5f67ec64e8f66e1ab898f7366c8e63901b9bcab1e5e51e72b7a5438b295
|
|
4
|
+
data.tar.gz: e30145eee641ae21272de856d6b2e56f9f9499e94bab4e975c44aeb30db213a9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e3a5cd28f04f014ff838d1ce81f16372033b11dd91047984e53b15b64e8696af56b3915a624935e913f64a42216db7960059ba7f558ca3ac865b1709843f1165
|
|
7
|
+
data.tar.gz: 7b0a0969e2b24116deeaf70cceb4fc85b7375f55118ccfb80ed4fa48594cecbde9bac650526c7d66ac162fef4d5eba90ac9cf4fee430b9e72de5f640b1cc6d2b
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,101 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
# 3.38.0
|
|
4
|
+
|
|
5
|
+
* Allow or disable real requests only within a block (#1110)
|
|
6
|
+
|
|
7
|
+
`WebMock.allow_net_connect!` and `WebMock.disable_net_connect!` now take a block, for example `WebMock.disable_net_connect!(allow: "api.example.com") { ... }`. The settings apply only while the block runs. When it ends, even with an exception, the settings from before the call come back.
|
|
8
|
+
|
|
9
|
+
Previously, either method ignored a block. In `WebMock.allow_net_connect! { Net::HTTP.get(uri) }`, the request never ran, and real requests stayed allowed until the settings changed again. Code in such a block now runs.
|
|
10
|
+
|
|
11
|
+
The settings are global, so other threads see them while the block runs. Don't run such blocks in several threads at once, because they can restore each other's settings in the wrong order.
|
|
12
|
+
|
|
13
|
+
Thanks to [Yegor Bugayenko](https://github.com/yegor256) for the suggestion
|
|
14
|
+
|
|
15
|
+
* Record the form Content-Type of Typhoeus requests with a hash body (#463)
|
|
16
|
+
|
|
17
|
+
Previously, `Typhoeus.post(url, body: {page: 2})` sent `Content-Type: application/x-www-form-urlencoded`, but WebMock recorded the request without it. A stub with `with(body: {page: 2})` didn't match, and neither did a stub that required that header.
|
|
18
|
+
|
|
19
|
+
WebMock now records the header whenever Typhoeus sends it for a hash body: for a POST, even with an empty hash, and for any other method except PUT and HEAD, unless the form is empty.
|
|
20
|
+
|
|
21
|
+
A request that sets its own Content-Type keeps it. Multipart forms, such as a hash with a file or a request with `multipart: true`, still have no Content-Type recorded, except an empty POST, which Typhoeus sends URL encoded even then. Requests with `nobody: true` have none either.
|
|
22
|
+
|
|
23
|
+
Callbacks, `WebMock.executed_requests` and VCR see the header too, and the stub suggested for an unmatched request includes it and shows a form body as a hash. Update assertions that compare all the headers of such a request. If VCR, hooked into WebMock, matches requests on their headers, record the affected cassettes again.
|
|
24
|
+
|
|
25
|
+
* Keep a `with` block when `with` is called again with options only
|
|
26
|
+
|
|
27
|
+
Previously, `stub_request(:post, url).with { |request| request.body == "yes" }.with(headers: {"X-A" => "1"})` dropped the block, so the stub also matched a request with that header and the body `"no"`. A later `with` call already kept the options it didn't give, such as `body:`, and now keeps the block too. The same applies to `a_request`.
|
|
28
|
+
|
|
29
|
+
A stub or request expectation that matched only because the block was dropped no longer matches. Calling `with` again with a block, or with an option given before, still replaces it.
|
|
30
|
+
|
|
31
|
+
* Match numbers and boolean values in query patterns (#371)
|
|
32
|
+
|
|
33
|
+
Previously, `with(query: hash_including(page: 2))` never matched `?page=2`, because query values in a request are strings and WebMock compared them with the Integer `2`. A plain hash had the same problem when the stub's URI was a regexp, a template or a lambda, as in `stub_request(:get, /example/).with(query: {page: 2})`. With a string URI, an array of numbers such as `{tag: [2, 10]}` didn't match `?tag[]=2&tag[]=10`.
|
|
34
|
+
|
|
35
|
+
WebMock now compares numbers, `true` and `false` in query patterns as strings, including inside nested `hash_including` and `hash_excluding` matchers.
|
|
36
|
+
|
|
37
|
+
This also makes `hash_excluding` work with such values. Previously, `with(query: hash_excluding(page: 2))` matched `?page=2`. A test that passed only because of this will now fail.
|
|
38
|
+
|
|
39
|
+
* Fix nested `hash_including` and `hash_excluding` with symbol keys in RSpec (#900)
|
|
40
|
+
|
|
41
|
+
Previously, in RSpec, `with(body: {user: hash_including(role: "admin")})` never matched the JSON body `{"user":{"role":"admin"}}`. WebMock converted the keys of the outer hash to strings, but not those of a matcher inside it, and the parsed body has string keys. For the same reason, a nested `hash_excluding` with symbol keys never excluded anything, so `with(body: {user: hash_excluding(role: "admin")})` matched that body too.
|
|
42
|
+
|
|
43
|
+
The same applied to query patterns: `with(query: hash_including(filter: hash_excluding(role: "admin")))` matched `?filter[role]=admin`.
|
|
44
|
+
|
|
45
|
+
Nested matchers now get string keys too, as do hashes inside RSpec's `array_including` and `array_excluding`. A test that passed only because a nested `hash_excluding` excluded nothing will now fail.
|
|
46
|
+
|
|
47
|
+
* Fix `hash_including` and `hash_excluding` nested in a query hash
|
|
48
|
+
|
|
49
|
+
Previously, `stub_request(:get, "www.example.com").with(query: {filter: hash_including("role" => "admin")})` never matched `?filter[role]=admin&filter[active]=true`, because WebMock wrote the matcher into the stub's URL as text. With a regexp, a template or a lambda URI, such a stub never matched in RSpec, where `hash_including` and `hash_excluding` are RSpec's own matchers.
|
|
50
|
+
|
|
51
|
+
Such stubs now match with every kind of URI.
|
|
52
|
+
|
|
53
|
+
* Ignore letter case in allowed host names (#819)
|
|
54
|
+
|
|
55
|
+
Previously, `WebMock.disable_net_connect!(allow: "Anachronic.local")` blocked requests to that host, because WebMock compared the allowed name with the request's host, which it writes in lowercase. The same applied to a host with a port, such as `"Anachronic.local:8080"`, and to host names given as `net_http_connect_on_start`.
|
|
56
|
+
|
|
57
|
+
Such requests are now allowed and reach the real server. Host names are not case sensitive, and allowed URLs have ignored the letter case of the host since 3.30.0.
|
|
58
|
+
|
|
59
|
+
* Fix URI templates not matching encoded characters in query values (#468)
|
|
60
|
+
|
|
61
|
+
Previously, `stub_request(:get, Addressable::Template.new("https://example.com/test{?q}"))` didn't match `https://example.com/test?q=env%3Aproduction`, although the template itself matches that URL. The same happened with `?q=env:production`, and with other punctuation such as `/`, `@` or `=`, encoded or not.
|
|
62
|
+
|
|
63
|
+
Both now match. As with string URIs, a stub can't tell such an encoded character from the character itself.
|
|
64
|
+
|
|
65
|
+
* Fix stubs with a hash body pattern raising on binary request bodies (#333)
|
|
66
|
+
|
|
67
|
+
Previously, a stub such as `stub_request(:post, url).with(body: {"name" => "x"})` raised `ArgumentError: invalid byte sequence in UTF-8` when the request body was binary data, such as an uploaded image. Since WebMock checks the newest stub first, this also broke requests that an older stub would have answered.
|
|
68
|
+
|
|
69
|
+
Such a body now doesn't match the hash pattern, so WebMock moves on to the next stub, as it already did for a body that isn't valid JSON.
|
|
70
|
+
|
|
71
|
+
* Fix unstubbed requests with some form bodies raising `ArgumentError` instead of WebMock's error
|
|
72
|
+
|
|
73
|
+
Previously, when a request with a URL encoded body matched no stub, WebMock could raise `ArgumentError` while it built the suggested stub for its error message. This happened when the body was binary data, or when it repeated a key, as in `a=1&a=2`, with `query_values_notation` set to `:flat`.
|
|
74
|
+
|
|
75
|
+
WebMock now raises its usual error, and the suggested stub shows such a body as a string.
|
|
76
|
+
|
|
77
|
+
* Fix Net::HTTP `OPTIONS *` requests raising `Addressable::URI::InvalidURIError` (#982)
|
|
78
|
+
|
|
79
|
+
Previously, `http.request(Net::HTTP::Options.new("*"))` raised `Invalid port number`, even with real connections allowed, because WebMock could not build a URI from the `*` request target.
|
|
80
|
+
|
|
81
|
+
WebMock now records such a request with the path `/*`, so stub it as `stub_request(:options, "www.example.com/*")`. Real requests still send `OPTIONS *`. Stubs and `assert_requested` can't tell such a request from an `OPTIONS` request for the path `/*`.
|
|
82
|
+
|
|
83
|
+
* Apply a changed `query_values_notation` to URLs that WebMock has already seen
|
|
84
|
+
|
|
85
|
+
Previously, after `WebMock::Config.instance.query_values_notation = :flat_array`, a URL that WebMock had already seen kept the query it got with the earlier notation, so `?a=1&a=2` still became `?a=2`. Whether a stub matched depended on which tests ran first.
|
|
86
|
+
|
|
87
|
+
WebMock now reads each request's URL with the current notation. A stub keeps the query as WebMock read it when the stub was declared, so set the notation before declaring stubs.
|
|
88
|
+
|
|
89
|
+
* Match `with(query: "...")` pairs in any order with `:flat_array`
|
|
90
|
+
|
|
91
|
+
Previously, with `WebMock::Config.instance.query_values_notation = :flat_array`, `with(query: "b=2&a=1")` didn't match a request for `?b=2&a=1`, and `with(query: "a=2&a=1")` didn't match `?a=2&a=1`. Only patterns with their pairs in sorted order matched.
|
|
92
|
+
|
|
93
|
+
Such patterns now match their pairs in any order, as a query in the stub's URL already did. A stub can't tell `?a=1&a=2` from `?a=2&a=1`.
|
|
94
|
+
|
|
95
|
+
* Fix URLs that repeat a query key without a value raising with `:flat_array`
|
|
96
|
+
|
|
97
|
+
Previously, with `:flat_array`, any request for a URL such as `?a&a=2` raised `ArgumentError`. It now matches `with(query: "a&a=2")`, but not `with(query: "a=&a=2")`, since the key has no value rather than an empty one.
|
|
98
|
+
|
|
3
99
|
# 3.37.0
|
|
4
100
|
|
|
5
101
|
* Add `executed_requests` to inspect the requests made (#1069)
|
data/README.md
CHANGED
|
@@ -23,7 +23,7 @@ Supported HTTP libraries
|
|
|
23
23
|
------------------------
|
|
24
24
|
|
|
25
25
|
* [Async::HTTP::Client](https://github.com/socketry/async-http)
|
|
26
|
-
* [Curb](https://github.com/taf2/curb) (
|
|
26
|
+
* [Curb](https://github.com/taf2/curb) (`Curl::Easy` only). `Curl::Multi` sends real requests even when a matching stub exists and `WebMock.disable_net_connect!` is in effect.
|
|
27
27
|
* [EM-HTTP-Request](https://github.com/igrigorik/em-http-request)
|
|
28
28
|
* [Excon](https://github.com/excon/excon)
|
|
29
29
|
* [HTTPClient](https://github.com/nahi/httpclient)
|
|
@@ -74,6 +74,8 @@ rake install
|
|
|
74
74
|
|
|
75
75
|
WebMock 2.x has changed somewhat since version 1.x. Changes are listed in [CHANGELOG.md](CHANGELOG.md)
|
|
76
76
|
|
|
77
|
+
## Setup
|
|
78
|
+
|
|
77
79
|
### Cucumber
|
|
78
80
|
|
|
79
81
|
Create a file `features/support/webmock.rb` with the following contents:
|
|
@@ -106,6 +108,48 @@ Add the following code to `test/test_helper.rb`
|
|
|
106
108
|
require 'webmock/test_unit'
|
|
107
109
|
```
|
|
108
110
|
|
|
111
|
+
### What the test framework integrations do
|
|
112
|
+
|
|
113
|
+
`webmock/rspec`, `webmock/minitest`, `webmock/test_unit` and `webmock/cucumber` do three things:
|
|
114
|
+
|
|
115
|
+
* They enable WebMock. `webmock/rspec` does it in a `before(:suite)` hook and disables WebMock again in `after(:suite)`. The others enable it as soon as they are required.
|
|
116
|
+
* They call `WebMock.reset!` after every example, test or scenario. This removes the stubs declared with `stub_request` and the history of requests, so a stub declared in RSpec's `before(:all)` or `before(:context)`, or in a minitest-hooks `before_all`, is gone after the first example. Declare such stubs in `before(:each)` or `setup` instead, or use [global stubs](#global-stubs), which `WebMock.reset!` keeps. If you override `teardown` in Minitest, call `super`, or WebMock's reset doesn't run.
|
|
117
|
+
* They leave the net connect settings alone. By default WebMock blocks real requests. `WebMock.allow_net_connect!` allows them all, and `WebMock.disable_net_connect!` blocks them again, except for what its options allow, such as `allow_localhost: true`. Each setting stays in effect for every later example until you change it, and each call to `disable_net_connect!` replaces the `allow`, `allow_localhost` and `net_http_connect_on_start` options given before.
|
|
118
|
+
|
|
119
|
+
Requiring one of these files in a spec helper or in any spec file enables WebMock for the whole run, not only for that file. To use WebMock only in some examples, require `webmock` and set it up yourself, for example for RSpec examples tagged with `:webmock`:
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
require 'webmock'
|
|
123
|
+
require 'webmock/rspec/matchers'
|
|
124
|
+
|
|
125
|
+
WebMock::AssertionFailure.error_class = RSpec::Expectations::ExpectationNotMetError
|
|
126
|
+
|
|
127
|
+
RSpec.configure do |config|
|
|
128
|
+
config.include WebMock::API, :webmock
|
|
129
|
+
config.include WebMock::Matchers, :webmock
|
|
130
|
+
|
|
131
|
+
config.around(:each, :webmock) do |example|
|
|
132
|
+
WebMock.enable!
|
|
133
|
+
WebMock.disable_net_connect!
|
|
134
|
+
example.run
|
|
135
|
+
ensure
|
|
136
|
+
WebMock.reset!
|
|
137
|
+
WebMock.disable!
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Examples without the tag then make real requests. Create HTTP clients inside each example: a client created while WebMock was disabled isn't intercepted, and a Curb or HTTPClient object created in a tagged example is still intercepted after it.
|
|
143
|
+
|
|
144
|
+
Enabling WebMock and allowing real requests are separate settings. `WebMock.enable!` and `WebMock.disable!` decide whether WebMock intercepts requests at all. `WebMock.disable_net_connect!` and `WebMock.allow_net_connect!` decide what happens to an intercepted request that no stub matches. While WebMock is disabled, it doesn't intercept requests, so these settings only take effect once it is enabled again.
|
|
145
|
+
|
|
146
|
+
For HTTP clients such as Net::HTTP, HTTPClient and Curb, WebMock intercepts requests by replacing their classes while it is enabled. `webmock/rspec` enables WebMock just before the first example, after your app and spec files have loaded. A class that inherits from `Net::HTTP`, or an HTTP client object such as a `Net::HTTP` or `HTTPClient` instance, created while they load uses the original class, and its requests reach the network. A client that creates its HTTP object for each request is not affected. If your app defines such a class or creates such an object while it loads, enable WebMock right after requiring it:
|
|
147
|
+
|
|
148
|
+
```ruby
|
|
149
|
+
require 'webmock/rspec'
|
|
150
|
+
WebMock.enable!
|
|
151
|
+
```
|
|
152
|
+
|
|
109
153
|
### Outside a test framework
|
|
110
154
|
|
|
111
155
|
You can also use WebMock outside a test framework:
|
|
@@ -335,6 +379,23 @@ RestClient.get("http://www.example.com/?a=b") # ===> Failure
|
|
|
335
379
|
RestClient.get("http://www.example.com/?a=c") # ===> Success
|
|
336
380
|
```
|
|
337
381
|
|
|
382
|
+
### Matching repeated query params
|
|
383
|
+
|
|
384
|
+
By default WebMock reads a query the way Rack and Rails do, so a key repeated without brackets keeps only its last value: `?a=1&a=2` is read as `?a=2`. To match repeated keys, switch to the `:flat_array` notation and give the query as a string:
|
|
385
|
+
|
|
386
|
+
```ruby
|
|
387
|
+
WebMock::Config.instance.query_values_notation = :flat_array
|
|
388
|
+
|
|
389
|
+
stub_request(:get, "www.example.com/?a=1&a=2")
|
|
390
|
+
# or
|
|
391
|
+
stub_request(:get, "www.example.com").with(query: "a=1&a=2")
|
|
392
|
+
|
|
393
|
+
RestClient.get("http://www.example.com/?a=1&a=2") # ===> Success
|
|
394
|
+
RestClient.get("http://www.example.com/?a=2") # ===> Failure
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
The notation applies to every stub and request. Hashes can't describe repeated keys, so with `:flat_array`, `hash_including`, `hash_excluding`, query hashes given with a regexp or template URI, and form bodies given as a hash don't match. Set the notation only in the tests that need it, and set it back to `:subscript` afterwards.
|
|
398
|
+
|
|
338
399
|
### Stubbing with custom response
|
|
339
400
|
|
|
340
401
|
```ruby
|
|
@@ -650,6 +711,26 @@ RestClient.get('bbc.com', '/') # ===> Allowed
|
|
|
650
711
|
RestClient.get('www.bbc.com', '/') # ===> Failure
|
|
651
712
|
```
|
|
652
713
|
|
|
714
|
+
### Real requests can be allowed or disabled only within a block
|
|
715
|
+
|
|
716
|
+
Given a block, `WebMock.allow_net_connect!` and `WebMock.disable_net_connect!` apply their settings only while the block runs and return the block's value. When the block ends, even with an exception, WebMock restores the settings from before the call.
|
|
717
|
+
|
|
718
|
+
```ruby
|
|
719
|
+
WebMock.disable_net_connect!
|
|
720
|
+
|
|
721
|
+
WebMock.allow_net_connect! do
|
|
722
|
+
Net::HTTP.get('www.something.com', '/') # ===> /.+Something.+/
|
|
723
|
+
end
|
|
724
|
+
|
|
725
|
+
WebMock.disable_net_connect!(allow: 'www.example.org') do
|
|
726
|
+
RestClient.get('www.example.org', '/') # ===> Allowed
|
|
727
|
+
end
|
|
728
|
+
|
|
729
|
+
Net::HTTP.get('www.something.com', '/') # ===> Failure
|
|
730
|
+
```
|
|
731
|
+
|
|
732
|
+
Settings changed inside the block, for example by another call to `WebMock.disable_net_connect!`, are undone as well. The settings are global, so other threads see them while the block runs, and blocks that overlap in different threads can restore each other's settings in the wrong order. Use the block form only where tests run one at a time in each process.
|
|
733
|
+
|
|
653
734
|
## Connecting on Net::HTTP.start
|
|
654
735
|
|
|
655
736
|
HTTP protocol has 3 steps: connect, request and response (or 4 with close). Most Ruby HTTP client libraries
|
|
@@ -726,6 +807,8 @@ assert_requested(stub_get)
|
|
|
726
807
|
assert_not_requested(stub_post)
|
|
727
808
|
```
|
|
728
809
|
|
|
810
|
+
See [What an expectation on a stub counts](#what-an-expectation-on-a-stub-counts) for which requests these assertions count.
|
|
811
|
+
|
|
729
812
|
|
|
730
813
|
### Setting expectations in RSpec on `WebMock` module
|
|
731
814
|
This style is borrowed from [fakeweb-matcher](http://github.com/pat/fakeweb-matcher)
|
|
@@ -802,6 +885,24 @@ expect(stub).to have_been_requested.once.with(body: {"a" => [4, 5, 6]})
|
|
|
802
885
|
|
|
803
886
|
Options and a block given to `with` take the same form as in `stub_request(...).with`. The expectation then counts only the requests that match both the stub and those conditions. The stub itself does not change.
|
|
804
887
|
|
|
888
|
+
### What an expectation on a stub counts
|
|
889
|
+
|
|
890
|
+
An expectation on a stub, such as `assert_requested(stub)` or `expect(stub).to have_been_requested`, counts the requests since the last `WebMock.reset!` or `WebMock.reset_executed_requests!` that match the stub's pattern. A matching request counts even if another stub answered it, if it was made before the stub was declared, or if it was made after the stub was removed with `remove_request_stub`.
|
|
891
|
+
|
|
892
|
+
```ruby
|
|
893
|
+
stub_any = stub_request(:post, "www.example.com")
|
|
894
|
+
stub_body = stub_request(:post, "www.example.com").with(body: "abc")
|
|
895
|
+
|
|
896
|
+
Net::HTTP.post(URI("http://www.example.com"), "abc") # answered by stub_body
|
|
897
|
+
|
|
898
|
+
expect(stub_body).to have_been_requested.once # ===> Success
|
|
899
|
+
expect(stub_any).to have_been_requested.once # ===> Success, as its pattern matches too
|
|
900
|
+
```
|
|
901
|
+
|
|
902
|
+
To make an expectation stricter, give the stub its own conditions, such as `with(body: "")` for requests without a body. In RSpec you can also add them to the expectation with `with`, as shown above. With `assert_requested`, pass the method and URL instead of the stub, for example `assert_requested(:post, "www.example.com", body: "")`. To count from zero, call `WebMock.reset_executed_requests!`.
|
|
903
|
+
|
|
904
|
+
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).
|
|
905
|
+
|
|
805
906
|
### Inspecting the requests made
|
|
806
907
|
|
|
807
908
|
`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.
|
|
@@ -828,14 +929,15 @@ A few things to know:
|
|
|
828
929
|
|
|
829
930
|
* The URI is normalized, with the port added and the query sorted, so compare its parts, such as `uri.host` or `uri.query_values`, rather than `uri.to_s`.
|
|
830
931
|
* The requests are copies, so changing them doesn't change what WebMock recorded.
|
|
831
|
-
* When a request uploads a file or stream, its body is the uploaded bytes. WebMock reads them without moving the stream, from regular files, tempfiles, `StringIO` and gzip readers over those. Other streams, such as pipes, are listed with the stream's `to_s`.
|
|
932
|
+
* When a Curb, Patron or em-http-request request uploads a file or stream, its body is the uploaded bytes. WebMock reads them without moving the stream, from regular files, tempfiles, `StringIO` and gzip readers over those. Other streams, such as pipes, are listed with the stream's `to_s`.
|
|
832
933
|
* Multipart fields of a Curb PUT are listed with their `name`, `content`, `content_type`, `local_file` and `remote_file`. Files in a Typhoeus body given as an array are listed as `Pathname` objects.
|
|
833
|
-
*
|
|
934
|
+
* With these three clients, blocks given to `stub_request(...).with` and `after_request` callbacks still receive the original file or stream, not the bytes. With other clients, such as Net::HTTP and Excon, the stream is read when the request is made, and blocks and callbacks receive the bytes.
|
|
935
|
+
* An expectation on a stub, such as `assert_requested(stub)`, reuses the decision the stub made when the upload happened, so a stub block that checks the stream gives the same result in the expectation. If the stub never saw the request, because another stub answered first or the stub was declared later, the expectation checks the uploaded bytes, as `a_request(...)` and conditions given to the expectation's `with` always do.
|
|
834
936
|
* Depending on the HTTP client, requests that WebMock blocked because real connections are disabled may be listed too.
|
|
835
937
|
|
|
836
938
|
## Clearing stubs and request history
|
|
837
939
|
|
|
838
|
-
If you want to reset all current stubs and history of requests use `WebMock.reset
|
|
940
|
+
If you want to reset all current stubs and history of requests use `WebMock.reset!`. [Global stubs](#global-stubs) stay.
|
|
839
941
|
|
|
840
942
|
```ruby
|
|
841
943
|
stub_request(:any, "www.example.com")
|
|
@@ -871,6 +973,28 @@ expect(stub).not_to have_been_requested
|
|
|
871
973
|
expect(stub2).not_to have_been_requested
|
|
872
974
|
```
|
|
873
975
|
|
|
976
|
+
## Global stubs
|
|
977
|
+
|
|
978
|
+
`WebMock.reset!` removes the stubs declared with `stub_request`, and `webmock/rspec`, `webmock/minitest`, `webmock/test_unit` and `webmock/cucumber` call it after every test or scenario. To answer some requests in every test, for example calls to a metrics service, declare a global stub once when the test suite starts:
|
|
979
|
+
|
|
980
|
+
```ruby
|
|
981
|
+
WebMock.globally_stub_request do |request|
|
|
982
|
+
{ status: 200, body: "{}" } if request.uri.host == "metrics.example.com"
|
|
983
|
+
end
|
|
984
|
+
```
|
|
985
|
+
|
|
986
|
+
WebMock calls the block with the request when it reaches the global stub. The block returns a response hash with the same keys as `to_return`, or `nil` to let other stubs handle the request. `WebMock.reset!` doesn't remove global stubs, and there is no way to remove only one, so declare each of them once, for example in RSpec's `before(:suite)`.
|
|
987
|
+
|
|
988
|
+
Global stubs are checked before the stubs declared with `stub_request`. To check a global stub after them instead, so a test can override it with its own stubs, declare it with `:after_local_stubs`:
|
|
989
|
+
|
|
990
|
+
```ruby
|
|
991
|
+
WebMock.globally_stub_request(:after_local_stubs) do |request|
|
|
992
|
+
{ status: 200, body: "{}" } if request.uri.host == "metrics.example.com"
|
|
993
|
+
end
|
|
994
|
+
```
|
|
995
|
+
|
|
996
|
+
Requests answered by a global stub are recorded like any other, so `a_request`, `assert_requested` and `executed_requests` include them.
|
|
997
|
+
|
|
874
998
|
## Disabling and enabling WebMock or only some http client adapters
|
|
875
999
|
|
|
876
1000
|
```ruby
|
|
@@ -908,6 +1032,8 @@ stub_request(:get, "www.example.com").to_return(body: "def")
|
|
|
908
1032
|
Net::HTTP.get('www.example.com', '/') # ====> "def"
|
|
909
1033
|
```
|
|
910
1034
|
|
|
1035
|
+
[Global stubs](#global-stubs) are checked before these stubs, or after them when declared with `:after_local_stubs`.
|
|
1036
|
+
|
|
911
1037
|
## Matching URIs
|
|
912
1038
|
|
|
913
1039
|
WebMock will match all different representations of the same URI.
|
|
@@ -1012,6 +1138,16 @@ WebMock.after_request(except: [:patron],
|
|
|
1012
1138
|
end
|
|
1013
1139
|
```
|
|
1014
1140
|
|
|
1141
|
+
#### Removing callbacks
|
|
1142
|
+
|
|
1143
|
+
Callbacks stay registered after `WebMock.reset!`, so a callback added in one test keeps running in later ones. To remove all callbacks, call:
|
|
1144
|
+
|
|
1145
|
+
```ruby
|
|
1146
|
+
WebMock.reset_callbacks
|
|
1147
|
+
```
|
|
1148
|
+
|
|
1149
|
+
This also removes callbacks registered by other libraries, such as VCR, so prefer registering your callbacks once, for example in your test helper.
|
|
1150
|
+
|
|
1015
1151
|
## Bugs and Issues
|
|
1016
1152
|
|
|
1017
1153
|
Please submit them here [http://github.com/bblimke/webmock/issues](http://github.com/bblimke/webmock/issues)
|
|
@@ -1342,6 +1478,7 @@ People who submitted patches and new features or suggested improvements. Many th
|
|
|
1342
1478
|
* Yang Bai
|
|
1343
1479
|
* Hamza Tarar
|
|
1344
1480
|
* John Gallagher
|
|
1481
|
+
* Yegor Bugayenko
|
|
1345
1482
|
|
|
1346
1483
|
For a full list of contributors you can visit the
|
|
1347
1484
|
[contributors](https://github.com/bblimke/webmock/contributors) page.
|
|
@@ -295,6 +295,8 @@ module WebMock
|
|
|
295
295
|
end
|
|
296
296
|
|
|
297
297
|
path = WebMock::Util::URI.heuristic_parse(path).request_uri if path =~ /^http/
|
|
298
|
+
# OPTIONS * (asterisk form) has no path; a URI can only hold it as /*
|
|
299
|
+
path = "/*" if path == "*" && request.method == "OPTIONS"
|
|
298
300
|
|
|
299
301
|
uri = get_uri(net_http, path)
|
|
300
302
|
method = request.method.downcase.to_sym
|
|
@@ -64,10 +64,8 @@ if defined?(Typhoeus)
|
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
body = req.options[:body]
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
body = WebMock::Util::QueryMapper.values_to_query(body)
|
|
70
|
-
end
|
|
67
|
+
form = body.is_a?(Hash)
|
|
68
|
+
body = WebMock::Util::QueryMapper.values_to_query(body) if form
|
|
71
69
|
|
|
72
70
|
request_signature = WebMock::RequestSignature.new(
|
|
73
71
|
req.options[:method] || :get,
|
|
@@ -76,11 +74,41 @@ if defined?(Typhoeus)
|
|
|
76
74
|
headers: headers
|
|
77
75
|
)
|
|
78
76
|
|
|
77
|
+
add_form_content_type(request_signature, req) if form
|
|
79
78
|
req.instance_variable_set(:@__webmock_request_signature, request_signature)
|
|
80
79
|
|
|
81
80
|
request_signature
|
|
82
81
|
end
|
|
83
82
|
|
|
83
|
+
# libcurl adds this header when Ethon sends a Hash body as a URL
|
|
84
|
+
# encoded form: for a POST, even with an empty form, and for other
|
|
85
|
+
# methods with a form that isn't empty, except PUT, which Ethon
|
|
86
|
+
# uploads, and HEAD, which sends no body. A multipart form, with a
|
|
87
|
+
# file or the multipart option, is sent with a boundary instead, which
|
|
88
|
+
# isn't recorded. Ethon's own form decides whether a form is empty or
|
|
89
|
+
# multipart, since that differs between Ethon versions. A Content-Type
|
|
90
|
+
# given as content_type counts as set, since WebMock reads header
|
|
91
|
+
# names that way, though libcurl then sends both headers. With the
|
|
92
|
+
# nobody option, or when Ethon can't read the form, such as for a
|
|
93
|
+
# file without a path, no header is recorded, as before.
|
|
94
|
+
def self.add_form_content_type(signature, req)
|
|
95
|
+
headers = signature.headers || {}
|
|
96
|
+
return if headers.key?('Content-Type') || req.options[:nobody] || !defined?(::Ethon::Easy::Form)
|
|
97
|
+
|
|
98
|
+
form = ::Ethon::Easy::Form.new(nil, req.options[:body], req.options[:multipart])
|
|
99
|
+
if form.empty?
|
|
100
|
+
return unless signature.method == :post
|
|
101
|
+
else
|
|
102
|
+
return if [:put, :head].include?(signature.method)
|
|
103
|
+
form.params_encoding = req.options.fetch(:params_encoding, :typhoeus)
|
|
104
|
+
return if form.multipart?
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
signature.headers = headers.merge('Content-Type' => 'application/x-www-form-urlencoded')
|
|
108
|
+
rescue StandardError
|
|
109
|
+
nil
|
|
110
|
+
end
|
|
111
|
+
|
|
84
112
|
def self.signature_for_history(signature)
|
|
85
113
|
return signature unless signature.body.is_a?(Array)
|
|
86
114
|
|
|
@@ -3,16 +3,48 @@
|
|
|
3
3
|
module WebMock
|
|
4
4
|
|
|
5
5
|
module RSpecMatcherDetector
|
|
6
|
+
extend self
|
|
7
|
+
|
|
6
8
|
def rSpecHashIncludingMatcher?(matcher)
|
|
7
|
-
matcher
|
|
9
|
+
rspec_argument_matcher_name(matcher) =~ /R?Spec::Mocks::ArgumentMatchers::HashIncludingMatcher/
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
def rSpecHashExcludingMatcher?(matcher)
|
|
11
|
-
matcher
|
|
13
|
+
rspec_argument_matcher_name(matcher) =~ /R?Spec::Mocks::ArgumentMatchers::HashExcludingMatcher/
|
|
12
14
|
end
|
|
13
15
|
|
|
14
16
|
def rSpecAnyArgMatcher?(matcher)
|
|
15
|
-
matcher
|
|
17
|
+
rspec_argument_matcher_name(matcher) =~ /R?Spec::Mocks::ArgumentMatchers::AnyArgMatcher/
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def rSpecHashMatcher?(matcher)
|
|
21
|
+
rspec_argument_matcher_name(matcher) =~ /R?Spec::Mocks::ArgumentMatchers::Hash(In|Ex)cludingMatcher/
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def rSpecArrayMatcher?(matcher)
|
|
25
|
+
rspec_argument_matcher_name(matcher) =~ /R?Spec::Mocks::ArgumentMatchers::Array(In|Ex)cludingMatcher/
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The class name of a possible RSpec matcher, or nil for values that
|
|
29
|
+
# can't be one, which are most values in a pattern. A custom matcher can
|
|
30
|
+
# be a BasicObject, which has no #class.
|
|
31
|
+
def rspec_argument_matcher_name(matcher)
|
|
32
|
+
case matcher
|
|
33
|
+
when String, Symbol, Numeric, Regexp, Hash, Array, nil, true, false then nil
|
|
34
|
+
when ::Object then matcher.class.name
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Turns RSpec's hash_including or hash_excluding into WebMock's matcher,
|
|
39
|
+
# which compares string keys. Returns anything else as it is.
|
|
40
|
+
def webmock_hash_matcher(matcher)
|
|
41
|
+
if rSpecHashIncludingMatcher?(matcher)
|
|
42
|
+
WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(matcher)
|
|
43
|
+
elsif rSpecHashExcludingMatcher?(matcher)
|
|
44
|
+
WebMock::Matchers::HashExcludingMatcher.from_rspec_matcher(matcher)
|
|
45
|
+
else
|
|
46
|
+
matcher
|
|
47
|
+
end
|
|
16
48
|
end
|
|
17
49
|
end
|
|
18
50
|
|
|
@@ -37,7 +69,8 @@ module WebMock
|
|
|
37
69
|
def with(options = {}, &block)
|
|
38
70
|
raise ArgumentError.new('#with method invoked with no arguments. Either options hash or block must be specified. Created a block with do..end? Try creating it with curly braces {} instead.') if options.empty? && !block_given?
|
|
39
71
|
assign_options(options)
|
|
40
|
-
|
|
72
|
+
# A later call with options only keeps the block given earlier.
|
|
73
|
+
@with_block = block if block
|
|
41
74
|
@match_token = (@body_pattern || @with_block) && Object.new
|
|
42
75
|
self
|
|
43
76
|
end
|
|
@@ -144,21 +177,19 @@ module WebMock
|
|
|
144
177
|
end
|
|
145
178
|
|
|
146
179
|
def add_query_params(query_params)
|
|
147
|
-
|
|
148
|
-
|
|
180
|
+
query_params = webmock_hash_matcher(query_params)
|
|
181
|
+
@query_params = if query_params.is_a?(Hash) \
|
|
182
|
+
|| query_params.is_a?(WebMock::Matchers::HashIncludingMatcher) \
|
|
183
|
+
|| query_params.is_a?(WebMock::Matchers::HashExcludingMatcher)
|
|
184
|
+
# Query values are always strings, so expected values such as 1 or
|
|
185
|
+
# true are compared as "1" and "true", in nested matchers too.
|
|
186
|
+
WebMock::Util::ValuesStringifier.stringify_query_values(query_params)
|
|
149
187
|
elsif query_params.equal?(:any) \
|
|
150
188
|
|| query_params.is_a?(WebMock::Matchers::AnyArgMatcher) \
|
|
151
189
|
|| rSpecAnyArgMatcher?(query_params)
|
|
152
190
|
WebMock::Matchers::AnyArgMatcher.new(nil)
|
|
153
|
-
elsif query_params.is_a?(WebMock::Matchers::HashIncludingMatcher) \
|
|
154
|
-
|| query_params.is_a?(WebMock::Matchers::HashExcludingMatcher)
|
|
155
|
-
query_params
|
|
156
|
-
elsif rSpecHashIncludingMatcher?(query_params)
|
|
157
|
-
WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(query_params)
|
|
158
|
-
elsif rSpecHashExcludingMatcher?(query_params)
|
|
159
|
-
WebMock::Matchers::HashExcludingMatcher.from_rspec_matcher(query_params)
|
|
160
191
|
elsif query_params.nil? || query_params.is_a?(String)
|
|
161
|
-
WebMock::Util::QueryMapper.query_to_values(query_params, notation: Config.instance.query_values_notation)
|
|
192
|
+
sorted_query_values(WebMock::Util::QueryMapper.query_to_values(query_params, notation: Config.instance.query_values_notation))
|
|
162
193
|
else
|
|
163
194
|
raise ArgumentError, "Query should be a String, Hash, :any, nil, or a hash_including or hash_excluding matcher. Got: #{query_params.class}"
|
|
164
195
|
end
|
|
@@ -185,7 +216,18 @@ module WebMock
|
|
|
185
216
|
end
|
|
186
217
|
|
|
187
218
|
def query_params_matches?(uri)
|
|
188
|
-
@query_params.nil?
|
|
219
|
+
return true if @query_params.nil?
|
|
220
|
+
|
|
221
|
+
actual = WebMock::Util::QueryMapper.query_to_values(uri.query, notation: Config.instance.query_values_notation)
|
|
222
|
+
actual = sorted_query_values(actual) if @query_params.is_a?(Array)
|
|
223
|
+
@query_params == actual
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# With :flat_array the values are pairs in the order given, so both
|
|
227
|
+
# sides are sorted, as the queries of normalized URIs are. Adapters
|
|
228
|
+
# pass normalized URIs, but a signature can be given any URI.
|
|
229
|
+
def sorted_query_values(values)
|
|
230
|
+
values.is_a?(Array) ? WebMock::Util::URI.sort_query_values(values) : values
|
|
189
231
|
end
|
|
190
232
|
end
|
|
191
233
|
|
|
@@ -243,11 +285,37 @@ module WebMock
|
|
|
243
285
|
rescue Addressable::URI::InvalidURIError
|
|
244
286
|
Addressable::Template.new(@pattern.pattern)
|
|
245
287
|
end
|
|
246
|
-
WebMock::Util::URI.variations_of_uri_as_strings(uri)
|
|
288
|
+
variations = WebMock::Util::URI.variations_of_uri_as_strings(uri)
|
|
289
|
+
# Normalized URIs decode reserved characters such as ":", "/" and "=" in
|
|
290
|
+
# the query, while RFC 6570 query expansion expects them encoded. The
|
|
291
|
+
# strict variations keep the path as normalized, since decoding it
|
|
292
|
+
# could turn an encoded "/" or "?" into a different path.
|
|
293
|
+
# Encoding can only change a query with other characters than these,
|
|
294
|
+
# or with a second "=" in a pair.
|
|
295
|
+
strict_query = uri.query && uri.query.match?(/[^A-Za-z0-9\-._~%=&]|=[^&]*=/) && strictly_encoded_query(uri.query)
|
|
296
|
+
if strict_query && strict_query != uri.query
|
|
297
|
+
suffix = "?#{strict_query}#{"##{uri.fragment}" if uri.fragment}"
|
|
298
|
+
base = uri.omit(:query, :fragment)
|
|
299
|
+
bases = [base]
|
|
300
|
+
bases << base.omit(:port) if base.port == Addressable::URI.port_mapping[base.scheme]
|
|
301
|
+
variations = (variations + bases.map { |b| "#{b}#{suffix}" }).uniq
|
|
302
|
+
end
|
|
303
|
+
variations.any? { |u|
|
|
247
304
|
template_matches_uri?(template, u)
|
|
248
305
|
}
|
|
249
306
|
end
|
|
250
307
|
|
|
308
|
+
def strictly_encoded_query(query)
|
|
309
|
+
query.split("&").map { |pair|
|
|
310
|
+
pair.split("=", 2).map { |part|
|
|
311
|
+
Addressable::URI.encode_component(
|
|
312
|
+
Addressable::URI.unencode_component(part),
|
|
313
|
+
Addressable::URI::CharacterClasses::UNRESERVED
|
|
314
|
+
)
|
|
315
|
+
}.join("=")
|
|
316
|
+
}.join("&")
|
|
317
|
+
end
|
|
318
|
+
|
|
251
319
|
def template_matches_uri?(template, uri)
|
|
252
320
|
template.match(uri)
|
|
253
321
|
rescue Addressable::URI::InvalidURIError
|
|
@@ -257,12 +325,21 @@ module WebMock
|
|
|
257
325
|
|
|
258
326
|
class URIStringPattern < URIPattern
|
|
259
327
|
def add_query_params(query_params)
|
|
328
|
+
# A query hash with a matcher, kept from an earlier call.
|
|
329
|
+
earlier = @query_params if @query_params.is_a?(Hash)
|
|
260
330
|
super
|
|
261
331
|
if @query_params.is_a?(Hash) || @query_params.is_a?(String)
|
|
262
|
-
query_hash = (WebMock::Util::QueryMapper.query_to_values(@pattern.query, notation: Config.instance.query_values_notation) || {}).merge(@query_params)
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
332
|
+
query_hash = (WebMock::Util::QueryMapper.query_to_values(@pattern.query, notation: Config.instance.query_values_notation) || {}).merge(earlier || {}).merge(@query_params)
|
|
333
|
+
if WebMock::Util::ValuesStringifier.includes_query_matcher?(query_hash)
|
|
334
|
+
# A matcher can't be written into the URI, so the whole query is
|
|
335
|
+
# compared on its own.
|
|
336
|
+
@pattern.query = nil
|
|
337
|
+
@query_params = query_hash
|
|
338
|
+
else
|
|
339
|
+
@pattern.query = WebMock::Util::QueryMapper.values_to_query(query_hash, notation: WebMock::Config.instance.query_values_notation)
|
|
340
|
+
@query_params = nil
|
|
341
|
+
end
|
|
342
|
+
elsif @query_params.is_a?(WebMock::Matchers::AnyArgMatcher) && (@pattern.query || earlier)
|
|
266
343
|
# The URI is then compared without its query, so it could never match.
|
|
267
344
|
raise_any_query_with_uri_query
|
|
268
345
|
end
|
|
@@ -291,21 +368,23 @@ module WebMock
|
|
|
291
368
|
class BodyPattern
|
|
292
369
|
include RSpecMatcherDetector
|
|
293
370
|
|
|
371
|
+
# A String body that isn't valid UTF-8, such as an uploaded file, can't
|
|
372
|
+
# be a URL encoded form, so it isn't parsed as one.
|
|
373
|
+
def self.form_parseable?(body)
|
|
374
|
+
return true unless body.is_a?(String)
|
|
375
|
+
return true if body.ascii_only?
|
|
376
|
+
|
|
377
|
+
body = body.dup.force_encoding(Encoding::UTF_8) unless body.encoding == Encoding::UTF_8
|
|
378
|
+
body.valid_encoding?
|
|
379
|
+
end
|
|
380
|
+
|
|
294
381
|
# Kept so existing references to the constant still resolve.
|
|
295
382
|
BODY_FORMATS = WebMock::Util::ContentType::BODY_FORMATS
|
|
296
383
|
|
|
297
384
|
attr_reader :pattern
|
|
298
385
|
|
|
299
386
|
def initialize(pattern)
|
|
300
|
-
@pattern =
|
|
301
|
-
normalize_hash(pattern)
|
|
302
|
-
elsif rSpecHashIncludingMatcher?(pattern)
|
|
303
|
-
WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(pattern)
|
|
304
|
-
elsif rSpecHashExcludingMatcher?(pattern)
|
|
305
|
-
WebMock::Matchers::HashExcludingMatcher.from_rspec_matcher(pattern)
|
|
306
|
-
else
|
|
307
|
-
pattern
|
|
308
|
-
end
|
|
387
|
+
@pattern = pattern.is_a?(Hash) ? normalize_hash(pattern) : webmock_hash_matcher(pattern)
|
|
309
388
|
end
|
|
310
389
|
|
|
311
390
|
def matches?(body, content_type = "", serialized_body = nil)
|
|
@@ -349,6 +428,9 @@ module WebMock
|
|
|
349
428
|
when :xml then
|
|
350
429
|
WebMock::Util::Parsers::XML.parse(body)
|
|
351
430
|
else
|
|
431
|
+
# A body that isn't valid UTF-8, such as an uploaded file, can't be
|
|
432
|
+
# a URL encoded form, so it doesn't match a hash.
|
|
433
|
+
return unless self.class.form_parseable?(body)
|
|
352
434
|
WebMock::Util::QueryMapper.query_to_values(body, notation: Config.instance.query_values_notation)
|
|
353
435
|
end
|
|
354
436
|
rescue WebMock::Util::Parsers::ParseError
|
data/lib/webmock/request_stub.rb
CHANGED
|
@@ -114,10 +114,14 @@ module WebMock
|
|
|
114
114
|
stub = self.new(signature.method.to_sym, signature.uri.to_s)
|
|
115
115
|
|
|
116
116
|
if signature.body.to_s != ''
|
|
117
|
-
body =
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
117
|
+
body = signature.body
|
|
118
|
+
if signature.url_encoded? && BodyPattern.form_parseable?(body)
|
|
119
|
+
begin
|
|
120
|
+
body = WebMock::Util::QueryMapper.query_to_values(body, notation: Config.instance.query_values_notation)
|
|
121
|
+
rescue ArgumentError
|
|
122
|
+
# Some notations, such as :flat, reject a repeated key, so the
|
|
123
|
+
# stub shows the body as it is.
|
|
124
|
+
end
|
|
121
125
|
end
|
|
122
126
|
stub.with(body: body)
|
|
123
127
|
end
|
|
@@ -18,10 +18,32 @@ module WebMock
|
|
|
18
18
|
[k,v]
|
|
19
19
|
}.inject([]) {|r,x| r + x}]
|
|
20
20
|
else
|
|
21
|
-
arg
|
|
21
|
+
options[:deep] ? with_string_keys(arg) : arg
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
+
# RSpec's hash_including and hash_excluding nested in a pattern keep
|
|
26
|
+
# their keys as given, so they get string keys here, as do hashes in
|
|
27
|
+
# array_including and array_excluding. They stay RSpec matchers, which
|
|
28
|
+
# compare nested values the way RSpec does. A matcher without keys to
|
|
29
|
+
# change is kept as it is.
|
|
30
|
+
def self.with_string_keys(arg)
|
|
31
|
+
# Defined in request_pattern.rb, which this file can be loaded without.
|
|
32
|
+
return arg unless defined?(WebMock::RSpecMatcherDetector)
|
|
33
|
+
|
|
34
|
+
name = if WebMock::RSpecMatcherDetector.rSpecHashMatcher?(arg)
|
|
35
|
+
:@expected
|
|
36
|
+
elsif WebMock::RSpecMatcherDetector.rSpecArrayMatcher?(arg)
|
|
37
|
+
arg.instance_variable_defined?(:@expected) ? :@expected : :@unexpected
|
|
38
|
+
end
|
|
39
|
+
return arg unless name
|
|
40
|
+
|
|
41
|
+
expected = arg.instance_variable_get(name)
|
|
42
|
+
converted = stringify_keys!(expected, deep: true)
|
|
43
|
+
converted == expected ? arg : arg.class.new(converted)
|
|
44
|
+
end
|
|
45
|
+
private_class_method :with_string_keys
|
|
46
|
+
|
|
25
47
|
end
|
|
26
48
|
end
|
|
27
49
|
end
|
data/lib/webmock/util/uri.rb
CHANGED
|
@@ -13,16 +13,20 @@ module WebMock
|
|
|
13
13
|
hash[key] = Addressable::URI.heuristic_parse(key)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
normalized_uri
|
|
16
|
+
# Keyed by query notation, then by URI, so changing
|
|
17
|
+
# Config.instance.query_values_notation applies to URIs seen before.
|
|
18
|
+
NORMALIZED_URIS = Hash.new do |by_notation, notation|
|
|
19
|
+
by_notation[notation] = Hash.new do |hash, uri|
|
|
20
|
+
normalized_uri = WebMock::Util::URI.heuristic_parse(uri)
|
|
21
|
+
if normalized_uri.query_values
|
|
22
|
+
sorted_query_values = sort_query_values(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query, notation: notation) || {})
|
|
23
|
+
normalized_uri.query = WebMock::Util::QueryMapper.values_to_query(sorted_query_values, notation: notation)
|
|
24
|
+
end
|
|
25
|
+
normalized_uri = normalized_uri.normalize #normalize! is slower
|
|
26
|
+
normalized_uri.query = normalized_uri.query.gsub("+", "%2B") if normalized_uri.query
|
|
27
|
+
normalized_uri.port = normalized_uri.inferred_port unless normalized_uri.port
|
|
28
|
+
hash[uri] = normalized_uri
|
|
21
29
|
end
|
|
22
|
-
normalized_uri = normalized_uri.normalize #normalize! is slower
|
|
23
|
-
normalized_uri.query = normalized_uri.query.gsub("+", "%2B") if normalized_uri.query
|
|
24
|
-
normalized_uri.port = normalized_uri.inferred_port unless normalized_uri.port
|
|
25
|
-
hash[uri] = normalized_uri
|
|
26
30
|
end
|
|
27
31
|
|
|
28
32
|
def self.heuristic_parse(uri)
|
|
@@ -32,7 +36,7 @@ module WebMock
|
|
|
32
36
|
def self.normalize_uri(uri)
|
|
33
37
|
return uri if uri.is_a?(Regexp)
|
|
34
38
|
uri = 'http://' + uri unless uri.match('^https?://') if uri.is_a?(String)
|
|
35
|
-
NORMALIZED_URIS[uri].dup
|
|
39
|
+
NORMALIZED_URIS[Config.instance.query_values_notation][uri].dup
|
|
36
40
|
end
|
|
37
41
|
|
|
38
42
|
def self.variations_of_uri_as_strings(uri_object, only_with_scheme: false)
|
|
@@ -76,8 +80,13 @@ module WebMock
|
|
|
76
80
|
private
|
|
77
81
|
|
|
78
82
|
def self.sort_query_values(query_values)
|
|
79
|
-
|
|
80
|
-
|
|
83
|
+
if query_values.is_a?(Hash)
|
|
84
|
+
Hash[*query_values.sort.inject([]) { |values, pair| values + pair}]
|
|
85
|
+
else
|
|
86
|
+
# Pairs from :flat_array. A key without "=" has a nil value, which
|
|
87
|
+
# can't be compared with a string, so it comes first.
|
|
88
|
+
query_values.sort_by { |key, value| value.nil? ? [key, 0, ""] : [key, 1, value] }
|
|
89
|
+
end
|
|
81
90
|
end
|
|
82
91
|
|
|
83
92
|
def self.uris_with_inferred_port_and_without(uris)
|
|
@@ -19,4 +19,58 @@ class WebMock::Util::ValuesStringifier
|
|
|
19
19
|
value
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
|
+
|
|
23
|
+
# Like stringify_values, for query patterns: also converts the values of
|
|
24
|
+
# nested hash_including and hash_excluding matchers. Strings are kept as
|
|
25
|
+
# given, so a String subclass keeps its own ===.
|
|
26
|
+
def self.stringify_query_values(value)
|
|
27
|
+
case value
|
|
28
|
+
when WebMock::Matchers::HashArgumentMatcher
|
|
29
|
+
# A copy, so a subclass keeps its own constructor and state.
|
|
30
|
+
value.dup.tap do |matcher|
|
|
31
|
+
matcher.instance_variable_set(:@expected, stringify_query_values(value.instance_variable_get(:@expected)))
|
|
32
|
+
end
|
|
33
|
+
when Hash
|
|
34
|
+
Hash[value.map { |k, v| [k, stringify_query_values(v)] }]
|
|
35
|
+
when Array
|
|
36
|
+
value.map { |v| stringify_query_values(v) }
|
|
37
|
+
when String
|
|
38
|
+
value
|
|
39
|
+
else
|
|
40
|
+
if WebMock::RSpecMatcherDetector.rSpecHashMatcher?(value)
|
|
41
|
+
expected = stringify_query_values(value.instance_variable_get(:@expected))
|
|
42
|
+
NestedRSpecMatcher.new(value.class.new(expected))
|
|
43
|
+
else
|
|
44
|
+
stringify_values(value)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Keeps RSpec's comparison of nested values, such as an array of regexps,
|
|
50
|
+
# and adds ==, which a query Hash uses to compare its values.
|
|
51
|
+
class NestedRSpecMatcher
|
|
52
|
+
def initialize(matcher)
|
|
53
|
+
@matcher = matcher
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def ===(actual)
|
|
57
|
+
@matcher === actual
|
|
58
|
+
end
|
|
59
|
+
alias == ===
|
|
60
|
+
|
|
61
|
+
def inspect
|
|
62
|
+
@matcher.description
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Whether a query pattern holds a matcher, so it can't be written into
|
|
67
|
+
# a URL.
|
|
68
|
+
def self.includes_query_matcher?(value)
|
|
69
|
+
case value
|
|
70
|
+
when Hash then value.each_value.any? { |v| includes_query_matcher?(v) }
|
|
71
|
+
when Array then value.any? { |v| includes_query_matcher?(v) }
|
|
72
|
+
when WebMock::Matchers::HashArgumentMatcher, NestedRSpecMatcher then true
|
|
73
|
+
else false
|
|
74
|
+
end
|
|
75
|
+
end
|
|
22
76
|
end
|
data/lib/webmock/version.rb
CHANGED
data/lib/webmock/webmock.rb
CHANGED
|
@@ -43,18 +43,39 @@ module WebMock
|
|
|
43
43
|
end
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
-
def self.allow_net_connect!(options = {})
|
|
46
|
+
def self.allow_net_connect!(options = {}, &block)
|
|
47
|
+
return restoring_net_connect_settings { allow_net_connect!(options); block.call } if block
|
|
48
|
+
|
|
47
49
|
Config.instance.allow_net_connect = true
|
|
48
50
|
Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
|
|
49
51
|
end
|
|
50
52
|
|
|
51
|
-
def self.disable_net_connect!(options = {})
|
|
53
|
+
def self.disable_net_connect!(options = {}, &block)
|
|
54
|
+
return restoring_net_connect_settings { disable_net_connect!(options); block.call } if block
|
|
55
|
+
|
|
52
56
|
Config.instance.allow_net_connect = false
|
|
53
57
|
Config.instance.allow_localhost = options[:allow_localhost]
|
|
54
58
|
Config.instance.allow = options[:allow]
|
|
55
59
|
Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
|
|
56
60
|
end
|
|
57
61
|
|
|
62
|
+
NET_CONNECT_SETTINGS = %i[allow_net_connect allow_localhost allow net_http_connect_on_start].freeze
|
|
63
|
+
private_constant :NET_CONNECT_SETTINGS
|
|
64
|
+
|
|
65
|
+
# Runs the block, then puts back the net connect settings from before it,
|
|
66
|
+
# even if the block raises. Settings assigned inside the block are undone.
|
|
67
|
+
# An allow list changed in place stays changed, and blocks overlapping in
|
|
68
|
+
# different threads can restore each other's settings in the wrong order.
|
|
69
|
+
def self.restoring_net_connect_settings
|
|
70
|
+
saved = NET_CONNECT_SETTINGS.map { |name| [name, Config.instance.public_send(name)] }
|
|
71
|
+
begin
|
|
72
|
+
yield
|
|
73
|
+
ensure
|
|
74
|
+
saved.each { |name, value| Config.instance.public_send(:"#{name}=", value) }
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
private_class_method :restoring_net_connect_settings
|
|
78
|
+
|
|
58
79
|
class << self
|
|
59
80
|
alias :enable_net_connect! :allow_net_connect!
|
|
60
81
|
alias :disallow_net_connect! :disable_net_connect!
|
|
@@ -90,8 +111,10 @@ module WebMock
|
|
|
90
111
|
allowed.match?(uri.to_s) ||
|
|
91
112
|
(uri.port == uri.default_port && allowed.match?(uri.omit(:port).to_s))
|
|
92
113
|
when String
|
|
93
|
-
|
|
94
|
-
|
|
114
|
+
# Host names are case insensitive. casecmp folds ASCII letters only,
|
|
115
|
+
# unlike casecmp?, which would also equate "straße" and "strasse".
|
|
116
|
+
(uri.host && allowed.casecmp(uri.host) == 0) ||
|
|
117
|
+
allowed.casecmp("#{uri.host}:#{uri.port}") == 0 ||
|
|
95
118
|
net_connect_allowed_url?(allowed, uri)
|
|
96
119
|
else
|
|
97
120
|
if allowed.respond_to?(:call)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: webmock
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.38.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bartosz Blimke
|
|
@@ -361,9 +361,9 @@ licenses:
|
|
|
361
361
|
- MIT
|
|
362
362
|
metadata:
|
|
363
363
|
bug_tracker_uri: https://github.com/bblimke/webmock/issues
|
|
364
|
-
changelog_uri: https://github.com/bblimke/webmock/blob/v3.
|
|
365
|
-
documentation_uri: https://www.rubydoc.info/gems/webmock/3.
|
|
366
|
-
source_code_uri: https://github.com/bblimke/webmock/tree/v3.
|
|
364
|
+
changelog_uri: https://github.com/bblimke/webmock/blob/v3.38.0/CHANGELOG.md
|
|
365
|
+
documentation_uri: https://www.rubydoc.info/gems/webmock/3.38.0
|
|
366
|
+
source_code_uri: https://github.com/bblimke/webmock/tree/v3.38.0
|
|
367
367
|
wiki_uri: https://github.com/bblimke/webmock/wiki
|
|
368
368
|
rdoc_options: []
|
|
369
369
|
require_paths:
|