Skip to content
This repository
Browse code

Don't convert params if the request isn't HTML - fixes #5341

(cherry picked from commit 7a80b69)

Conflicts:

	actionpack/test/controller/test_test.rb
  • Loading branch information...
commit d6bbd337ef1b96ed246bccaaf6e8b15a7077b9aa 1 parent beba826
Andrew White authored April 29, 2012
24  actionpack/lib/action_controller/test_case.rb
@@ -147,17 +147,23 @@ def assign_parameters(routes, controller_path, action, parameters = {})
147 147
       extra_keys = routes.extra_keys(parameters)
148 148
       non_path_parameters = get? ? query_parameters : request_parameters
149 149
       parameters.each do |key, value|
150  
-        if value.is_a? Fixnum
151  
-          value = value.to_s
152  
-        elsif value.is_a? Array
153  
-          value = Result.new(value.map { |v| v.is_a?(String) ? v.dup : v })
154  
-        elsif value.is_a? String
  150
+        if value.is_a?(Array) && (value.frozen? || value.any?(&:frozen?))
  151
+          value = value.map{ |v| v.duplicable? ? v.dup : v }
  152
+        elsif value.is_a?(Hash) && (value.frozen? || value.any?{ |k,v| v.frozen? })
  153
+          value = Hash[value.map{ |k,v| [k, v.duplicable? ? v.dup : v] }]
  154
+        elsif value.frozen? && value.duplicable?
155 155
           value = value.dup
156 156
         end
157 157
 
158 158
         if extra_keys.include?(key.to_sym)
159 159
           non_path_parameters[key] = value
160 160
         else
  161
+          if value.is_a?(Array)
  162
+            value = Result.new(value.map(&:to_param))
  163
+          else
  164
+            value = value.to_param
  165
+          end
  166
+
161 167
           path_parameters[key.to_s] = value
162 168
         end
163 169
       end
@@ -426,7 +432,7 @@ def paramify_values(hash_or_array_or_value)
426 432
       def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
427 433
         # Ensure that numbers and symbols passed as params are converted to
428 434
         # proper params, as is the case when engaging rack.
429  
-        parameters = paramify_values(parameters)
  435
+        parameters = paramify_values(parameters) if html_format?(parameters)
430 436
 
431 437
         # Sanity check for required instance variables so we can give an
432 438
         # understandable error message.
@@ -507,6 +513,12 @@ def build_request_uri(action, parameters)
507 513
           @request.env["QUERY_STRING"] = query_string || ""
508 514
         end
509 515
       end
  516
+
  517
+      def html_format?(parameters)
  518
+        return true unless parameters.is_a?(Hash)
  519
+        format = Mime[parameters[:format]]
  520
+        format.nil? || format.html?
  521
+      end
510 522
     end
511 523
 
512 524
     # When the request.remote_addr remains the default for testing, which is 0.0.0.0, the exception is simply raised inline
42  actionpack/test/controller/test_test.rb
@@ -46,6 +46,10 @@ def test_uri
46 46
       render :text => request.fullpath
47 47
     end
48 48
 
  49
+    def test_format
  50
+      render :text => request.format
  51
+    end
  52
+
49 53
     def test_query_string
50 54
       render :text => request.query_string
51 55
     end
@@ -527,14 +531,34 @@ def test_params_passing_with_fixnums
527 531
     )
528 532
   end
529 533
 
  534
+  def test_params_passing_with_fixnums_when_not_html_request
  535
+    get :test_params, :format => 'json', :count => 999
  536
+    parsed_params = eval(@response.body)
  537
+    assert_equal(
  538
+      {'controller' => 'test_test/test', 'action' => 'test_params',
  539
+       'format' => 'json', 'count' => 999 },
  540
+      parsed_params
  541
+    )
  542
+  end
  543
+
  544
+  def test_params_passing_path_parameter_is_string_when_not_html_request
  545
+    get :test_params, :format => 'json', :id => 1
  546
+    parsed_params = eval(@response.body)
  547
+    assert_equal(
  548
+      {'controller' => 'test_test/test', 'action' => 'test_params',
  549
+       'format' => 'json', 'id' => '1' },
  550
+      parsed_params
  551
+    )
  552
+  end
  553
+
530 554
   def test_params_passing_with_frozen_values
531 555
     assert_nothing_raised do
532  
-      get :test_params, :frozen => 'icy'.freeze, :frozens => ['icy'.freeze].freeze
  556
+      get :test_params, :frozen => 'icy'.freeze, :frozens => ['icy'.freeze].freeze, :deepfreeze => { :frozen => 'icy'.freeze }.freeze
533 557
     end
534 558
     parsed_params = eval(@response.body)
535 559
     assert_equal(
536 560
       {'controller' => 'test_test/test', 'action' => 'test_params',
537  
-       'frozen' => 'icy', 'frozens' => ['icy']},
  561
+       'frozen' => 'icy', 'frozens' => ['icy'], 'deepfreeze' => { 'frozen' => 'icy' }},
538 562
       parsed_params
539 563
     )
540 564
   end
@@ -635,6 +659,20 @@ def test_request_protocol_is_reset_after_request
635 659
     assert_equal "http://", @response.body
636 660
   end
637 661
 
  662
+  def test_request_format
  663
+    get :test_format, :format => 'html'
  664
+    assert_equal 'text/html', @response.body
  665
+
  666
+    get :test_format, :format => 'json'
  667
+    assert_equal 'application/json', @response.body
  668
+
  669
+    get :test_format, :format => 'xml'
  670
+    assert_equal 'application/xml', @response.body
  671
+
  672
+    get :test_format
  673
+    assert_equal 'text/html', @response.body
  674
+  end
  675
+
638 676
   def test_should_have_knowledge_of_client_side_cookie_state_even_if_they_are_not_set
639 677
     cookies['foo'] = 'bar'
640 678
     get :no_op

0 notes on commit d6bbd33

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