From 3e1f936990788b1112da508eac28ff1a8f7956a6 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@jeremyevans.net>
Date: Tue, 28 Jan 2020 17:23:54 -0800
Subject: More covering tests

3352 / 3378 LOC (99.23%) covered
---
 lib/rack/utils.rb     |  2 ++
 test/spec_mock.rb     |  7 +++++++
 test/spec_request.rb  | 39 +++++++++++++++++++++++++++++++++++++++
 test/spec_sendfile.rb | 23 +++++++++++++++++++++++
 test/spec_utils.rb    | 26 ++++++++++++++++++++++++++
 5 files changed, 97 insertions(+)

diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb
index 900eaf60..2efeb3da 100644
--- a/lib/rack/utils.rb
+++ b/lib/rack/utils.rb
@@ -99,9 +99,11 @@ module Rack
         Process.clock_gettime(Process::CLOCK_MONOTONIC)
       end
     else
+      # :nocov:
       def clock_time
         Time.now.to_f
       end
+      # :nocov:
     end
     module_function :clock_time
 
diff --git a/test/spec_mock.rb b/test/spec_mock.rb
index d7246d3f..e439e37b 100644
--- a/test/spec_mock.rb
+++ b/test/spec_mock.rb
@@ -320,6 +320,9 @@ describe Rack::MockResponse do
     res = Rack::MockRequest.new(app).get("")
     res.body.must_match(/rack/)
     assert_match(res, /rack/)
+
+    res.match('rack')[0].must_equal 'rack'
+    res.match('banana').must_be_nil
   end
 
   it "provide access to the Rack errors" do
@@ -341,6 +344,10 @@ describe Rack::MockResponse do
     lambda {
       Rack::MockRequest.new(app).get("/?error=foo", fatal: true)
     }.must_raise Rack::MockRequest::FatalWarning
+
+    lambda {
+      Rack::MockRequest.new(lambda { |env| env['rack.errors'].write(env['rack.errors'].string) }).get("/", fatal: true)
+    }.must_raise(Rack::MockRequest::FatalWarning).message.must_equal ''
   end
 end
 
diff --git a/test/spec_request.rb b/test/spec_request.rb
index 01f2bf79..fade919f 100644
--- a/test/spec_request.rb
+++ b/test/spec_request.rb
@@ -388,6 +388,7 @@ class RackRequestTest < Minitest::Spec
     req.content_type.must_equal 'text/plain;charset=utf-8'
     req.media_type.must_equal 'text/plain'
     req.media_type_params['charset'].must_equal 'utf-8'
+    req.content_charset.must_equal 'utf-8'
     req.POST.must_be :empty?
     req.params.must_equal "foo" => "quux"
     req.body.read.must_equal "foo=bar&quux=bla"
@@ -434,11 +435,35 @@ class RackRequestTest < Minitest::Spec
     req.POST.must_equal "foo" => "bar", "quux" => "bla"
   end
 
+  it "have params only return GET if POST cannot be processed" do
+    obj = Object.new 
+    def obj.read(*) raise EOFError end
+    def obj.set_encoding(*) end
+    def obj.rewind(*) end
+    req = make_request Rack::MockRequest.env_for("/", 'REQUEST_METHOD' => 'POST', :input => obj)
+    req.params.must_equal req.GET
+    req.params.wont_be_same_as req.GET
+  end
+
   it "get value by key from params with #[]" do
     req = make_request \
       Rack::MockRequest.env_for("?foo=quux")
     req['foo'].must_equal 'quux'
     req[:foo].must_equal 'quux'
+
+    next if self.class == TestProxyRequest
+    verbose = $VERBOSE
+    warn_arg = nil
+    req.define_singleton_method(:warn) do |arg|
+      warn_arg = arg
+    end
+    begin
+      $VERBOSE = true
+      req['foo'].must_equal 'quux'
+      warn_arg.must_equal "Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead"
+    ensure
+      $VERBOSE = verbose
+    end
   end
 
   it "set value to key on params with #[]=" do
@@ -461,6 +486,20 @@ class RackRequestTest < Minitest::Spec
     req.params.must_equal 'foo' => 'jaz'
     req['foo'].must_equal 'jaz'
     req[:foo].must_equal 'jaz'
+
+    verbose = $VERBOSE
+    warn_arg = nil
+    req.define_singleton_method(:warn) do |arg|
+      warn_arg = arg
+    end
+    begin
+      $VERBOSE = true
+      req['foo'] = 'quux'
+      warn_arg.must_equal "Request#[]= is deprecated and will be removed in a future version of Rack. Please use request.params[]= instead"
+      req.params['foo'].must_equal 'quux'
+    ensure
+      $VERBOSE = verbose
+    end
   end
 
   it "return values for the keys in the order given from values_at" do
diff --git a/test/spec_sendfile.rb b/test/spec_sendfile.rb
index cbed8db3..5c55f4ff 100644
--- a/test/spec_sendfile.rb
+++ b/test/spec_sendfile.rb
@@ -43,6 +43,18 @@ describe Rack::Sendfile do
     end
   end
 
+  it "does nothing and logs to rack.errors when incorrect X-Sendfile-Type header present" do
+    io = StringIO.new
+    request 'HTTP_X_SENDFILE_TYPE' => 'X-Banana', 'rack.errors' => io do |response|
+      response.must_be :ok?
+      response.body.must_equal 'Hello World'
+      response.headers.wont_include 'X-Sendfile'
+
+      io.rewind
+      io.read.must_equal "Unknown x-sendfile variation: 'X-Banana'.\n"
+    end
+  end
+
   it "sets X-Sendfile response header and discards body" do
     request 'HTTP_X_SENDFILE_TYPE' => 'X-Sendfile' do |response|
       response.must_be :ok?
@@ -142,6 +154,7 @@ describe Rack::Sendfile do
     begin
       dir1 = Dir.mktmpdir
       dir2 = Dir.mktmpdir
+      dir3 = Dir.mktmpdir
 
       first_body = open_file(File.join(dir1, 'rack_sendfile'))
       first_body.puts 'hello world'
@@ -149,6 +162,9 @@ describe Rack::Sendfile do
       second_body = open_file(File.join(dir2, 'rack_sendfile'))
       second_body.puts 'goodbye world'
 
+      third_body = open_file(File.join(dir3, 'rack_sendfile'))
+      third_body.puts 'hello again world'
+
       headers = {
         'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect',
         'HTTP_X_ACCEL_MAPPING' => "#{dir1}/=/foo/bar/, #{dir2}/=/wibble/"
@@ -167,6 +183,13 @@ describe Rack::Sendfile do
         response.headers['Content-Length'].must_equal '0'
         response.headers['X-Accel-Redirect'].must_equal '/wibble/rack_sendfile'
       end
+
+      request(headers, third_body) do |response|
+        response.must_be :ok?
+        response.body.must_be :empty?
+        response.headers['Content-Length'].must_equal '0'
+        response.headers['X-Accel-Redirect'].must_equal "#{dir3}/rack_sendfile"
+      end
     ensure
       FileUtils.remove_entry_secure dir1
       FileUtils.remove_entry_secure dir2
diff --git a/test/spec_utils.rb b/test/spec_utils.rb
index 67b77b0d..48dc17df 100644
--- a/test/spec_utils.rb
+++ b/test/spec_utils.rb
@@ -545,6 +545,30 @@ describe Rack::Utils, "cookies" do
       Rack::Utils.add_cookie_to_header(Object.new, 'name', 'value')
     }.must_raise ArgumentError
   end
+
+  it "sets and deletes cookies in header hash" do
+    header = {'Set-Cookie'=>''}
+    Rack::Utils.set_cookie_header!(header, 'name', 'value').must_be_nil
+    header['Set-Cookie'].must_equal 'name=value'
+    Rack::Utils.set_cookie_header!(header, 'name2', 'value2').must_be_nil
+    header['Set-Cookie'].must_equal "name=value\nname2=value2"
+    Rack::Utils.set_cookie_header!(header, 'name2', 'value3').must_be_nil
+    header['Set-Cookie'].must_equal "name=value\nname2=value2\nname2=value3"
+
+    Rack::Utils.delete_cookie_header!(header, 'name2').must_be_nil
+    header['Set-Cookie'].must_equal "name=value\nname2=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT"
+    Rack::Utils.delete_cookie_header!(header, 'name').must_be_nil
+    header['Set-Cookie'].must_equal "name2=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT\nname=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT"
+
+    header = {'Set-Cookie'=>nil}
+    Rack::Utils.delete_cookie_header!(header, 'name').must_be_nil
+    header['Set-Cookie'].must_equal "name=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT"
+
+    header = {'Set-Cookie'=>[]}
+    Rack::Utils.delete_cookie_header!(header, 'name').must_be_nil
+    header['Set-Cookie'].must_equal "name=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT"
+  end
+
 end
 
 describe Rack::Utils, "byte_range" do
@@ -748,6 +772,8 @@ describe Rack::Utils::Context do
     r2.must_equal 4
     r3 = c3.call(:misc_symbol)
     r3.must_be_nil
+    r3 = c2.context(:misc_symbol, test_target3)
+    r3.must_be_nil
     r4 = Rack::MockRequest.new(a4).get('/')
     r4.status.must_equal 200
     r5 = Rack::MockRequest.new(a5).get('/')
-- 
2.30.2

