webmock 3.36.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +335 -0
  3. data/README.md +227 -6
  4. data/lib/webmock/api.rb +4 -0
  5. data/lib/webmock/callback_registry.rb +14 -7
  6. data/lib/webmock/errors.rb +1 -1
  7. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +18 -1
  8. data/lib/webmock/http_lib_adapters/curb_adapter.rb +292 -46
  9. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +11 -3
  10. data/lib/webmock/http_lib_adapters/excon_adapter.rb +23 -2
  11. data/lib/webmock/http_lib_adapters/http_rb/response.rb +3 -0
  12. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +92 -5
  13. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +127 -109
  14. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +1 -1
  15. data/lib/webmock/http_lib_adapters/net_http.rb +78 -10
  16. data/lib/webmock/http_lib_adapters/patron_adapter.rb +32 -13
  17. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +65 -7
  18. data/lib/webmock/json_body_hint.rb +119 -12
  19. data/lib/webmock/request_body_diff.rb +16 -6
  20. data/lib/webmock/request_execution_verifier.rb +19 -6
  21. data/lib/webmock/request_pattern.rb +178 -33
  22. data/lib/webmock/request_registry.rb +75 -5
  23. data/lib/webmock/request_signature.rb +44 -5
  24. data/lib/webmock/request_signature_snippet.rb +33 -4
  25. data/lib/webmock/request_stub.rb +32 -7
  26. data/lib/webmock/response.rb +42 -3
  27. data/lib/webmock/stub_registry.rb +7 -6
  28. data/lib/webmock/stub_request_snippet.rb +9 -3
  29. data/lib/webmock/trailing_slash_hint.rb +55 -0
  30. data/lib/webmock/util/hash_counter.rb +27 -8
  31. data/lib/webmock/util/hash_keys_stringifier.rb +23 -1
  32. data/lib/webmock/util/headers.rb +17 -0
  33. data/lib/webmock/util/message_string.rb +28 -0
  34. data/lib/webmock/util/parsers/json.rb +23 -54
  35. data/lib/webmock/util/upload_body.rb +63 -0
  36. data/lib/webmock/util/uri.rb +21 -12
  37. data/lib/webmock/util/values_stringifier.rb +54 -0
  38. data/lib/webmock/version.rb +1 -1
  39. data/lib/webmock/webmock.rb +35 -4
  40. data/lib/webmock.rb +3 -0
  41. metadata +10 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ac78ad5e14ee19d53223a19af699b7b22b18ba06fdfcd58837daa4810df48e3
4
- data.tar.gz: 18bea0ee19e26728813e849777d377cb048ffc7da4f2927901cd80d86e579b90
3
+ metadata.gz: 4c4c96d7db983ae7dfbdf251c8d0a146a64761b0716cd8b1f2cde3ba5d523146
4
+ data.tar.gz: 685b2118f07a46a1bcd2f02cb96d24d035087cae2504f45a7927b6673d27ccfd
5
5
  SHA512:
6
- metadata.gz: 91d74df9d7351cad99a7b51b162f3d2beb2e89e76969d570d650cbabcf3d3123f2a2d098c00f73af1e45983f4757f2693d894b5875d3ec941d57918c19304a3b
7
- data.tar.gz: f8d50c967121c7e843dcf6d0ea52ad467359cb023ae72c928151bd9cb1ff71188a46cd258470357a02156bb51682486e621726679eb48b1ac86ad2b0f60b5a77
6
+ metadata.gz: cb1a375f97aab7f5d298dedbcb12561537b82e9484d0bf4f64338e768142cbeb6f90f21b29ac6323a283a788294c126702ba6e4e5d20a4973540ab1a8a07a808
7
+ data.tar.gz: 2e42da579d5100fbcb2633838726795cccf41e1459d96d4cba4e6da566d876c0583cfa364e579b54bf2858ef363b8871c1f60bdba7493863760c716b76f84040
data/CHANGELOG.md CHANGED
@@ -1,5 +1,340 @@
1
1
  # Changelog
2
2
 
3
+ # 4.0.0
4
+
5
+ 4.0 requires Ruby 3.0 or later. Its other changes fix behaviour that was wrong in 3.x, so a spec fails only if it passed because of one of them. A typical test suite needs no changes: a stub such as `stub_request(:get, url).to_return(body: "ok")` works as before, a request that sends `{id: 1}.to_json` still matches `with(body: {id: 1})`, and VCR records and replays as before.
6
+
7
+ ## Breaking change
8
+
9
+ * **Ruby 3.0 or later, and crack 0.4.6 or later.** On Ruby 2.6 and 2.7, Bundler keeps installing WebMock 3.x. On JRuby, 4.0 needs JRuby 9.4 or later. If your Gemfile pins crack below 0.4.6, loosen the pin.
10
+
11
+ ## Fixes that can fail a spec
12
+
13
+ * **Stubs that didn't do what they said now fail where they are declared.** A bare `to_return` crashed on the first matching request, `to_raise(:timeout)` returned a 200 instead of raising, and `to_return(status: 404) { ... }` dropped the status. These now raise `ArgumentError`.
14
+
15
+ * **Request bodies that aren't valid JSON no longer match a hash pattern.** A request that sent `{a: 1}` as JSON matched `with(body: {a: 1})`, although a real server would reject it. JSON from `to_json` isn't affected. Before upgrading, run your specs on 3.42.0 and fix any JSON body warnings it prints.
16
+
17
+ * **Real Net::HTTP requests behave as without WebMock.** This only matters if your specs make real requests through Net::HTTP that aren't stubbed or recorded with VCR. The block given to `request` now gets the response while Net::HTTP reads it, instead of after WebMock has read all of it.
18
+
19
+ Statuses such as `:ok` still print the deprecation warning added in 3.39.0, rather than raising. Use a number, such as `200`.
20
+
21
+ ## If a spec fails
22
+
23
+ ### Stub declarations
24
+
25
+ * For an empty 200 response, leave off a bare `to_return`, or give it a response, such as `to_return(status: 200)`. `to_return` and `to_return_json` with no arguments, an empty list or empty `**options` raise `ArgumentError`, and so does a `nil` response, which raised `NoMethodError` before.
26
+ * Give `to_raise` an exception class, an exception or a message, such as `to_raise(StandardError)`. Use `to_timeout` for a timeout.
27
+ * Give `to_return` either responses or a block, such as `to_return { |request| {status: 404, body: "x"} }`.
28
+
29
+ The same checks apply to `and_return`, `and_return_json` and `and_raise`.
30
+
31
+ Thanks to [Daniel Pepper](https://github.com/dpep)
32
+
33
+ ### JSON request bodies (#432)
34
+
35
+ 3.41.0 started reading JSON request bodies with `JSON.parse`, and fell back to WebMock's old JSON parser with a deprecation warning. 4.0 drops the old parser, so a body declared as a hash, an array, `hash_including` or `hash_excluding` no longer matches:
36
+
37
+ * a request body that isn't valid JSON, such as `{a: 1}` or `{'a': 1}`. Send `{a: 1}.to_json` instead.
38
+ * a value the old parser read differently. The body `{"amount":1e+20}` matches `{amount: 1e20}`, not `{amount: "1e+20"}`.
39
+
40
+ A lambda or matcher in a body pattern now gets only the value `JSON.parse` returns, such as the Float `1.0e+20`. One that calls a String method on it, such as `start_with?`, raises `NoMethodError`, which fails the request even when another stub would match. 3.42.0 warns about such a lambda only when the old reading matched.
41
+
42
+ When the only reason a stub doesn't match is that the request body isn't valid JSON, the failure message now says so and quotes the parser's error, unless body diffs are hidden with `WebMock.hide_body_diff!`.
43
+
44
+ ### Real Net::HTTP requests (#742)
45
+
46
+ Previously, with real requests allowed, WebMock read the whole response before calling the block given to `request`. So `response.read_body { |chunk| ... }` got the body in one piece, and `response.decode_content = true` in the block left a gzip body compressed. Now the block gets the response while Net::HTTP reads it, as without WebMock, unless an `after_request` callback for Net::HTTP, such as VCR's, needs the whole body first. A callback registered with `except: [:net_http]` doesn't.
47
+
48
+ Code that relied on WebMock reading the body first can now fail, as it does without WebMock:
49
+
50
+ * Another request on the same connection, sent inside the block or after breaking out of it before the body is read, can fail. Send it after the block returns, or on another connection.
51
+ * `read_body` with a block, called after a request made with a block has returned, raises `IOError`. Call it inside the block.
52
+ * When the connection drops while the body is read, the net-http versions in Ruby 3.4 and later raise instead of retrying. That includes `http.get` without a block of your own, since it passes one to `request`. The net-http versions in Ruby 3.3 and earlier retry a GET and run the block again, even when the block raised `IOError`.
53
+
54
+ # 3.42.0
55
+
56
+ * Fix VCR failing to replay responses recorded with the `auto_inflate` feature of the `http` gem (#1154)
57
+
58
+ Previously, for a real request made with `HTTP.use(:auto_inflate)`, `after_request` callbacks got the inflated body together with the `Content-Encoding: gzip` header of the compressed one. VCR, hooked into WebMock, recorded both, and replaying the cassette raised `Zlib::DataError: incorrect header check`, because the feature tried to inflate the body again. With VCR's `decode_compressed_response` option, recording raised `Zlib::GzipFile::Error` instead.
59
+
60
+ Callbacks now get the inflated body without the `Content-Encoding` header, as they do for a body that Net::HTTP inflates. A `Content-Length` header, when the response has one, gives the size of the inflated body. The response the client returns is unchanged, and so are the headers of a response with an empty body, such as one to a HEAD request.
61
+
62
+ A cassette recorded this way replays the response with those headers, while the real response keeps the headers of the compressed body.
63
+
64
+ This doesn't cover a client that inflates with a subclass of the feature, or responses returned by the `caching` feature of `http` 6. Callbacks get those as before.
65
+
66
+ Cassettes recorded with older versions that have the `Content-Encoding` header on an inflated body still fail. Record them again, or remove that header from their responses. Their `Content-Length` is still that of the compressed body.
67
+
68
+ # 3.41.0
69
+
70
+ WebMock now parses JSON request bodies with Ruby's `JSON.parse`, instead of its old parser, which converted JSON to YAML and failed on or misread some valid JSON. Every body pattern that matched on 3.40.0 still matches. If one matches only as the old parser reads the body, WebMock prints a deprecation warning, because 4.0 drops the old parser and the pattern won't match there.
71
+
72
+ * Match JSON request bodies with an escaped backslash in a string (#432)
73
+
74
+ Previously, a hash body pattern didn't match a JSON body with an escaped backslash inside a string, such as JSON inside JSON, because the old parser failed on it. For example, `with(body: {data: '{"url":"/p?a=1\u0026b=2"}'})` didn't match the body `{"data":"{\"url\":\"/p?a=1\\u0026b=2\"}"}`. It now matches.
75
+
76
+ Strings that the old parser changed now match too, for example `"a\u000ab"`, which it read as `"a b"`.
77
+
78
+ * Match numbers with an exponent in JSON request bodies
79
+
80
+ Previously, a number such as `1e2` or `1e+20` in a JSON body was read as a String, so `with(body: {amount: 1e20})` didn't match the body `{"amount":1e+20}`, which `to_json` generates for that hash with recent json versions, such as the one in Ruby 4.0. It now matches.
81
+
82
+ * Deprecate body patterns that match a JSON request body only as the old parser reads it
83
+
84
+ The old parser read some bodies that aren't valid JSON, and some values differently from `JSON.parse`. For example, `with(body: {a: 1})` matches the body `{a: 1}`, and `with(body: {amount: "1e+20"})` matches the body `{"amount":1e+20}`, because the old parser read that number as a String. Such a pattern still matches, and WebMock prints a warning once for each pattern:
85
+
86
+ ```
87
+ WebMock deprecation warning: the body pattern {"a" => 1} matched the request body "{a: 1}" only as WebMock's old JSON parser reads it. WebMock 4.0 drops that parser, so it won't match. Send valid JSON, for example from to_json, and write the pattern for what JSON.parse returns.
88
+ ```
89
+
90
+ To fix it, send valid JSON, for example `{a: 1}.to_json`, and write the pattern for what `JSON.parse` returns, such as `{amount: 1e20}`.
91
+
92
+ * Don't raise Psych errors for JSON request bodies such as one with an unquoted date
93
+
94
+ Previously, with Psych 4 or later (the default since Ruby 3.1), a JSON body such as `{"a": 2020-01-01}`, which isn't valid JSON, raised `Psych::DisallowedClass` when a stub had a hash body pattern, instead of WebMock's error for an unregistered request. It now doesn't match the pattern.
95
+
96
+ * Name a future major version in the deprecation warning for statuses such as `:ok`
97
+
98
+ The deprecation warning for a status such as `:ok`, added in 3.39.0, said that WebMock 4.0 would raise `ArgumentError` for it. 4.0 changes how JSON request bodies are read and doesn't change statuses yet, so the warning now names a future major version.
99
+
100
+ # 3.40.0
101
+
102
+ * Add support for `Curl::Multi` (#535)
103
+
104
+ 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.
105
+
106
+ 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.
107
+
108
+ 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.
109
+
110
+ Thanks to [Todd Fisher](https://github.com/taf2)
111
+
112
+ * Drop curb callbacks on `Curl::Easy#reset` for stubbed requests, as curb does
113
+
114
+ 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`.
115
+
116
+ # 3.39.0
117
+
118
+ * Fix tracing libraries that wrap HTTPClient failing on stubbed requests (#1010)
119
+
120
+ 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.
121
+
122
+ It now returns the response, as it already did for real requests.
123
+
124
+ * Match and record Net::HTTP URL encoded forms set with `set_form` (#959)
125
+
126
+ 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.
127
+
128
+ 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.
129
+
130
+ * Fix async-websocket connections failing with WebMock enabled (#953)
131
+
132
+ 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.
133
+
134
+ 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: ...)`.
135
+
136
+ * Match the Cookie header that Typhoeus sends for the `cookie` option (#813)
137
+
138
+ 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.
139
+
140
+ 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.
141
+
142
+ * Fix VCR failing to replay Typhoeus requests when hooked into both Typhoeus and WebMock (#973)
143
+
144
+ 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`.
145
+
146
+ WebMock now leaves out requests it didn't see. On older versions, `hook_into :webmock` alone records and replays Typhoeus requests too.
147
+
148
+ * Run the instrumentation of the `http` gem 6 for stubbed requests (#881)
149
+
150
+ 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.
151
+
152
+ 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.
153
+
154
+ * Fix Net::HTTP uploads raising `ArgumentError` for body streams whose `read` needs a length (#644)
155
+
156
+ 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.
157
+
158
+ WebMock now reads such a stream in chunks.
159
+
160
+ * Fix failure messages raising `Encoding::CompatibilityError` for bodies in different encodings (#668)
161
+
162
+ 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.
163
+
164
+ Such messages now show the failure, with bytes that aren't valid UTF-8 written as escapes, such as `\xFF`.
165
+
166
+ * Show URI templates, regexps and query options as Ruby in the list of registered stubs (#938)
167
+
168
+ 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.
169
+
170
+ 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.
171
+
172
+ * Leave out of the body diff the values that a regexp in a stub's body pattern matches (#840)
173
+
174
+ 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.
175
+
176
+ The diff now leaves out the values that such a regexp matches.
177
+
178
+ * Leave the User-Agent header out of the suggested stub for an unmatched request (#596)
179
+
180
+ 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.
181
+
182
+ The suggestion now leaves `User-Agent` out, so it matches whatever the client sends. The message still shows the request with all its headers.
183
+
184
+ * Fix loading a YAML or Marshal dump of an em-http-request client while WebMock is enabled (#729)
185
+
186
+ 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.
187
+
188
+ Such a dump now loads. A dump made while WebMock is enabled names WebMock's subclass, so load it where WebMock is loaded.
189
+
190
+ * Convert a status given as a string of digits, and deprecate statuses such as `:ok` (#802)
191
+
192
+ 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.
193
+
194
+ 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.
195
+
196
+ * Keep each cookie of a stubbed response with several `Set-Cookie` headers, in order (#470)
197
+
198
+ 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.
199
+
200
+ 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.
201
+
202
+ 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.
203
+
204
+ * Point out when a request URL and a stub's URL differ in a slash at the end of the path (#864)
205
+
206
+ 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.
207
+
208
+ 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.
209
+
210
+ # 3.38.0
211
+
212
+ * Allow or disable real requests only within a block (#1110)
213
+
214
+ `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.
215
+
216
+ 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.
217
+
218
+ 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.
219
+
220
+ Thanks to [Yegor Bugayenko](https://github.com/yegor256) for the suggestion
221
+
222
+ * Record the form Content-Type of Typhoeus requests with a hash body (#463)
223
+
224
+ 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.
225
+
226
+ 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.
227
+
228
+ 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.
229
+
230
+ 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.
231
+
232
+ * Keep a `with` block when `with` is called again with options only
233
+
234
+ 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`.
235
+
236
+ 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.
237
+
238
+ * Match numbers and boolean values in query patterns (#371)
239
+
240
+ 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`.
241
+
242
+ WebMock now compares numbers, `true` and `false` in query patterns as strings, including inside nested `hash_including` and `hash_excluding` matchers.
243
+
244
+ 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.
245
+
246
+ * Fix nested `hash_including` and `hash_excluding` with symbol keys in RSpec (#900)
247
+
248
+ 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.
249
+
250
+ The same applied to query patterns: `with(query: hash_including(filter: hash_excluding(role: "admin")))` matched `?filter[role]=admin`.
251
+
252
+ 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.
253
+
254
+ * Fix `hash_including` and `hash_excluding` nested in a query hash
255
+
256
+ 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.
257
+
258
+ Such stubs now match with every kind of URI.
259
+
260
+ * Ignore letter case in allowed host names (#819)
261
+
262
+ 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`.
263
+
264
+ 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.
265
+
266
+ * Fix URI templates not matching encoded characters in query values (#468)
267
+
268
+ 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.
269
+
270
+ Both now match. As with string URIs, a stub can't tell such an encoded character from the character itself.
271
+
272
+ * Fix stubs with a hash body pattern raising on binary request bodies (#333)
273
+
274
+ 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.
275
+
276
+ 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.
277
+
278
+ * Fix unstubbed requests with some form bodies raising `ArgumentError` instead of WebMock's error
279
+
280
+ 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`.
281
+
282
+ WebMock now raises its usual error, and the suggested stub shows such a body as a string.
283
+
284
+ * Fix Net::HTTP `OPTIONS *` requests raising `Addressable::URI::InvalidURIError` (#982)
285
+
286
+ 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.
287
+
288
+ 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 `/*`.
289
+
290
+ * Apply a changed `query_values_notation` to URLs that WebMock has already seen
291
+
292
+ 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.
293
+
294
+ 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.
295
+
296
+ * Match `with(query: "...")` pairs in any order with `:flat_array`
297
+
298
+ 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.
299
+
300
+ 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`.
301
+
302
+ * Fix URLs that repeat a query key without a value raising with `:flat_array`
303
+
304
+ 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.
305
+
306
+ # 3.37.0
307
+
308
+ * Add `executed_requests` to inspect the requests made (#1069)
309
+
310
+ `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)`.
311
+
312
+ Previously, this needed WebMock internals such as `WebMock::RequestRegistry.instance.requested_signatures.hash.keys`, which list each distinct request only once.
313
+
314
+ Thanks to [John Gallagher](https://github.com/johngallagher) for the idea, which we started pairing on at EuRuKo 2024 in Sarajevo.
315
+
316
+ * Fix stub matching for consecutive Curb PUT requests on the same handle
317
+
318
+ 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.
319
+
320
+ * Run HTTPClient request filters once per attempt on real requests
321
+
322
+ 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.
323
+
324
+ * Fix async HTTPClient requests hanging when an `after_request` callback is registered
325
+
326
+ 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.
327
+
328
+ * Match file and stream uploads by their content
329
+
330
+ 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.
331
+
332
+ Blocks given to `stub_request(...).with` still receive the original file or stream.
333
+
334
+ * Count em-http-request requests blocked by `disable_net_connect!`
335
+
336
+ Previously, WebMock did not record them, so `assert_requested` did not count them. They are now counted, as with other HTTP clients.
337
+
3
338
  # 3.36.0
4
339
 
5
340
  * Fix matching of indexed arrays nested inside arrays of hashes (#1144)