Skip to content

Commit 85b6be0

Browse files
committed
Fix additional incompatible character encodings error when building uploaded bodies
Force all strings concatenated to the uploaded body to be binary strings. At this point, the force_encoding calls are unnecesary, so they can be removed. Fixes #311
1 parent 0e565f0 commit 85b6be0

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

History.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.0.2 / 2022-06-28
2+
3+
* Bug fixes:
4+
* Fix additional incompatible character encodings error when building
5+
uploaded bodies (Jeremy Evans #311)
6+
17
## 2.0.1 / 2022-06-27
28

39
* Bug fixes:

lib/rack/test/utils.rb

+6-11
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ def build_multipart(params, _first = true, multipart = false)
5353

5454
buffer = String.new
5555
build_parts(buffer, params)
56-
# :nocov:
57-
buffer.force_encoding(Encoding::BINARY) if Rack::Test.encoding_aware_strings?
58-
# :nocov:
5956
buffer
6057
end
6158

@@ -125,11 +122,10 @@ def build_primitive_part(buffer, parameter_name, value)
125122
buffer <<
126123
START_BOUNDARY <<
127124
"content-disposition: form-data; name=\"" <<
128-
parameter_name.to_s <<
125+
parameter_name.to_s.b <<
129126
"\"\r\n\r\n" <<
130-
value.to_s <<
127+
value.to_s.b <<
131128
"\r\n"
132-
buffer.force_encoding(Encoding::BINARY)
133129
buffer
134130
end
135131

@@ -138,15 +134,14 @@ def build_file_part(buffer, parameter_name, uploaded_file)
138134
buffer <<
139135
START_BOUNDARY <<
140136
"content-disposition: form-data; name=\"" <<
141-
parameter_name.to_s <<
137+
parameter_name.to_s.b <<
142138
"\"; filename=\"" <<
143-
escape_path(uploaded_file.original_filename) <<
139+
escape_path(uploaded_file.original_filename).b <<
144140
"\"\r\ncontent-type: " <<
145-
uploaded_file.content_type.to_s <<
141+
uploaded_file.content_type.to_s.b <<
146142
"\r\ncontent-length: " <<
147-
uploaded_file.size.to_s <<
143+
uploaded_file.size.to_s.b <<
148144
"\r\n\r\n"
149-
buffer.force_encoding(Encoding::BINARY)
150145

151146
# Handle old versions of Capybara::RackTest::Form::NilUploadedFile
152147
if uploaded_file.respond_to?(:set_encoding)

spec/rack/test/utils_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ def respond_to?(m, *a)
183183
params['people'][0]['files'][:filename].must_equal 'mb.txt'
184184
params['people'][0]['files'][:tempfile].read.must_equal "\u2345".b
185185
params['foo'].must_equal %w[1 2]
186+
187+
files = Rack::Test::UploadedFile.new(multipart_file('mb.txt'))
188+
data = Rack::Test::Utils.build_multipart('people' => [{ 'files' => files, 'submit-name' => "\u1234" }], 'foo' => %w[1 2])
189+
190+
options = {
191+
'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
192+
'CONTENT_LENGTH' => data.length.to_s,
193+
:input => StringIO.new(data)
194+
}
195+
env = Rack::MockRequest.env_for('/', options)
196+
params = Rack::Multipart.parse_multipart(env)
197+
params['people'][0]['submit-name'].b.must_equal "\u1234".b
198+
params['people'][0]['files'][:filename].must_equal 'mb.txt'
199+
params['people'][0]['files'][:tempfile].read.must_equal "\u2345".b
200+
params['foo'].must_equal %w[1 2]
186201
end
187202

188203
it 'builds nested multipart bodies with an array of hashes' do

0 commit comments

Comments
 (0)