test/test_clogger.rb in clogger-0.9.0 vs test/test_clogger.rb in clogger-1.0.0

- old
+ new

@@ -1,8 +1,9 @@ # -*- encoding: binary -*- $stderr.sync = $stdout.sync = true require "test/unit" +require "time" require "date" require "stringio" require "tempfile" require "rack" @@ -15,10 +16,12 @@ class TestClogger < Test::Unit::TestCase include Clogger::Format def setup + @tz = ENV["TZ"] + @nginx_fmt = "%d/%b/%Y:%H:%M:%S %z" @req = { "REQUEST_METHOD" => "GET", "HTTP_VERSION" => "HTTP/1.0", "HTTP_USER_AGENT" => 'echo and socat \o/', "PATH_INFO" => "/hello", @@ -27,18 +30,27 @@ "rack.input" => File.open('/dev/null', 'rb'), "REMOTE_ADDR" => 'home', } end + def teardown + ENV["TZ"] = @tz + end + def test_init_basic Clogger.new(lambda { |env| [ 0, {}, [] ] }) end def test_init_noargs assert_raise(ArgumentError) { Clogger.new } end + def test_clogger_sym_format + app = lambda { |env| [ 0, {}, [] ] } + tmp = Clogger.new app, :format => :Rack_1_0, :logger => $stderr + end + def test_init_stderr cl = Clogger.new(lambda { |env| [ 0, {}, [] ] }, :logger => $stderr) assert_kind_of(Integer, cl.fileno) assert_equal $stderr.fileno, cl.fileno end @@ -154,17 +166,16 @@ '"$request" $status $body_bytes_sent "$http_referer" ' \ '"$http_user_agent" "$http_cookie" $request_time ' \ '$env{rack.url_scheme}' \ "\n") } - longest_day = Time.at(26265600).strftime('%d/%b/%Y:%H:%M:%S %z') expect = [ [ Clogger::OP_REQUEST, "REMOTE_ADDR" ], [ Clogger::OP_LITERAL, " - " ], [ Clogger::OP_REQUEST, "REMOTE_USER" ], [ Clogger::OP_LITERAL, " [" ], - [ Clogger::OP_TIME_LOCAL, '%d/%b/%Y:%H:%M:%S %z', longest_day ], + [ Clogger::OP_SPECIAL, Clogger::SPECIAL_VARS[:time_local] ], [ Clogger::OP_LITERAL, "] \"" ], [ Clogger::OP_SPECIAL, Clogger::SPECIAL_VARS[:request] ], [ Clogger::OP_LITERAL, "\" "], [ Clogger::OP_SPECIAL, Clogger::SPECIAL_VARS[:status] ], [ Clogger::OP_LITERAL, " "], @@ -694,9 +705,68 @@ s = [] app = lambda { |env| [200, [], [] ] } cl = Clogger.new(app, :logger => s, :format => "$time_utc") status, headers, body = cl.call(@req) assert %r!\A\d+/\w+/\d{4}:\d\d:\d\d:\d\d \+0000\n\z! =~ s[0], s.inspect + end + + def test_time_iso8601 + s = [] + app = lambda { |env| [200, [], [] ] } + cl = Clogger.new(app, :logger => s, :format => "$time_iso8601") + status, headers, body = cl.call(@req) + t = Time.parse(s[0]) + assert_equal t.iso8601, s[0].strip + end + + def test_time_iso8601_pst8pdt + ENV["TZ"] = "PST8PDT" + s = [] + app = lambda { |env| [200, [], [] ] } + cl = Clogger.new(app, :logger => s, :format => "$time_iso8601") + status, headers, body = cl.call(@req) + t = Time.parse(s[0]) + assert_equal t.iso8601, s[0].strip + end + + def test_time_iso8601_utc + ENV["TZ"] = "UTC" + s = [] + app = lambda { |env| [200, [], [] ] } + cl = Clogger.new(app, :logger => s, :format => "$time_iso8601") + status, headers, body = cl.call(@req) + t = Time.parse(s[0]) + assert_equal t.iso8601, s[0].strip + end + + def test_time_local + s = [] + app = lambda { |env| [200, [], [] ] } + cl = Clogger.new(app, :logger => s, :format => "$time_local") + status, headers, body = cl.call(@req) + t = DateTime.strptime(s[0].strip, @nginx_fmt) + assert_equal t.strftime(@nginx_fmt), s[0].strip + end + + def test_time_local_pst8pdt + orig = ENV["TZ"] + ENV["TZ"] = "PST8PDT" + s = [] + app = lambda { |env| [200, [], [] ] } + cl = Clogger.new(app, :logger => s, :format => "$time_local") + status, headers, body = cl.call(@req) + t = DateTime.strptime(s[0].strip, @nginx_fmt) + assert_equal t.strftime(@nginx_fmt), s[0].strip + end + + def test_time_local_utc + ENV["TZ"] = "UTC" + s = [] + app = lambda { |env| [200, [], [] ] } + cl = Clogger.new(app, :logger => s, :format => "$time_local") + status, headers, body = cl.call(@req) + t = DateTime.strptime(s[0].strip, @nginx_fmt) + assert_equal t.strftime(@nginx_fmt), s[0].strip end def test_method_missing s = [] body = []