summary refs log tree commit
diff options
context:
space:
mode:
authorJanko Marohnić <janko.marohnic@gmail.com>2017-04-29 10:11:14 +1000
committerJanko Marohnić <janko.marohnic@gmail.com>2017-05-01 01:41:39 +1000
commit25ca3e5ebfc761041d3b5f375ad62d507c8dd14f (patch)
tree0db94dc378ab06a753d02c8eb6128dbc55d71e7d
parentc6cf1f69f0591e4fbe0882342a70588d1ceb6da0 (diff)
downloadrack-25ca3e5ebfc761041d3b5f375ad62d507c8dd14f.tar.gz
Don't require input to respond to #size in MockRequest.env_for
The #size method isn't part of the Rack input specification, so
technically the given Rack input object doesn't have to respond to
\#size.

This allows us to test our Rack applications with Rack input that
doesn't respond to #size, verifying that we're not relying on it.
-rw-r--r--lib/rack/mock.rb2
-rw-r--r--test/spec_mock.rb3
-rw-r--r--test/spec_multipart.rb7
3 files changed, 5 insertions, 7 deletions
diff --git a/lib/rack/mock.rb b/lib/rack/mock.rb
index d2ef2659..c35c00e2 100644
--- a/lib/rack/mock.rb
+++ b/lib/rack/mock.rb
@@ -139,7 +139,7 @@ module Rack
       rack_input.set_encoding(Encoding::BINARY)
       env[RACK_INPUT] = rack_input
 
-      env["CONTENT_LENGTH"] ||= env[RACK_INPUT].size.to_s
+      env["CONTENT_LENGTH"] ||= env[RACK_INPUT].size.to_s if env[RACK_INPUT].respond_to?(:size)
 
       opts.each { |field, value|
         env[field] = value  if String === field
diff --git a/test/spec_mock.rb b/test/spec_mock.rb
index 6e5555d1..d6e1028e 100644
--- a/test/spec_mock.rb
+++ b/test/spec_mock.rb
@@ -90,6 +90,9 @@ describe Rack::MockRequest do
 
     env = Rack::MockRequest.env_for("/", :input => Tempfile.new("name").tap { |t| t << "foo" })
     env["CONTENT_LENGTH"].must_equal "3"
+
+    env = Rack::MockRequest.env_for("/", :input => IO.pipe.first)
+    env["CONTENT_LENGTH"].must_be_nil
   end
 
   it "allow posting" do
diff --git a/test/spec_multipart.rb b/test/spec_multipart.rb
index 6f1f0d9a..40bab4cd 100644
--- a/test/spec_multipart.rb
+++ b/test/spec_multipart.rb
@@ -107,11 +107,6 @@ describe Rack::Multipart do
     def rd.rewind; end
     wr.sync = true
 
-    # mock out size to make this pipe look like a Tempfile
-    def rd.size
-      1024 * 1024 * 8
-    end
-
     # write to a pipe in a background thread, this will write a lot
     # unless Rack (properly) shuts down the read end
     thr = Thread.new do
@@ -136,7 +131,7 @@ describe Rack::Multipart do
 
     fixture = {
       "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
-      "CONTENT_LENGTH" => rd.size.to_s,
+      "CONTENT_LENGTH" => (1024 * 1024 * 8).to_s,
       :input => rd,
     }