Skip to content
This repository
Browse code

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...
commit 0635f633ccfdef719e805de20b3b5b385bf72b57 1 parent 03d37a2
authored March 05, 2006
11  actionpack/lib/action_controller/base.rb
@@ -278,16 +278,11 @@ class Base
278 278
     # behavior you can re-register XmlSimple as application/xml handler and enable application/x-yaml like 
279 279
     # this:
280 280
     #
281  
-    #   ActionController::Base.param_parsers['application/xml'] = Proc.new do |data| 
282  
-    #     XmlSimple.xml_in(data, 'ForceArray' => false)
283  
-    #   end
284  
-    #
285  
-    #   ActionController::Base.param_parsers['application/x-yaml'] = Proc.new do |data| 
286  
-    #     |post| YAML.load(post)
287  
-    #   end
  281
+    #   ActionController::Base.param_parsers['application/xml'] = :xml_simple
  282
+    #   ActionController::Base.param_parsers['application/x-yaml'] = :yaml
288 283
     #
289 284
     @@param_parsers = {
290  
-      'application/xml'     => Proc.new { |post| node = XmlNode.from_xml(post); { node.node_name => node } },
  285
+      'application/xml'     => :xml_node
291 286
     }
292 287
     cattr_accessor :param_parsers 
293 288
 
14  actionpack/lib/action_controller/cgi_ext/cgi_methods.rb
@@ -59,7 +59,19 @@ def CGIMethods.parse_request_parameters(params)
59 59
     end
60 60
 
61 61
     def self.parse_formatted_request_parameters(format, raw_post_data)
62  
-      ActionController::Base.param_parsers[format].call(raw_post_data) || {}
  62
+      params = case strategy = ActionController::Base.param_parsers[format]
  63
+      when Proc
  64
+        strategy.call(raw_post_data)
  65
+      when :xml_node
  66
+        node = XmlNode.from_xml(raw_post_data)
  67
+        { node.node_name => node }
  68
+      when :xml_simple
  69
+        XmlSimple.xml_in(raw_post_data, 'ForceArray' => false)
  70
+      when :yaml
  71
+        YAML.load(raw_post_data)
  72
+      end
  73
+      
  74
+      params || {}
63 75
     rescue Object => e
64 76
       { "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace, 
65 77
         "raw_post_data" => raw_post_data, "format" => format }
22  actionpack/test/controller/webservice_test.rb
@@ -27,6 +27,8 @@ def rescue_action(e) raise end
27 27
   
28 28
   def setup
29 29
     @controller = TestController.new
  30
+    ActionController::Base.param_parsers.clear
  31
+    ActionController::Base.param_parsers['application/xml'] = :xml_node
30 32
   end
31 33
   
32 34
   def test_check_parameters
@@ -55,12 +57,28 @@ def test_put_xml
55 57
   def test_register_and_use_yaml
56 58
     ActionController::Base.param_parsers['application/x-yaml'] = Proc.new { |d| YAML.load(d) }
57 59
     process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml)
  60
+    assert_equal 'entry', @controller.response.body
58 61
     assert @controller.params.has_key?(:entry)
59 62
     assert_equal 'loaded from yaml', @controller.params["entry"]
60  
-  ensure
61  
-    ActionController::Base.param_parsers['application/x-yaml'] = nil
62 63
   end
63 64
   
  65
+  def test_register_and_use_yaml_as_symbol
  66
+    ActionController::Base.param_parsers['application/x-yaml'] = :yaml
  67
+    process('POST', 'application/x-yaml', {"entry" => "loaded from yaml"}.to_yaml)
  68
+    assert_equal 'entry', @controller.response.body
  69
+    assert @controller.params.has_key?(:entry)
  70
+    assert_equal 'loaded from yaml', @controller.params["entry"]
  71
+  end
  72
+
  73
+  def test_register_and_use_xml_simple
  74
+    ActionController::Base.param_parsers['application/xml'] = :xml_simple
  75
+    process('POST', 'application/xml', '<request><summary>content...</summary><title>SimpleXml</title></request>' )
  76
+    assert_equal 'summary, title', @controller.response.body
  77
+    assert @controller.params.has_key?(:summary)
  78
+    assert @controller.params.has_key?(:title)
  79
+    assert_equal 'content...', @controller.params["summary"]
  80
+    assert_equal 'SimpleXml', @controller.params["title"]
  81
+  end
64 82
   
65 83
   def test_deprecated_request_methods
66 84
     process('POST', 'application/x-yaml')

0 notes on commit 0635f63

Please sign in to comment.
Something went wrong with that request. Please try again.