Comments (13)
The binary
parameter of Rack::Test::UploadedFile
does fix the Encoding::CompatibilityError
:
- https://github.com/rack/rack-test/blob/v2.0.0/lib/rack/test/uploaded_file.rb#L31
- https://github.com/rack/rack-test/blob/v1.1.0/lib/rack/test/uploaded_file.rb#L30
Rack::Test::UploadedFile.new(first_test_file_path, 'application/octet-stream', true)
from rack-test.
Can you try this patch?:
diff --git a/lib/rack/test/utils.rb b/lib/rack/test/utils.rb
index 8f37058..079aee1 100644
--- a/lib/rack/test/utils.rb
+++ b/lib/rack/test/utils.rb
@@ -129,6 +129,8 @@ module Rack
"\"\r\n\r\n" <<
value.to_s <<
"\r\n"
+ buffer.force_encoding(Encoding::BINARY)
+ buffer
end
# Append the multipart fragment for a parameter that is a file upload to the buffer.
@@ -144,6 +146,7 @@ module Rack
"\r\ncontent-length: " <<
uploaded_file.size.to_s <<
"\r\n\r\n"
+ buffer.force_encoding(Encoding::BINARY)
# Handle old versions of Capybara::RackTest::Form::NilUploadedFile
if uploaded_file.respond_to?(:set_encoding)
diff --git a/spec/rack/test/utils_spec.rb b/spec/rack/test/utils_spec.rb
index 7bb72ae..a787e18 100644
--- a/spec/rack/test/utils_spec.rb
+++ b/spec/rack/test/utils_spec.rb
@@ -168,6 +168,23 @@ describe 'Rack::Test::Utils.build_multipart' do
params['foo'].must_equal %w[1 2]
end
+ it 'builds nested multipart bodies with UTF-8 data' do
+ files = Rack::Test::UploadedFile.new(multipart_file('mb.txt'))
+ data = Rack::Test::Utils.build_multipart('people' => [{ 'submit-name' => "\u1234", 'files' => files }], 'foo' => %w[1 2])
+
+ options = {
+ 'CONTENT_TYPE' => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
+ 'CONTENT_LENGTH' => data.length.to_s,
+ :input => StringIO.new(data)
+ }
+ env = Rack::MockRequest.env_for('/', options)
+ params = Rack::Multipart.parse_multipart(env)
+ params['people'][0]['submit-name'].must_equal "\u1234"
+ params['people'][0]['files'][:filename].must_equal 'mb.txt'
+ params['people'][0]['files'][:tempfile].read.must_equal "\u2345".b
+ params['foo'].must_equal %w[1 2]
+ end
+
it 'builds nested multipart bodies with an array of hashes' do
files = Rack::Test::UploadedFile.new(multipart_file('foo.txt'))
data = Rack::Test::Utils.build_multipart('files' => files, 'foo' => [{ 'id' => '1', 'name' => 'Dave' }, { 'id' => '2', 'name' => 'Steve' }])
diff --git a/spec/fixtures/mb.txt b/spec/fixtures/mb.txt
new file mode 100644
index 0000000..6b68062
--- /dev/null
+++ b/spec/fixtures/mb.txt
@@ -0,0 +1 @@
+<E2><8D><85>
\ No newline at end of file
from rack-test.
@rainerborene Thanks for the feedback! I'll push this change and try to get a 2.0.1 release out quickly.
from rack-test.
@jeremyevans Same problem here. I've tried your patch and it works fine.
from rack-test.
@jeremyevans That was fast. Thanks! π―
from rack-test.
@jeremyevans Can you release the new version on Rubygems please?
from rack-test.
@jeremyevans Can you release the new version on Rubygems please?
I already said I would do that:
@rainerborene Thanks for the feedback! I'll push this change and try to get a 2.0.1 release out quickly.
from rack-test.
@jeremyevans oops, my mistake. Thank you again π
from rack-test.
For me, this problem occured on Windows (Rails controller got only part of file). But in Linux it works without binary flag.
from rack-test.
@ukolovda Are you able to reproduce the issue with the current rack-test on Windows? I wonder if we need to pass binmode: true
option when opening the Tempfile on Windows.
from rack-test.
@ukolovda Are you able to reproduce the issue with the current rack-test on Windows? I wonder if we need to pass
binmode: true
option when opening the Tempfile on Windows.
Yes, I checked now. I can reproduce the issue.
Some files:
# Gemfile
...
gem "rack-test", git: "https://github.com/rack/rack-test", branch: "main"
...
# Gemfile.lock
GIT
remote: https://github.com/rack/rack-test
revision: c69b465e98d0d959f2014ab3a025264039a7ab4f
branch: main
specs:
rack-test (2.1.0)
rack (>= 1.3)
...
# some_controller_test.rb
require 'test_helper'
class SomeControllerTest < ActionController::TestCase
should 'show map fields for one row' do
filename = File.join(Rails.root, 'test', 'fixtures', 'files', 'one_row.xlsx')
file = Rack::Test::UploadedFile.new(filename, 'application/xlsx')
post :map_fields, params: {files: [file]}
assert_response :success
assert_template 'map_fields'
end
end
It failed on Windows 10 (origin file have 20 Kb size, but in controller params[:files][0]
have only ~800 bytes).
But works in Linux environment.
from rack-test.
from rack-test.
I think you need to pass the binary argument to UploadedFile:
file = Rack::Test::UploadedFile.new(filename, 'application/xlsx', true)
from rack-test.
Related Issues (20)
- uploaded_file.rb#append_to: incompatible character encodings: UTF-8 and ASCII-8BIT HOT 3
- clarify that minitest is used for testing HOT 1
- utils.rb#build_primitive_part: incompatible character encodings: UTF-8 and ASCII-8BIT HOT 4
- Π‘an`t pass cookies HOT 1
- Setting original_filename when the content is not StringIO HOT 4
- build_primitive_part: undefined method `b' while testing docx HOT 4
- ActiveSupport::MessageVerifier::InvalidSignature error while uploading file in RSpec Tests HOT 2
- Rack::Test::UploadedFile#test_0007_finalizes on garbage collection fails on sparc HOT 3
- Resolving dependencies for published CVE's HOT 1
- Cannot set CONTENT_TYPE at Rack::MockRequest.env_for HOT 1
- Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead HOT 2
- something amiss with release tags HOT 3
- Preferred way to set :domain option on cookie jar while setting a cookie using []=? HOT 2
- Rack >= 2.2.2 tests skip `Rack::Events#on_finish` HOT 3
- Is `Rack::Test::UploadedFile` supposed to be used with StringIO instead of real files? HOT 1
- Accessing Tempfile after GC of UploadedFile will cause IOError HOT 2
- Vulnerability scan shows rack-test is bringing in a vulnerable version of rack HOT 4
- Request body received as empty string in POST request (sinatra 4, rack 3) HOT 4
- no _dump_data is defined for class StringIO when running tests HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rack-test.