-
Notifications
You must be signed in to change notification settings - Fork 252
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
Issues with hidden field + file field #301
Comments
Thanks for the report. I think the check before set_encoding was removed because there was no test for it, and it appeared to be related to Ruby 1.8 support, checking if the object responded to |
I looked into this and I'm not sure how it happens. The error message and failing line shows that I'll keep looking, but this would be much easier if you could provide the arguments you passed to |
I'm guessing this is using Capybara, so I tried to build a simple app that uses capybara. I couldn't replicate the require 'capybara'
require 'capybara/dsl'
require 'rack/test'
require 'minitest/autorun'
Capybara.app = lambda do |env|
[200, {}, [env['REQUEST_METHOD'] == 'POST' ? env['rack.input'].read : (<<END)]]
<!DOCTYPE html>
<html>
<body>
<form method="POST">
<input type="hidden" name="file" value="none" />
<input type="file" name="file" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
END
end
class Minitest::Spec
include Rack::Test::Methods
include Capybara::DSL
def teardown
Capybara.reset_sessions!
Capybara.use_default_driver
end
def test_works
visit '/'
click_button 'Submit'
assert_equal 'file=', page.html
end
end I do see that the empty ["http://www.example.com/", {"file"=>""}, {"HTTP_REFERER"=>"http://www.example.com/"}] So that issue does appear to be confirmed as a capybara issue and not a rack-test issue. If you can provide a reproducer for the set_encoding issue, I would greatly appreciate it. |
Looks like I spoke to soon. Needed to add |
Actually, this is a bug in Capybara, as I can see from the arguments it is passing to
Capybara is passing an instance of it's own class that does not mirror the UploadedFile API. That's a horrible idea. However, since one of the goals of rack-test is to remain compatible with Capybara, I will add back the related code checking for set_encoding. |
…File Capybara subclasses Rack::Test::UploadedFile, overrides initialize and redefines a bunch of methods. This is a terrible approach, but as one of the goals is to keep capybara working with rack-test, this hack is needed for compatibility. Note that Capybara master already defines set_encoding and append_to, so this is only needed for Capybara 3.37.1 and older. Fixes rack#301
I've added a pull request to fix this, so that rack-test 2.0.0 will work with older versions of Capybara (fixing the set_encoding issue). Note that Capybara master already had related fixes: teamcapybara/capybara@82138ab You'll still need to contact Capybara if you want them to make it so unfilled file inputs are ignored, so previous hidden inputs can take precedence. However, there must be a reason they are using |
Apologies. It was my intent to prepare one if needed but didn't do so preemptively as it was well into the evening for me.
Thank you. I'll reach out there. Because this works in capybara drivers which execute a real browser I jumped to the conclusion that it might be rack-test. I should have considered it could be the capybara driver later itself for rack-test. I appreciate the time and attention you gave the issue. |
Wow it looks like this issue is potentially 12 years old -- teamcapybara/capybara@2989829 That commit changed the behaviour from not submitting an unfilled file field to the current state of posting the weird That seems to be referring to rack-test/lib/rack/test/utils.rb Lines 32 to 51 in cf9d2e0
multipart value at all: Line 304 in cf9d2e0
|
Should be fairly easy to make |
Yeah that looks pretty straight forward. I plan to chase up an issue with Capybara later today so I'll reference that issue. However, given rack-test hasn't had a release in 3ish years, I think I'll need to find an alternate workaround anyway. |
Release date for rack-test should be pretty soon. Looks like capybara already works with it. With both Capybara and Rails confirmed to be working, a release can happen shortly, maybe as early as next week. |
Good to know. I'll see about making a case for fixing this hidden field issue as part of any potential Capybara release then, but that would probably require them to depend on the new rack-test version as a minimum. |
I have opened teamcapybara/capybara#2556 |
Hi Jeremy, hope you're well.
Today I noticed a discrepancy between rack-test (1.1.0) and a real browser, which led me to #278 as possibly related, but trying latest GitHub version causes a separate error. I'll describe both and then we can see if they should be split up.
The primary issue is the combination of a hidden field and a file field of the same param name. In a real browser, if the file field (which is after the hidden field in the DOM) is left blank, the hidden field string content is posted under that parameter name. If the file field is filled out, the hidden field param value is absent and a multipart part for the file is submitted with that param name.
In rack-test however, a hidden field and a subsequent empty file field which share a name results in an empty param for that name.
This led me to #278 as possibly related, so I upgraded to 2988e7e. Unfortunately, this version raises the following error for any unfilled file field:
In 1.1.0, an empty field is not part of the params at all. However, it also seems to erase a non-empty hidden field of the same name.
The text was updated successfully, but these errors were encountered: