Skip to content

Commit

Permalink
Merge pull request #1 from econsultancy/econsultancy-20110708
Browse files Browse the repository at this point in the history
Fix support for params with arrays of hashes in multipart forms.
  • Loading branch information
h-lame committed Dec 14, 2012
2 parents 1b1e730 + 03683cd commit e69dbd2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rack/test/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def build_multipart(params, first = true)

if (v.is_a?(Hash))
build_multipart(v, false).each { |subkey, subvalue|
flattened_params["#{k}[]#{subkey}"] = subvalue
flattened_params["#{k}[]#{subkey}"] ||= []
flattened_params["#{k}[]#{subkey}"] << subvalue
}
else
flattened_params["#{k}[]"] = value
Expand Down
16 changes: 16 additions & 0 deletions spec/rack/test/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@
check params["foo"].should == ["1", "2"]
end

it "builds nested multipart bodies with an array of hashes" do
files = Rack::Test::UploadedFile.new(multipart_file("foo.txt"))
data = build_multipart("files" => files, "foo" => [{"id" => "1"}, {"id" => "2"}])

options = {
"CONTENT_TYPE" => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
"CONTENT_LENGTH" => data.length.to_s,
:input => StringIO.new(data)
}
env = Rack::MockRequest.env_for("/", options)
params = Rack::Utils::Multipart.parse_multipart(env)
check params["files"][:filename].should == "foo.txt"
params["files"][:tempfile].read.should == "bar\n"
check params["foo"].should == [{"id" => "1"}, {"id" => "2"}]
end

it "returns nil if no UploadedFiles were used" do
data = build_multipart("people" => [{"submit-name" => "Larry", "files" => "contents"}])
data.should be_nil
Expand Down

0 comments on commit e69dbd2

Please sign in to comment.