summary refs log tree commit
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2017-05-15 09:50:13 -0700
committerGitHub <noreply@github.com>2017-05-15 09:50:13 -0700
commitd4793a0707efa9011ded6ebc88154ac9d7f98c8a (patch)
tree31c5619108ff61e7cf98ccf311f1ff0d38ccc361
parentf2dacc6bb881ed1f588eecf00134abca8e26665e (diff)
parent25ca3e5ebfc761041d3b5f375ad62d507c8dd14f (diff)
downloadrack-d4793a0707efa9011ded6ebc88154ac9d7f98c8a.tar.gz
Merge pull request #1163 from janko-m/rely-on-size-in-mock-request
Rely on input responding to #size instead of #length in MockRequest.env_for
-rw-r--r--lib/rack/mock.rb2
-rw-r--r--test/spec_mock.rb9
-rw-r--r--test/spec_multipart.rb7
3 files changed, 11 insertions, 7 deletions
diff --git a/lib/rack/mock.rb b/lib/rack/mock.rb
index 10070f11..914bf3b5 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].length.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 e383f203..c7992321 100644
--- a/test/spec_mock.rb
+++ b/test/spec_mock.rb
@@ -84,6 +84,15 @@ describe Rack::MockRequest do
   it "set content length" do
     env = Rack::MockRequest.env_for("/", :input => "foo")
     env["CONTENT_LENGTH"].must_equal "3"
+
+    env = Rack::MockRequest.env_for("/", :input => StringIO.new("foo"))
+    env["CONTENT_LENGTH"].must_equal "3"
+
+    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 2f957d92..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 length to make this pipe look like a Tempfile
-    def rd.length
-      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.length.to_s,
+      "CONTENT_LENGTH" => (1024 * 1024 * 8).to_s,
       :input => rd,
     }