Skip to content
Browse files

Fix for integration tests not serializing arrays in multipart forms c…

…orrectly.

Signed-off-by: wycats <wycats@gmail.com>
  • Loading branch information...
1 parent 4ae4828 commit fb615cd7fd5eb81308edf095c00e576cddb0c7e7 @jyurek jyurek committed with wycats Jun 4, 2010
View
17 actionpack/lib/action_controller/integration.rb
@@ -414,15 +414,25 @@ def requestify(parameters, prefix=nil)
end
def multipart_requestify(params, first=true)
- returning Hash.new do |p|
+ returning Array.new do |p|
params.each do |key, value|
k = first ? key.to_s : "[#{key.to_s}]"
if Hash === value
multipart_requestify(value, false).each do |subkey, subvalue|
- p[k + subkey] = subvalue
+ p << [k + subkey, subvalue]
+ end
+ elsif Array === value
+ value.each do |element|
+ if Hash === element || Array === element
+ multipart_requestify(element, false).each do |subkey, subvalue|
+ p << ["#{k}[]#{subkey}", subvalue]
+ end
+ else
+ p << ["#{k}[]", element]
+ end
end
else
- p[k] = value
+ p << [k, value]
end
end
end
@@ -453,6 +463,7 @@ def multipart_body(params, boundary)
end
end.join("")+"--#{boundary}--\r"
end
+
end
# A module used to extend ActionController::Base, so that integration tests
View
26 actionpack/test/controller/integration_test.rb
@@ -266,6 +266,14 @@ def multipart_post_with_multiparameter_params
render :text => "foo(1i): #{params[:"foo(1i)"]}, foo(2i): #{params[:"foo(2i)"]}, filesize: #{params[:file].size}", :status => 200
end
+ def multipart_post_with_nested_params
+ render :text => "foo: #{params[:foo][0]}, #{params[:foo][1]}; [filesize: #{params[:file_list][0][:content].size}, filesize: #{params[:file_list][1][:content].size}]", :status => 200
+ end
+
+ def multipart_post_with_multiparameter_complex_params
+ render :text => "foo(1i): #{params[:"foo(1i)"]}, foo(2i): #{params[:"foo(2i)"]}, [filesize: #{params[:file_list][0][:content].size}, filesize: #{params[:file_list][1][:content].size}]", :status => 200
+ end
+
def post
render :text => "Created", :status => 201
end
@@ -405,6 +413,24 @@ def test_multipart_post_with_multiparameter_attribute_parameters
end
end
+ def test_multipart_post_with_nested_params
+ with_test_route_set do
+ post '/multipart_post_with_nested_params', :"foo" => ['a', 'b'], :file_list => [{:content => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")}, {:content => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")}]
+
+ assert_equal 200, status
+ assert_equal "foo: a, b; [filesize: 159528, filesize: 159528]", response.body
+ end
+ end
+
+ def test_multipart_post_with_multiparameter_complex_attribute_parameters
+ with_test_route_set do
+ post '/multipart_post_with_multiparameter_complex_params', :"foo(1i)" => "bar", :"foo(2i)" => "baz", :file_list => [{:content => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")}, {:content => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")}]
+
+ assert_equal 200, status
+ assert_equal "foo(1i): bar, foo(2i): baz, [filesize: 159528, filesize: 159528]", response.body
+ end
+ end
+
def test_head
with_test_route_set do
head '/get'
View
15 actionpack/test/controller/request/multipart_params_parsing_test.rb
@@ -14,6 +14,10 @@ def parse
def read
render :text => "File: #{params[:uploaded_data].read}"
end
+
+ def read_complex
+ render :text => "File: #{params[:level0][:level1][0][:file_data].read}"
+ end
end
FIXTURE_PATH = File.dirname(__FILE__) + '/../../fixtures/multipart'
@@ -133,6 +137,17 @@ def teardown
end
end
+ test "uploads and reads file in complex parameter" do
+ with_test_routing do
+ post '/read_complex',
+ :level0 => {
+ :level1 => [ { :file_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain") }
+ ]
+ }
+ assert_equal "File: Hello", response.body
+ end
+ end
+
private
def fixture(name)
File.open(File.join(FIXTURE_PATH, name), 'rb') do |file|

0 comments on commit fb615cd

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