summary refs log tree commit
diff options
context:
space:
mode:
authorBrian Palmer <brianp@instructure.com>2014-04-05 10:56:15 -0600
committerSantiago Pastorino <santiago@wyeworks.com>2014-11-27 11:15:11 -0200
commita60492466576fa65090483f07404b28a6d658427 (patch)
treeac0a6dce01e7574cdcf795cd49790248623ab83f
parenta7cb4302dd4b3fe532799b8303af2e6a8ad0a85c (diff)
downloadrack-a60492466576fa65090483f07404b28a6d658427.tar.gz
add rack.multipart options to lint and SPEC
-rw-r--r--SPEC2
-rw-r--r--lib/rack/lint.rb17
-rw-r--r--test/spec_lint.rb18
3 files changed, 37 insertions, 0 deletions
diff --git a/SPEC b/SPEC
index 0deb57b5..5b665955 100644
--- a/SPEC
+++ b/SPEC
@@ -112,6 +112,8 @@ be implemented by the server.
                         warn(message, &block)
                         error(message, &block)
                         fatal(message, &block)
+<tt>rack.multipart.buffer_size</tt>:: An Integer hint to the multipart parser as to what chunk size to use for reads and writes.
+<tt>rack.multipart.tempfile_factory</tt>:: An object responding to #call with two arguments, the filename and content_type given for the multipart form field, and returning an IO-like object that responds to #<< and optionally #rewind. This factory will be used to instantiate the tempfile for each multipart form file upload field, rather than the default class of Tempfile.
 The server or the application can store their own data in the
 environment, too.  The keys must contain at least one dot,
 and should be prefixed uniquely.  The prefix <tt>rack.</tt>
diff --git a/lib/rack/lint.rb b/lib/rack/lint.rb
index 5cdef3f8..0fcdc5c4 100644
--- a/lib/rack/lint.rb
+++ b/lib/rack/lint.rb
@@ -228,6 +228,23 @@ module Rack
         }
       end
 
+      ## <tt>rack.multipart.buffer_size</tt>:: An Integer hint to the multipart parser as to what chunk size to use for reads and writes.
+      if bufsize = env['rack.multipart.buffer_size']
+        assert("rack.multipart.buffer_size must be an Integer > 0 if specified") {
+          bufsize.is_a?(Integer) && bufsize > 0
+        }
+      end
+
+      ## <tt>rack.multipart.tempfile_factory</tt>:: An object responding to #call with two arguments, the filename and content_type given for the multipart form field, and returning an IO-like object that responds to #<< and optionally #rewind. This factory will be used to instantiate the tempfile for each multipart form file upload field, rather than the default class of Tempfile.
+      if tempfile_factory = env['rack.multipart.tempfile_factory']
+        assert("rack.multipart.tempfile_factory must respond to #call") { tempfile_factory.respond_to?(:call) }
+        env['rack.multipart.tempfile_factory'] = lambda do |filename, content_type|
+          io = tempfile_factory.call(filename, content_type)
+          assert("rack.multipart.tempfile_factory return value must respond to #<<") { io.respond_to?(:<<) }
+          io
+        end
+      end
+
       ## The server or the application can store their own data in the
       ## environment, too.  The keys must contain at least one dot,
       ## and should be prefixed uniquely.  The prefix <tt>rack.</tt>
diff --git a/test/spec_lint.rb b/test/spec_lint.rb
index fb60b7ef..4686a509 100644
--- a/test/spec_lint.rb
+++ b/test/spec_lint.rb
@@ -1,4 +1,5 @@
 require 'stringio'
+require 'tempfile'
 require 'rack/lint'
 require 'rack/mock'
 
@@ -75,6 +76,23 @@ describe Rack::Lint do
       message.should.equal("logger [] must respond to info")
 
     lambda {
+      Rack::Lint.new(nil).call(env("rack.multipart.buffer_size" => 0))
+    }.should.raise(Rack::Lint::LintError).
+      message.should.equal("rack.multipart.buffer_size must be an Integer > 0 if specified")
+
+    lambda {
+      Rack::Lint.new(nil).call(env("rack.multipart.tempfile_factory" => Tempfile))
+    }.should.raise(Rack::Lint::LintError).
+      message.should.equal("rack.multipart.tempfile_factory must respond to #call")
+
+    lambda {
+      Rack::Lint.new(lambda { |env|
+        env['rack.multipart.tempfile_factory'].call("testfile", "text/plain")
+      }).call(env("rack.multipart.tempfile_factory" => lambda { |filename, content_type| Object.new }))
+    }.should.raise(Rack::Lint::LintError).
+      message.should.equal("rack.multipart.tempfile_factory return value must respond to #<<")
+
+    lambda {
       Rack::Lint.new(nil).call(env("REQUEST_METHOD" => "FUCKUP?"))
     }.should.raise(Rack::Lint::LintError).
       message.should.match(/REQUEST_METHOD/)