webmock 3.38.0 → 3.40.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 +110 -0
- data/README.md +24 -1
- data/lib/webmock/errors.rb +1 -1
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +17 -0
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +234 -30
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +4 -0
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +22 -1
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +32 -3
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +4 -2
- data/lib/webmock/http_lib_adapters/net_http.rb +56 -1
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +18 -1
- data/lib/webmock/request_body_diff.rb +11 -1
- data/lib/webmock/request_execution_verifier.rb +13 -2
- data/lib/webmock/request_pattern.rb +36 -0
- data/lib/webmock/request_registry.rb +1 -1
- data/lib/webmock/request_signature.rb +4 -2
- data/lib/webmock/request_signature_snippet.rb +23 -2
- data/lib/webmock/response.rb +42 -3
- data/lib/webmock/stub_request_snippet.rb +9 -3
- data/lib/webmock/trailing_slash_hint.rb +55 -0
- data/lib/webmock/util/headers.rb +17 -0
- data/lib/webmock/util/message_string.rb +28 -0
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +8 -0
- data/lib/webmock.rb +2 -0
- metadata +6 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: aa061376d2227028da9e7ca34091d2ec9b5afb3208f740fe5c7d981a903427d3
|
|
4
|
+
data.tar.gz: 8699e49411631af8a7d21312b23fdc98799cc0066be20c1df1ad7e8286a626b3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f8690d37653f8c4719c827dc5a761c30f00d8419240eef5bbad8284dca046bccae7978b4cc9b4094d8122bf5c0d4169be75c9a4c35442516fdd5ff99974d4e61
|
|
7
|
+
data.tar.gz: '0583f069649719f6eff6cb01addb0a640ecbe4014ce94e1319699cd8cdc977b1b8367f2a7127c2f109714388adc99c85c522ca63d8cbb99ad92398ff389e859a'
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,115 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
# 3.40.0
|
|
4
|
+
|
|
5
|
+
* Add support for `Curl::Multi` (#535)
|
|
6
|
+
|
|
7
|
+
Previously, WebMock didn't see requests sent with `Curl::Multi`. A handle added with `multi.add` and sent with `multi.perform` went to the real server, even when a stub matched it or real connections were disabled, and WebMock didn't record it.
|
|
8
|
+
|
|
9
|
+
Now a handle that matches a stub gets the stubbed response during `multi.perform`, and one that is neither stubbed nor allowed raises `WebMock::NetConnectNotAllowedError` from `multi.add`. Allowed requests still run together in curb's own multi loop. WebMock records both, and `WebMock.after_request` callbacks get both.
|
|
10
|
+
|
|
11
|
+
If your tests send `Curl::Multi` requests to real servers, stub them or allow their hosts, for example with `WebMock.disable_net_connect!(allow: "api.example.com")`. VCR, hooked into WebMock, now records and replays these requests too, and raises `VCR::Errors::UnhandledHTTPRequestError` for one that a cassette doesn't have, so record such cassettes again.
|
|
12
|
+
|
|
13
|
+
Thanks to [Todd Fisher](https://github.com/taf2)
|
|
14
|
+
|
|
15
|
+
* Drop curb callbacks on `Curl::Easy#reset` for stubbed requests, as curb does
|
|
16
|
+
|
|
17
|
+
Previously, a callback such as `on_complete`, set before `reset`, still ran for a stubbed request made after it. Curb drops every callback on `reset`, so a real request didn't run it. If your tests rely on such a callback, set it again after `reset`.
|
|
18
|
+
|
|
19
|
+
# 3.39.0
|
|
20
|
+
|
|
21
|
+
* Fix tracing libraries that wrap HTTPClient failing on stubbed requests (#1010)
|
|
22
|
+
|
|
23
|
+
Previously, for a stubbed request, WebMock's version of HTTPClient's private `do_get_block` method returned a `Thread::Queue` instead of the response that HTTPClient's own version returns. Libraries that wrap the method to trace requests, such as Datadog's, read that result as the response.
|
|
24
|
+
|
|
25
|
+
It now returns the response, as it already did for real requests.
|
|
26
|
+
|
|
27
|
+
* Match and record Net::HTTP URL encoded forms set with `set_form` (#959)
|
|
28
|
+
|
|
29
|
+
Previously, WebMock saw no body for a request whose form was set with `request.set_form([["foo", "bar"]])`, because Net::HTTP encodes such a form only when it sends the request. A stub such as `with(body: {"foo" => "bar"})` didn't match, and callbacks and VCR recorded the request without its body.
|
|
30
|
+
|
|
31
|
+
WebMock now records the body of a URL encoded form given as a hash or an array of pairs, as Net::HTTP encodes it, and leaves sending the request to Net::HTTP. A multipart form, or a form given as another enumerable, which may be read only once, is recorded without a body, as before. If VCR, hooked into WebMock, matches requests on their body, record cassettes that contain such requests again.
|
|
32
|
+
|
|
33
|
+
* Fix async-websocket connections failing with WebMock enabled (#953)
|
|
34
|
+
|
|
35
|
+
Previously, connecting with `Async::WebSocket::Client` raised `NoMethodError: undefined method 'acquire' for nil`, even to a host that allowed real connections. async-websocket takes a connection from the async-http client's pool, and WebMock's version of the client had no pool.
|
|
36
|
+
|
|
37
|
+
The client now hands out its real connection pool when WebMock's settings allow real connections to the host, and raises `WebMock::NetConnectNotAllowedError` otherwise. WebMock can't stub or record what a library sends over such a connection, and neither can VCR, so with VCR hooked into WebMock, allow the host with `WebMock.disable_net_connect!(allow: ...)`.
|
|
38
|
+
|
|
39
|
+
* Match the Cookie header that Typhoeus sends for the `cookie` option (#813)
|
|
40
|
+
|
|
41
|
+
Previously, `Typhoeus.get(url, cookie: "key=value")` sent `Cookie: key=value`, but WebMock recorded the request without it, so a stub with `with(headers: {"Cookie" => "key=value"})` didn't match.
|
|
42
|
+
|
|
43
|
+
WebMock now records the header, unless the request sets its own Cookie header, which is then the one sent, or uses libcurl's cookie engine with `cookiefile`, `cookiejar` or `cookielist`, which adds its own cookies to it. If VCR, hooked into WebMock, matches requests on their headers, record cassettes that contain such requests again. VCR now records the cookie too, so filter it, for example with `filter_sensitive_data`, if it is a secret.
|
|
44
|
+
|
|
45
|
+
* Fix VCR failing to replay Typhoeus requests when hooked into both Typhoeus and WebMock (#973)
|
|
46
|
+
|
|
47
|
+
Previously, with `hook_into :typhoeus, :webmock`, VCR warned that "There appears to be a bug in WebMock's after_request hook", and replaying a Typhoeus request from a cassette raised `ArgumentError`. VCR's Typhoeus hook answered the request before WebMock saw it, and WebMock still passed the request to `after_request` callbacks, as `nil`.
|
|
48
|
+
|
|
49
|
+
WebMock now leaves out requests it didn't see. On older versions, `hook_into :webmock` alone records and replays Typhoeus requests too.
|
|
50
|
+
|
|
51
|
+
* Run the instrumentation of the `http` gem 6 for stubbed requests (#881)
|
|
52
|
+
|
|
53
|
+
Previously, with version 6 of the `http` gem (HTTP.rb), a stubbed request skipped the instrumentation feature, so `HTTP.use(instrumentation: {instrumenter: ...})` emitted no `request.http` events for it. In every version, a stubbed timeout or `HTTP::Error` emitted no `error.http` event.
|
|
54
|
+
|
|
55
|
+
Stubbed requests now emit the same instrumentation events as real ones. Other features that HTTP.rb 6 runs around a request, such as caching and digest auth, still don't run for stubbed requests.
|
|
56
|
+
|
|
57
|
+
* Fix Net::HTTP uploads raising `ArgumentError` for body streams whose `read` needs a length (#644)
|
|
58
|
+
|
|
59
|
+
Previously, a request with `body_stream` set to an object with `def read(length, buffer = nil)` raised `ArgumentError: wrong number of arguments`, even with real connections allowed, because WebMock called `read` without arguments. Net::HTTP itself always passes a length, so the upload worked without WebMock.
|
|
60
|
+
|
|
61
|
+
WebMock now reads such a stream in chunks.
|
|
62
|
+
|
|
63
|
+
* Fix failure messages raising `Encoding::CompatibilityError` for bodies in different encodings (#668)
|
|
64
|
+
|
|
65
|
+
Previously, the message for an unstubbed request, or for a failed `assert_requested`, could raise `Encoding::CompatibilityError: incompatible character encodings: BINARY (ASCII-8BIT) and UTF-8` instead of showing the failure. This happened when the message listed a UTF-8 body with non ASCII characters, such as a stub `with(body: "café")`, next to a binary one, such as an uploaded image.
|
|
66
|
+
|
|
67
|
+
Such messages now show the failure, with bytes that aren't valid UTF-8 written as escapes, such as `\xFF`.
|
|
68
|
+
|
|
69
|
+
* Show URI templates, regexps and query options as Ruby in the list of registered stubs (#938)
|
|
70
|
+
|
|
71
|
+
Previously, the registered stubs listed for an unstubbed request put every URI in quotes, so a template stub was shown as `stub_request(:get, ""http://localhost:9200/{index}/_search"")`, a regexp as a string, and a query given to `with` was written inside the quotes.
|
|
72
|
+
|
|
73
|
+
The list now shows `stub_request(:get, Addressable::Template.new("http://localhost:9200/{index}/_search"))`, a regexp as a regexp, and a query that isn't part of the URL as `with(query: ...)`. The suggested stub for the unmatched request is unchanged.
|
|
74
|
+
|
|
75
|
+
* Leave out of the body diff the values that a regexp in a stub's body pattern matches (#840)
|
|
76
|
+
|
|
77
|
+
Previously, when a request didn't match a stub such as `with(body: {"email" => /@/}, headers: {"X-Token" => "1"})` because of its headers, the body diff in the message still listed `["~", "email", "milo@carter.net", /@/]`, so the regexp looked like the cause.
|
|
78
|
+
|
|
79
|
+
The diff now leaves out the values that such a regexp matches.
|
|
80
|
+
|
|
81
|
+
* Leave the User-Agent header out of the suggested stub for an unmatched request (#596)
|
|
82
|
+
|
|
83
|
+
Previously, the stub that WebMock suggests, as in "You can stub this request with the following snippet", required the request's `User-Agent`, such as `'User-Agent'=>'Ruby'` or one with the client's version. A stub pasted from it stopped matching when the client was upgraded or the tests ran on another platform.
|
|
84
|
+
|
|
85
|
+
The suggestion now leaves `User-Agent` out, so it matches whatever the client sends. The message still shows the request with all its headers.
|
|
86
|
+
|
|
87
|
+
* Fix loading a YAML or Marshal dump of an em-http-request client while WebMock is enabled (#729)
|
|
88
|
+
|
|
89
|
+
Previously, `YAML.unsafe_load(client.to_yaml)` or `Marshal.load(Marshal.dump(client))` raised `ArgumentError: undefined class/module EventMachine::HttpClient::CookieJar`, because WebMock replaces `EventMachine::HttpClient` with a subclass.
|
|
90
|
+
|
|
91
|
+
Such a dump now loads. A dump made while WebMock is enabled names WebMock's subclass, so load it where WebMock is loaded.
|
|
92
|
+
|
|
93
|
+
* Convert a status given as a string of digits, and deprecate statuses such as `:ok` (#802)
|
|
94
|
+
|
|
95
|
+
Previously, `to_return(status: "201")` gave the code `"2"` in Net::HTTP, because WebMock kept the string and adapters used its first character as the code. `to_return(status: :ok)` gave the code `"o"`, or `0` in some clients, without an error.
|
|
96
|
+
|
|
97
|
+
A string of digits is now converted to a number. A status such as `:ok`, `"ok"` or `200.0` now prints a deprecation warning, and WebMock 4.0 will raise `ArgumentError` for it. Use a number such as `200`, or `Rack::Utils.status_code(:ok)` in a Rack app. A code and reason pair such as `[200, "OK"]` works as before.
|
|
98
|
+
|
|
99
|
+
* Keep each cookie of a stubbed response with several `Set-Cookie` headers, in order (#470)
|
|
100
|
+
|
|
101
|
+
Previously, a raw response such as `to_return(File.read("response.txt"))` with two `Set-Cookie` lines gave clients one comma joined `Set-Cookie` value, which can't be split back, since a cookie such as `a=1; Expires=Wed, 21 Oct 2026 07:28:00 GMT` contains a comma. A stub such as `to_return(headers: {"Set-Cookie" => ["sid=old; Max-Age=0", "sid=new"]})` gave the cookies sorted, so a client applied `sid=old; Max-Age=0` last and deleted the cookie, and Curb got them on one header line.
|
|
102
|
+
|
|
103
|
+
Each client now gets the cookies as it would from a server sending these lines: separately, in the given order, on lines of their own in Curb, and joined in `headers["Set-Cookie"]` and listed in `response[:cookies]` in Excon. Other repeated response headers are unchanged.
|
|
104
|
+
|
|
105
|
+
A test that expected the cookies of such a stub joined, or sorted, now gets them as a real server would send them. For a real response with several cookies, `after_request` callbacks, and so VCR, now get them in the order the server sent them instead of sorted, and for Excon each cookie instead of the joined header, so recordings and snapshots of them can change.
|
|
106
|
+
|
|
107
|
+
* Point out when a request URL and a stub's URL differ in a slash at the end of the path (#864)
|
|
108
|
+
|
|
109
|
+
WebMock treats `http://example.com/users` and `http://example.com/users/` as different URLs, since servers can too. Previously, when a request missed a stub because of that slash, the message didn't say why.
|
|
110
|
+
|
|
111
|
+
The registered stubs listed for an unstubbed request, and the message of a failed `assert_requested` or `have_been_made`, now point out the slash. Matching is unchanged.
|
|
112
|
+
|
|
3
113
|
# 3.38.0
|
|
4
114
|
|
|
5
115
|
* Allow or disable real requests only within a block (#1110)
|
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) (`Curl::Easy`
|
|
26
|
+
* [Curb](https://github.com/taf2/curb) (`Curl::Easy` and `Curl::Multi`)
|
|
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)
|
|
@@ -412,6 +412,12 @@ Set appropriate Content-Type for HTTParty's `parsed_response`.
|
|
|
412
412
|
stub_request(:any, "www.example.com").to_return body: '{}', headers: {content_type: 'application/json'}
|
|
413
413
|
```
|
|
414
414
|
|
|
415
|
+
For JSON, `to_return_json` sets it for you:
|
|
416
|
+
|
|
417
|
+
```ruby
|
|
418
|
+
stub_request(:any, "www.example.com").to_return_json(body: {})
|
|
419
|
+
```
|
|
420
|
+
|
|
415
421
|
### Response with body specified as IO object
|
|
416
422
|
|
|
417
423
|
```ruby
|
|
@@ -433,6 +439,8 @@ stub_request(:any, "www.example.com").
|
|
|
433
439
|
Net::HTTP.get('www.example.com', '/') # ===> "{\"foo\": \"bar\"}"
|
|
434
440
|
```
|
|
435
441
|
|
|
442
|
+
`to_return_json` encodes the body and sets the `Content-Type` header to `application/json`, so there is no need to call `to_json` or set the header yourself. A `Content-Type` given in `headers` takes precedence.
|
|
443
|
+
|
|
436
444
|
A `File` or `Pathname` body is returned as it is, so it should hold JSON that is already encoded:
|
|
437
445
|
|
|
438
446
|
```ruby
|
|
@@ -562,6 +570,20 @@ stub_request(:any, 'www.example.net').to_timeout
|
|
|
562
570
|
RestClient.post('www.example.net', 'abc') # ===> RestClient::RequestTimeout
|
|
563
571
|
```
|
|
564
572
|
|
|
573
|
+
### Curb multi requests
|
|
574
|
+
|
|
575
|
+
`Curl::Multi` matches each handle against stubs when it is added. An unstubbed
|
|
576
|
+
handle raises `WebMock::NetConnectNotAllowedError` from `multi.add` unless its
|
|
577
|
+
URL is allowed by WebMock's network settings. Stubbed handles complete when
|
|
578
|
+
`multi.perform` runs; allowed handles still run together on curb's native multi
|
|
579
|
+
loop. Both kinds of requests are recorded and invoke callbacks, including
|
|
580
|
+
`WebMock.after_request`. Callbacks registered with `real_requests_only: true`
|
|
581
|
+
receive only real transfers.
|
|
582
|
+
|
|
583
|
+
This also applies to `Curl::Multi.get`, `post`, `put` and `http`, and the `Curl.get`
|
|
584
|
+
and `Curl.post` convenience methods. Create the multi and easy handles after
|
|
585
|
+
WebMock is enabled, as with `Curl::Easy` requests.
|
|
586
|
+
|
|
565
587
|
### Multiple responses for repeated requests
|
|
566
588
|
|
|
567
589
|
```ruby
|
|
@@ -1479,6 +1501,7 @@ People who submitted patches and new features or suggested improvements. Many th
|
|
|
1479
1501
|
* Hamza Tarar
|
|
1480
1502
|
* John Gallagher
|
|
1481
1503
|
* Yegor Bugayenko
|
|
1504
|
+
* Todd Fisher
|
|
1482
1505
|
|
|
1483
1506
|
For a full list of contributors you can visit the
|
|
1484
1507
|
[contributors](https://github.com/bblimke/webmock/contributors) page.
|
data/lib/webmock/errors.rb
CHANGED
|
@@ -103,6 +103,23 @@ if defined?(Async::HTTP)
|
|
|
103
103
|
@webmock_client.close
|
|
104
104
|
end
|
|
105
105
|
|
|
106
|
+
# Some libraries, such as async-websocket, take a connection from the
|
|
107
|
+
# pool instead of calling #call. WebMock can't see or stub what they
|
|
108
|
+
# send, so hand out the real pool only when WebMock's own settings
|
|
109
|
+
# allow real connections to the host the pool connects to: the
|
|
110
|
+
# endpoint's, whatever authority the requests name, with a ws or wss
|
|
111
|
+
# endpoint checked as http or https. VCR can't record or replay such
|
|
112
|
+
# connections, so it can't allow them.
|
|
113
|
+
def pool
|
|
114
|
+
host = @endpoint.hostname
|
|
115
|
+
host = "[#{host}]" if host.include?(":") && !host.start_with?("[")
|
|
116
|
+
uri = Addressable::URI.new(scheme: @endpoint.secure? ? "https" : "http", host: host, port: @endpoint.port, path: "/")
|
|
117
|
+
unless WebMock.net_connect_allowed_by_settings?(uri)
|
|
118
|
+
raise WebMock::NetConnectNotAllowedError.new(WebMock::RequestSignature.new(:get, uri))
|
|
119
|
+
end
|
|
120
|
+
@network_client.pool
|
|
121
|
+
end
|
|
122
|
+
|
|
106
123
|
private
|
|
107
124
|
|
|
108
125
|
def build_request_signature(request)
|
|
@@ -22,14 +22,20 @@ if defined?(Curl)
|
|
|
22
22
|
|
|
23
23
|
OriginalCurlEasy = Curl::Easy unless const_defined?(:OriginalCurlEasy)
|
|
24
24
|
|
|
25
|
+
OriginalCurlMulti = Curl::Multi unless const_defined?(:OriginalCurlMulti)
|
|
26
|
+
|
|
25
27
|
def self.enable!
|
|
26
28
|
Curl.send(:remove_const, :Easy)
|
|
27
29
|
Curl.send(:const_set, :Easy, Curl::WebMockCurlEasy)
|
|
30
|
+
Curl.send(:remove_const, :Multi)
|
|
31
|
+
Curl.send(:const_set, :Multi, Curl::WebMockCurlMulti)
|
|
28
32
|
end
|
|
29
33
|
|
|
30
34
|
def self.disable!
|
|
31
35
|
Curl.send(:remove_const, :Easy)
|
|
32
36
|
Curl.send(:const_set, :Easy, OriginalCurlEasy)
|
|
37
|
+
Curl.send(:remove_const, :Multi)
|
|
38
|
+
Curl.send(:const_set, :Multi, OriginalCurlMulti)
|
|
33
39
|
end
|
|
34
40
|
|
|
35
41
|
# Borrowed from Patron:
|
|
@@ -61,6 +67,82 @@ if defined?(Curl)
|
|
|
61
67
|
end
|
|
62
68
|
|
|
63
69
|
module Curl
|
|
70
|
+
class WebMockCurlMulti < Curl::Multi
|
|
71
|
+
# Intercept the native attachment boundary, retaining curb's public add,
|
|
72
|
+
# remove, request table and native event loop for allowed transfers.
|
|
73
|
+
def _add(easy)
|
|
74
|
+
# A handle of curb's own Curl::Easy class, such as a subclass defined
|
|
75
|
+
# before WebMock was loaded, one made while WebMock was disabled or a
|
|
76
|
+
# copy made with Curl::Easy#dup, makes its request without WebMock, as
|
|
77
|
+
# it always has. Easy#perform runs such handles through a multi too.
|
|
78
|
+
return super if !easy.is_a?(WebMockCurlEasy) || easy.webmock_performing_easy?
|
|
79
|
+
|
|
80
|
+
begin
|
|
81
|
+
signature, response = easy.webmock_request
|
|
82
|
+
rescue Exception # NetConnectNotAllowedError inherits directly from Exception.
|
|
83
|
+
# Older curb versions insert into requests before calling _add.
|
|
84
|
+
requests.delete(easy.object_id)
|
|
85
|
+
easy.webmock_restore_multi_url
|
|
86
|
+
raise
|
|
87
|
+
end
|
|
88
|
+
if response
|
|
89
|
+
easy.multi = self
|
|
90
|
+
webmock_pending[easy.object_id] = [easy, signature, response]
|
|
91
|
+
else
|
|
92
|
+
easy.webmock_record_multi_request(signature)
|
|
93
|
+
super
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def _remove(easy)
|
|
98
|
+
if webmock_pending.delete(easy.object_id)
|
|
99
|
+
easy.multi = nil
|
|
100
|
+
return self
|
|
101
|
+
end
|
|
102
|
+
super
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def perform(&block)
|
|
106
|
+
complete_webmock_requests
|
|
107
|
+
super do |multi|
|
|
108
|
+
begin
|
|
109
|
+
complete_webmock_requests
|
|
110
|
+
block.call(multi) if block
|
|
111
|
+
end until webmock_pending.empty?
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def webmock_pending
|
|
118
|
+
@webmock_pending ||= {}
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def complete_webmock_requests
|
|
122
|
+
until webmock_pending.empty?
|
|
123
|
+
easy, signature, response = webmock_pending.values.first
|
|
124
|
+
# Curb 1.5 takes a handle out of the multi on reset or close without
|
|
125
|
+
# calling _remove, so it no longer belongs to this multi.
|
|
126
|
+
attached = requests.key?(easy.object_id)
|
|
127
|
+
# Detach before callbacks so they can remove or requeue handles.
|
|
128
|
+
remove(easy)
|
|
129
|
+
webmock_pending.delete(easy.object_id)
|
|
130
|
+
next unless attached
|
|
131
|
+
|
|
132
|
+
if easy.webmock_follow_multi_redirect(signature, response)
|
|
133
|
+
add(easy)
|
|
134
|
+
else
|
|
135
|
+
begin
|
|
136
|
+
easy.multi = self
|
|
137
|
+
easy.webmock_complete_multi_request(signature, response)
|
|
138
|
+
ensure
|
|
139
|
+
easy.multi = nil if easy.multi == self && !requests.key?(easy.object_id)
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
64
146
|
class WebMockCurlEasy < Curl::Easy
|
|
65
147
|
# Curb's native PostField cannot be duplicated. Its public attributes
|
|
66
148
|
# describe the field without running a content callback or opening a file.
|
|
@@ -81,7 +163,10 @@ if defined?(Curl)
|
|
|
81
163
|
end
|
|
82
164
|
private_constant :MultipartField
|
|
83
165
|
|
|
84
|
-
def
|
|
166
|
+
def webmock_request
|
|
167
|
+
@webmock_method ||= :get
|
|
168
|
+
clear_webmock_response
|
|
169
|
+
webmock_clear_multi_request
|
|
85
170
|
request_signature = build_request_signature
|
|
86
171
|
recorded = signature_for_history(request_signature)
|
|
87
172
|
if request_signature.method == :put && @put_data.respond_to?(:read)
|
|
@@ -90,13 +175,22 @@ if defined?(Curl)
|
|
|
90
175
|
|
|
91
176
|
webmock_response = WebMock::StubRegistry.instance.response_for_request(
|
|
92
177
|
request_signature, record: recorded)
|
|
178
|
+
unless webmock_response || WebMock.net_connect_allowed?(request_signature.uri)
|
|
179
|
+
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
|
180
|
+
end
|
|
181
|
+
[request_signature, webmock_response]
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def curb_or_webmock
|
|
185
|
+
request_signature, webmock_response = webmock_request
|
|
93
186
|
if webmock_response
|
|
94
187
|
build_curb_response(webmock_response)
|
|
95
188
|
WebMock::CallbackRegistry.invoke_callbacks(
|
|
96
189
|
{lib: :curb}, request_signature, webmock_response)
|
|
97
190
|
invoke_curb_callbacks
|
|
98
191
|
true
|
|
99
|
-
|
|
192
|
+
else
|
|
193
|
+
@webmock_performing_easy = true
|
|
100
194
|
res = yield
|
|
101
195
|
if WebMock::CallbackRegistry.any_callbacks?
|
|
102
196
|
webmock_response = build_webmock_response
|
|
@@ -105,9 +199,104 @@ if defined?(Curl)
|
|
|
105
199
|
webmock_response)
|
|
106
200
|
end
|
|
107
201
|
res
|
|
202
|
+
end
|
|
203
|
+
ensure
|
|
204
|
+
@webmock_performing_easy = false
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def webmock_performing_easy?
|
|
208
|
+
@webmock_performing_easy
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def webmock_record_multi_request(signature)
|
|
212
|
+
@webmock_multi_request = signature
|
|
213
|
+
on_complete(&@on_complete)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def webmock_clear_multi_request
|
|
217
|
+
@webmock_multi_request = nil
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
%w[body header].each do |part|
|
|
221
|
+
define_method("on_#{part}") do |&block|
|
|
222
|
+
instance_variable_set("@on_#{part}", block)
|
|
223
|
+
if block
|
|
224
|
+
super() do |data|
|
|
225
|
+
if @webmock_multi_request
|
|
226
|
+
buffer = instance_variable_get("@webmock_#{part}") || +""
|
|
227
|
+
buffer << data
|
|
228
|
+
instance_variable_set("@webmock_#{part}", buffer)
|
|
229
|
+
end
|
|
230
|
+
block.call(data)
|
|
231
|
+
end
|
|
232
|
+
else
|
|
233
|
+
super(&block)
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def on_complete(&block)
|
|
239
|
+
@on_complete = block
|
|
240
|
+
super do |easy|
|
|
241
|
+
signature = @webmock_multi_request
|
|
242
|
+
webmock_restore_multi_url
|
|
243
|
+
webmock_clear_multi_request
|
|
244
|
+
if signature && WebMock::CallbackRegistry.any_callbacks?
|
|
245
|
+
WebMock::CallbackRegistry.invoke_callbacks(
|
|
246
|
+
{lib: :curb, real_request: true}, signature, build_webmock_response)
|
|
247
|
+
end
|
|
248
|
+
block.call(easy) if block
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def webmock_follow_multi_redirect(signature, response)
|
|
253
|
+
location = response.headers && response.headers['Location']
|
|
254
|
+
return false unless follow_location? && (300..399).include?(response.status[0]) && location
|
|
255
|
+
return false if response.should_timeout || response.exception
|
|
256
|
+
|
|
257
|
+
@webmock_multi_redirects ||= 0
|
|
258
|
+
limit = max_redirects || 50
|
|
259
|
+
if limit >= 0 && @webmock_multi_redirects >= limit
|
|
260
|
+
@webmock_last_result = 47 # CURLE_TOO_MANY_REDIRECTS
|
|
261
|
+
return false
|
|
262
|
+
end
|
|
263
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :curb}, signature, response)
|
|
264
|
+
@webmock_multi_original_url ||= url
|
|
265
|
+
@webmock_multi_redirects += 1
|
|
266
|
+
self.url = WebMock::Util::URI.heuristic_parse(url).join(location).to_s
|
|
267
|
+
true
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def webmock_restore_multi_url
|
|
271
|
+
return unless @webmock_multi_original_url
|
|
272
|
+
@last_effective_url = last_effective_url
|
|
273
|
+
self.url = @webmock_multi_original_url
|
|
274
|
+
@webmock_multi_original_url = @webmock_multi_redirects = nil
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def webmock_complete_multi_request(signature, response)
|
|
278
|
+
if response.should_timeout
|
|
279
|
+
@body_str = ""
|
|
280
|
+
@header_str = ""
|
|
281
|
+
@response_code = 0
|
|
282
|
+
@webmock_last_result = 28 # CURLE_OPERATION_TIMEDOUT
|
|
108
283
|
else
|
|
109
|
-
|
|
284
|
+
build_curb_response(response, follow_redirects: false)
|
|
285
|
+
@webmock_last_result ||= 0
|
|
110
286
|
end
|
|
287
|
+
webmock_restore_multi_url
|
|
288
|
+
WebMock::CallbackRegistry.invoke_callbacks({lib: :curb}, signature, response)
|
|
289
|
+
invoke_curb_callbacks(multi: true)
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def last_result
|
|
293
|
+
@webmock_last_result || super
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def clear_webmock_response
|
|
297
|
+
@body_str = @header_str = @response_code = @content_type = nil
|
|
298
|
+
@last_effective_url = @transfer_encoding = @webmock_last_result = nil
|
|
299
|
+
@webmock_body = @webmock_header = nil
|
|
111
300
|
end
|
|
112
301
|
|
|
113
302
|
def signature_for_history(signature)
|
|
@@ -138,7 +327,7 @@ if defined?(Curl)
|
|
|
138
327
|
nil
|
|
139
328
|
end
|
|
140
329
|
|
|
141
|
-
if
|
|
330
|
+
if @on_debug
|
|
142
331
|
@on_debug.call("Trying 127.0.0.1...\r\n", 0)
|
|
143
332
|
@on_debug.call('Connected to ' + uri.hostname + "\r\n", 0)
|
|
144
333
|
@debug_method = method.upcase
|
|
@@ -188,7 +377,7 @@ if defined?(Curl)
|
|
|
188
377
|
end
|
|
189
378
|
end
|
|
190
379
|
|
|
191
|
-
def build_curb_response(webmock_response)
|
|
380
|
+
def build_curb_response(webmock_response, follow_redirects: true)
|
|
192
381
|
raise Curl::Err::TimeoutError if webmock_response.should_timeout
|
|
193
382
|
webmock_response.raise_error_if_any
|
|
194
383
|
|
|
@@ -197,18 +386,22 @@ if defined?(Curl)
|
|
|
197
386
|
|
|
198
387
|
@header_str = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}\r\n".dup
|
|
199
388
|
|
|
200
|
-
@on_debug.call(@header_str, 1) if
|
|
389
|
+
@on_debug.call(@header_str, 1) if @on_debug
|
|
201
390
|
|
|
202
391
|
if webmock_response.headers
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
392
|
+
# Each cookie gets a line of its own, as servers send them.
|
|
393
|
+
@header_str << webmock_response.headers.flat_map do |k,v|
|
|
394
|
+
values = k == "Set-Cookie" ? Array(v) : [v.is_a?(Array) ? v.join(", ") : v]
|
|
395
|
+
values.map do |value|
|
|
396
|
+
header = "#{k}: #{value}"
|
|
397
|
+
@on_debug.call(header + "\r\n", 1) if @on_debug
|
|
398
|
+
header
|
|
399
|
+
end
|
|
207
400
|
end.join("\r\n")
|
|
208
|
-
@on_debug.call("\r\n", 1) if
|
|
401
|
+
@on_debug.call("\r\n", 1) if @on_debug
|
|
209
402
|
|
|
210
403
|
location = webmock_response.headers['Location']
|
|
211
|
-
if self.follow_location? && location
|
|
404
|
+
if follow_redirects && self.follow_location? && location
|
|
212
405
|
@last_effective_url = location
|
|
213
406
|
webmock_follow_location(location)
|
|
214
407
|
end
|
|
@@ -231,10 +424,10 @@ if defined?(Curl)
|
|
|
231
424
|
self.url = first_url
|
|
232
425
|
end
|
|
233
426
|
|
|
234
|
-
def invoke_curb_callbacks
|
|
235
|
-
@on_progress.call(0.0,1.0,0.0,1.0) if
|
|
236
|
-
self.header_str.lines.each { |header_line| @on_header.call header_line } if
|
|
237
|
-
if
|
|
427
|
+
def invoke_curb_callbacks(multi: false)
|
|
428
|
+
@on_progress.call(0.0,1.0,0.0,1.0) if @on_progress
|
|
429
|
+
self.header_str.lines.each { |header_line| @on_header.call header_line } if @on_header
|
|
430
|
+
if @on_body
|
|
238
431
|
if chunked_response?
|
|
239
432
|
self.body_str.each do |chunk|
|
|
240
433
|
@on_body.call(chunk)
|
|
@@ -243,15 +436,24 @@ if defined?(Curl)
|
|
|
243
436
|
@on_body.call(self.body_str)
|
|
244
437
|
end
|
|
245
438
|
end
|
|
246
|
-
|
|
439
|
+
completed_code = response_code
|
|
440
|
+
completed_result = last_result if multi
|
|
441
|
+
@on_complete.call(self) if @on_complete
|
|
442
|
+
|
|
443
|
+
if multi && completed_result != 0
|
|
444
|
+
@on_failure.call(self, Curl::Easy.error(completed_result)) if @on_failure
|
|
445
|
+
return
|
|
446
|
+
end
|
|
247
447
|
|
|
248
|
-
case
|
|
448
|
+
case completed_code
|
|
249
449
|
when 200..299
|
|
250
|
-
@on_success.call(self) if
|
|
450
|
+
@on_success.call(self) if @on_success
|
|
451
|
+
when 300..399
|
|
452
|
+
@on_redirect.call(self, Curl::Easy.error(0)) if multi && @on_redirect && max_redirects != 0
|
|
251
453
|
when 400..499
|
|
252
|
-
@on_missing.call(self, self.response_code) if
|
|
454
|
+
@on_missing.call(self, multi ? Curl::Easy.error(0) : self.response_code) if @on_missing
|
|
253
455
|
when 500..599
|
|
254
|
-
@on_failure.call(self, self.response_code) if
|
|
456
|
+
@on_failure.call(self, multi ? Curl::Easy.error(0) : self.response_code) if @on_failure
|
|
255
457
|
end
|
|
256
458
|
end
|
|
257
459
|
|
|
@@ -261,9 +463,9 @@ if defined?(Curl)
|
|
|
261
463
|
|
|
262
464
|
def build_webmock_response
|
|
263
465
|
status, headers =
|
|
264
|
-
WebMock::HttpLibAdapters::CurbAdapter.parse_header_string(self.header_str)
|
|
466
|
+
WebMock::HttpLibAdapters::CurbAdapter.parse_header_string((@webmock_header || self.header_str).to_s)
|
|
265
467
|
|
|
266
|
-
if
|
|
468
|
+
if @on_debug
|
|
267
469
|
http_response = ["HTTP/1.0 #{@debug_method} #{@debug_path}"]
|
|
268
470
|
headers.each do |name, value|
|
|
269
471
|
http_response << "#{name}: #{value}"
|
|
@@ -275,7 +477,7 @@ if defined?(Curl)
|
|
|
275
477
|
|
|
276
478
|
webmock_response = WebMock::Response.new
|
|
277
479
|
webmock_response.status = [self.response_code, status]
|
|
278
|
-
webmock_response.body = self.body_str
|
|
480
|
+
webmock_response.body = @webmock_body || self.body_str
|
|
279
481
|
webmock_response.headers = headers
|
|
280
482
|
webmock_response
|
|
281
483
|
end
|
|
@@ -391,7 +593,7 @@ if defined?(Curl)
|
|
|
391
593
|
@content_type ||= super
|
|
392
594
|
end
|
|
393
595
|
|
|
394
|
-
%w[ success failure missing
|
|
596
|
+
%w[ success failure missing redirect progress debug ].each do |callback|
|
|
395
597
|
class_eval <<-METHOD, __FILE__, __LINE__
|
|
396
598
|
def on_#{callback} &block
|
|
397
599
|
@on_#{callback} = block
|
|
@@ -406,12 +608,14 @@ if defined?(Curl)
|
|
|
406
608
|
|
|
407
609
|
def reset
|
|
408
610
|
@put_data = nil
|
|
611
|
+
clear_webmock_response
|
|
612
|
+
webmock_clear_multi_request
|
|
409
613
|
reset_webmock_method
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
614
|
+
@webmock_multi_original_url = @webmock_multi_redirects = nil
|
|
615
|
+
# Curb's reset drops every callback, so a stubbed request, or a real one
|
|
616
|
+
# WebMock records, doesn't run the ones set before it either.
|
|
617
|
+
@on_success = @on_failure = @on_missing = @on_redirect = @on_complete = nil
|
|
618
|
+
@on_body = @on_header = @on_progress = @on_debug = nil
|
|
415
619
|
super
|
|
416
620
|
end
|
|
417
621
|
end
|
|
@@ -84,6 +84,10 @@ if defined?(EventMachine::HttpClient)
|
|
|
84
84
|
|
|
85
85
|
class WebMockHttpClient < EventMachine::HttpClient
|
|
86
86
|
include HttpEncoding
|
|
87
|
+
# A YAML or Marshal dump names the cookie jar EventMachine::HttpClient::CookieJar,
|
|
88
|
+
# which is this class while WebMock is enabled, and loading a dump looks
|
|
89
|
+
# the constant up without the superclass.
|
|
90
|
+
CookieJar = superclass.const_get(:CookieJar, false) if superclass.const_defined?(:CookieJar, false)
|
|
87
91
|
|
|
88
92
|
def uri
|
|
89
93
|
@req.uri
|
|
@@ -129,18 +129,39 @@ if defined?(Excon)
|
|
|
129
129
|
def self.real_response(mock)
|
|
130
130
|
raise Excon::Errors::Timeout if mock.should_timeout
|
|
131
131
|
mock.raise_error_if_any
|
|
132
|
+
headers = mock.headers || {}
|
|
133
|
+
# Copies, so changes to a response's cookies can't reach the stub.
|
|
134
|
+
cookies = Array(headers["Set-Cookie"]).map(&:dup)
|
|
135
|
+
# Excon joins repeated headers, and lists each cookie in :cookies.
|
|
136
|
+
headers = headers.merge("Set-Cookie" => join_cookies(cookies)) if cookies.size > 1
|
|
132
137
|
{
|
|
133
138
|
body: mock.body,
|
|
134
139
|
status: mock.status[0].to_i,
|
|
135
140
|
reason_phrase: mock.status[1],
|
|
136
|
-
headers:
|
|
141
|
+
headers: headers,
|
|
142
|
+
cookies: cookies
|
|
137
143
|
}
|
|
138
144
|
end
|
|
139
145
|
|
|
146
|
+
# Joined as Excon joins them, or as bytes when they are in
|
|
147
|
+
# incompatible encodings, as a server sends them.
|
|
148
|
+
def self.join_cookies(cookies)
|
|
149
|
+
cookies.join(", ")
|
|
150
|
+
rescue Encoding::CompatibilityError
|
|
151
|
+
cookies.map(&:b).join(", ".b)
|
|
152
|
+
end
|
|
153
|
+
|
|
140
154
|
def self.mock_response(real)
|
|
141
155
|
mock = WebMock::Response.new
|
|
142
156
|
mock.status = [real.status, real.reason_phrase]
|
|
143
157
|
mock.headers = real.headers
|
|
158
|
+
# Excon joins the cookies of a response in its header, so callbacks,
|
|
159
|
+
# such as VCR's, get each one from the list Excon keeps.
|
|
160
|
+
# Copies, so a callback that changes one doesn't change the response.
|
|
161
|
+
cookies = real.data[:cookies]
|
|
162
|
+
if cookies && cookies.size > 1
|
|
163
|
+
mock.headers = real.headers.to_h.reject { |name, _| name.casecmp?("Set-Cookie") }.merge("Set-Cookie" => cookies.map(&:dup))
|
|
164
|
+
end
|
|
144
165
|
mock.body = real.body.dup
|
|
145
166
|
mock
|
|
146
167
|
end
|