Skip to content

Upgrading webmock 3 to 4

Breaking3.42.0 → 4.0.0Breaking15Deprecated1Changed2

BreakingUpgrade verdict

15 breaking changes.

  • Some stub setups that used to crash later or quietly do the wrong thing now fail immediately with an error.
  • If a request sends something that only looks like JSON but is not valid JSON, it no longer matches a hash body pattern.
  • When specs make real, unstubbed Net::HTTP requests, the response block now runs while the body is being read, like plain Net::HTTP.
  • Stub response helpers now reject empty or nil responses and require to_raise to get an exception or message.
Top 4 of 16 that need code or config changes

Ruby requirement changed in 4.0.0: >= 2.6 → >= 3.0.

Moves crack >= 0.3.2 → >= 0.4.6. Your build may pick other versions.

Breaking15
Stub declarations that were silently wrong now raise ArgumentError where they are declared.
to_returnto_raiseArgumentError
Request bodies that are not valid JSON no longer match hash, array, hash_including or hash_excluding body patterns.
withto_jsonhash_includinghash_excluding
Real Net::HTTP requests now pass the response to the request block while Net::HTTP reads it, as without WebMock.
Net::HTTPrequest
to_return, to_return_json, and_return, and_return_json, and_raise and to_raise now require valid responses, exceptions or blocks.
to_returnto_return_jsonand_returnand_return_jsonArgumentError
to_raise now needs an exception class, exception or message; use to_timeout for timeouts.
to_raiseto_timeoutand_raise
to_return must get either responses or a block, not both.
to_return
The same stub declaration checks apply to and_return, and_return_json and and_raise.
and_returnand_return_jsonand_raise
WebMock 4.0 drops the old JSON parser, so body patterns match only what JSON.parse returns.
JSON.parsehash_includinghash_excluding
Invalid JSON request bodies such as {a: 1} no longer match hash body patterns; send {a: 1}.to_json instead.
to_json
Values the old parser read differently now match as JSON.parse reads them, e.g. 1e+20 matches 1e20 not "1e+20".
Body pattern lambdas and matchers now receive only the JSON.parse value, so String methods on it raise NoMethodError.
JSON.parsestart_with?NoMethodError
Real Net::HTTP request blocks now run while the body is read, so read_body and connection reuse inside the block can fail.
requestread_bodydecode_contentafter_requestexcept
Sending another request on the same connection inside the block or after breaking out before the body is read can now fail.
Calling read_body with a block after a request made with a block has returned now raises IOError; call it inside the block.
read_bodyIOError
On Ruby 3.4 and later, a dropped connection during body read raises instead of retrying, including http.get without a block.
http.getrequestIOError
Deprecated1
Statuses such as :ok still print the deprecation warning added in 3.39.0 instead of raising; use a number such as 200.

Generated by DepDelta from published Ruby source. Not mixed with prose notes.

  • added: const JsonBodyHint.INVALID_JSON_ADVICE
  • added: const JsonBodyHint.INVALID_JSON_HINTS
  • added: method BodyPattern.parses_body?()
  • added: static method CallbackRegistry.any_callbacks_for?(_)
  • added: static method JsonBodyHint.expectation_hint(_, _)
  • added: static method JsonBodyHint.invalid_json_hint(_, _, _)
  • removed: protected static method JSON.convert_json_to_yaml(_)
  • removed: protected static method JSON.unescape(_)
  • removed: static method JSON.parse_legacy(_)
  • added: const JSON.ERROR_PREFIX