1 # -*- encoding: binary -*-
2 $stderr.sync = $stdout.sync = true
9 class MyBody < Struct.new(:to_path, :closed)
11 raise RuntimeError, "each should never get called"
19 class TestCloggerToPath < Test::Unit::TestCase
23 "REQUEST_METHOD" => "GET",
24 "HTTP_VERSION" => "HTTP/1.0",
25 "HTTP_USER_AGENT" => 'echo and socat \o/',
28 "rack.errors" => $stderr,
29 "rack.input" => File.open('/dev/null', 'rb'),
30 "REMOTE_ADDR" => '127.0.0.1',
35 assert body.respond_to?(:to_path)
36 assert body.respond_to?("to_path")
38 assert ! body.respond_to?(:to_Path)
39 assert ! body.respond_to?("to_Path")
42 def test_wraps_to_path
44 tmp = Tempfile.new('')
46 app = Rack::Builder.new do
47 tmp.syswrite(' ' * 365)
49 'Content-Length' => '0',
50 'Content-Type' => 'text/plain',
55 :format => '$body_bytes_sent $status'
56 run lambda { |env| [ 200, h, b = MyBody.new(tmp.path) ] }
59 status, headers, body = app.call(@req)
60 assert_instance_of(Clogger, body)
62 assert logger.string.empty?
63 assert_equal tmp.path, body.to_path
65 assert b.closed, "close passed through"
66 assert_equal "365 200\n", logger.string
69 def test_wraps_to_path_dev_fd
71 tmp = Tempfile.new('')
73 app = Rack::Builder.new do
74 tmp.syswrite(' ' * 365)
76 'Content-Length' => '0',
77 'Content-Type' => 'text/plain',
82 :format => '$body_bytes_sent $status'
83 run lambda { |env| [ 200, h, b = MyBody.new("/dev/fd/#{tmp.fileno}") ] }
86 status, headers, body = app.call(@req)
87 assert_instance_of(Clogger, body)
89 assert logger.string.empty?
90 assert_equal "/dev/fd/#{tmp.fileno}", body.to_path
93 assert_equal "365 200\n", logger.string
96 def test_wraps_to_path_to_io
98 tmp = Tempfile.new('')
100 @to_io_called = super
105 app = Rack::Builder.new do
106 tmp.syswrite(' ' * 365)
109 'Content-Length' => '0',
110 'Content-Type' => 'text/plain',
115 :format => '$body_bytes_sent $status'
116 run lambda { |env| [ 200, h, tmp ] }
119 status, headers, body = app.call(@req)
120 assert_instance_of(Clogger, body)
124 assert_kind_of IO, tmp.instance_variable_get(:@to_io_called)
125 assert logger.string.empty?
129 assert_equal "365 200\n", logger.string
132 def test_does_not_wrap_to_path
133 logger = StringIO.new
134 app = Rack::Builder.new do
136 'Content-Length' => '3',
137 'Content-Type' => 'text/plain',
142 :format => '$body_bytes_sent $status'
143 run lambda { |env| [ 200, h, [ "hi\n" ] ] }
145 status, headers, body = app.call(@req)
146 assert_instance_of(Clogger, body)
147 assert ! body.respond_to?(:to_path)
148 assert ! body.respond_to?("to_path")
149 assert logger.string.empty?
151 assert ! logger.string.empty?