Skip to content

Keep URI query in HTTPClient request signature when query is passed - #1145

Merged
bblimke merged 1 commit into
bblimke:masterfrom
AmeenKhan12345:fix-httpclient-uri-query-dropped
Nov 20, 2026
Merged

bblimke merged 1 commit into
bblimke:masterfrom
AmeenKhan12345:fix-httpclient-uri-query-dropped

Conversation

@AmeenKhan12345

@AmeenKhan12345 AmeenKhan12345 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #641

Problem

With HTTPClient, you can put query params in the URL and also pass more in the query argument. HTTPClient sends both:

HTTPClient.new.get("https://x.test/Item?a=1", { "b" => "2" })
# HTTPClient requests: /Item?a=1&b=2

WebMock doesn't record that URL. It drops the params that were already in the URL and keeps only the query argument:

query argument HTTPClient requests WebMock records (before) WebMock records (after)
{ "b" => "2" } /Item?a=1&b=2 /Item?b=2 /Item?a=1&b=2
{} /Item?a=1& /Item /Item?a=1
nil /Item?a=1 /Item?a=1 /Item?a=1

Because of this, stubs and a_request expectations can match a URL that was never requested. Tests pass even though the real request is different.

We ran into this in a real project. Our code passed {} for every GET, and the URLs already had query params. An external API started rejecting those requests, but our tests kept passing, because WebMock recorded the URL with no query at all.

Reproduction

require "httpclient"
require "webmock"
include WebMock::API
WebMock.enable!

seen = []
WebMock.after_request { |sig, _| seen << sig.uri.to_s }
stub_request(:any, /x\.test/)

uri = URI("https://x.test/Item?a=1")
[{}, nil, { "b" => "2" }].each do |q|
  wire = HTTP::Message.new_request(:get, uri, q).header.create_query_uri
  HTTPClient.new.get(uri, q)
  puts "query=#{q.inspect.ljust(14)} sent: #{wire.ljust(16)} webmock: #{seen.last}"
end

Output on WebMock 3.34.0 with HTTPClient 2.10.0:

query={}             sent: /Item?a=1&       webmock: https://x.test:443/Item
query=nil            sent: /Item?a=1        webmock: https://x.test:443/Item?a=1
query={"b" => "2"}   sent: /Item?a=1&b=2    webmock: https://x.test:443/Item?b=2

What this PR changes

  1. Adapter: when building the request signature, keep the query that's already in the URL and add the query argument to it, the same way HTTPClient does.
  2. Spec helper: the HTTPClient spec helper was passing every query twice, once in the URL and again as the query argument. Real HTTPClient would send each param twice there; the bug above hid it. The helper now passes the query once, as the query argument.
  3. New specs: a URL query combined with a hash, an empty hash, and nil, plus a hash on a URL with no query. The first two fail without the fix.

Tests

  • HTTPClient specs: 654 examples, 0 failures (NO_CONNECTION=1).
  • Full suite (NO_CONNECTION=1): the only failures are 2 Patron get_file examples, which fail the same way on master on my machine.

Note

A test that stubbed only the query argument part (for example ?b=2, when the real request is ?a=1&b=2) will stop matching after this change. That's expected, because the stub now has to match the URL that is really requested, but it may be worth a line in the changelog.

The 2 failures on Ruby 3.3 / 3.4 / 4.0 are unrelated to this change. They're the Patron get_file specs, and they also fail on unmodified master with Patron 1.1.0, which was released after the last green master build (14 Oct).

HTTPClient appends the query argument to any query already in the URI:
get("http://example.com/?a=1", {"b" => "2"}) requests /?a=1&b=2. The
HTTPClient adapter replaced the URI query with the query argument, so
the signature was /?b=2. With an empty hash it was /, dropping the
query completely. Stubs and expectations could then match a request
that was never made.

Combine the URI query with the query argument, like HTTPClient does.

The HTTPClient acceptance spec helper passed every query twice (in the
URI and as the query argument), which the old behaviour hid. It now
passes the query once, as the query argument.

Fixes bblimke#641
@bblimke
bblimke merged commit 4bd2170 into bblimke:master Nov 20, 2026
5 of 8 checks passed
bblimke added a commit that referenced this pull request Nov 20, 2026
Drop a check that could not fail and explain the order of the query
parts. The spec helper sends the query in the URI, as most callers do,
so HTTPClient sends what the shared specs expect, and the adapter specs
cover the query argument. Add a changelog entry.
@bblimke

bblimke commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thank you @AmeenKhan12345! This is released in 3.36.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.

Query parameters in url get's deleted

2 participants