-
Fix tracing libraries that wrap HTTPClient failing on stubbed requests (#1010)
Previously, for a stubbed request, WebMock's version of HTTPClient's private
do_get_blockmethod returned aThread::Queueinstead 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.It now returns the response, as it already did for real requests.
-
Match and record Net::HTTP URL encoded forms set with
set_form(#959)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 aswith(body: {"foo" => "bar"})didn't match, and callbacks and VCR recorded the request without its body.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.
-
Fix async-websocket connections failing with WebMock enabled (#953)
Previously, connecting with
Async::WebSocket::ClientraisedNoMethodError: 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.The client now hands out its real connection pool when WebMock's settings allow real connections to the host, and raises
WebMock::NetConnectNotAllowedErrorotherwise. 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 withWebMock.disable_net_connect!(allow: ...). -
Match the Cookie header that Typhoeus sends for the
cookieoption (#813)Previously,
Typhoeus.get(url, cookie: "key=value")sentCookie: key=value, but WebMock recorded the request without it, so a stub withwith(headers: {"Cookie" => "key=value"})didn't match.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,cookiejarorcookielist, 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 withfilter_sensitive_data, if it is a secret. -
Fix VCR failing to replay Typhoeus requests when hooked into both Typhoeus and WebMock (#973)
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 raisedArgumentError. VCR's Typhoeus hook answered the request before WebMock saw it, and WebMock still passed the request toafter_requestcallbacks, asnil.WebMock now leaves out requests it didn't see. On older versions,
hook_into :webmockalone records and replays Typhoeus requests too. -
Run the instrumentation of the
httpgem 6 for stubbed requests (#881)Previously, with version 6 of the
httpgem (HTTP.rb), a stubbed request skipped the instrumentation feature, soHTTP.use(instrumentation: {instrumenter: ...})emitted norequest.httpevents for it. In every version, a stubbed timeout orHTTP::Erroremitted noerror.httpevent.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.
-
Fix Net::HTTP uploads raising
ArgumentErrorfor body streams whosereadneeds a length (#644)Previously, a request with
body_streamset to an object withdef read(length, buffer = nil)raisedArgumentError: wrong number of arguments, even with real connections allowed, because WebMock calledreadwithout arguments. Net::HTTP itself always passes a length, so the upload worked without WebMock.WebMock now reads such a stream in chunks.
-
Fix failure messages raising
Encoding::CompatibilityErrorfor bodies in different encodings (#668)Previously, the message for an unstubbed request, or for a failed
assert_requested, could raiseEncoding::CompatibilityError: incompatible character encodings: BINARY (ASCII-8BIT) and UTF-8instead of showing the failure. This happened when the message listed a UTF-8 body with non ASCII characters, such as a stubwith(body: "café"), next to a binary one, such as an uploaded image.Such messages now show the failure, with bytes that aren't valid UTF-8 written as escapes, such as
\xFF. -
Show URI templates, regexps and query options as Ruby in the list of registered stubs (#938)
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 towithwas written inside the quotes.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 aswith(query: ...). The suggested stub for the unmatched request is unchanged. -
Leave out of the body diff the values that a regexp in a stub's body pattern matches (#840)
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.The diff now leaves out the values that such a regexp matches.
-
Leave the User-Agent header out of the suggested stub for an unmatched request (#596)
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.The suggestion now leaves
User-Agentout, so it matches whatever the client sends. The message still shows the request with all its headers. -
Fix loading a YAML or Marshal dump of an em-http-request client while WebMock is enabled (#729)
Previously,
YAML.unsafe_load(client.to_yaml)orMarshal.load(Marshal.dump(client))raisedArgumentError: undefined class/module EventMachine::HttpClient::CookieJar, because WebMock replacesEventMachine::HttpClientwith a subclass.Such a dump now loads. A dump made while WebMock is enabled names WebMock's subclass, so load it where WebMock is loaded.
-
Convert a status given as a string of digits, and deprecate statuses such as
:ok(#802)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", or0in some clients, without an error.A string of digits is now converted to a number. A status such as
:ok,"ok"or200.0now prints a deprecation warning, and WebMock 4.0 will raiseArgumentErrorfor it. Use a number such as200, orRack::Utils.status_code(:ok)in a Rack app. A code and reason pair such as[200, "OK"]works as before. -
Keep each cookie of a stubbed response with several
Set-Cookieheaders, in order (#470)Previously, a raw response such as
to_return(File.read("response.txt"))with twoSet-Cookielines gave clients one comma joinedSet-Cookievalue, which can't be split back, since a cookie such asa=1; Expires=Wed, 21 Oct 2026 07:28:00 GMTcontains a comma. A stub such asto_return(headers: {"Set-Cookie" => ["sid=old; Max-Age=0", "sid=new"]})gave the cookies sorted, so a client appliedsid=old; Max-Age=0last and deleted the cookie, and Curb got them on one header line.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 inresponse[:cookies]in Excon. Other repeated response headers are unchanged.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_requestcallbacks, 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. -
Point out when a request URL and a stub's URL differ in a slash at the end of the path (#864)
WebMock treats
http://example.com/usersandhttp://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.The registered stubs listed for an unstubbed request, and the message of a failed
assert_requestedorhave_been_made, now point out the slash. Matching is unchanged.