Code Monkey home page Code Monkey logo

Comments (13)

vakuum avatar vakuum commented on July 17, 2024 1

The binary parameter of Rack::Test::UploadedFile does fix the Encoding::CompatibilityError:

Rack::Test::UploadedFile.new(first_test_file_path, 'application/octet-stream', true)

from rack-test.

jeremyevans avatar jeremyevans commented on July 17, 2024 1

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.

jeremyevans avatar jeremyevans commented on July 17, 2024 1

@rainerborene Thanks for the feedback! I'll push this change and try to get a 2.0.1 release out quickly.

from rack-test.

rainerborene avatar rainerborene commented on July 17, 2024

@jeremyevans Same problem here. I've tried your patch and it works fine.

from rack-test.

rainerborene avatar rainerborene commented on July 17, 2024

@jeremyevans That was fast. Thanks! πŸ’―

from rack-test.

rainerborene avatar rainerborene commented on July 17, 2024

@jeremyevans Can you release the new version on Rubygems please?

from rack-test.

jeremyevans avatar jeremyevans commented on July 17, 2024

@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.

rainerborene avatar rainerborene commented on July 17, 2024

@jeremyevans oops, my mistake. Thank you again πŸ‘

from rack-test.

ukolovda avatar ukolovda commented on July 17, 2024

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.

jeremyevans avatar jeremyevans commented on July 17, 2024

@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 avatar ukolovda commented on July 17, 2024

@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.

ukolovda avatar ukolovda commented on July 17, 2024

@jeremyevans

from rack-test.

jeremyevans avatar jeremyevans commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.