clogger.git  about / heads / tags
configurable request logging for Rack
blob 4cc173898c6b763b9021fc2e2ddb303b685d95f1 3204 bytes (raw)
$ git show HEAD:test/test_clogger_to_path.rb	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 
# -*- encoding: binary -*-
$stderr.sync = $stdout.sync = true
require "test/unit"
require "date"
require "stringio"
require "tempfile"
require "rack"
require "clogger"

class MyBody < Struct.new(:to_path, :closed)
  def each(&block)
    raise RuntimeError, "each should never get called"
  end

  def close
    self.closed = true
  end

private
  def privtest
  end
end

class TestCloggerToPath < Test::Unit::TestCase

  def setup
    @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" => "",
      "rack.errors" => $stderr,
      "rack.input" => File.open('/dev/null', 'rb'),
      "REMOTE_ADDR" => '127.0.0.1',
    }
  end

  def check_body(body)
    assert body.respond_to?(:to_path)
    assert body.respond_to?("to_path")

    assert ! body.respond_to?(:to_Path)
    assert ! body.respond_to?("to_Path")
  end

  def test_wraps_to_path
    logger = StringIO.new
    tmp = Tempfile.new('')
    b = nil
    app = Rack::Builder.new do
      tmp.syswrite(' ' * 365)
      h = {
        'content-length' => '0',
        'content-type' => 'text/plain',
      }
      use Clogger,
        :logger => logger,
        :reentrant => true,
        :format => '$body_bytes_sent $status'
      run lambda { |env| [ 200, h, b = MyBody.new(tmp.path) ] }
    end.to_app

    status, headers, body = app.call(@req)
    assert_instance_of(Clogger, body)
    check_body(body)

    assert ! body.respond_to?(:privtest)
    assert body.respond_to?(:privtest, true)
    assert ! body.respond_to?(:privtest, false)

    assert logger.string.empty?
    assert_equal tmp.path, body.to_path
    body.close
    assert b.closed, "close passed through"
    assert_equal "365 200\n", logger.string
  end

  def test_wraps_to_path_dev_fd
    logger = StringIO.new
    tmp = Tempfile.new('')
    b = nil
    app = Rack::Builder.new do
      tmp.syswrite(' ' * 365)
      h = {
        'content-length' => '0',
        'content-type' => 'text/plain',
      }
      use Clogger,
        :logger => logger,
        :reentrant => true,
        :format => '$body_bytes_sent $status'
      run lambda { |env| [ 200, h, b = MyBody.new("/dev/fd/#{tmp.fileno}") ] }
    end.to_app

    status, headers, body = app.call(@req)
    assert_instance_of(Clogger, body)
    check_body(body)
    assert logger.string.empty?
    assert_equal "/dev/fd/#{tmp.fileno}", body.to_path
    body.close
    assert b.closed
    assert_equal "365 200\n", logger.string
  end

  def test_does_not_wrap_to_path
    logger = StringIO.new
    app = Rack::Builder.new do
      h = {
        'content-length' => '3',
        'content-type' => 'text/plain',
      }
      use Clogger,
        :logger => logger,
        :reentrant => true,
        :format => '$body_bytes_sent $status'
      run lambda { |env| [ 200, h, [ "hi\n" ] ] }
    end.to_app
    status, headers, body = app.call(@req)
    assert_instance_of(Clogger, body)
    assert ! body.respond_to?(:to_path)
    assert ! body.respond_to?("to_path")
    assert logger.string.empty?
    body.close
    assert ! logger.string.empty?
  end

end

git clone https://yhbt.net/clogger.git