From c87587bf8bab5d61479f798c9113f5c951c6102d Mon Sep 17 00:00:00 2001 From: Oskar Eichler <62393985+OskarEichler@users.noreply.github.com> Date: Sat, 29 Aug 2026 21:18:31 +0200 Subject: [PATCH] Preserve the source encoding when building multipart uploads --- lib/rack/test/utils.rb | 1 - spec/rack/test/utils_spec.rb | 12 ++++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/rack/test/utils.rb b/lib/rack/test/utils.rb index d1f7456..aea7193 100644 --- a/lib/rack/test/utils.rb +++ b/lib/rack/test/utils.rb @@ -152,7 +152,6 @@ def build_file_part(buffer, parameter_name, uploaded_file) # Handle old versions of Capybara::RackTest::Form::NilUploadedFile if uploaded_file.respond_to?(:set_encoding) - uploaded_file.set_encoding(Encoding::BINARY) uploaded_file.append_to(buffer) end diff --git a/spec/rack/test/utils_spec.rb b/spec/rack/test/utils_spec.rb index 090276d..7771909 100644 --- a/spec/rack/test/utils_spec.rb +++ b/spec/rack/test/utils_spec.rb @@ -78,6 +78,18 @@ describe 'Rack::Test::Utils.build_multipart' do include Rack::Test::Utils + it 'preserves the source encoding of uploaded StringIO content' do + source = +"café" + file = Rack::Test::UploadedFile.new( + StringIO.new(source), + original_filename: 'content.txt' + ) + + Rack::Test::Utils.build_multipart('file' => file) + + source.encoding.must_equal Encoding::UTF_8 + end + it 'builds multipart bodies' do files = Rack::Test::UploadedFile.new(multipart_file('foo.txt')) data = Rack::Test::Utils.build_multipart('submit-name' => 'Larry', 'files' => files)