New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller tests no longer accept ActionDispatch::Http::UploadedFile #26404

Closed
kirs opened this Issue Sep 6, 2016 · 5 comments

Comments

Projects
None yet
3 participants
@kirs
Member

kirs commented Sep 6, 2016

In the process of upgrading to Rails 5, I've discovered a piece of code that worked fine on 4.2 and no longer works on 5-0-stable:

class PostsController < ApplicationController
  def upload
    raise "content: #{params[:file]}, class: #{params[:file].class}"
  end
end

class PostsControllerTest < ActionController::TestCase
  test "upload" do
    artwork = ActionDispatch::Http::UploadedFile.new(
      filename: 'artwork.jpeg',
      content_type: 'image/jpeg',
      tempfile: StringIO.new,
    )

    post :upload, params: { file: artwork }
  end
end
$ rails test test/controllers/posts_controller_test.rb
# Running:
Error:
PostsControllerTest#test_upload:
RuntimeError: content: #<ActionDispatch::Http::UploadedFile:0x007fb0c877acd8>, class: String
    app/controllers/posts_controller.rb:16:in `upload'
    test/controllers/posts_controller_test.rb:18:in `block in <class:PostsControllerTest>'

As you see, submitting a form with ActionDispatch::Http::UploadedFile instance gets the upload casted to String.

The reason why it happens is here: https://github.com/rails/rails/blob/5-0-stable/actionpack/lib/action_controller/test_case.rb#L137

It makes sense that Rails 5 is expecting Rack::Test::UploadedFile in test env, but I think that we shouldn't have break code that worked on 4.2. It seems like a regression to me.

cc @tenderlove since you've introduced that line.

@kirs

This comment has been minimized.

Show comment
Hide comment
@kirs

kirs Sep 6, 2016

Member

The reason why I cannot use Rack::Test::UploadedFile in my test suite is that it only accepts a path to real file. In my test case, I want to test how the controller behaves with an empty file (ActionDispatch::Http::UploadedFile.new(tempfile: StringIO.new))

Member

kirs commented Sep 6, 2016

The reason why I cannot use Rack::Test::UploadedFile in my test suite is that it only accepts a path to real file. In my test case, I want to test how the controller behaves with an empty file (ActionDispatch::Http::UploadedFile.new(tempfile: StringIO.new))

@maclover7 maclover7 added the actionpack label Sep 7, 2016

@maclover7

This comment has been minimized.

Show comment
Hide comment
@maclover7

maclover7 Sep 7, 2016

Member

👍 Makes sense to me, can you open up a PR?

Member

maclover7 commented Sep 7, 2016

👍 Makes sense to me, can you open up a PR?

@kirs

This comment has been minimized.

Show comment
Hide comment
@kirs

kirs Sep 8, 2016

Member

@maclover7 I had a chat with @rafaelfranca and it turns out that we can't send a valid file just with StringIO.new, because upload should also contain filename and mime type.

I'm going to close the issue.

Member

kirs commented Sep 8, 2016

@maclover7 I had a chat with @rafaelfranca and it turns out that we can't send a valid file just with StringIO.new, because upload should also contain filename and mime type.

I'm going to close the issue.

@kirs kirs closed this Sep 8, 2016

@jaredbeck

This comment has been minimized.

Show comment
Hide comment
@jaredbeck

jaredbeck Feb 9, 2017

Contributor

Using ::Rack::Test::UploadedFile instead of ::ActionDispatch::Http::UploadedFile worked for me, thanks. The constructor is a bit different, but no problem.

It would be great if someone could add this to the upgrade guide:
http://guides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-4-2-to-rails-5-0

Contributor

jaredbeck commented Feb 9, 2017

Using ::Rack::Test::UploadedFile instead of ::ActionDispatch::Http::UploadedFile worked for me, thanks. The constructor is a bit different, but no problem.

It would be great if someone could add this to the upgrade guide:
http://guides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-4-2-to-rails-5-0

@maclover7

This comment has been minimized.

Show comment
Hide comment
@maclover7
Member

maclover7 commented Feb 9, 2017

@jaredbeck opened #27954

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment