Skip to content

Load the http_rb adapter only where HTTP::Client is defined - #1139

Merged
bblimke merged 1 commit into
bblimke:masterfrom
thoran:http-rb-adapter-needs-http-client
Nov 18, 2026
Merged

bblimke merged 1 commit into
bblimke:masterfrom
thoran:http-rb-adapter-needs-http-client

Conversation

@thoran

@thoran thoran commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

The http_rb adapter is loaded wherever there's an HTTP::VERSION, but other gems define a top-level HTTP with a VERSION too. When one of them is loaded, require "webmock" fails with:

http_rb/client.rb:5:in 'Module#alias_method': undefined method 'perform' for class 'HTTP::Client' (NameError)

so WebMock can't be used at all, not even with Net::HTTP.

The gem I ran into this with is the http.rb gem on RubyGems (https://rubygems.org/gems/http.rb). Despite the name, it isn't the http gem (httprb/http) this adapter is for. It's a separate, smaller library, built on Net::HTTP, which WebMock's Net::HTTP adapter already covers. It defines a top-level HTTP module with a VERSION, but no HTTP::Client. Its load file is lib/http.rb, so wherever it's installed the adapter's own require "http" can load it, and WebMock then fails whatever order things are required in. (Up to 1.6.0 the file was lib/HTTP.rb, which require "http" found only on a case-insensitive filesystem such as macOS's default.) With any gem like it, WebMock fails where that gem is loaded first.

This adds defined?(HTTP::Client), the class the adapter patches, to the guard. Where the http gem is loaded, HTTP::Client is always defined, since require "http" loads it, so nothing changes there.

The spec loads WebMock in a separate Ruby process with a stand-in http.rb ahead of it on the load path, which defines HTTP::VERSION and nothing else, and checks that WebMock loads and doesn't define the adapter. Without the change, that process raises the NameError above.

I haven't added a CHANGELOG entry, since those seem to be written at release time.

Other libraries define a top-level HTTP with a VERSION, and require
"http" can load one of them. WebMock then took it for the http gem and
raised NameError aliasing HTTP::Client#perform, so WebMock couldn't be
loaded at all.
@thoran
thoran force-pushed the http-rb-adapter-needs-http-client branch from 9ef7cf3 to d7f80d3 Compare October 18, 2026 15:52
@bblimke
bblimke merged commit b5f065c into bblimke:master Nov 18, 2026
8 checks passed
bblimke added a commit that referenced this pull request Nov 18, 2026
Sort indexed keys by number and then by text, so "01" and "1" always
come out in the same order, as they did with the old text sort.
sort_by is not stable, so the order of such keys depended on the
order of the query. Check for digit keys with \A and \z, so a key with
digits on just one of its lines stays a hash key instead of becoming
index 0.

Drop the redundant defined?(HTTP) from the http_rb adapter guard, use
map in dehash, and add specs for indexed hashes in a query, for keys
with leading zeros and for a key spanning lines.

Document both PRs in the CHANGELOG, including what to do with stubs
that list indexed values in the old order.
@bblimke

bblimke commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thank you @thoran! This is released in 3.35.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants