Trending

World technology news

→
A programmer working on unit test code.
RubyGemsIn-depth report

WebMock 4.0 updates Ruby software tests for malformed JSON request bodies

The 4.0.0 release requires Ruby 3.0 or later. Invalid JSON can no longer match a body hash, and some invalid stub declarations now fail during setup.

·RubyGems
Reading progress0%

A major version with a new Ruby minimum

WebMock 4.0.0 reached RubyGems at 01:22:36 on November 29 in Istanbul and appears in Open Source Insights. The changelog in the package sets a minimum of Ruby 3.0 and crack 0.4.6. An older interpreter or a stricter dependency pin can affect resolution before a test runs. Compare the versions used by local and automated test environments when introducing the release. The downloaded gem archive was examined for its documented changes. This report does not provide results from a downstream application.

Invalid JSON is different from a matching object

The changelog says a body presented as JSON must be valid JSON to match a hash-style expectation. Earlier tests could appear to succeed for a string that a real endpoint would reject. Build one fixture from actual JSON serialization and another from a deliberately malformed test value. Verify both with the intended content type and body matcher. Checking only the method and path can conceal the difference. A fixture that exercises the body catches a contract error before an application depends on the stub.

Some invalid declarations fail earlier

Several inconsistent stub declarations now produce an error when configured. This makes the failure appear during test setup instead of on the next matching request. A suite can state success, a timeout and an application error as explicit separate cases. When a response is fixed, state its status and body at the test boundary. When a case depends on request data, return a response from the callback according to the documented interface. The changelog gives ordinary GET and properly serialized JSON as examples whose established behaviour is retained.

Unstubbed Net::HTTP streaming has its own check

The package changelog also describes a change for real Net::HTTP requests that are not intercepted by a stub or replayed. The response block sees the response as it is read. A small local HTTP fixture can verify when a consumer receives headers and body chunks. A fully stubbed unit test answers a different question from a test of actual streaming. Keep the two checks separate when investigating a regression. No external requests or downstream tests were run to inspect this distribution.

Check the behaviour the suite depends on

A migration can record the Ruby and gem versions and rerun a valid body, a malformed body and an invalid declaration as separate cases. A project that relies on actual response streaming can test that behaviour in a controlled local setup. These checks distinguish changed validation from a failure to resolve the new gem. The picture is an archival illustration of writing unit tests. It does not depict a specific WebMock run. The changelog lists JRuby 9.4 as the supported minimum for that interpreter. Record the interpreter as well as the gem version when collecting migration results.

Photo · Joonspoon / Wikimedia Commons. Resized and converted to WebP. ↗ · CC BY-SA 4.0

Sources and verification
Be the first reader ✎ Write a comment

Reader discussion

Discussion prompt

Which alternative architecture would you try for the same problem?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.