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"
23 class TestCloggerToPath < Test::Unit::TestCase
27 "REQUEST_METHOD" => "GET",
28 "HTTP_VERSION" => "HTTP/1.0",
29 "HTTP_USER_AGENT" => 'echo and socat \o/',
32 "rack.errors" => $stderr,
33 "rack.input" => File.open('/dev/null', 'rb'),
34 "REMOTE_ADDR" => '127.0.0.1',
39 assert body.respond_to?(:to_path)
40 assert body.respond_to?("to_path")
42 assert ! body.respond_to?(:to_Path)
43 assert ! body.respond_to?("to_Path")
46 def test_wraps_to_path
48 tmp = Tempfile.new('')
50 app = Rack::Builder.new do
51 tmp.syswrite(' ' * 365)
53 'Content-Length' => '0',
54 'Content-Type' => 'text/plain',
59 :format => '$body_bytes_sent $status'
60 run lambda { |env| [ 200, h, b = MyBody.new(tmp.path) ] }
63 status, headers, body = app.call(@req)
64 assert_instance_of(Clogger, body)
67 assert ! body.respond_to?(:privtest)
68 assert body.respond_to?(:privtest, true)
69 assert ! body.respond_to?(:privtest, false)
71 assert logger.string.empty?
72 assert_equal tmp.path, body.to_path
74 assert b.closed, "close passed through"
75 assert_equal "365 200\n", logger.string
78 def test_wraps_to_path_dev_fd
80 tmp = Tempfile.new('')
82 app = Rack::Builder.new do
83 tmp.syswrite(' ' * 365)
85 'Content-Length' => '0',
86 'Content-Type' => 'text/plain',
91 :format => '$body_bytes_sent $status'
92 run lambda { |env| [ 200, h, b = MyBody.new("/dev/fd/#{tmp.fileno}") ] }
95 status, headers, body = app.call(@req)
96 assert_instance_of(Clogger, body)
98 assert logger.string.empty?
99 assert_equal "/dev/fd/#{tmp.fileno}", body.to_path
102 assert_equal "365 200\n", logger.string
105 def test_does_not_wrap_to_path
106 logger = StringIO.new
107 app = Rack::Builder.new do
109 'Content-Length' => '3',
110 'Content-Type' => 'text/plain',
115 :format => '$body_bytes_sent $status'
116 run lambda { |env| [ 200, h, [ "hi\n" ] ] }
118 status, headers, body = app.call(@req)
119 assert_instance_of(Clogger, body)
120 assert ! body.respond_to?(:to_path)
121 assert ! body.respond_to?("to_path")
122 assert logger.string.empty?
124 assert ! logger.string.empty?