From 25ca3e5ebfc761041d3b5f375ad62d507c8dd14f Mon Sep 17 00:00:00 2001 From: Janko Marohnić Date: Sat, 29 Apr 2017 10:11:14 +1000 Subject: 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. --- lib/rack/mock.rb | 2 +- test/spec_mock.rb | 3 +++ test/spec_multipart.rb | 7 +------ 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, } -- cgit v1.2.3-24-ge0c7