1 # -*- encoding: binary -*-
2 $stderr.sync = $stdout.sync = true
10 class MyBody < Struct.new(:to_path, :closed)
12 raise RuntimeError, "each should never get called"
24 class TestCloggerToPath < Test::Unit::TestCase
28 "REQUEST_METHOD" => "GET",
29 "HTTP_VERSION" => "HTTP/1.0",
30 "SERVER_PROTOCOL" => "HTTP/1.0",
31 "HTTP_USER_AGENT" => 'echo and socat \o/',
34 "rack.errors" => $stderr,
35 "rack.input" => File.open('/dev/null', 'rb'),
36 "REMOTE_ADDR" => '127.0.0.1',
41 assert body.respond_to?(:to_path)
42 assert body.respond_to?("to_path")
44 assert ! body.respond_to?(:to_Path)
45 assert ! body.respond_to?("to_Path")
48 def test_wraps_to_path
50 tmp = Tempfile.new('')
52 app = Rack::Builder.new do
53 tmp.syswrite(' ' * 365)
55 'content-length' => '0',
56 'content-type' => 'text/plain',
61 :format => '$body_bytes_sent $status'
62 run lambda { |env| [ 200, h, b = MyBody.new(tmp.path) ] }
65 status, headers, body = app.call(@req)
66 assert_instance_of(Clogger, body)
69 assert ! body.respond_to?(:privtest)
70 assert body.respond_to?(:privtest, true)
71 assert ! body.respond_to?(:privtest, false)
73 assert logger.string.empty?
74 assert_equal tmp.path, body.to_path
76 assert b.closed, "close passed through"
77 assert_equal "365 200\n", logger.string
80 def test_wraps_to_path_dev_fd
82 tmp = Tempfile.new('')
84 app = Rack::Builder.new do
85 tmp.syswrite(' ' * 365)
87 'content-length' => '0',
88 'content-type' => 'text/plain',
93 :format => '$body_bytes_sent $status'
94 run lambda { |env| [ 200, h, b = MyBody.new("/dev/fd/#{tmp.fileno}") ] }
97 status, headers, body = app.call(@req)
98 assert_instance_of(Clogger, body)
100 assert logger.string.empty?
101 assert_equal "/dev/fd/#{tmp.fileno}", body.to_path
104 assert_equal "365 200\n", logger.string
107 def test_does_not_wrap_to_path
108 logger = StringIO.new
109 app = Rack::Builder.new do
111 'content-length' => '3',
112 'content-type' => 'text/plain',
117 :format => '$body_bytes_sent $status'
118 run lambda { |env| [ 200, h, [ "hi\n" ] ] }
120 status, headers, body = app.call(@req)
121 assert_instance_of(Clogger, body)
122 assert ! body.respond_to?(:to_path)
123 assert ! body.respond_to?("to_path")
124 assert logger.string.empty?
126 assert ! logger.string.empty?