Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor request multipart params parsing tests

  • Loading branch information...
commit 92dbf5ba832c2c4d8f6fda8b151090069cd701f3 1 parent 40a75a5
@josh josh authored
View
65 actionpack/test/controller/integration_upload_test.rb
@@ -1,65 +0,0 @@
-require 'abstract_unit'
-
-unless defined? ApplicationController
- class ApplicationController < ActionController::Base
- end
-end
-
-class UploadTestController < ActionController::Base
- def update
- SessionUploadTest.last_request_type = ActionController::Base.param_parsers[request.content_type]
- render :text => "got here"
- end
-
- def read
- render :text => "File: #{params[:uploaded_data].read}"
- end
-end
-
-class SessionUploadTest < ActionController::IntegrationTest
- FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart'
-
- class << self
- attr_accessor :last_request_type
- end
-
- def test_upload_and_read_file
- with_test_routing do
- post '/read', :uploaded_data => fixture_file_upload(FILES_DIR + "/hello.txt", "text/plain")
- assert_equal "File: Hello", response.body
- end
- end
-
- # The lint wrapper is used in integration tests
- # instead of a normal StringIO class
- InputWrapper = Rack::Lint::InputWrapper
-
- def test_post_with_upload_with_unrewindable_input
- InputWrapper.any_instance.expects(:rewind).raises(Errno::ESPIPE)
-
- with_test_routing do
- post '/read', :uploaded_data => fixture_file_upload(FILES_DIR + "/hello.txt", "text/plain")
- assert_equal "File: Hello", response.body
- end
- end
-
- def test_post_with_upload_with_params_parsing
- with_test_routing do
- params = { :uploaded_data => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") }
- post '/update', params, :location => 'blah'
- assert_equal(:multipart_form, SessionUploadTest.last_request_type)
- end
- end
-
- private
- def with_test_routing
- with_routing do |set|
- set.draw do |map|
- map.update 'update', :controller => "upload_test", :action => "update", :method => :post
- map.read 'read', :controller => "upload_test", :action => "read", :method => :post
- end
-
- yield
- end
- end
-end
View
167 actionpack/test/controller/request/multipart_params_parsing_test.rb
@@ -0,0 +1,167 @@
+require 'abstract_unit'
+
+class MultipartParamsParsingTest < ActionController::IntegrationTest
+ class TestController < ActionController::Base
+ class << self
+ attr_accessor :last_request_parameters, :last_request_type
+ end
+
+ def parse
+ self.class.last_request_type = ActionController::Base.param_parsers[request.content_type]
+ self.class.last_request_parameters = request.request_parameters
+ head :ok
+ end
+
+ def read
+ render :text => "File: #{params[:uploaded_data].read}"
+ end
+ end
+
+ FIXTURE_PATH = File.dirname(__FILE__) + '/../../fixtures/multipart'
+
+ def teardown
+ TestController.last_request_parameters = nil
+ TestController.last_request_type = nil
+ end
+
+ test "parses single parameter" do
+ assert_equal({ 'foo' => 'bar' }, parse_multipart('single_parameter'))
+ end
+
+ test "parses bracketed parameters" do
+ assert_equal({ 'foo' => { 'baz' => 'bar'}}, parse_multipart('bracketed_param'))
+ end
+
+ test "parses text file" do
+ params = parse_multipart('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", file.content_type
+ assert_equal 'contents', file.read
+ end
+
+ test "parses boundary problem file" do
+ params = parse_multipart('boundary_problem_file')
+ assert_equal %w(file foo), params.keys.sort
+
+ file = params['file']
+ foo = params['foo']
+
+ assert_kind_of Tempfile, file
+
+ assert_equal 'file.txt', file.original_filename
+ assert_equal "text/plain", file.content_type
+
+ assert_equal 'bar', foo
+ end
+
+ test "parses large text file" do
+ params = parse_multipart('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", file.content_type
+ assert ('a' * 20480) == file.read
+ end
+
+ test "parses binary file" do
+ params = parse_multipart('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.csv', file.original_filename
+ assert_nil 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", file.content_type
+ assert_equal 19512, file.size
+ end
+
+ test "parses mixed files" do
+ params = parse_multipart('mixed_files')
+ assert_equal %w(files foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+
+ # Ruby CGI doesn't handle multipart/mixed for us.
+ files = params['files']
+ assert_kind_of String, files
+ files.force_encoding('ASCII-8BIT') if files.respond_to?(:force_encoding)
+ assert_equal 19756, files.size
+ end
+
+ test "uploads and parses parameters" do
+ with_test_routing do
+ params = { :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/mona_lisa.jpg", "image/jpg") }
+ post '/parse', params, :location => 'blah'
+ assert_equal(:multipart_form, TestController.last_request_type)
+ end
+ end
+
+ test "uploads and reads file" do
+ with_test_routing do
+ post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain")
+ assert_equal "File: Hello", response.body
+ end
+ end
+
+ # The lint wrapper is used in integration tests
+ # instead of a normal StringIO class
+ InputWrapper = Rack::Lint::InputWrapper
+
+ test "parses unwindable stream" do
+ InputWrapper.any_instance.expects(:rewind).raises(Errno::ESPIPE)
+ params = parse_multipart('large_text_file')
+ assert_equal %w(file foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+ end
+
+ test "uploads and reads file with unwindable input" do
+ InputWrapper.any_instance.expects(:rewind).raises(Errno::ESPIPE)
+
+ with_test_routing do
+ post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain")
+ assert_equal "File: Hello", response.body
+ end
+ end
+
+ private
+ def fixture(name)
+ File.open(File.join(FIXTURE_PATH, name), 'rb') do |file|
+ { "rack.input" => file.read,
+ "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
+ "CONTENT_LENGTH" => file.stat.size.to_s }
+ end
+ end
+
+ def parse_multipart(name)
+ with_test_routing do
+ headers = fixture(name)
+ post "/parse", headers.delete("rack.input"), headers
+ assert_response :ok
+ TestController.last_request_parameters
+ end
+ end
+
+ def with_test_routing
+ with_routing do |set|
+ set.draw do |map|
+ map.connect ':action', :controller => "multipart_params_parsing_test/test"
+ end
+ yield
+ end
+ end
+end
View
104 actionpack/test/controller/request_test.rb
@@ -606,107 +606,3 @@ def test_parse_params_with_complex_nesting
assert_equal expected, ActionController::RequestParser.parse_request_parameters(input)
end
end
-
-class MultipartRequestParameterParsingTest < ActiveSupport::TestCase
- FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart'
-
- def test_single_parameter
- params = parse_multipart('single_parameter')
- assert_equal({ 'foo' => 'bar' }, params)
- end
-
- def test_bracketed_param
- assert_equal({ 'foo' => { 'baz' => 'bar'}}, parse_multipart('bracketed_param'))
- end
-
- def test_text_file
- params = parse_multipart('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", file.content_type
- assert_equal 'contents', file.read
- end
-
- def test_boundary_problem_file
- params = parse_multipart('boundary_problem_file')
- assert_equal %w(file foo), params.keys.sort
-
- file = params['file']
- foo = params['foo']
-
- assert_kind_of Tempfile, file
-
- assert_equal 'file.txt', file.original_filename
- assert_equal "text/plain", file.content_type
-
- assert_equal 'bar', foo
- end
-
- def test_large_text_file
- params = parse_multipart('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", file.content_type
- assert ('a' * 20480) == file.read
- end
-
- uses_mocha "test_no_rewind_stream" do
- def test_no_rewind_stream
- # Ensures that parse_multipart_form_parameters works with streams that cannot be rewound
- file = File.open(File.join(FIXTURE_PATH, 'large_text_file'), 'rb')
- file.expects(:rewind).raises(Errno::ESPIPE)
- params = ActionController::RequestParser.parse_multipart_form_parameters(file, 'AaB03x', file.stat.size, {})
- assert_not_equal 0, file.pos # file was not rewound after reading
- end
- end
-
- def test_binary_file
- params = parse_multipart('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.csv', file.original_filename
- assert_nil 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", 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 = parse_multipart('mixed_files')
- assert_equal %w(files foo), params.keys.sort
- assert_equal 'bar', params['foo']
-
- # Ruby CGI doesn't handle multipart/mixed for us.
- files = params['files']
- assert_kind_of String, files
- files.force_encoding('ASCII-8BIT') if files.respond_to?(:force_encoding)
- assert_equal 19756, files.size
- end
-
- private
- def parse_multipart(name)
- File.open(File.join(FIXTURE_PATH, name), 'rb') do |file|
- params = ActionController::RequestParser.parse_multipart_form_parameters(file, 'AaB03x', file.stat.size, {})
- assert_equal 0, file.pos # file was rewound after reading
- params
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.