README: updated to use concise format syntax
[clogger.git] / test / test_clogger_to_path.rb
blob4cc9027e9dc99cdec85c88c7c9c7f49a75fbfd35
1 # -*- encoding: binary -*-
2 $stderr.sync = $stdout.sync = true
3 require "test/unit"
4 require "date"
5 require "stringio"
6 require "rack"
7 require "clogger"
9 class MyBody < Struct.new(:to_path, :closed)
10   def each(&block)
11     raise RuntimeError, "each should never get called"
12   end
14   def close
15     self.closed = true
16   end
17 end
19 class TestCloggerToPath < Test::Unit::TestCase
21   def setup
22     @req = {
23       "REQUEST_METHOD" => "GET",
24       "HTTP_VERSION" => "HTTP/1.0",
25       "HTTP_USER_AGENT" => 'echo and socat \o/',
26       "PATH_INFO" => "/",
27       "QUERY_STRING" => "",
28       "rack.errors" => $stderr,
29       "rack.input" => File.open('/dev/null', 'rb'),
30       "REMOTE_ADDR" => '127.0.0.1',
31     }
32   end
34   def check_body(body)
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")
40   end
42   def test_wraps_to_path
43     logger = StringIO.new
44     tmp = Tempfile.new('')
45     b = nil
46     app = Rack::Builder.new do
47       tmp.syswrite(' ' * 365)
48       h = {
49         'Content-Length' => '0',
50         'Content-Type' => 'text/plain',
51       }
52       use Clogger,
53         :logger => logger,
54         :reentrant => true,
55         :format => '$body_bytes_sent $status'
56       run lambda { |env| [ 200, h, b = MyBody.new(tmp.path) ] }
57     end.to_app
59     status, headers, body = app.call(@req)
60     assert_instance_of(Clogger, body)
61     check_body(body)
62     assert logger.string.empty?
63     assert_equal tmp.path, body.to_path
64     body.close
65     assert b.closed, "close passed through"
66     assert_equal "365 200\n", logger.string
67   end
69   def test_wraps_to_path_dev_fd
70     logger = StringIO.new
71     tmp = Tempfile.new('')
72     b = nil
73     app = Rack::Builder.new do
74       tmp.syswrite(' ' * 365)
75       h = {
76         'Content-Length' => '0',
77         'Content-Type' => 'text/plain',
78       }
79       use Clogger,
80         :logger => logger,
81         :reentrant => true,
82         :format => '$body_bytes_sent $status'
83       run lambda { |env| [ 200, h, b = MyBody.new("/dev/fd/#{tmp.fileno}") ] }
84     end.to_app
86     status, headers, body = app.call(@req)
87     assert_instance_of(Clogger, body)
88     check_body(body)
89     assert logger.string.empty?
90     assert_equal "/dev/fd/#{tmp.fileno}", body.to_path
91     body.close
92     assert b.closed
93     assert_equal "365 200\n", logger.string
94   end
96   def test_wraps_to_path_to_io
97     logger = StringIO.new
98     tmp = Tempfile.new('')
99     def tmp.to_io
100       @to_io_called = super
101     end
102     def tmp.to_path
103       path
104     end
105     app = Rack::Builder.new do
106       tmp.syswrite(' ' * 365)
107       tmp.sysseek(0)
108       h = {
109         'Content-Length' => '0',
110         'Content-Type' => 'text/plain',
111       }
112       use Clogger,
113         :logger => logger,
114         :reentrant => true,
115         :format => '$body_bytes_sent $status'
116       run lambda { |env| [ 200, h, tmp ] }
117     end.to_app
119     status, headers, body = app.call(@req)
120     assert_instance_of(Clogger, body)
121     check_body(body)
123     assert_equal tmp.path, body.to_path
124     assert_nothing_raised { body.to_io }
125     assert_kind_of IO, tmp.instance_variable_get(:@to_io_called)
126     assert logger.string.empty?
127     assert ! tmp.closed?
128     body.close
129     assert tmp.closed?
130     assert_equal "365 200\n", logger.string
131   end
133   def test_does_not_wrap_to_path
134     logger = StringIO.new
135     app = Rack::Builder.new do
136       h = {
137         'Content-Length' => '3',
138         'Content-Type' => 'text/plain',
139       }
140       use Clogger,
141         :logger => logger,
142         :reentrant => true,
143         :format => '$body_bytes_sent $status'
144       run lambda { |env| [ 200, h, [ "hi\n" ] ] }
145     end.to_app
146     status, headers, body = app.call(@req)
147     assert_instance_of(Clogger, body)
148     assert ! body.respond_to?(:to_path)
149     assert ! body.respond_to?("to_path")
150     assert logger.string.empty?
151     body.close
152     assert ! logger.string.empty?
153   end