webmock 3.36.0 → 3.37.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 +32 -0
- data/README.md +60 -1
- data/lib/webmock/api.rb +4 -0
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +1 -1
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +57 -9
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +7 -3
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +1 -1
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +1 -1
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +123 -107
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +1 -1
- data/lib/webmock/http_lib_adapters/net_http.rb +1 -1
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +32 -13
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +15 -2
- data/lib/webmock/request_pattern.rb +24 -4
- data/lib/webmock/request_registry.rb +74 -4
- data/lib/webmock/request_signature.rb +40 -3
- data/lib/webmock/stub_registry.rb +7 -6
- data/lib/webmock/util/hash_counter.rb +27 -8
- data/lib/webmock/util/upload_body.rb +63 -0
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock.rb +1 -0
- metadata +5 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dcdb50457447f4b9234934f5211c2ccd45a55e11c164a0faaeb6ba8a47d6949a
|
|
4
|
+
data.tar.gz: 28bdd8fac8769c6d3b7f2f63f6070f60f9c602f090ae14f9b957ddbf5bb4f93f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 90780a4cf7639e3a09132f59c2479d042eb2ee3c769835b2957d0a508c666a9c0c039884459bdd51aefdaa3f6efae8d3caad58ee80efcf41d8af0e202b04e1ad
|
|
7
|
+
data.tar.gz: 4fdddbbae16604e0b21b567e4e69038567c1eefb9e357b03998583379b6d7393e04ea68d490edb3f7175bb5082fcc187d8069402521bdc8072f956018a612ba8
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
# 3.37.0
|
|
4
|
+
|
|
5
|
+
* Add `executed_requests` to inspect the requests made (#1069)
|
|
6
|
+
|
|
7
|
+
`WebMock.executed_requests` returns every request made since the last reset, in order, including repeated ones. You can assert on each one's body or headers, for example `JSON.parse(WebMock.executed_requests.last.body)`.
|
|
8
|
+
|
|
9
|
+
Previously, this needed WebMock internals such as `WebMock::RequestRegistry.instance.requested_signatures.hash.keys`, which list each distinct request only once.
|
|
10
|
+
|
|
11
|
+
Thanks to [John Gallagher](https://github.com/johngallagher) for the idea, which we started pairing on at EuRuKo 2024 in Sarajevo.
|
|
12
|
+
|
|
13
|
+
* Fix stub matching for consecutive Curb PUT requests on the same handle
|
|
14
|
+
|
|
15
|
+
After `curl.http_put("first")`, WebMock could match a subsequent `curl.http_put` against the previous body. It now matches the second request with an empty body.
|
|
16
|
+
|
|
17
|
+
* Run HTTPClient request filters once per attempt on real requests
|
|
18
|
+
|
|
19
|
+
Previously, WebMock could run a filter twice, so a filter that generated a request ID could send a different ID to the server than the one WebMock recorded.
|
|
20
|
+
|
|
21
|
+
* Fix async HTTPClient requests hanging when an `after_request` callback is registered
|
|
22
|
+
|
|
23
|
+
Previously, with real connections allowed, WebMock read the response body for the callback from the same stream as your code, so reading part of a `get_async` response before calling `join` could hang.
|
|
24
|
+
|
|
25
|
+
* Match file and stream uploads by their content
|
|
26
|
+
|
|
27
|
+
Previously, a body pattern such as `with(body: "abc")` never matched a Curb, Patron or em-http-request request that uploaded a file or stream, because WebMock compared the pattern with the file object. Stubs now compare it with the uploaded bytes for Curb and Patron, and `assert_requested` does so for all three.
|
|
28
|
+
|
|
29
|
+
Blocks given to `stub_request(...).with` still receive the original file or stream.
|
|
30
|
+
|
|
31
|
+
* Count em-http-request requests blocked by `disable_net_connect!`
|
|
32
|
+
|
|
33
|
+
Previously, WebMock did not record them, so `assert_requested` did not count them. They are now counted, as with other HTTP clients.
|
|
34
|
+
|
|
3
35
|
# 3.36.0
|
|
4
36
|
|
|
5
37
|
* Fix matching of indexed arrays nested inside arrays of hashes (#1144)
|
data/README.md
CHANGED
|
@@ -802,6 +802,37 @@ expect(stub).to have_been_requested.once.with(body: {"a" => [4, 5, 6]})
|
|
|
802
802
|
|
|
803
803
|
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
804
|
|
|
805
|
+
### Inspecting the requests made
|
|
806
|
+
|
|
807
|
+
`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.
|
|
808
|
+
|
|
809
|
+
```ruby
|
|
810
|
+
stub_request(:post, "www.example.com/orders")
|
|
811
|
+
|
|
812
|
+
Net::HTTP.post(URI("http://www.example.com/orders"), '{"name":"John"}', "Content-Type" => "application/json")
|
|
813
|
+
|
|
814
|
+
request = WebMock.executed_requests.last
|
|
815
|
+
request.method # ===> :post
|
|
816
|
+
request.uri.path # ===> "/orders"
|
|
817
|
+
request.headers["Content-Type"] # ===> "application/json"
|
|
818
|
+
JSON.parse(request.body) # ===> {"name" => "John"}
|
|
819
|
+
```
|
|
820
|
+
|
|
821
|
+
In RSpec, Minitest and Test::Unit you can call it as `executed_requests`, and it starts empty in every test. For example, to check which requests were made and in what order:
|
|
822
|
+
|
|
823
|
+
```ruby
|
|
824
|
+
expect(executed_requests.map { |request| request.uri.path }).to eq(["/orders", "/orders/1/confirm"])
|
|
825
|
+
```
|
|
826
|
+
|
|
827
|
+
A few things to know:
|
|
828
|
+
|
|
829
|
+
* 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
|
+
* 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`.
|
|
832
|
+
* 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
|
+
* Blocks given to `stub_request(...).with` and `after_request` callbacks still receive the original file or stream, and `assert_requested(stub)` counts the requests that stub matched.
|
|
834
|
+
* Depending on the HTTP client, requests that WebMock blocked because real connections are disabled may be listed too.
|
|
835
|
+
|
|
805
836
|
## Clearing stubs and request history
|
|
806
837
|
|
|
807
838
|
If you want to reset all current stubs and history of requests use `WebMock.reset!`
|
|
@@ -820,7 +851,7 @@ assert_not_requested :get, "www.example.com" # ===> Success
|
|
|
820
851
|
|
|
821
852
|
## Clearing request counters
|
|
822
853
|
|
|
823
|
-
If you want to reset **only** the counters
|
|
854
|
+
If you want to reset **only** the counters and the list of executed requests, and keep the stubs, use `WebMock.reset_executed_requests!`
|
|
824
855
|
|
|
825
856
|
```ruby
|
|
826
857
|
stub = stub_request(:get, "www.example.com")
|
|
@@ -1283,6 +1314,34 @@ People who submitted patches and new features or suggested improvements. Many th
|
|
|
1283
1314
|
* Erik Berlin
|
|
1284
1315
|
* Josch Bockler
|
|
1285
1316
|
* Leslie Hoare
|
|
1317
|
+
* Chris Gunther
|
|
1318
|
+
* hikmetba-bit
|
|
1319
|
+
* Daniel Pepper
|
|
1320
|
+
* Christian Bruckmayer
|
|
1321
|
+
* Chris
|
|
1322
|
+
* Jan Graichen
|
|
1323
|
+
* Olivier Brisse
|
|
1324
|
+
* Henry Blyth
|
|
1325
|
+
* Sébastien S.
|
|
1326
|
+
* Euisan Kim
|
|
1327
|
+
* Alex
|
|
1328
|
+
* Andy Williams
|
|
1329
|
+
* Tim Mertens
|
|
1330
|
+
* Mike Taylor
|
|
1331
|
+
* James Rodovich
|
|
1332
|
+
* Michael Spiz
|
|
1333
|
+
* Tom Brammar
|
|
1334
|
+
* Benoit Tigeot
|
|
1335
|
+
* nightpool
|
|
1336
|
+
* Andrii Furmanets
|
|
1337
|
+
* thoran
|
|
1338
|
+
* Julik Tarkhanov
|
|
1339
|
+
* Bob Aman
|
|
1340
|
+
* AmeenKhan12345
|
|
1341
|
+
* Andrew K. Fader
|
|
1342
|
+
* Yang Bai
|
|
1343
|
+
* Hamza Tarar
|
|
1344
|
+
* John Gallagher
|
|
1286
1345
|
|
|
1287
1346
|
For a full list of contributors you can visit the
|
|
1288
1347
|
[contributors](https://github.com/bblimke/webmock/contributors) page.
|
data/lib/webmock/api.rb
CHANGED
|
@@ -72,6 +72,10 @@ module WebMock
|
|
|
72
72
|
WebMock::RequestRegistry.instance.reset!
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
+
def executed_requests
|
|
76
|
+
WebMock::RequestRegistry.instance.executed_requests
|
|
77
|
+
end
|
|
78
|
+
|
|
75
79
|
private
|
|
76
80
|
|
|
77
81
|
def convert_uri_method_and_options_to_request_and_options(method, uri, options, &block)
|
|
@@ -66,7 +66,7 @@ if defined?(Async::HTTP)
|
|
|
66
66
|
request.authority ||= self.authority
|
|
67
67
|
|
|
68
68
|
request_signature = build_request_signature(request)
|
|
69
|
-
WebMock::RequestRegistry.instance.
|
|
69
|
+
WebMock::RequestRegistry.instance.put(request_signature)
|
|
70
70
|
webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
71
71
|
net_connect_allowed = WebMock.net_connect_allowed?(request_signature.uri)
|
|
72
72
|
real_request = false
|
|
@@ -62,11 +62,35 @@ if defined?(Curl)
|
|
|
62
62
|
|
|
63
63
|
module Curl
|
|
64
64
|
class WebMockCurlEasy < Curl::Easy
|
|
65
|
+
# Curb's native PostField cannot be duplicated. Its public attributes
|
|
66
|
+
# describe the field without running a content callback or opening a file.
|
|
67
|
+
MultipartField = Struct.new(:name, :content, :content_type, :local_file, :remote_file) do
|
|
68
|
+
def self.from(field)
|
|
69
|
+
new(*members.map { |attribute| field.public_send(attribute)&.to_s })
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def to_s
|
|
73
|
+
return super unless content && !local_file && !remote_file
|
|
74
|
+
Curl::PostField.content(name, content).to_s
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def initialize_copy(original)
|
|
78
|
+
super
|
|
79
|
+
each_pair { |attribute, value| self[attribute] = value.dup }
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
private_constant :MultipartField
|
|
83
|
+
|
|
65
84
|
def curb_or_webmock
|
|
66
85
|
request_signature = build_request_signature
|
|
67
|
-
|
|
86
|
+
recorded = signature_for_history(request_signature)
|
|
87
|
+
if request_signature.method == :put && @put_data.respond_to?(:read)
|
|
88
|
+
request_signature = request_signature.with_upload_body(@put_data)
|
|
89
|
+
end
|
|
68
90
|
|
|
69
|
-
|
|
91
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(
|
|
92
|
+
request_signature, record: recorded)
|
|
93
|
+
if webmock_response
|
|
70
94
|
build_curb_response(webmock_response)
|
|
71
95
|
WebMock::CallbackRegistry.invoke_callbacks(
|
|
72
96
|
{lib: :curb}, request_signature, webmock_response)
|
|
@@ -86,6 +110,13 @@ if defined?(Curl)
|
|
|
86
110
|
end
|
|
87
111
|
end
|
|
88
112
|
|
|
113
|
+
def signature_for_history(signature)
|
|
114
|
+
body = signature.body
|
|
115
|
+
return signature unless body.is_a?(Curl::PostField) || (body.is_a?(Array) && multipart_form_post?)
|
|
116
|
+
signature.with_body_values { |field| MultipartField.from(field) }
|
|
117
|
+
end
|
|
118
|
+
private :signature_for_history
|
|
119
|
+
|
|
89
120
|
def build_request_signature
|
|
90
121
|
method = @webmock_method.to_s.downcase.to_sym
|
|
91
122
|
|
|
@@ -98,7 +129,11 @@ if defined?(Curl)
|
|
|
98
129
|
when :post, :patch
|
|
99
130
|
self.post_body || @post_body
|
|
100
131
|
when :put
|
|
101
|
-
@put_data
|
|
132
|
+
if @put_data.respond_to?(:read)
|
|
133
|
+
WebMock::Util::UploadBody.read(@put_data, headers)
|
|
134
|
+
else
|
|
135
|
+
@put_data
|
|
136
|
+
end
|
|
102
137
|
else
|
|
103
138
|
nil
|
|
104
139
|
end
|
|
@@ -270,10 +305,19 @@ if defined?(Curl)
|
|
|
270
305
|
end
|
|
271
306
|
end
|
|
272
307
|
|
|
273
|
-
def http_put
|
|
308
|
+
def http_put(data = nil)
|
|
309
|
+
# put_data= can supply the upcoming request's body until perform resets the method.
|
|
310
|
+
data = @put_data if data.nil? && @webmock_method == :put
|
|
274
311
|
@webmock_method = :put
|
|
275
|
-
@put_data = data
|
|
276
|
-
super
|
|
312
|
+
@put_data = data
|
|
313
|
+
return super('') if data.nil?
|
|
314
|
+
return super(data) if data.is_a?(String) || data.respond_to?(:read) || multipart_form_post?
|
|
315
|
+
|
|
316
|
+
# Curb joins other positional arguments as it would form fields.
|
|
317
|
+
@put_data = [data].join('&')
|
|
318
|
+
super(@put_data)
|
|
319
|
+
ensure
|
|
320
|
+
@put_data = nil
|
|
277
321
|
end
|
|
278
322
|
alias put http_put
|
|
279
323
|
|
|
@@ -293,10 +337,12 @@ if defined?(Curl)
|
|
|
293
337
|
@post_body = nil
|
|
294
338
|
end
|
|
295
339
|
|
|
296
|
-
def put_data=
|
|
340
|
+
def put_data=(data)
|
|
341
|
+
body = data.respond_to?(:read) ? data : data&.to_s
|
|
342
|
+
super(body)
|
|
297
343
|
@webmock_method = :put
|
|
298
|
-
@put_data =
|
|
299
|
-
|
|
344
|
+
@put_data = body
|
|
345
|
+
data
|
|
300
346
|
end
|
|
301
347
|
|
|
302
348
|
def post_body= data
|
|
@@ -359,6 +405,8 @@ if defined?(Curl)
|
|
|
359
405
|
end
|
|
360
406
|
|
|
361
407
|
def reset
|
|
408
|
+
@put_data = nil
|
|
409
|
+
reset_webmock_method
|
|
362
410
|
instance_variable_set(:@body_str, nil)
|
|
363
411
|
instance_variable_set(:@content_type, nil)
|
|
364
412
|
instance_variable_set(:@header_str, nil)
|
|
@@ -107,8 +107,6 @@ if defined?(EventMachine::HttpClient)
|
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
def send_request(head, body)
|
|
110
|
-
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
|
|
111
|
-
|
|
112
110
|
if stubbed_webmock_response
|
|
113
111
|
WebMock::CallbackRegistry.invoke_callbacks({lib: :em_http_request}, request_signature, stubbed_webmock_response)
|
|
114
112
|
@uri ||= nil
|
|
@@ -144,7 +142,13 @@ if defined?(EventMachine::HttpClient)
|
|
|
144
142
|
|
|
145
143
|
def stubbed_webmock_response
|
|
146
144
|
unless defined?(@stubbed_webmock_response)
|
|
147
|
-
|
|
145
|
+
signature = request_signature
|
|
146
|
+
recorded = if signature.body.respond_to?(:read) && signature.body.respond_to?(:eof?)
|
|
147
|
+
signature.with_body_values { |stream| WebMock::Util::UploadBody.read(stream, signature.headers) }
|
|
148
|
+
else
|
|
149
|
+
signature
|
|
150
|
+
end
|
|
151
|
+
@stubbed_webmock_response = WebMock::StubRegistry.instance.response_for_request(signature, record: recorded)
|
|
148
152
|
end
|
|
149
153
|
|
|
150
154
|
@stubbed_webmock_response
|
|
@@ -53,7 +53,7 @@ if defined?(Excon)
|
|
|
53
53
|
|
|
54
54
|
def self.handle_request(params)
|
|
55
55
|
mock_request = self.build_request params.dup
|
|
56
|
-
WebMock::RequestRegistry.instance.
|
|
56
|
+
WebMock::RequestRegistry.instance.put(mock_request)
|
|
57
57
|
|
|
58
58
|
if mock_response = WebMock::StubRegistry.instance.response_for_request(mock_request)
|
|
59
59
|
self.perform_callbacks(mock_request, mock_response, real_request: false)
|
|
@@ -9,6 +9,7 @@ rescue LoadError
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
if defined?(::HTTPClient)
|
|
12
|
+
require 'delegate'
|
|
12
13
|
|
|
13
14
|
module WebMock
|
|
14
15
|
module HttpLibAdapters
|
|
@@ -45,84 +46,107 @@ if defined?(::HTTPClient)
|
|
|
45
46
|
end
|
|
46
47
|
|
|
47
48
|
module WebMockHTTPClients
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
# HTTPClient publishes headers before it writes the body to the caller's
|
|
50
|
+
# pipe. Capture the chunks upstream so callbacks never read from that pipe.
|
|
51
|
+
class RecordingSession < SimpleDelegator
|
|
52
|
+
def initialize(session, body)
|
|
53
|
+
super(session)
|
|
54
|
+
@body = body
|
|
55
|
+
end
|
|
52
56
|
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
def get_body
|
|
58
|
+
__getobj__.get_body do |chunk|
|
|
59
|
+
@body << chunk.b
|
|
60
|
+
yield chunk
|
|
61
|
+
end
|
|
62
|
+
end
|
|
55
63
|
end
|
|
56
64
|
|
|
57
|
-
|
|
58
|
-
|
|
65
|
+
class RecordingSessionManager < Struct.new(:manager, :body)
|
|
66
|
+
def query(req, proxy)
|
|
67
|
+
RecordingSession.new(manager.query(req, proxy), body)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def keep(session)
|
|
71
|
+
manager.keep(session.__getobj__)
|
|
72
|
+
end
|
|
59
73
|
end
|
|
60
74
|
|
|
61
|
-
|
|
62
|
-
|
|
75
|
+
class RecordingConnection
|
|
76
|
+
attr_reader :response, :body
|
|
63
77
|
|
|
64
|
-
|
|
78
|
+
def initialize(connection)
|
|
79
|
+
@connection = connection
|
|
80
|
+
@body = ''.b
|
|
81
|
+
end
|
|
65
82
|
|
|
66
|
-
|
|
83
|
+
def push(response)
|
|
84
|
+
@response = response
|
|
85
|
+
# HTTPClient's loopback responses bypass the session entirely.
|
|
86
|
+
@body.replace(response.content.string) if response.content.is_a?(StringIO)
|
|
87
|
+
@connection.push(response)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
67
90
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
91
|
+
class ResponseFilter < Struct.new(:filter)
|
|
92
|
+
def filter_request(_req)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def filter_response(req, response)
|
|
96
|
+
filter.filter_response(req, response)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
private_constant :RecordingConnection, :RecordingSessionManager, :RecordingSession, :ResponseFilter
|
|
100
|
+
|
|
101
|
+
def do_get_block(req, proxy, conn, &block)
|
|
102
|
+
filters = apply_request_filters(req, proxy)
|
|
103
|
+
request_signature = build_request_signature(req)
|
|
104
|
+
WebMock::RequestRegistry.instance.put(request_signature)
|
|
105
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
106
|
+
if webmock_response
|
|
107
|
+
response = build_httpclient_response(webmock_response, false, req.header, &block)
|
|
108
|
+
filters.each { |filter| filter.filter_response(req, response) }
|
|
74
109
|
res = conn.push(response)
|
|
75
110
|
WebMock::CallbackRegistry.invoke_callbacks(
|
|
76
111
|
{lib: :httpclient}, request_signature, webmock_response)
|
|
77
112
|
res
|
|
78
113
|
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
|
79
|
-
|
|
80
|
-
webmock_responses.delete(request_signature)
|
|
81
|
-
|
|
82
|
-
res = if stream
|
|
83
|
-
do_get_stream_without_webmock(req, proxy, conn, &block)
|
|
84
|
-
elsif block
|
|
85
|
-
body = ''
|
|
86
|
-
do_get_block_without_webmock(req, proxy, conn) do |http_res, chunk|
|
|
87
|
-
if chunk && chunk.bytesize > 0
|
|
88
|
-
body += chunk
|
|
89
|
-
block.call(http_res, chunk)
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
else
|
|
93
|
-
do_get_block_without_webmock(req, proxy, conn)
|
|
94
|
-
end
|
|
95
|
-
res = conn.pop
|
|
96
|
-
conn.push(res)
|
|
97
|
-
if WebMock::CallbackRegistry.any_callbacks?
|
|
98
|
-
webmock_response = build_webmock_response(res, body)
|
|
99
|
-
WebMock::CallbackRegistry.invoke_callbacks(
|
|
100
|
-
{lib: :httpclient, real_request: true}, request_signature,
|
|
101
|
-
webmock_response)
|
|
102
|
-
end
|
|
103
|
-
res
|
|
114
|
+
clone(freeze: false).send_prepared_sync_request(req, proxy, conn, filters, request_signature, &block)
|
|
104
115
|
else
|
|
105
116
|
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
106
117
|
end
|
|
107
118
|
end
|
|
108
119
|
|
|
109
120
|
def do_request_async(method, uri, query, body, extheader)
|
|
110
|
-
clear_thread_variables
|
|
111
121
|
req = create_request(method, uri, query, body, extheader)
|
|
122
|
+
proxy = no_proxy?(uri) ? nil : @proxy
|
|
123
|
+
filters = apply_request_filters(req, proxy)
|
|
112
124
|
request_signature = build_request_signature(req)
|
|
113
|
-
|
|
125
|
+
WebMock::RequestRegistry.instance.put(request_signature)
|
|
114
126
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
async_conn_for_stubbed_request(req, request_signature, webmock_response)
|
|
118
|
-
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
|
119
|
-
conn = super
|
|
120
|
-
conn.async_thread[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES]
|
|
121
|
-
conn.async_thread[WEBMOCK_HTTPCLIENT_RESPONSES] = Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES]
|
|
122
|
-
conn
|
|
123
|
-
else
|
|
127
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
128
|
+
unless webmock_response || WebMock.net_connect_allowed?(request_signature.uri)
|
|
124
129
|
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
125
130
|
end
|
|
131
|
+
|
|
132
|
+
# Keep the prepared request, including any headers its filters assigned.
|
|
133
|
+
# HTTPClient's do_request_async would create a second request in the worker.
|
|
134
|
+
conn = ::HTTPClient::Connection.new
|
|
135
|
+
conn.async_thread = Thread.new do
|
|
136
|
+
begin
|
|
137
|
+
if webmock_response
|
|
138
|
+
response = build_httpclient_response(webmock_response, true, req.header)
|
|
139
|
+
filters.each { |filter| filter.filter_response(req, response) }
|
|
140
|
+
conn.push(response)
|
|
141
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :httpclient}, request_signature, webmock_response)
|
|
142
|
+
else
|
|
143
|
+
clone(freeze: false).send_prepared_async_request(req, proxy, conn, filters, request_signature)
|
|
144
|
+
end
|
|
145
|
+
rescue Exception => e
|
|
146
|
+
conn.push e
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
conn
|
|
126
150
|
end
|
|
127
151
|
|
|
128
152
|
def build_httpclient_response(webmock_response, stream = false, req_header = nil, &block)
|
|
@@ -167,11 +191,7 @@ if defined?(::HTTPClient)
|
|
|
167
191
|
webmock_response
|
|
168
192
|
end
|
|
169
193
|
|
|
170
|
-
def build_request_signature(req
|
|
171
|
-
@request_filter.each do |filter|
|
|
172
|
-
filter.filter_request(req)
|
|
173
|
-
end
|
|
174
|
-
|
|
194
|
+
def build_request_signature(req)
|
|
175
195
|
uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
|
|
176
196
|
if req.header.request_query
|
|
177
197
|
# HTTPClient sends the query argument after the query in the URI
|
|
@@ -187,38 +207,65 @@ if defined?(::HTTPClient)
|
|
|
187
207
|
end
|
|
188
208
|
headers = headers_from_session(uri).merge(headers)
|
|
189
209
|
|
|
190
|
-
|
|
210
|
+
WebMock::RequestSignature.new(
|
|
191
211
|
req.header.request_method.downcase.to_sym,
|
|
192
212
|
uri.to_s,
|
|
193
213
|
body: req.http_body.dump,
|
|
194
214
|
headers: headers
|
|
195
215
|
)
|
|
196
|
-
|
|
197
|
-
# reuse a previous identical signature object if we stored one for later use
|
|
198
|
-
if reuse_existing && previous_signature = previous_signature_for(signature)
|
|
199
|
-
return previous_signature
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
signature
|
|
203
216
|
end
|
|
204
217
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
218
|
+
protected
|
|
219
|
+
|
|
220
|
+
# Only the per-request copy changes its filter list. It shares the client's
|
|
221
|
+
# sessions and lets HTTPClient run the response filters at the normal time.
|
|
222
|
+
def send_prepared_sync_request(req, proxy, conn, filters, request_signature, &block)
|
|
223
|
+
@request_filter = filters.map { |filter| ResponseFilter.new(filter) }
|
|
224
|
+
if block
|
|
225
|
+
body = ''
|
|
226
|
+
do_get_block_without_webmock(req, proxy, conn) do |http_res, chunk|
|
|
227
|
+
if chunk && chunk.bytesize > 0
|
|
228
|
+
body += chunk
|
|
229
|
+
block.call(http_res, chunk)
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
else
|
|
233
|
+
do_get_block_without_webmock(req, proxy, conn)
|
|
234
|
+
end
|
|
235
|
+
res = conn.pop
|
|
236
|
+
conn.push(res)
|
|
237
|
+
if WebMock::CallbackRegistry.any_callbacks?
|
|
238
|
+
webmock_response = build_webmock_response(res, body)
|
|
239
|
+
WebMock::CallbackRegistry.invoke_callbacks(
|
|
240
|
+
{lib: :httpclient, real_request: true}, request_signature,
|
|
241
|
+
webmock_response)
|
|
208
242
|
end
|
|
243
|
+
res
|
|
209
244
|
end
|
|
210
245
|
|
|
211
|
-
def
|
|
212
|
-
|
|
213
|
-
|
|
246
|
+
def send_prepared_async_request(req, proxy, conn, filters, request_signature)
|
|
247
|
+
@request_filter = filters.map { |filter| ResponseFilter.new(filter) }
|
|
248
|
+
if WebMock::CallbackRegistry.any_callbacks?
|
|
249
|
+
recorder = RecordingConnection.new(conn)
|
|
250
|
+
@session_manager = RecordingSessionManager.new(@session_manager, recorder.body)
|
|
251
|
+
end
|
|
214
252
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
253
|
+
protect_keep_alive_disconnected { do_get_stream(req, proxy, recorder || conn) }
|
|
254
|
+
if recorder
|
|
255
|
+
response = build_webmock_response(recorder.response, recorder.body)
|
|
256
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :httpclient, real_request: true}, request_signature, response)
|
|
257
|
+
end
|
|
218
258
|
end
|
|
219
259
|
|
|
220
260
|
private
|
|
221
261
|
|
|
262
|
+
def apply_request_filters(req, proxy)
|
|
263
|
+
# Recent HTTPClient versions select the applicable proxy filters here.
|
|
264
|
+
return filter_request(req, proxy) if respond_to?(:filter_request, true)
|
|
265
|
+
|
|
266
|
+
@request_filter.each { |filter| filter.filter_request(req) }
|
|
267
|
+
end
|
|
268
|
+
|
|
222
269
|
# some of the headers sent by HTTPClient are derived from
|
|
223
270
|
# the client session
|
|
224
271
|
def headers_from_session(uri)
|
|
@@ -229,40 +276,10 @@ if defined?(::HTTPClient)
|
|
|
229
276
|
hdrs
|
|
230
277
|
end
|
|
231
278
|
end
|
|
232
|
-
|
|
233
|
-
def clear_thread_variables
|
|
234
|
-
Thread.current[WEBMOCK_HTTPCLIENT_REQUEST_SIGNATURES] = nil
|
|
235
|
-
Thread.current[WEBMOCK_HTTPCLIENT_RESPONSES] = nil
|
|
236
|
-
end
|
|
237
|
-
|
|
238
|
-
# The async thread must not go through do_get, which would rebuild the request signature.
|
|
239
|
-
# Rebuilding calls headers_from_session again, and the session sets a Date header with
|
|
240
|
-
# 1 second resolution, so a signature built later can differ from the one the response was
|
|
241
|
-
# cached under, which makes the thread evaluate the stub registry again and consume
|
|
242
|
-
# an extra response from the stub sequence. The thread only delivers what the caller
|
|
243
|
-
# already evaluated, mirroring do_get's stubbed branch.
|
|
244
|
-
def async_conn_for_stubbed_request(req, request_signature, webmock_response)
|
|
245
|
-
conn = HTTPClient::Connection.new
|
|
246
|
-
conn.async_thread = Thread.new do
|
|
247
|
-
begin
|
|
248
|
-
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
|
|
249
|
-
response = build_httpclient_response(webmock_response, true, req.header)
|
|
250
|
-
@request_filter.each do |filter|
|
|
251
|
-
filter.filter_response(req, response)
|
|
252
|
-
end
|
|
253
|
-
conn.push(response)
|
|
254
|
-
WebMock::CallbackRegistry.invoke_callbacks({lib: :httpclient}, request_signature, webmock_response)
|
|
255
|
-
rescue Exception => e
|
|
256
|
-
conn.push e
|
|
257
|
-
end
|
|
258
|
-
end
|
|
259
|
-
conn
|
|
260
|
-
end
|
|
261
279
|
end
|
|
262
280
|
|
|
263
281
|
class WebMockHTTPClient < HTTPClient
|
|
264
282
|
alias_method :do_get_block_without_webmock, :do_get_block
|
|
265
|
-
alias_method :do_get_stream_without_webmock, :do_get_stream
|
|
266
283
|
|
|
267
284
|
include WebMockHTTPClients
|
|
268
285
|
end
|
|
@@ -270,7 +287,6 @@ if defined?(::HTTPClient)
|
|
|
270
287
|
if defined? ::JSONClient
|
|
271
288
|
class WebMockJSONClient < JSONClient
|
|
272
289
|
alias_method :do_get_block_without_webmock, :do_get_block
|
|
273
|
-
alias_method :do_get_stream_without_webmock, :do_get_stream
|
|
274
290
|
|
|
275
291
|
include WebMockHTTPClients
|
|
276
292
|
end
|
|
@@ -54,7 +54,7 @@ if defined?(Manticore)
|
|
|
54
54
|
|
|
55
55
|
def response_object_for(request, context, &block)
|
|
56
56
|
request_signature = generate_webmock_request_signature(request, context)
|
|
57
|
-
WebMock::RequestRegistry.instance.
|
|
57
|
+
WebMock::RequestRegistry.instance.put(request_signature)
|
|
58
58
|
|
|
59
59
|
if webmock_response = registered_response_for(request_signature)
|
|
60
60
|
webmock_response.raise_error_if_any
|
|
@@ -100,7 +100,7 @@ module WebMock
|
|
|
100
100
|
|
|
101
101
|
request_signature = WebMock::NetHTTPUtility.request_signature_from_request(self, request, body)
|
|
102
102
|
|
|
103
|
-
WebMock::RequestRegistry.instance.
|
|
103
|
+
WebMock::RequestRegistry.instance.put(request_signature)
|
|
104
104
|
|
|
105
105
|
if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
106
106
|
# Close the real connection left by an earlier request, if any.
|
|
@@ -43,11 +43,11 @@ if defined?(::Patron::Session)
|
|
|
43
43
|
return super unless block
|
|
44
44
|
|
|
45
45
|
req = build_request(action, url, headers, **options)
|
|
46
|
-
request_signature, webmock_response = PatronAdapter.register_request(req)
|
|
46
|
+
request_signature, webmock_response, recorded_body = PatronAdapter.register_request(req)
|
|
47
47
|
|
|
48
48
|
if webmock_response
|
|
49
49
|
PatronAdapter.stream_stubbed_response(webmock_response, request_signature,
|
|
50
|
-
default_response_charset, &block)
|
|
50
|
+
default_response_charset, recorded_body.to_s.bytesize, &block)
|
|
51
51
|
else
|
|
52
52
|
# Callbacks run once the head has arrived, so they also run
|
|
53
53
|
# when the block leaves before the end of the body
|
|
@@ -181,13 +181,28 @@ if defined?(::Patron::Session)
|
|
|
181
181
|
uri.path = uri.normalized_path.gsub("[^:]//","/")
|
|
182
182
|
|
|
183
183
|
if [:put, :post, :patch].include?(req.action)
|
|
184
|
-
|
|
185
|
-
|
|
184
|
+
data = req.upload_data
|
|
185
|
+
path = req.file_name || (data.to_path if data.respond_to?(:to_path))
|
|
186
|
+
if path
|
|
187
|
+
if !File.exist?(path) || !File.readable?(path)
|
|
186
188
|
raise ArgumentError.new("Unable to open specified file.")
|
|
187
189
|
end
|
|
188
|
-
request_body = File.
|
|
189
|
-
|
|
190
|
-
|
|
190
|
+
request_body = if !File.file?(path)
|
|
191
|
+
# Leave a named pipe or device for Patron to open.
|
|
192
|
+
path.to_s
|
|
193
|
+
elsif req.file_name
|
|
194
|
+
# Keep the encoding used by existing post_file and put_file stubs.
|
|
195
|
+
File.read(path)
|
|
196
|
+
else
|
|
197
|
+
File.binread(path)
|
|
198
|
+
end
|
|
199
|
+
elsif data.respond_to?(:read) && OriginalPatronSession.public_method_defined?(:stream)
|
|
200
|
+
# Patron 1.1 added streaming uploads and Session#stream together.
|
|
201
|
+
request_body = WebMock::Util::UploadBody.read(data, req.headers)
|
|
202
|
+
elsif data
|
|
203
|
+
# A multipart post keeps its form fields as a Hash. The copy
|
|
204
|
+
# lets stubs and callbacks still receive the caller's Hash.
|
|
205
|
+
request_body = data.is_a?(Hash) ? data.dup : data.to_s
|
|
191
206
|
else
|
|
192
207
|
raise ArgumentError.new("Must provide either data or a filename when doing a PUT or POST")
|
|
193
208
|
end
|
|
@@ -240,15 +255,19 @@ if defined?(::Patron::Session)
|
|
|
240
255
|
# Records the request and returns its signature with the stubbed
|
|
241
256
|
# response, which is nil when a real request is allowed
|
|
242
257
|
def self.register_request(req)
|
|
243
|
-
|
|
244
|
-
|
|
258
|
+
recorded = build_request_signature(req)
|
|
259
|
+
request_signature = recorded
|
|
260
|
+
if recorded.body && !req.file_name && req.upload_data && !req.upload_data.equal?(recorded.body)
|
|
261
|
+
request_signature = recorded.with_upload_body(req.upload_data)
|
|
262
|
+
end
|
|
245
263
|
|
|
246
|
-
webmock_response = WebMock::StubRegistry.instance.response_for_request(
|
|
264
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(
|
|
265
|
+
request_signature, record: recorded)
|
|
247
266
|
if webmock_response.nil? && !WebMock.net_connect_allowed?(request_signature.uri)
|
|
248
267
|
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
249
268
|
end
|
|
250
269
|
|
|
251
|
-
[request_signature, webmock_response]
|
|
270
|
+
[request_signature, webmock_response, recorded.body]
|
|
252
271
|
end
|
|
253
272
|
|
|
254
273
|
def self.invoke_callbacks(request_signature, webmock_response)
|
|
@@ -266,7 +285,7 @@ if defined?(::Patron::Session)
|
|
|
266
285
|
|
|
267
286
|
# Callbacks run before the block, so they also run when it leaves
|
|
268
287
|
# before the end of the body
|
|
269
|
-
def self.stream_stubbed_response(webmock_response, request_signature, default_response_charset)
|
|
288
|
+
def self.stream_stubbed_response(webmock_response, request_signature, default_response_charset, uploaded_bytes)
|
|
270
289
|
head = build_patron_response(webmock_response, default_response_charset, with_body: false)
|
|
271
290
|
invoke_callbacks(request_signature, webmock_response)
|
|
272
291
|
# Kept out of the stream, so the stream has only the methods of
|
|
@@ -285,7 +304,7 @@ if defined?(::Patron::Session)
|
|
|
285
304
|
redirect_count: head.redirect_count,
|
|
286
305
|
header_data: build_header_data(webmock_response),
|
|
287
306
|
downloaded_bytes: body.pos,
|
|
288
|
-
uploaded_bytes:
|
|
307
|
+
uploaded_bytes: uploaded_bytes,
|
|
289
308
|
timings: {},
|
|
290
309
|
complete: body.eof?,
|
|
291
310
|
default_charset: default_response_charset
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
begin
|
|
4
4
|
require 'typhoeus'
|
|
5
|
+
require 'pathname'
|
|
5
6
|
rescue LoadError
|
|
6
7
|
# typhoeus not found
|
|
7
8
|
end
|
|
@@ -80,6 +81,18 @@ if defined?(Typhoeus)
|
|
|
80
81
|
request_signature
|
|
81
82
|
end
|
|
82
83
|
|
|
84
|
+
def self.signature_for_history(signature)
|
|
85
|
+
return signature unless signature.body.is_a?(Array)
|
|
86
|
+
|
|
87
|
+
# Ethon uploads file parts by path and stringifies non-scalar form values.
|
|
88
|
+
signature.with_body_values do |value|
|
|
89
|
+
case value
|
|
90
|
+
when File, Tempfile then Pathname.new(value.path)
|
|
91
|
+
when String, Numeric, Symbol, TrueClass, FalseClass, NilClass then value
|
|
92
|
+
else value.to_s
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
83
96
|
|
|
84
97
|
def self.build_webmock_response(typhoeus_response)
|
|
85
98
|
webmock_response = WebMock::Response.new
|
|
@@ -192,9 +205,9 @@ if defined?(Typhoeus)
|
|
|
192
205
|
request_signature = ::WebMock::HttpLibAdapters::TyphoeusAdapter.build_request_signature(request, url)
|
|
193
206
|
request.block_connection = false;
|
|
194
207
|
|
|
195
|
-
::WebMock::
|
|
208
|
+
recorded = ::WebMock::HttpLibAdapters::TyphoeusAdapter.signature_for_history(request_signature)
|
|
196
209
|
|
|
197
|
-
if webmock_response = ::WebMock::StubRegistry.instance.response_for_request(request_signature)
|
|
210
|
+
if webmock_response = ::WebMock::StubRegistry.instance.response_for_request(request_signature, record: recorded)
|
|
198
211
|
response = ::WebMock::HttpLibAdapters::TyphoeusAdapter.generate_typhoeus_response(webmock_response, url)
|
|
199
212
|
if request.respond_to?(:on_headers)
|
|
200
213
|
request.execute_headers_callbacks(response)
|
|
@@ -20,6 +20,10 @@ module WebMock
|
|
|
20
20
|
|
|
21
21
|
attr_reader :method_pattern, :uri_pattern, :body_pattern, :headers_pattern, :with_block
|
|
22
22
|
|
|
23
|
+
# History uses an opaque key so it needn't retain a matcher block or
|
|
24
|
+
# an upload captured by that block. Changing the pattern replaces the key.
|
|
25
|
+
attr_reader :match_token
|
|
26
|
+
|
|
23
27
|
def initialize(method, uri, options = {})
|
|
24
28
|
@method_pattern = MethodPattern.new(method)
|
|
25
29
|
@uri_pattern = create_uri_pattern(uri)
|
|
@@ -27,21 +31,26 @@ module WebMock
|
|
|
27
31
|
@headers_pattern = nil
|
|
28
32
|
@with_block = nil
|
|
29
33
|
assign_options(options)
|
|
34
|
+
@match_token = Object.new if @body_pattern
|
|
30
35
|
end
|
|
31
36
|
|
|
32
37
|
def with(options = {}, &block)
|
|
33
38
|
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?
|
|
34
39
|
assign_options(options)
|
|
35
40
|
@with_block = block
|
|
41
|
+
@match_token = (@body_pattern || @with_block) && Object.new
|
|
36
42
|
self
|
|
37
43
|
end
|
|
38
44
|
|
|
39
45
|
def matches?(request_signature)
|
|
40
|
-
@method_pattern.matches?(request_signature.method) &&
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
return false unless @method_pattern.matches?(request_signature.method) && @uri_pattern.matches?(request_signature.uri)
|
|
47
|
+
|
|
48
|
+
token = @match_token
|
|
49
|
+
matched = (@body_pattern.nil? || body_matches?(request_signature)) &&
|
|
43
50
|
(@headers_pattern.nil? || @headers_pattern.matches?(request_signature.headers)) &&
|
|
44
51
|
(@with_block.nil? || @with_block.call(request_signature))
|
|
52
|
+
yield(token, matched) if token && block_given?
|
|
53
|
+
matched
|
|
45
54
|
end
|
|
46
55
|
|
|
47
56
|
def to_s
|
|
@@ -55,6 +64,10 @@ module WebMock
|
|
|
55
64
|
|
|
56
65
|
private
|
|
57
66
|
|
|
67
|
+
def body_matches?(request)
|
|
68
|
+
serialized_body = request.serialized_body if request.respond_to?(:serialized_body)
|
|
69
|
+
@body_pattern.matches?(request.body, request.content_type, serialized_body)
|
|
70
|
+
end
|
|
58
71
|
|
|
59
72
|
def assign_options(options)
|
|
60
73
|
options = WebMock::Util::HashKeysStringifier.stringify_keys!(options, deep: true)
|
|
@@ -295,11 +308,18 @@ module WebMock
|
|
|
295
308
|
end
|
|
296
309
|
end
|
|
297
310
|
|
|
298
|
-
def matches?(body, content_type = "")
|
|
311
|
+
def matches?(body, content_type = "", serialized_body = nil)
|
|
299
312
|
# Checked on the raw value, so body patterns used with mixed case
|
|
300
313
|
# multipart content types keep working as in earlier versions.
|
|
301
314
|
assert_non_multipart_body(content_type)
|
|
302
315
|
|
|
316
|
+
if serialized_body
|
|
317
|
+
case @pattern
|
|
318
|
+
when String, Regexp, Hash, Array, WebMock::Matchers::HashArgumentMatcher, nil
|
|
319
|
+
body = serialized_body
|
|
320
|
+
end
|
|
321
|
+
end
|
|
322
|
+
|
|
303
323
|
if (@pattern).is_a?(Hash)
|
|
304
324
|
return true if @pattern.empty?
|
|
305
325
|
matching_body_hashes?(body_as_hash(body, content_type), @pattern, content_type)
|
|
@@ -5,20 +5,81 @@ module WebMock
|
|
|
5
5
|
class RequestRegistry
|
|
6
6
|
include Singleton
|
|
7
7
|
|
|
8
|
+
# Used by #atomically, allocated once rather than on every request.
|
|
9
|
+
DEFER_INTERRUPTS = { Object => :on_blocking }.freeze
|
|
10
|
+
private_constant :DEFER_INTERRUPTS
|
|
11
|
+
|
|
8
12
|
attr_accessor :requested_signatures
|
|
9
13
|
|
|
10
14
|
def initialize
|
|
15
|
+
@lock = ::Mutex.new
|
|
11
16
|
reset!
|
|
12
17
|
end
|
|
13
18
|
|
|
19
|
+
# The counter and the list change together, under one lock, so they
|
|
20
|
+
# always agree.
|
|
14
21
|
def reset!
|
|
15
|
-
|
|
22
|
+
atomically do
|
|
23
|
+
@executed_requests = []
|
|
24
|
+
@live_matches = {}
|
|
25
|
+
self.requested_signatures = Util::HashCounter.new
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Records a request made through an HTTP client adapter. The list holds
|
|
30
|
+
# the copy the counter keeps of each distinct request, so a caller reusing
|
|
31
|
+
# its body buffer cannot change it. A repeated request adds only a
|
|
32
|
+
# reference to that copy, so a suite that never resets keeps one copy of
|
|
33
|
+
# each distinct request.
|
|
34
|
+
def put(request_signature, track_live_matches: false)
|
|
35
|
+
stored = nil
|
|
36
|
+
live_matches = atomically do
|
|
37
|
+
stored = requested_signatures.put(request_signature)
|
|
38
|
+
@executed_requests << stored
|
|
39
|
+
@live_matches
|
|
40
|
+
end
|
|
41
|
+
return unless track_live_matches
|
|
42
|
+
|
|
43
|
+
# Matching happens outside the lock. Capture this generation so a
|
|
44
|
+
# concurrent reset cannot attach an old match to a later request.
|
|
45
|
+
lambda do |token, matched|
|
|
46
|
+
@lock.synchronize do
|
|
47
|
+
counts = live_matches[token] ||= {}
|
|
48
|
+
checked, accepted = counts.fetch(stored, [0, 0])
|
|
49
|
+
accepted += 1 if matched
|
|
50
|
+
counts[stored] = [checked + 1, accepted]
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Every request made since the last reset, in order, repeats included,
|
|
56
|
+
# as copies, so a test cannot change WebMock's records.
|
|
57
|
+
def executed_requests
|
|
58
|
+
requests = @lock.synchronize { @executed_requests.dup }
|
|
59
|
+
copies = {}.compare_by_identity
|
|
60
|
+
requests.map { |request| copies[request] ||= request.dup }.freeze
|
|
16
61
|
end
|
|
17
62
|
|
|
18
63
|
def times_executed(request_pattern)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
64
|
+
pattern, conditions = request_pattern.respond_to?(:patterns) ? request_pattern.patterns : [request_pattern]
|
|
65
|
+
base = pattern.is_a?(RequestStub) ? pattern.request_pattern : pattern
|
|
66
|
+
token = base.match_token if base.is_a?(RequestPattern)
|
|
67
|
+
counts, observations = @lock.synchronize { [requested_signatures.snapshot, @live_matches[token]&.dup] }
|
|
68
|
+
|
|
69
|
+
counts.sum do |signature, total|
|
|
70
|
+
observed = observations && observations[signature]
|
|
71
|
+
if observed
|
|
72
|
+
# The same snapshot can represent both a stream and a string.
|
|
73
|
+
# Only recheck the requests for which the stub wasn't evaluated.
|
|
74
|
+
checked, accepted = observed
|
|
75
|
+
remaining = total - checked
|
|
76
|
+
accepted += remaining if remaining > 0 && pattern.matches?(signature)
|
|
77
|
+
next 0 if accepted > 0 && conditions && !conditions.matches?(signature)
|
|
78
|
+
accepted
|
|
79
|
+
else
|
|
80
|
+
request_pattern.matches?(signature) ? total : 0
|
|
81
|
+
end
|
|
82
|
+
end
|
|
22
83
|
end
|
|
23
84
|
|
|
24
85
|
def to_s
|
|
@@ -33,5 +94,14 @@ module WebMock
|
|
|
33
94
|
end
|
|
34
95
|
end
|
|
35
96
|
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
# An interrupt such as Timeout can still stop a thread waiting for the
|
|
100
|
+
# lock, but not one between the two updates, where nothing blocks.
|
|
101
|
+
def atomically(&block)
|
|
102
|
+
Thread.handle_interrupt(DEFER_INTERRUPTS) do
|
|
103
|
+
@lock.synchronize(&block)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
36
106
|
end
|
|
37
107
|
end
|
|
@@ -5,11 +5,12 @@ module WebMock
|
|
|
5
5
|
class RequestSignature
|
|
6
6
|
|
|
7
7
|
attr_accessor :uri, :body
|
|
8
|
-
attr_reader :method, :headers
|
|
8
|
+
attr_reader :method, :headers, :serialized_body
|
|
9
9
|
|
|
10
10
|
def initialize(method, uri, options = {})
|
|
11
11
|
self.method = method
|
|
12
12
|
self.uri = uri.is_a?(Addressable::URI) ? uri : WebMock::Util::URI.normalize_uri(uri)
|
|
13
|
+
@serialized_body = options[:serialized_body]
|
|
13
14
|
assign_options(options)
|
|
14
15
|
end
|
|
15
16
|
|
|
@@ -23,6 +24,27 @@ module WebMock
|
|
|
23
24
|
string
|
|
24
25
|
end
|
|
25
26
|
|
|
27
|
+
# Build a signature for history while preserving the original client
|
|
28
|
+
# objects for stub matchers and callbacks. Keep nested form structure.
|
|
29
|
+
def with_body_values(&block)
|
|
30
|
+
self.class.new(method, uri, body: map_body_values(body, &block), headers: headers)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Keep the native upload available to custom matchers and callbacks while
|
|
34
|
+
# allowing string and structured body patterns to match its contents.
|
|
35
|
+
def with_upload_body(upload)
|
|
36
|
+
self.class.new(method, uri, body: upload, headers: headers, serialized_body: body)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A copy that shares no mutable state with the original.
|
|
40
|
+
def initialize_copy(original)
|
|
41
|
+
super
|
|
42
|
+
@uri = @uri.dup
|
|
43
|
+
@body = map_body_values(@body, &:dup)
|
|
44
|
+
@serialized_body = @serialized_body&.dup
|
|
45
|
+
@headers = @headers&.transform_values { |value| value.is_a?(Array) ? value.map(&:dup) : value.dup }
|
|
46
|
+
end
|
|
47
|
+
|
|
26
48
|
def method=(method)
|
|
27
49
|
@method = method.to_sym.downcase
|
|
28
50
|
end
|
|
@@ -32,11 +54,13 @@ module WebMock
|
|
|
32
54
|
end
|
|
33
55
|
|
|
34
56
|
def hash
|
|
35
|
-
|
|
57
|
+
[method, uri, body, headers].hash
|
|
36
58
|
end
|
|
37
59
|
|
|
38
60
|
def eql?(other)
|
|
39
|
-
|
|
61
|
+
other.is_a?(RequestSignature) &&
|
|
62
|
+
method == other.method && uri.eql?(other.uri) &&
|
|
63
|
+
body.eql?(other.body) && headers.eql?(other.headers)
|
|
40
64
|
end
|
|
41
65
|
alias == eql?
|
|
42
66
|
|
|
@@ -56,6 +80,19 @@ module WebMock
|
|
|
56
80
|
|
|
57
81
|
private
|
|
58
82
|
|
|
83
|
+
def map_body_values(body, &block)
|
|
84
|
+
case body
|
|
85
|
+
when Array
|
|
86
|
+
body.map { |item| map_body_values(item, &block) }
|
|
87
|
+
when Hash
|
|
88
|
+
body.each_with_object({}) do |(key, value), copy|
|
|
89
|
+
copy[map_body_values(key, &block)] = map_body_values(value, &block)
|
|
90
|
+
end
|
|
91
|
+
else
|
|
92
|
+
yield body
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
59
96
|
def assign_options(options)
|
|
60
97
|
self.body = options[:body] if options.has_key?(:body)
|
|
61
98
|
self.headers = options[:headers] if options.has_key?(:headers)
|
|
@@ -62,18 +62,19 @@ module WebMock
|
|
|
62
62
|
request_stub_for(request_signature)
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
def response_for_request(request_signature)
|
|
66
|
-
|
|
65
|
+
def response_for_request(request_signature, record: nil)
|
|
66
|
+
if record
|
|
67
|
+
remember = RequestRegistry.instance.put(record, track_live_matches: !record.equal?(request_signature))
|
|
68
|
+
end
|
|
69
|
+
stub = request_stub_for(request_signature, &remember)
|
|
67
70
|
stub ? evaluate_response_for_request(stub.response, request_signature) : nil
|
|
68
71
|
end
|
|
69
72
|
|
|
70
73
|
private
|
|
71
74
|
|
|
72
|
-
def request_stub_for(request_signature)
|
|
75
|
+
def request_stub_for(request_signature, &remember)
|
|
73
76
|
(global_stubs[:before_local_stubs] + request_stubs + global_stubs[:after_local_stubs])
|
|
74
|
-
.detect { |
|
|
75
|
-
registered_request_stub.request_pattern.matches?(request_signature)
|
|
76
|
-
}
|
|
77
|
+
.detect { |stub| stub.request_pattern.matches?(request_signature, &remember) }
|
|
77
78
|
end
|
|
78
79
|
|
|
79
80
|
def evaluate_response_for_request(response, request_signature)
|
|
@@ -5,19 +5,32 @@ require 'thread'
|
|
|
5
5
|
module WebMock
|
|
6
6
|
module Util
|
|
7
7
|
class HashCounter
|
|
8
|
+
# The key the counter holds, its count, and when it was last put.
|
|
9
|
+
Entry = Struct.new(:key, :count, :position)
|
|
10
|
+
|
|
8
11
|
attr_accessor :hash
|
|
9
12
|
|
|
10
13
|
def initialize
|
|
11
14
|
self.hash = Hash.new(0)
|
|
12
|
-
@
|
|
15
|
+
@entries = {}
|
|
13
16
|
@max = 0
|
|
14
17
|
@lock = ::Mutex.new
|
|
15
18
|
end
|
|
16
19
|
|
|
20
|
+
# Returns the key the counter holds: a copy of the first one put for an
|
|
21
|
+
# equal key. The copy is also what the counter looks keys up by, so a
|
|
22
|
+
# caller changing its key later cannot change the counts.
|
|
17
23
|
def put(key, num=1)
|
|
18
24
|
@lock.synchronize do
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
entry = @entries[key]
|
|
26
|
+
unless entry
|
|
27
|
+
copy = key.dup
|
|
28
|
+
entry = @entries[copy] = Entry.new(copy, 0, 0)
|
|
29
|
+
end
|
|
30
|
+
entry.count += num
|
|
31
|
+
entry.position = @max += 1
|
|
32
|
+
hash[entry.key] = entry.count
|
|
33
|
+
entry.key
|
|
21
34
|
end
|
|
22
35
|
end
|
|
23
36
|
|
|
@@ -27,18 +40,24 @@ module WebMock
|
|
|
27
40
|
end
|
|
28
41
|
end
|
|
29
42
|
|
|
43
|
+
# The block runs on a snapshot, outside the lock, so it can make
|
|
44
|
+
# requests or call WebMock without deadlocking.
|
|
30
45
|
def select(&block)
|
|
31
46
|
return unless block_given?
|
|
32
47
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
48
|
+
snapshot.select(&block)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def snapshot
|
|
52
|
+
@lock.synchronize { hash.dup }
|
|
36
53
|
end
|
|
37
54
|
|
|
55
|
+
# Like select, it yields from a snapshot, outside the lock.
|
|
38
56
|
def each(&block)
|
|
39
|
-
|
|
40
|
-
|
|
57
|
+
entries = @lock.synchronize do
|
|
58
|
+
@entries.values.sort_by(&:position).map { |entry| [entry.key, entry.count] }
|
|
41
59
|
end
|
|
60
|
+
entries.each { |key, count| yield(key, count) }
|
|
42
61
|
end
|
|
43
62
|
end
|
|
44
63
|
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'stringio'
|
|
4
|
+
|
|
5
|
+
module WebMock
|
|
6
|
+
module Util
|
|
7
|
+
module UploadBody
|
|
8
|
+
def self.read(stream, headers)
|
|
9
|
+
# Only inspect readers whose buffering and pushback semantics we know.
|
|
10
|
+
# A wrapper can delegate ungetbyte to a file while transforming read.
|
|
11
|
+
read_owner = stream.method(:read).owner
|
|
12
|
+
if defined?(Tempfile) && stream.is_a?(Tempfile) && read_owner == Tempfile.instance_method(:read).owner
|
|
13
|
+
stream = stream.to_io
|
|
14
|
+
read_owner = stream.method(:read).owner
|
|
15
|
+
end
|
|
16
|
+
gzip = defined?(Zlib::GzipReader) && read_owner == Zlib::GzipReader
|
|
17
|
+
source = gzip ? stream.to_io : stream
|
|
18
|
+
return stream.to_s unless (read_owner == IO || read_owner == StringIO || gzip) &&
|
|
19
|
+
(source.is_a?(StringIO) || (source.is_a?(IO) && source.stat.file?))
|
|
20
|
+
|
|
21
|
+
_, length = headers.find { |name, _| name.to_s.casecmp?('Content-Length') }
|
|
22
|
+
return ''.b if length && length.to_i.zero?
|
|
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)
|
|
29
|
+
# Flush pending writes before checking the descriptor offset.
|
|
30
|
+
# sysseek rejects buffered input, which IO#pos would discard.
|
|
31
|
+
stream.read(0)
|
|
32
|
+
begin
|
|
33
|
+
position = stream.sysseek(0, IO::SEEK_CUR)
|
|
34
|
+
rescue IOError
|
|
35
|
+
pushback = :ungetbyte
|
|
36
|
+
end
|
|
37
|
+
else
|
|
38
|
+
position = stream.pos
|
|
39
|
+
end
|
|
40
|
+
body = ''.b
|
|
41
|
+
each_chunk(stream, length&.to_i) { |chunk| body << chunk.b }
|
|
42
|
+
body
|
|
43
|
+
ensure
|
|
44
|
+
if pushback && body && !body.empty?
|
|
45
|
+
stream.public_send(pushback, body)
|
|
46
|
+
elsif position
|
|
47
|
+
stream.seek(position)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.each_chunk(stream, length)
|
|
52
|
+
while length.nil? || length > 0
|
|
53
|
+
size = length ? [length, 64 * 1024].min : 64 * 1024
|
|
54
|
+
chunk = stream.read(size)
|
|
55
|
+
break if chunk.nil? || chunk.empty?
|
|
56
|
+
yield chunk
|
|
57
|
+
length -= chunk.bytesize if length
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
private_class_method :each_chunk
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/webmock/version.rb
CHANGED
data/lib/webmock.rb
CHANGED
|
@@ -15,6 +15,7 @@ require_relative 'webmock/util/uri'
|
|
|
15
15
|
require_relative 'webmock/util/headers'
|
|
16
16
|
require_relative 'webmock/util/content_type'
|
|
17
17
|
require_relative 'webmock/util/hash_counter'
|
|
18
|
+
require_relative 'webmock/util/upload_body'
|
|
18
19
|
require_relative 'webmock/util/hash_keys_stringifier'
|
|
19
20
|
require_relative 'webmock/util/values_stringifier'
|
|
20
21
|
require_relative 'webmock/util/parsers/json'
|
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.37.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bartosz Blimke
|
|
@@ -350,6 +350,7 @@ files:
|
|
|
350
350
|
- lib/webmock/util/parsers/parse_error.rb
|
|
351
351
|
- lib/webmock/util/parsers/xml.rb
|
|
352
352
|
- lib/webmock/util/query_mapper.rb
|
|
353
|
+
- lib/webmock/util/upload_body.rb
|
|
353
354
|
- lib/webmock/util/uri.rb
|
|
354
355
|
- lib/webmock/util/values_stringifier.rb
|
|
355
356
|
- lib/webmock/util/version_checker.rb
|
|
@@ -360,9 +361,9 @@ licenses:
|
|
|
360
361
|
- MIT
|
|
361
362
|
metadata:
|
|
362
363
|
bug_tracker_uri: https://github.com/bblimke/webmock/issues
|
|
363
|
-
changelog_uri: https://github.com/bblimke/webmock/blob/v3.
|
|
364
|
-
documentation_uri: https://www.rubydoc.info/gems/webmock/3.
|
|
365
|
-
source_code_uri: https://github.com/bblimke/webmock/tree/v3.
|
|
364
|
+
changelog_uri: https://github.com/bblimke/webmock/blob/v3.37.0/CHANGELOG.md
|
|
365
|
+
documentation_uri: https://www.rubydoc.info/gems/webmock/3.37.0
|
|
366
|
+
source_code_uri: https://github.com/bblimke/webmock/tree/v3.37.0
|
|
366
367
|
wiki_uri: https://github.com/bblimke/webmock/wiki
|
|
367
368
|
rdoc_options: []
|
|
368
369
|
require_paths:
|