From a60492466576fa65090483f07404b28a6d658427 Mon Sep 17 00:00:00 2001 From: Brian Palmer Date: Sat, 5 Apr 2014 10:56:15 -0600 Subject: add rack.multipart options to lint and SPEC --- SPEC | 2 ++ lib/rack/lint.rb | 17 +++++++++++++++++ test/spec_lint.rb | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+) 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) +rack.multipart.buffer_size:: An Integer hint to the multipart parser as to what chunk size to use for reads and writes. +rack.multipart.tempfile_factory:: 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 rack. 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 + ## rack.multipart.buffer_size:: 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 + + ## rack.multipart.tempfile_factory:: 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 rack. 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' @@ -74,6 +75,23 @@ describe Rack::Lint do }.should.raise(Rack::Lint::LintError). 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). -- cgit v1.2.3-24-ge0c7