about summary refs log tree commit homepage
path: root/test
diff options
context:
space:
mode:
authorStefan Sundin <git@stefansundin.com>2024-01-27 17:23:00 -0800
committerEric Wong <bofh@yhbt.net>2024-02-01 07:17:42 +0000
commit6b0768f8791c5d6dd2e8a5aea0da76d46549477e (patch)
tree4e13b686f9afec6ea9f74c5cf0faaec597379cfd /test
parent2991f00afa4e445214f3b997bb37cb746c01cd2d (diff)
downloadclogger-6b0768f8791c5d6dd2e8a5aea0da76d46549477e.tar.gz
Rack::Utils::HeaderHash will be removed in rack 3.1 so these changes mostly
address that. The initializer in Rack::Headers inherits from Hash, so
switching to the ::[] class method to achieve the same result.
Diffstat (limited to 'test')
-rw-r--r--test/test_clogger.rb61
-rw-r--r--test/test_clogger_to_path.rb14
2 files changed, 51 insertions, 24 deletions
diff --git a/test/test_clogger.rb b/test/test_clogger.rb
index 1dee652..5646025 100644
--- a/test/test_clogger.rb
+++ b/test/test_clogger.rb
@@ -10,6 +10,16 @@ require "rack"
 
 require "clogger"
 
+IS_RACK3 = Gem::Version.new(Rack.release) >= Gem::Version.new('3.0.0')
+
+RackHeaders = if Object.const_defined?("Rack::Headers")
+  # Rack >= 3.0
+  Rack::Headers
+else
+  # Rack < 3.0
+  Rack::Utils::HeaderHash
+end
+
 # used to test subclasses
 class FooString < String
 end
@@ -22,6 +32,7 @@ class TestClogger < Test::Unit::TestCase
     @nginx_fmt = "%d/%b/%Y:%H:%M:%S %z"
     @req = {
       "REQUEST_METHOD" => "GET",
+      "SERVER_PROTOCOL" => "HTTP/1.0",
       "HTTP_VERSION" => "HTTP/1.0",
       "HTTP_USER_AGENT" => 'echo and socat \o/',
       "PATH_INFO" => "/hello",
@@ -85,13 +96,13 @@ class TestClogger < Test::Unit::TestCase
   def test_clen_stringio
     start = DateTime.now - 1
     str = StringIO.new
-    app = lambda { |env| [ 301, {'Content-Length' => '5'}, ['abcde'] ] }
+    app = lambda { |env| [ 301, {'content-length' => '5'}, ['abcde'] ] }
     format = Common.dup
     assert format.gsub!(/response_length/, 'sent_http_content_length')
     cl = Clogger.new(app, :logger => str, :format => format)
     status, headers, body = cl.call(@req)
     assert_equal(301, status)
-    assert_equal({'Content-Length' => '5'}, headers)
+    assert_equal({'content-length' => '5'}, headers)
     body.each { |part| assert_equal('abcde', part) }
     str = str.string
     r = %r{\Ahome - - \[[^\]]+\] "GET /hello\?goodbye=true HTTP/1.0" 301 5\n\z}
@@ -253,7 +264,7 @@ class TestClogger < Test::Unit::TestCase
   def test_rack_1_0
     start = DateTime.now - 1
     str = StringIO.new
-    app = lambda { |env| [ 200, {'Content-Length'=>'0'}, %w(a b c)] }
+    app = lambda { |env| [ 200, {'content-length'=>'0'}, %w(a b c)] }
     cl = Clogger.new(app, :logger => str, :format => Rack_1_0)
     status, headers, body = cl.call(@req)
     tmp = []
@@ -344,7 +355,7 @@ class TestClogger < Test::Unit::TestCase
   def test_combined
     start = DateTime.now - 1
     str = StringIO.new
-    app = lambda { |env| [ 200, {'Content-Length'=>'3'}, %w(a b c)] }
+    app = lambda { |env| [ 200, {'content-length'=>'3'}, %w(a b c)] }
     cl = Clogger.new(app, :logger => str, :format => Combined)
     status, headers, body = cl.call(@req)
     tmp = []
@@ -365,7 +376,7 @@ class TestClogger < Test::Unit::TestCase
 
   def test_rack_errors_fallback
     err = StringIO.new
-    app = lambda { |env| [ 200, {'Content-Length'=>'3'}, %w(a b c)] }
+    app = lambda { |env| [ 200, {'content-length'=>'3'}, %w(a b c)] }
     cl = Clogger.new(app, :format => '$pid')
     req = @req.merge('rack.errors' => err)
     status, headers, body = cl.call(req)
@@ -374,7 +385,7 @@ class TestClogger < Test::Unit::TestCase
 
   def test_body_close
     s_body = StringIO.new(%w(a b c).join("\n"))
-    app = lambda { |env| [ 200, {'Content-Length'=>'5'}, s_body] }
+    app = lambda { |env| [ 200, {'content-length'=>'5'}, s_body] }
     cl = Clogger.new(app, :logger => [], :format => '$pid')
     status, headers, body = cl.call(@req)
     assert ! s_body.closed?
@@ -384,7 +395,7 @@ class TestClogger < Test::Unit::TestCase
 
   def test_escape
     str = StringIO.new
-    app = lambda { |env| [ 200, {'Content-Length'=>'5'}, [] ] }
+    app = lambda { |env| [ 200, {'content-length'=>'5'}, [] ] }
     cl = Clogger.new(app,
       :logger => str,
       :format => '$http_user_agent "$request"')
@@ -398,13 +409,21 @@ class TestClogger < Test::Unit::TestCase
     assert_equal expect, str.string
   end
 
-  # rack allows repeated headers with "\n":
-  # { 'Set-Cookie' => "a\nb" } =>
-  #   Set-Cookie: a
-  #   Set-Cookie: b
-  def test_escape_header_newlines
-    str = StringIO.new
-    app = lambda { |env| [302, { 'Set-Cookie' => "a\nb" }, [] ] }
+  # rack >= 3 allows repeated headers with array:
+  # { 'set-cookie' => ["a","b"] } =>
+  #   set-cookie: a
+  #   set-cookie: b
+  # rack < 3 allows repeated headers with "\n":
+  # { 'set-cookie' => "a\nb" } =>
+  #   set-cookie: a
+  #   set-cookie: b
+  def test_multiheader
+    str = StringIO.new
+    if IS_RACK3 then
+      app = lambda { |env| [302, { 'set-cookie' => ["a","b"] }, [] ] }
+    else
+      app = lambda { |env| [302, { 'set-cookie' => "a\nb" }, [] ] }
+    end
     cl = Clogger.new(app, :logger => str, :format => '$sent_http_set_cookie')
     cl.call(@req)
     assert_equal "a\\x0Ab\n", str.string
@@ -463,12 +482,16 @@ class TestClogger < Test::Unit::TestCase
     str = StringIO.new
     app = lambda { |env| [302, [ %w(a) ], []] }
     cl = Clogger.new(app, :logger => str, :format => '$sent_http_set_cookie')
-    assert_nothing_raised { cl.call(@req) }
+    if IS_RACK3 then
+      assert_raise(ArgumentError) { cl.call(@req) }
+    else
+      assert_nothing_raised { cl.call(@req) }
+    end
   end
 
   def test_subclass_hash
     str = StringIO.new
-    req = Rack::Utils::HeaderHash.new(@req)
+    req = RackHeaders[@req]
     app = lambda { |env| [302, [ %w(a) ], []] }
     cl = Clogger.new(app, :logger => str, :format => Rack_1_0)
     assert_nothing_raised { cl.call(req).last.each {}.close }
@@ -877,19 +900,21 @@ class TestClogger < Test::Unit::TestCase
   end
 
   def test_lint_error_wrapper
-    require 'rack/lobster'
+    require 'rackup/lobster'
     @req["SERVER_NAME"] = "FOO"
     @req["SERVER_PORT"] = "666"
     @req["rack.version"] = [1,1]
     @req["rack.multithread"] = true
     @req["rack.multiprocess"] = true
     @req["rack.run_once"] = false
-    app = Rack::ContentLength.new(Rack::ContentType.new(Rack::Lobster.new))
+    app = Rack::ContentLength.new(Rack::ContentType.new(Rackup::Lobster.new))
     cl = Clogger.new(app, :format => :Combined)
     @req["rack.errors"] = err = StringIO.new
     status, headers, body = r = Rack::Lint.new(cl).call(@req)
     body.each { |x| assert_kind_of String, x.to_str }
     body.close # might raise here
     assert_match(%r{GET /hello}, err.string)
+  rescue LoadError, Gem::ConflictError
+    # This test only works on Rack >= 3.0
   end
 end
diff --git a/test/test_clogger_to_path.rb b/test/test_clogger_to_path.rb
index f74b991..4cc1738 100644
--- a/test/test_clogger_to_path.rb
+++ b/test/test_clogger_to_path.rb
@@ -3,6 +3,7 @@ $stderr.sync = $stdout.sync = true
 require "test/unit"
 require "date"
 require "stringio"
+require "tempfile"
 require "rack"
 require "clogger"
 
@@ -26,6 +27,7 @@ class TestCloggerToPath < Test::Unit::TestCase
     @req = {
       "REQUEST_METHOD" => "GET",
       "HTTP_VERSION" => "HTTP/1.0",
+      "SERVER_PROTOCOL" => "HTTP/1.0",
       "HTTP_USER_AGENT" => 'echo and socat \o/',
       "PATH_INFO" => "/",
       "QUERY_STRING" => "",
@@ -50,8 +52,8 @@ class TestCloggerToPath < Test::Unit::TestCase
     app = Rack::Builder.new do
       tmp.syswrite(' ' * 365)
       h = {
-        'Content-Length' => '0',
-        'Content-Type' => 'text/plain',
+        'content-length' => '0',
+        'content-type' => 'text/plain',
       }
       use Clogger,
         :logger => logger,
@@ -82,8 +84,8 @@ class TestCloggerToPath < Test::Unit::TestCase
     app = Rack::Builder.new do
       tmp.syswrite(' ' * 365)
       h = {
-        'Content-Length' => '0',
-        'Content-Type' => 'text/plain',
+        'content-length' => '0',
+        'content-type' => 'text/plain',
       }
       use Clogger,
         :logger => logger,
@@ -106,8 +108,8 @@ class TestCloggerToPath < Test::Unit::TestCase
     logger = StringIO.new
     app = Rack::Builder.new do
       h = {
-        'Content-Length' => '3',
-        'Content-Type' => 'text/plain',
+        'content-length' => '3',
+        'content-type' => 'text/plain',
       }
       use Clogger,
         :logger => logger,