switch to TypedData macros for allocation
[clogger.git] / test / test_clogger_to_path.rb
blob4cc173898c6b763b9021fc2e2ddb303b685d95f1
1 # -*- encoding: binary -*-
2 $stderr.sync = $stdout.sync = true
3 require "test/unit"
4 require "date"
5 require "stringio"
6 require "tempfile"
7 require "rack"
8 require "clogger"
10 class MyBody < Struct.new(:to_path, :closed)
11   def each(&block)
12     raise RuntimeError, "each should never get called"
13   end
15   def close
16     self.closed = true
17   end
19 private
20   def privtest
21   end
22 end
24 class TestCloggerToPath < Test::Unit::TestCase
26   def setup
27     @req = {
28       "REQUEST_METHOD" => "GET",
29       "HTTP_VERSION" => "HTTP/1.0",
30       "SERVER_PROTOCOL" => "HTTP/1.0",
31       "HTTP_USER_AGENT" => 'echo and socat \o/',
32       "PATH_INFO" => "/",
33       "QUERY_STRING" => "",
34       "rack.errors" => $stderr,
35       "rack.input" => File.open('/dev/null', 'rb'),
36       "REMOTE_ADDR" => '127.0.0.1',
37     }
38   end
40   def check_body(body)
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")
46   end
48   def test_wraps_to_path
49     logger = StringIO.new
50     tmp = Tempfile.new('')
51     b = nil
52     app = Rack::Builder.new do
53       tmp.syswrite(' ' * 365)
54       h = {
55         'content-length' => '0',
56         'content-type' => 'text/plain',
57       }
58       use Clogger,
59         :logger => logger,
60         :reentrant => true,
61         :format => '$body_bytes_sent $status'
62       run lambda { |env| [ 200, h, b = MyBody.new(tmp.path) ] }
63     end.to_app
65     status, headers, body = app.call(@req)
66     assert_instance_of(Clogger, body)
67     check_body(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
75     body.close
76     assert b.closed, "close passed through"
77     assert_equal "365 200\n", logger.string
78   end
80   def test_wraps_to_path_dev_fd
81     logger = StringIO.new
82     tmp = Tempfile.new('')
83     b = nil
84     app = Rack::Builder.new do
85       tmp.syswrite(' ' * 365)
86       h = {
87         'content-length' => '0',
88         'content-type' => 'text/plain',
89       }
90       use Clogger,
91         :logger => logger,
92         :reentrant => true,
93         :format => '$body_bytes_sent $status'
94       run lambda { |env| [ 200, h, b = MyBody.new("/dev/fd/#{tmp.fileno}") ] }
95     end.to_app
97     status, headers, body = app.call(@req)
98     assert_instance_of(Clogger, body)
99     check_body(body)
100     assert logger.string.empty?
101     assert_equal "/dev/fd/#{tmp.fileno}", body.to_path
102     body.close
103     assert b.closed
104     assert_equal "365 200\n", logger.string
105   end
107   def test_does_not_wrap_to_path
108     logger = StringIO.new
109     app = Rack::Builder.new do
110       h = {
111         'content-length' => '3',
112         'content-type' => 'text/plain',
113       }
114       use Clogger,
115         :logger => logger,
116         :reentrant => true,
117         :format => '$body_bytes_sent $status'
118       run lambda { |env| [ 200, h, [ "hi\n" ] ] }
119     end.to_app
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?
125     body.close
126     assert ! logger.string.empty?
127   end