Fix support for a file input which is not filled in.

This commit is contained in:
Rob Holland 2009-12-03 11:56:39 +00:00
parent e71d66b791
commit 70fe114518
4 changed files with 16 additions and 5 deletions

View File

@ -83,10 +83,12 @@ class Capybara::Driver::RackTest
agg
end
params += node.xpath(".//input[@type='file']").inject([]) do |agg, input|
if multipart?
agg << [input['name'].to_s, Rack::Test::UploadedFile.new(input['value'].to_s)]
else
agg << [input['name'].to_s, File.basename(input['value'].to_s)]
if input['value'].to_s.any?
if multipart?
agg << [input['name'].to_s, Rack::Test::UploadedFile.new(input['value'].to_s)]
else
agg << [input['name'].to_s, File.basename(input['value'].to_s)]
end
end
agg
end

View File

@ -486,6 +486,10 @@ shared_examples_for "session" do
@session.click_button('Upload')
@session.body.should include(File.read(@test_file_path))
end
it "should not break if no file is submitted" do
@session.click_button('Upload')
end
end
end

View File

@ -50,7 +50,7 @@ class TestApp < Sinatra::Base
end
post '/upload' do
params[:form][:document][:tempfile].read
params[:form][:document][:tempfile].read rescue ''
end
end

View File

@ -101,6 +101,11 @@
</form>
<form action="/upload" method="post" enctype="multipart/form-data">
<p>
<label for="form_name">Name</label>
<input type="file" name="form[name]" id="form_name"/>
</p>
<p>
<label for="form_document">Document</label>
<input type="file" name="form[document]" id="form_document"/>