Implement file uploading for :rack

This commit is contained in:
Simon Rozet 2009-06-25 23:33:32 +02:00
parent f2882ef8b2
commit 3760867d0f
3 changed files with 29 additions and 0 deletions

View File

@ -366,6 +366,8 @@ module Webrat
when :merb
# TODO: support content_type
File.new(@value)
when :rack
Rack::Test::UploadedFile.new(@value, content_type)
end
end

View File

@ -33,6 +33,14 @@ class RackApp < Sinatra::Base
@email = params[:email]
erb :hello
end
get "/upload" do
erb :uploader
end
post "/upload" do
params[:uploaded_file].to_yaml
end
end
__END__
@ -71,3 +79,11 @@ __END__
@@ hello
<p>Hello, <%= @user %></p>
<p>Your email is: <%= @email %></p>
@@ uploader
<form action="/upload" method="post">
<label>
File <input type="file" name="uploaded_file" />
</label>
<input type="submit" value="Upload">
</form>

View File

@ -42,6 +42,17 @@ class WebratRackTest < Test::Unit::TestCase
visit "/absolute_redirect"
assert_contain "spam"
end
def test_upload_file
visit "/upload"
attach_file "File", __FILE__, "text/ruby"
click_button "Upload"
upload = YAML.load(response_body)
assert_equal "text/ruby", upload[:type]
assert_equal "webrat_rack_test.rb", upload[:filename]
assert upload[:tempfile].respond_to?(:read)
end
end
class WebratRackSetupTest < Test::Unit::TestCase