gems webmock 3.39.0

58 minutes ago
  • 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_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.

    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 as with(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::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.

    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: ...).

  • Match the Cookie header that Typhoeus sends for the cookie option (#813)

    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.

    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.

  • 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 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.

    WebMock now leaves out requests it didn't see. On older versions, hook_into :webmock alone records and replays Typhoeus requests too.

  • Run the instrumentation of the http gem 6 for stubbed requests (#881)

    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.

    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 ArgumentError for body streams whose read needs a length (#644)

    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.

    WebMock now reads such a stream in chunks.

  • Fix failure messages raising Encoding::CompatibilityError for bodies in different encodings (#668)

    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.

    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 to with was 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 as with(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-Agent out, 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) or Marshal.load(Marshal.dump(client)) raised ArgumentError: undefined class/module EventMachine::HttpClient::CookieJar, because WebMock replaces EventMachine::HttpClient with 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", or 0 in some clients, without an error.

    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.

  • Keep each cookie of a stubbed response with several Set-Cookie headers, in order (#470)

    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.

    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.

    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.

  • 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/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.

    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.

Don't miss a new webmock release

NewReleases is sending notifications on new releases.