From f73e1c2ec0711782870b7935da84b40596ad3577 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 15 May 2007 22:10:03 +0000 Subject: Move request parameter parsing from CGI to AbstractRequest. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6742 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_controller/cgi_ext/parameters.rb | 14 --------- actionpack/lib/action_controller/cgi_process.rb | 2 +- actionpack/lib/action_controller/request.rb | 17 ++++++++++ actionpack/test/controller/cgi_test.rb | 35 +-------------------- actionpack/test/controller/request_test.rb | 36 ++++++++++++++++++++++ 5 files changed, 55 insertions(+), 49 deletions(-) diff --git a/actionpack/lib/action_controller/cgi_ext/parameters.rb b/actionpack/lib/action_controller/cgi_ext/parameters.rb index a7f5393272..d84d0e72e8 100644 --- a/actionpack/lib/action_controller/cgi_ext/parameters.rb +++ b/actionpack/lib/action_controller/cgi_ext/parameters.rb @@ -62,20 +62,6 @@ module ActionController parser.result end - def parse_formatted_request_parameters(mime_type, body) - case strategy = ActionController::Base.param_parsers[mime_type] - when Proc - strategy.call(body) - when :xml_simple, :xml_node - body.blank? ? {} : Hash.from_xml(body).with_indifferent_access - when :yaml - YAML.load(body) - end - rescue Exception => e # YAML, XML or Ruby code block errors - { "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace, - "body" => body, "format" => mime_type } - end - private def get_typed_value(value) case value diff --git a/actionpack/lib/action_controller/cgi_process.rb b/actionpack/lib/action_controller/cgi_process.rb index ee6e5ca250..13467be8ef 100644 --- a/actionpack/lib/action_controller/cgi_process.rb +++ b/actionpack/lib/action_controller/cgi_process.rb @@ -76,7 +76,7 @@ module ActionController #:nodoc: def request_parameters @request_parameters ||= if ActionController::Base.param_parsers.has_key?(content_type) - CGI.parse_formatted_request_parameters(content_type, body.read) + self.class.parse_formatted_request_parameters(content_type, body.read) else CGI.parse_request_parameters(@cgi.params) end diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb index 3ed8c2f08d..6a9b74d426 100755 --- a/actionpack/lib/action_controller/request.rb +++ b/actionpack/lib/action_controller/request.rb @@ -295,5 +295,22 @@ module ActionController def reset_session #:nodoc: end + + + def self.parse_formatted_request_parameters(mime_type, body) + case strategy = ActionController::Base.param_parsers[mime_type] + when Proc + strategy.call(body) + when :xml_simple, :xml_node + body.blank? ? {} : Hash.from_xml(body).with_indifferent_access + when :yaml + YAML.load(body) + else + {} + end + rescue Exception => e # YAML, XML or Ruby code block errors + { "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace, + "body" => body, "format" => mime_type } + end end end diff --git a/actionpack/test/controller/cgi_test.rb b/actionpack/test/controller/cgi_test.rb index 877423a0d1..b8535548b7 100755 --- a/actionpack/test/controller/cgi_test.rb +++ b/actionpack/test/controller/cgi_test.rb @@ -274,35 +274,6 @@ class CGITest < Test::Unit::TestCase end -class XmlCGITest < Test::Unit::TestCase - def test_single_file - raw_post_data = - "David#{Base64.encode64('ABC')}" - person = CGI.parse_formatted_request_parameters(Mime::XML, raw_post_data) - assert_equal "image/jpg", person['person']['avatar'].content_type - assert_equal "me.jpg", person['person']['avatar'].original_filename - assert_equal "ABC", person['person']['avatar'].read - end - - def test_multiple_files - raw_post_data = - "David" + - "#{Base64.encode64('ABC')}" + - "#{Base64.encode64('DEF')}" + - "" - person = CGI.parse_formatted_request_parameters(Mime::XML, raw_post_data) - - assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type - assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename - assert_equal "ABC", person['person']['avatars']['avatar'].first.read - - assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type - assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename - assert_equal "DEF", person['person']['avatars']['avatar'].last.read - end -end - - class MultipartCGITest < Test::Unit::TestCase FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart' @@ -379,15 +350,11 @@ class MultipartCGITest < Test::Unit::TestCase 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 - @cgi = CGI.new + @cgi = CGI.new('query', file) CGI.parse_request_parameters @cgi.params end - ensure - $stdin = old_stdin end end diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb index b939f6f975..9da2cf485f 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -355,3 +355,39 @@ class RequestTest < Test::Unit::TestCase @request.instance_eval { @request_method = nil } end end + + +class RequestParameterParsingTest < Test::Unit::TestCase + def test_xml_with_single_file + body = "David#{Base64.encode64('ABC')}" + + person = ActionController::AbstractRequest.parse_formatted_request_parameters(Mime::XML, body) + + assert_equal "image/jpg", person['person']['avatar'].content_type + assert_equal "me.jpg", person['person']['avatar'].original_filename + assert_equal "ABC", person['person']['avatar'].read + end + + def test_xml_with_multiple_files + body = <<-end_body + + David + + #{Base64.encode64('ABC')} + #{Base64.encode64('DEF')} + + + end_body + + person = ActionController::AbstractRequest.parse_formatted_request_parameters(Mime::XML, body) + + assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type + assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename + assert_equal "ABC", person['person']['avatars']['avatar'].first.read + + assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type + assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename + assert_equal "DEF", person['person']['avatars']['avatar'].last.read + end +end + -- cgit v1.2.3