summary refs log tree commit
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-04-28 00:36:35 -0500
committerJoshua Peek <josh@joshpeek.com>2009-04-28 00:36:35 -0500
commit5a2eae5c8533812d9ddaf7e14ecd295922da8155 (patch)
tree95514673895ce7197a9464b6c462feff561e1b23
parent4c6efcad787683fb9e93ac490628a33f86b7c9f9 (diff)
downloadrack-5a2eae5c8533812d9ddaf7e14ecd295922da8155.tar.gz
Utils::Multipart.build_multipart returns nil if no UploadedFiles are included
-rw-r--r--lib/rack/mock.rb19
-rw-r--r--lib/rack/utils.rb20
-rw-r--r--test/spec_rack_utils.rb11
3 files changed, 34 insertions, 16 deletions
diff --git a/lib/rack/mock.rb b/lib/rack/mock.rb
index bae6c5bd..079efac0 100644
--- a/lib/rack/mock.rb
+++ b/lib/rack/mock.rb
@@ -101,22 +101,9 @@ module Rack
         elsif !opts.has_key?(:input)
           opts["CONTENT_TYPE"] = "application/x-www-form-urlencoded"
           if params.is_a?(Hash)
-            multipart = false
-            query = lambda { |value|
-              case value
-              when Array
-                value.each(&query)
-              when Hash
-                value.values.each(&query)
-              when Utils::Multipart::UploadedFile
-                multipart = true
-              end
-            }
-            opts[:params].values.each(&query)
-
-            if multipart
-              opts[:input] = Utils::Multipart.build_multipart(params)
-              opts["CONTENT_LENGTH"] ||= opts[:input].length.to_s
+            if data = Utils::Multipart.build_multipart(params)
+              opts[:input] = data
+              opts["CONTENT_LENGTH"] ||= data.length.to_s
               opts["CONTENT_TYPE"] = "multipart/form-data; boundary=#{Utils::Multipart::MULTIPART_BOUNDARY}"
             else
               opts[:input] = Utils.build_nested_query(params)
diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb
index 767969cc..631a38f5 100644
--- a/lib/rack/utils.rb
+++ b/lib/rack/utils.rb
@@ -441,6 +441,26 @@ module Rack
       end
 
       def self.build_multipart(params, first = true)
+        if first
+          unless params.is_a?(Hash)
+            raise ArgumentError, "value must be a Hash"
+          end
+
+          multipart = false
+          query = lambda { |value|
+            case value
+            when Array
+              value.each(&query)
+            when Hash
+              value.values.each(&query)
+            when UploadedFile
+              multipart = true
+            end
+          }
+          params.values.each(&query)
+          return nil unless multipart
+        end
+
         flattened_params = Hash.new
 
         params.each do |key, value|
diff --git a/test/spec_rack_utils.rb b/test/spec_rack_utils.rb
index 5abeaf42..cdf25c37 100644
--- a/test/spec_rack_utils.rb
+++ b/test/spec_rack_utils.rb
@@ -405,6 +405,17 @@ context "Rack::Utils::Multipart" do
     params["people"][0]["files"][:tempfile].read.should.equal "contents"
   end
 
+  specify "should return nil if no UploadedFiles were used" do
+    data = Rack::Utils::Multipart.build_multipart("people" => [{"submit-name" => "Larry", "files" => "contents"}])
+    data.should.equal nil
+  end
+
+  specify "should raise ArgumentError if params is not a Hash" do
+    lambda { Rack::Utils::Multipart.build_multipart("foo=bar") }.
+      should.raise(ArgumentError).
+      message.should.equal "value must be a Hash"
+  end
+
   private
     def multipart_fixture(name)
       file = multipart_file(name)