diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 052d223ec8815f29810e4d1c42f12f98eee815a5..9e604e37a5e90e37f6c769a4cf59749d5c349db9 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,10 @@ *SVN* +* Added easy support for testing file uploads with fixture_file_upload #4105 [turnip@turnipspatch.com]. Example: + + # Looks in Test::Unit::TestCase.fixture_path + '/files/spongebob.png' + post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png') + * Fixed UrlHelper#current_page? to behave even when url-escaped entities are present #3929 [jeremy@planetargon.com] * Add ability for relative_url_root to be specified via an environment variable RAILS_RELATIVE_URL_ROOT. [isaac@reuben.com, Nicholas Seckar] diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index 85bcc7372db2aa70efff4bd171a7ecc33817ef27..5e6f4219467f8ee641564e5e3a839bacb5955ea0 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -289,6 +289,40 @@ def close() end def delete() @attributes = {} end end + # Essentially generates a modified Tempfile object similar to the object + # you'd get from the standard library CGI module in a multipart + # request. This means you can use an ActionController::TestUploadedFile + # object in the params of a test request in order to simulate + # a file upload. + # + # Usage example, within a functional test: + # post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png') + class TestUploadedFile + # The filename, *not* including the path, of the "uploaded" file + attr_reader :original_filename + + # The content type of the "uploaded" file + attr_reader :content_type + + def initialize(path, content_type = 'text/plain') + raise "file does not exist" unless File.exist?(path) + @content_type = content_type + @original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 } + @tempfile = Tempfile.new(@original_filename) + FileUtils.copy_file(path, @tempfile.path) + end + + def path #:nodoc: + @tempfile.path + end + + alias local_path path + + def method_missing(method_name, *args, &block) #:nodoc: + @tempfile.send(method_name, *args, &block) + end + end + module TestProcess def self.included(base) # execute the request simulating a specific http method and set/volley the response @@ -393,6 +427,15 @@ def method_missing(selector, *args) return @controller.send(selector, *args) if ActionController::Routing::NamedRoutes::Helpers.include?(selector) return super end + + # Shortcut for ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type). Example: + # post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png') + def fixture_file_upload(path, mime_type = nil) + ActionController::TestUploadedFile.new( + Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase + path : path, + mime_type + ) + end # A helper to make it easier to test different route configurations. # This method temporarily replaces ActionController::Routing::Routes diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb index 25c514d5a3bd12f1d814f5c0709928ef96ff8f4c..06a2ce3eb78c5a20711f245f47e69ce753bbebab 100644 --- a/actionpack/test/controller/test_test.rb +++ b/actionpack/test/controller/test_test.rb @@ -49,6 +49,10 @@ def test_only_one_param def test_remote_addr render :text => (request.remote_addr || "not specified") end + + def test_file_upload + render :text => params[:file].size + end def rescue_action(e) raise e @@ -366,4 +370,27 @@ def test_header_properly_reset_after_get_request end end end + + FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart' + + def test_test_uploaded_file + filename = 'mona_lisa.jpg' + path = "#{FILES_DIR}/#{filename}" + content_type = 'image/png' + + file = ActionController::TestUploadedFile.new(path, content_type) + assert_equal filename, file.original_filename + assert_equal content_type, file.content_type + assert_equal file.path, file.local_path + assert_equal File.read(path), file.read + end + + def test_fixture_file_upload + post :test_file_upload, :file => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") + assert_equal 159528, @response.body + end + + def test_test_uploaded_file_exception_when_file_doesnt_exist + assert_raise(RuntimeError) { ActionController::TestUploadedFile.new('non_existent_file') } + end end diff --git a/actionpack/test/fixtures/multipart/mona_lisa.jpg b/actionpack/test/fixtures/multipart/mona_lisa.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5cf3bef3d03b6ad0c49615447b4fcf32aace32f7 Binary files /dev/null and b/actionpack/test/fixtures/multipart/mona_lisa.jpg differ