Permalink
Browse files

ActionController::Base.param_parsers now accept symbols. currently su…

…pported are :xml_node, :xml_simple and :yaml

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3778 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
Tobias Lütke
Tobias Lütke committed Mar 5, 2006
1 parent 03d37a2 commit 0635f633ccfdef719e805de20b3b5b385bf72b57
@@ -278,16 +278,11 @@ class Base
# behavior you can re-register XmlSimple as application/xml handler and enable application/x-yaml like
# this:
#
- # ActionController::Base.param_parsers['application/xml'] = Proc.new do |data|
- # XmlSimple.xml_in(data, 'ForceArray' => false)
- # end
- #
- # ActionController::Base.param_parsers['application/x-yaml'] = Proc.new do |data|
- # |post| YAML.load(post)
- # end
+ # ActionController::Base.param_parsers['application/xml'] = :xml_simple
+ # ActionController::Base.param_parsers['application/x-yaml'] = :yaml
#
@@param_parsers = {
- 'application/xml' => Proc.new { |post| node = XmlNode.from_xml(post); { node.node_name => node } },
+ 'application/xml' => :xml_node
}
cattr_accessor :param_parsers
@@ -59,7 +59,19 @@ def CGIMethods.parse_request_parameters(params)
end
def self.parse_formatted_request_parameters(format, raw_post_data)
- ActionController::Base.param_parsers[format].call(raw_post_data) || {}
+ params = case strategy = ActionController::Base.param_parsers[format]
+ when Proc
+ strategy.call(raw_post_data)
+ when :xml_node
+ node = XmlNode.from_xml(raw_post_data)
+ { node.node_name => node }
+ when :xml_simple
+ XmlSimple.xml_in(raw_post_data, 'ForceArray' => false)
+ when :yaml
+ YAML.load(raw_post_data)
+ end
+
+ params || {}
rescue Object => e
{ "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace,
"raw_post_data" => raw_post_data, "format" => format }
@@ -27,6 +27,8 @@ def rescue_action(e) raise end
def setup
@controller = TestController.new
+ ActionController::Base.param_parsers.clear
+ ActionController::Base.param_parsers['application/xml'] = :xml_node
end
def test_check_parameters
@@ -55,12 +57,28 @@ def test_put_xml
def test_register_and_use_yaml
ActionController::Base.param_parsers['application/x-yaml'] = Proc.new { |d| YAML.load(d) }
process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml)
+ assert_equal 'entry', @controller.response.body
assert @controller.params.has_key?(:entry)
assert_equal 'loaded from yaml', @controller.params["entry"]
- ensure
- ActionController::Base.param_parsers['application/x-yaml'] = nil
end
+ def test_register_and_use_yaml_as_symbol
+ ActionController::Base.param_parsers['application/x-yaml'] = :yaml
+ process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml)
+ assert_equal 'entry', @controller.response.body
+ assert @controller.params.has_key?(:entry)
+ assert_equal 'loaded from yaml', @controller.params["entry"]
+ end
+
+ def test_register_and_use_xml_simple
+ ActionController::Base.param_parsers['application/xml'] = :xml_simple
+ process('POST', 'application/xml', '<request><summary>content...</summary><title>SimpleXml</title></request>' )
+ assert_equal 'summary, title', @controller.response.body
+ assert @controller.params.has_key?(:summary)
+ assert @controller.params.has_key?(:title)
+ assert_equal 'content...', @controller.params["summary"]
+ assert_equal 'SimpleXml', @controller.params["title"]
+ end
def test_deprecated_request_methods
process('POST', 'application/x-yaml')

0 comments on commit 0635f63

Please sign in to comment.