From bd3e3281fc2793a379850ff495b61d2a2da4889d Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 28 Sep 2025 12:56:48 -0700 Subject: [PATCH] Add Rack::Test::Utils.override_build_nested_query The new Rack::Test::Utils.override_build_nested_query allows library users to make rack-test reuse Rack::Utils#build_nested_query instead of replacing it. The rack package already provides a Rack::Utils#build_nested_query. So the rack-test doesn't need to override or re-implement it. By reusing the one from rack, this package will receive improvements as they are made there. For example, the upstream method has improved URL encoding since commit: https://github.com/rack/rack/commit/10864b644c233c463cb2844a27f98200eb94a05 --- lib/rack/test/utils.rb | 7 +++++++ spec/rack/test/utils_spec.rb | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/rack/test/utils.rb b/lib/rack/test/utils.rb index a79a8e7a..d1f74568 100644 --- a/lib/rack/test/utils.rb +++ b/lib/rack/test/utils.rb @@ -6,9 +6,16 @@ module Utils # :nodoc: include Rack::Utils extend self + class << self + attr_accessor :override_build_nested_query + end + self.override_build_nested_query = false + # Build a query string for the given value and prefix. The value # can be an array or hash of parameters. def build_nested_query(value, prefix = nil) + return super if Rack::Test::Utils.override_build_nested_query + case value when Array if value.empty? diff --git a/spec/rack/test/utils_spec.rb b/spec/rack/test/utils_spec.rb index 9cb3670f..090276d3 100644 --- a/spec/rack/test/utils_spec.rb +++ b/spec/rack/test/utils_spec.rb @@ -58,6 +58,21 @@ input = { collection: [] } build_nested_query(input).must_equal 'collection[]=' end + + it 'percent encodes brackets with override_build_nested_query' do + original = Rack::Test::Utils.override_build_nested_query + Rack::Test::Utils.override_build_nested_query = true + begin + query = if Gem::Version.new(Rack.release) < Gem::Version.new("3.1") + 'a[b]=c' + else + 'a%5Bb%5D=c' + end + build_nested_query({"a" => { "b" => "c" }}).must_equal query + ensure + Rack::Test::Utils.override_build_nested_query = original + end + end end describe 'Rack::Test::Utils.build_multipart' do