Permalink
Browse files

Unit tests and fixtures for multipart posts.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2389 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 9084f5b commit 5d19fd784127f3e3ae0be94d718b89c255edf065 @jeremy jeremy committed Sep 28, 2005
@@ -1,8 +1,4 @@
-$:.unshift File.dirname(__FILE__) + "/../../lib"
-
-require 'test/unit'
-require 'action_controller/cgi_ext/cgi_methods'
-require 'stringio'
+require File.dirname(__FILE__) + '/../abstract_unit'
class CGITest < Test::Unit::TestCase
def setup
@@ -228,3 +224,82 @@ def test_parse_params_with_unmatched_brackets_and_array
end
end
+class MultipartCGITest < Test::Unit::TestCase
+ FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart'
+
+ def setup
+ ENV['REQUEST_METHOD'] = 'POST'
+ ENV['CONTENT_LENGTH'] = '0'
+ ENV['CONTENT_TYPE'] = 'multipart/form-data, boundary=AaB03x'
+ end
+
+ def test_single_parameter
+ params = process('single_parameter')
+ assert_equal({ 'foo' => 'bar' }, params)
+ end
+
+ def test_text_file
+ params = process('text_file')
+ assert_equal %w(file foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+
+ file = params['file']
+ assert_kind_of StringIO, file
+ assert_equal 'file.txt', file.original_filename
+ assert_equal "text/plain\r", file.content_type
+ assert_equal 'contents', file.read
+ end
+
+ def test_large_text_file
+ params = process('large_text_file')
+ assert_equal %w(file foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+
+ file = params['file']
+ assert_kind_of Tempfile, file
+ assert_equal 'file.txt', file.original_filename
+ assert_equal "text/plain\r", file.content_type
+ assert ('a' * 20480) == file.read
+ end
+
+ def test_binary_file
+ params = process('binary_file')
+ assert_equal %w(file flowers foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+
+ file = params['file']
+ assert_kind_of StringIO, file
+ assert_equal 'file.txt', file.original_filename
+ assert_equal "text/plain\r", file.content_type
+ assert_equal 'contents', file.read
+
+ file = params['flowers']
+ assert_kind_of StringIO, file
+ assert_equal 'flowers.jpg', file.original_filename
+ assert_equal "image/jpeg\r", file.content_type
+ assert_equal 19512, file.size
+ #assert_equal File.read(File.dirname(__FILE__) + '/../../../activerecord/test/fixtures/flowers.jpg'), file.read
+ end
+
+ def test_mixed_files
+ params = process('mixed_files')
+ assert_equal %w(files foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+
+ # Ruby CGI doesn't handle multipart/mixed for us.
+ assert_kind_of StringIO, params['files']
+ assert_equal 19756, params['files'].size
+ end
+
+ private
+ def process(name)
+ old_stdin = $stdin
+ File.open(File.join(FIXTURE_PATH, name), 'rb') do |file|
+ ENV['CONTENT_LENGTH'] = file.stat.size.to_s
+ $stdin = file
+ CGIMethods.parse_request_parameters CGI.new.params
+ end
+ ensure
+ $stdin = old_stdin
+ end
+end
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
Binary file not shown.
@@ -0,0 +1,5 @@
+--AaB03x
+Content-Disposition: form-data; name="foo"
+
+bar
+--AaB03x--
@@ -0,0 +1,10 @@
+--AaB03x
+Content-Disposition: form-data; name="foo"
+
+bar
+--AaB03x
+Content-Disposition: form-data; name="file"; filename="file.txt"
+Content-Type: text/plain
+
+contents
+--AaB03x--

0 comments on commit 5d19fd7

Please sign in to comment.