gems webmock 4.0.0

one hour ago

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.

Breaking change

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

Fixes that can fail a spec

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

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

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

Statuses such as :ok still print the deprecation warning added in 3.39.0, rather than raising. Use a number, such as 200.

If a spec fails

Stub declarations

  • 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.
  • Give to_raise an exception class, an exception or a message, such as to_raise(StandardError). Use to_timeout for a timeout.
  • Give to_return either responses or a block, such as to_return { |request| {status: 404, body: "x"} }.

The same checks apply to and_return, and_return_json and and_raise.

Thanks to Daniel Pepper

JSON request bodies (#432)

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:

  • a request body that isn't valid JSON, such as {a: 1} or {'a': 1}. Send {a: 1}.to_json instead.
  • a value the old parser read differently. The body {"amount":1e+20} matches {amount: 1e20}, not {amount: "1e+20"}.

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.

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

Real Net::HTTP requests (#742)

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.

Code that relied on WebMock reading the body first can now fail, as it does without WebMock:

  • 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.
  • read_body with a block, called after a request made with a block has returned, raises IOError. Call it inside the block.
  • 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.

Don't miss a new webmock release

NewReleases is sending notifications on new releases.