Update respond_to? calls for second argument.
[clogger.git] / test / test_clogger_to_path.rb
blobf74b991809288ed234f3b056c33800637c1f1fff
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
18 private
19   def privtest
20   end
21 end
23 class TestCloggerToPath < Test::Unit::TestCase
25   def setup
26     @req = {
27       "REQUEST_METHOD" => "GET",
28       "HTTP_VERSION" => "HTTP/1.0",
29       "HTTP_USER_AGENT" => 'echo and socat \o/',
30       "PATH_INFO" => "/",
31       "QUERY_STRING" => "",
32       "rack.errors" => $stderr,
33       "rack.input" => File.open('/dev/null', 'rb'),
34       "REMOTE_ADDR" => '127.0.0.1',
35     }
36   end
38   def check_body(body)
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")
44   end
46   def test_wraps_to_path
47     logger = StringIO.new
48     tmp = Tempfile.new('')
49     b = nil
50     app = Rack::Builder.new do
51       tmp.syswrite(' ' * 365)
52       h = {
53         'Content-Length' => '0',
54         'Content-Type' => 'text/plain',
55       }
56       use Clogger,
57         :logger => logger,
58         :reentrant => true,
59         :format => '$body_bytes_sent $status'
60       run lambda { |env| [ 200, h, b = MyBody.new(tmp.path) ] }
61     end.to_app
63     status, headers, body = app.call(@req)
64     assert_instance_of(Clogger, body)
65     check_body(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
73     body.close
74     assert b.closed, "close passed through"
75     assert_equal "365 200\n", logger.string
76   end
78   def test_wraps_to_path_dev_fd
79     logger = StringIO.new
80     tmp = Tempfile.new('')
81     b = nil
82     app = Rack::Builder.new do
83       tmp.syswrite(' ' * 365)
84       h = {
85         'Content-Length' => '0',
86         'Content-Type' => 'text/plain',
87       }
88       use Clogger,
89         :logger => logger,
90         :reentrant => true,
91         :format => '$body_bytes_sent $status'
92       run lambda { |env| [ 200, h, b = MyBody.new("/dev/fd/#{tmp.fileno}") ] }
93     end.to_app
95     status, headers, body = app.call(@req)
96     assert_instance_of(Clogger, body)
97     check_body(body)
98     assert logger.string.empty?
99     assert_equal "/dev/fd/#{tmp.fileno}", body.to_path
100     body.close
101     assert b.closed
102     assert_equal "365 200\n", logger.string
103   end
105   def test_does_not_wrap_to_path
106     logger = StringIO.new
107     app = Rack::Builder.new do
108       h = {
109         'Content-Length' => '3',
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, [ "hi\n" ] ] }
117     end.to_app
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?
123     body.close
124     assert ! logger.string.empty?
125   end