aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Lütke <tobias.luetke@gmail.com>2006-03-05 19:16:55 +0000
committerTobias Lütke <tobias.luetke@gmail.com>2006-03-05 19:16:55 +0000
commit0635f633ccfdef719e805de20b3b5b385bf72b57 (patch)
tree8ee1664aa17a70d9628108e45f214ecc57dd21a7
parent03d37a2d68c1940f65d5a65a51ae747955a5b075 (diff)
downloadrails-0635f633ccfdef719e805de20b3b5b385bf72b57.tar.gz
rails-0635f633ccfdef719e805de20b3b5b385bf72b57.tar.bz2
rails-0635f633ccfdef719e805de20b3b5b385bf72b57.zip
ActionController::Base.param_parsers now accept symbols. currently supported are :xml_node, :xml_simple and :yaml
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3778 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rwxr-xr-xactionpack/lib/action_controller/base.rb11
-rwxr-xr-xactionpack/lib/action_controller/cgi_ext/cgi_methods.rb14
-rw-r--r--actionpack/test/controller/webservice_test.rb22
3 files changed, 36 insertions, 11 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index ab8cc1802c..e4877f82fd 100755
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -278,16 +278,11 @@ module ActionController #:nodoc:
# 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
diff --git a/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb b/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
index 89f982f56f..feab806077 100755
--- a/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
+++ b/actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
@@ -59,7 +59,19 @@ class CGIMethods #:nodoc:
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 }
diff --git a/actionpack/test/controller/webservice_test.rb b/actionpack/test/controller/webservice_test.rb
index 844ac723ba..6275cfb6d0 100644
--- a/actionpack/test/controller/webservice_test.rb
+++ b/actionpack/test/controller/webservice_test.rb
@@ -27,6 +27,8 @@ class WebServiceTest < Test::Unit::TestCase
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 @@ class WebServiceTest < Test::Unit::TestCase
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')