tests: add full URL example
[clogger.git] / test / test_clogger.rb
blob35a211a601c1a7c348a025ba1dca596bf7cd9cc8
1 # -*- encoding: binary -*-
2 $stderr.sync = $stdout.sync = true
3 require "test/unit"
4 require "time"
5 require "date"
6 require "stringio"
7 require "tempfile"
9 require "rack"
11 require "clogger"
13 # used to test subclasses
14 class FooString < String
15 end
17 class TestClogger < Test::Unit::TestCase
18   include Clogger::Format
20   def setup
21     @tz = ENV["TZ"]
22     @nginx_fmt = "%d/%b/%Y:%H:%M:%S %z"
23     @req = {
24       "REQUEST_METHOD" => "GET",
25       "HTTP_VERSION" => "HTTP/1.0",
26       "HTTP_USER_AGENT" => 'echo and socat \o/',
27       "PATH_INFO" => "/hello",
28       "QUERY_STRING" => "goodbye=true",
29       "rack.errors" => $stderr,
30       "rack.input" => File.open('/dev/null', 'rb'),
31       "rack.url_scheme" => "http",
32       "REMOTE_ADDR" => 'home',
33     }
34   end
36   def teardown
37     ENV["TZ"] = @tz
38   end
40   def test_init_basic
41     Clogger.new(lambda { |env| [ 0, {}, [] ] })
42   end
44   def test_init_noargs
45     assert_raise(ArgumentError) { Clogger.new }
46   end
48   def test_clogger_sym_format
49     app = lambda { |env| [ 0, {}, [] ] }
50     tmp = Clogger.new app, :format => :Rack_1_0, :logger => $stderr
51   end
53   def test_init_stderr
54     cl = Clogger.new(lambda { |env| [ 0, {}, [] ] }, :logger => $stderr)
55     assert_kind_of(Integer, cl.fileno)
56     assert_equal $stderr.fileno, cl.fileno
57   end
59   def test_init_stringio
60     cl = Clogger.new(lambda { |env| [ 0, {}, [] ] }, :logger => StringIO.new)
61     assert_nil cl.fileno
62   end
64   def test_write_stringio
65     start = DateTime.now - 1
66     str = StringIO.new
67     cl = Clogger.new(lambda { |env| [ "302 Found", {}, [] ] }, :logger => str)
68     status, headers, body = cl.call(@req)
69     assert_equal("302 Found", status)
70     assert_equal({}, headers)
71     body.each { |part| assert false }
72     body.close
73     str = str.string
74     r = %r{\Ahome - - \[[^\]]+\] "GET /hello\?goodbye=true HTTP/1.0" 302 -\n\z}
75     assert_match r, str
76     %r{\[([^\]]+)\]} =~ str
77     tmp = nil
78     assert_nothing_raised {
79       tmp = DateTime.strptime($1, "%d/%b/%Y:%H:%M:%S %z")
80     }
81     assert tmp >= start
82     assert tmp <= DateTime.now
83   end
85   def test_clen_stringio
86     start = DateTime.now - 1
87     str = StringIO.new
88     app = lambda { |env| [ 301, {'Content-Length' => '5'}, ['abcde'] ] }
89     format = Common.dup
90     assert format.gsub!(/response_length/, 'sent_http_content_length')
91     cl = Clogger.new(app, :logger => str, :format => format)
92     status, headers, body = cl.call(@req)
93     assert_equal(301, status)
94     assert_equal({'Content-Length' => '5'}, headers)
95     body.each { |part| assert_equal('abcde', part) }
96     str = str.string
97     r = %r{\Ahome - - \[[^\]]+\] "GET /hello\?goodbye=true HTTP/1.0" 301 5\n\z}
98     assert_match r, str
99     %r{\[([^\]]+)\]} =~ str
100     tmp = nil
101     assert_nothing_raised {
102       tmp = DateTime.strptime($1, "%d/%b/%Y:%H:%M:%S %z")
103     }
104     assert tmp >= start
105     assert tmp <= DateTime.now
106   end
108   def test_compile_ambiguous
109     cl = Clogger.new(nil, :logger => $stderr)
110     ary = nil
111     cl.instance_eval {
112       ary = compile_format(
113         '$remote_addr $$$$pid' \
114         "\n")
115     }
116     expect = [
117       [ Clogger::OP_REQUEST, "REMOTE_ADDR" ],
118       [ Clogger::OP_LITERAL, " " ],
119       [ Clogger::OP_LITERAL, "$$$" ],
120       [ Clogger::OP_SPECIAL, Clogger::SPECIAL_VARS[:pid] ],
121       [ Clogger::OP_LITERAL, "\n" ],
122       ]
123     assert_equal expect, ary
124   end
126   def test_compile_auto_newline
127     cl = Clogger.new(nil, :logger => $stderr)
128     ary = nil
129     cl.instance_eval { ary = compile_format('$remote_addr $request') }
130     expect = [
131       [ Clogger::OP_REQUEST, "REMOTE_ADDR" ],
132       [ Clogger::OP_LITERAL, " " ],
133       [ Clogger::OP_SPECIAL, Clogger::SPECIAL_VARS[:request] ],
134       [ Clogger::OP_LITERAL, "\n" ],
135       ]
136     assert_equal expect, ary
137   end
139   def test_big_log
140     str = StringIO.new
141     fmt = '$remote_addr $pid $remote_user [$time_local] ' \
142           '"$request" $status $body_bytes_sent "$http_referer" ' \
143          '"$http_user_agent" "$http_cookie" $request_time $http_host'
144     app = lambda { |env| [ 302, {}, [] ] }
145     cl = Clogger.new(app, :logger => str, :format => fmt)
146     cookie = "foo=bar#{'f' * 256}".freeze
147     req = {
148       'HTTP_HOST' => 'example.com:12345',
149       'HTTP_COOKIE' => cookie,
150     }
151     req = @req.merge(req)
152     body = cl.call(req).last
153     body.each { |part| part }
154     body.close
155     str = str.string
156     assert(str.size > 128)
157     assert_match %r["echo and socat \\o/" "#{cookie}" \d+\.\d{3}], str
158     assert_match %r["#{cookie}" \d+\.\d{3} example\.com:12345\n\z], str
159   end
161   def test_compile
162     cl = Clogger.new(nil, :logger => $stderr)
163     ary = nil
164     cl.instance_eval {
165       ary = compile_format(
166         '$remote_addr - $remote_user [$time_local] ' \
167         '"$request" $status $body_bytes_sent "$http_referer" ' \
168         '"$http_user_agent" "$http_cookie" $request_time ' \
169         '$env{rack.url_scheme}' \
170         "\n")
171     }
172     expect = [
173       [ Clogger::OP_REQUEST, "REMOTE_ADDR" ],
174       [ Clogger::OP_LITERAL, " - " ],
175       [ Clogger::OP_REQUEST, "REMOTE_USER" ],
176       [ Clogger::OP_LITERAL, " [" ],
177       [ Clogger::OP_SPECIAL, Clogger::SPECIAL_VARS[:time_local] ],
178       [ Clogger::OP_LITERAL, "] \"" ],
179       [ Clogger::OP_SPECIAL, Clogger::SPECIAL_VARS[:request] ],
180       [ Clogger::OP_LITERAL, "\" "],
181       [ Clogger::OP_SPECIAL, Clogger::SPECIAL_VARS[:status] ],
182       [ Clogger::OP_LITERAL, " "],
183       [ Clogger::OP_SPECIAL, Clogger::SPECIAL_VARS[:body_bytes_sent] ],
184       [ Clogger::OP_LITERAL, " \"" ],
185       [ Clogger::OP_REQUEST, "HTTP_REFERER" ],
186       [ Clogger::OP_LITERAL, "\" \"" ],
187       [ Clogger::OP_REQUEST, "HTTP_USER_AGENT" ],
188       [ Clogger::OP_LITERAL, "\" \"" ],
189       [ Clogger::OP_REQUEST, "HTTP_COOKIE" ],
190       [ Clogger::OP_LITERAL, "\" " ],
191       [ Clogger::OP_REQUEST_TIME, '%d.%03d', 1000 ],
192       [ Clogger::OP_LITERAL, " " ],
193       [ Clogger::OP_REQUEST, "rack.url_scheme" ],
194       [ Clogger::OP_LITERAL, "\n" ],
195     ]
196     assert_equal expect, ary
197   end
199   def test_eval
200     current = Thread.current.to_s
201     str = StringIO.new
202     app = lambda { |env| [ 302, {}, [] ] }
203     cl = Clogger.new(app,
204                     :logger => str,
205                     :format => "-$e{Thread.current}-\n")
206     status, headers, body = cl.call(@req)
207     assert_equal "-#{current}-\n", str.string
208   end
210   def test_pid
211     str = StringIO.new
212     app = lambda { |env| [ 302, {}, [] ] }
213     cl = Clogger.new(app, :logger => str, :format => "[$pid]\n")
214     status, headers, body = cl.call(@req)
215     assert_equal "[#$$]\n", str.string
216   end
218   def test_rack_xff
219     str = StringIO.new
220     app = lambda { |env| [ 302, {}, [] ] }
221     cl = Clogger.new(app, :logger => str, :format => "$ip")
222     req = @req.merge("HTTP_X_FORWARDED_FOR" => '192.168.1.1')
223     status, headers, body = cl.call(req)
224     assert_equal "192.168.1.1\n", str.string
225     str.rewind
226     str.truncate(0)
227     status, headers, body = cl.call(@req)
228     assert_equal "home\n", str.string
229     str.rewind
230     str.truncate(0)
231   end
233   def test_rack_1_0
234     start = DateTime.now - 1
235     str = StringIO.new
236     app = lambda { |env| [ 200, {'Content-Length'=>'0'}, %w(a b c)] }
237     cl = Clogger.new(app, :logger => str, :format => Rack_1_0)
238     status, headers, body = cl.call(@req)
239     tmp = []
240     body.each { |s| tmp << s }
241     body.close
242     assert_equal %w(a b c), tmp
243     str = str.string
244     assert_match %r[" 200 3 \d+\.\d{4}\n\z], str
245     tmp = nil
246     %r{\[(\d+/\w+/\d+ \d+:\d+:\d+)\]} =~ str
247     assert $1
248     assert_nothing_raised { tmp = DateTime.strptime($1, "%d/%b/%Y %H:%M:%S") }
249     assert tmp >= start
250     assert tmp <= DateTime.now
251   end
253   def test_msec
254     str = StringIO.new
255     app = lambda { |env| [ 200, {}, [] ] }
256     cl = Clogger.new(app, :logger => str, :format => '$msec')
257     a = Time.now.to_f - 0.001
258     status, header, bodies = cl.call(@req)
259     assert_match %r(\A\d+\.\d{3}\n\z), str.string
260     b = Time.now.to_f + 0.001
261     logged = str.string.to_f
262     assert logged >= a, "#{logged} >= #{a}"
263     assert logged <= b, "#{logged} <= #{b}"
264   end
266   def test_usec
267     str = StringIO.new
268     app = lambda { |env| [ 200, {}, [] ] }
269     cl = Clogger.new(app, :logger => str, :format => '$usec')
270     a = Time.now.to_f - 0.000001
271     status, header, bodies = cl.call(@req)
272     assert_match %r(\A\d+\.\d{6}\n\z), str.string
273     b = Time.now.to_f + 0.000001
274     logged = str.string.to_f
275     assert logged >= a, "#{logged} >= #{a}"
276     assert logged <= b, "#{logged} <= #{b}"
277   end
279   def test_time_0
280     str = StringIO.new
281     app = lambda { |env| [ 200, {}, [] ] }
282     cl = Clogger.new(app, :logger => str, :format => '$time{0}')
283     a = Time.now.to_f - 1
284     status, header, bodies = cl.call(@req)
285     assert_match %r(\A\d+\n\z), str.string
286     b = Time.now.to_f + 1
287     logged = str.string.to_f
288     assert logged >= a, "#{logged} >= #{a}"
289     assert logged <= b, "#{logged} <= #{b}"
290   end
292   def test_time_1
293     str = StringIO.new
294     app = lambda { |env| [ 200, {}, [] ] }
295     cl = Clogger.new(app, :logger => str, :format => '$time{1}')
296     a = Time.now.to_f - 0.5
297     status, header, bodies = cl.call(@req)
298     assert_match %r(\A\d+\.\d\n\z), str.string
299     b = Time.now.to_f + 0.5
300     logged = str.string.to_f
301     assert logged >= a, "#{logged} >= #{a}"
302     assert logged <= b, "#{logged} <= #{b}"
303   end
305   def test_request_length
306     str = StringIO.new
307     input = StringIO.new('.....')
308     app = lambda { |env| [ 200, {}, [] ] }
309     cl = Clogger.new(app, :logger => str, :format => '$request_length')
310     status, header, bodies = cl.call(@req.merge('rack.input' => input))
311     assert_equal "5\n", str.string
312   end
314   def test_response_length_0
315     str = StringIO.new
316     app = lambda { |env| [ 200, {}, [] ] }
317     cl = Clogger.new(app, :logger => str, :format => '$response_length')
318     status, header, bodies = cl.call(@req)
319     bodies.each { |part| part }
320     bodies.close
321     assert_equal "-\n", str.string
322   end
324   def test_combined
325     start = DateTime.now - 1
326     str = StringIO.new
327     app = lambda { |env| [ 200, {'Content-Length'=>'3'}, %w(a b c)] }
328     cl = Clogger.new(app, :logger => str, :format => Combined)
329     status, headers, body = cl.call(@req)
330     tmp = []
331     body.each { |s| tmp << s }
332     body.close
333     assert_equal %w(a b c), tmp
334     str = str.string
335     assert_match %r[" 200 3 "-" "echo and socat \\o/"\n\z], str
336     tmp = nil
337     %r{\[(\d+/\w+/\d+:\d+:\d+:\d+ .+)\]} =~ str
338     assert $1
339     assert_nothing_raised {
340       tmp = DateTime.strptime($1, "%d/%b/%Y:%H:%M:%S %z")
341     }
342     assert tmp >= start
343     assert tmp <= DateTime.now
344   end
346   def test_rack_errors_fallback
347     err = StringIO.new
348     app = lambda { |env| [ 200, {'Content-Length'=>'3'}, %w(a b c)] }
349     cl = Clogger.new(app, :format => '$pid')
350     req = @req.merge('rack.errors' => err)
351     status, headers, body = cl.call(req)
352     assert_equal "#$$\n", err.string
353   end
355   def test_body_close
356     s_body = StringIO.new(%w(a b c).join("\n"))
357     app = lambda { |env| [ 200, {'Content-Length'=>'5'}, s_body] }
358     cl = Clogger.new(app, :logger => [], :format => '$pid')
359     status, headers, body = cl.call(@req)
360     assert ! s_body.closed?
361     assert_nothing_raised { body.close }
362     assert s_body.closed?
363   end
365   def test_escape
366     str = StringIO.new
367     app = lambda { |env| [ 200, {'Content-Length'=>'5'}, [] ] }
368     cl = Clogger.new(app,
369       :logger => str,
370       :format => '$http_user_agent "$request"')
371     bad = {
372       'HTTP_USER_AGENT' => '"asdf"',
373       'QUERY_STRING' => 'sdf=bar"',
374       'PATH_INFO' => '/"<>"',
375     }
376     status, headers, body = cl.call(@req.merge(bad))
377     expect = '\x22asdf\x22 "GET /\x22<>\x22?sdf=bar\x22 HTTP/1.0"' << "\n"
378     assert_equal expect, str.string
379   end
381   # rack allows repeated headers with "\n":
382   # { 'Set-Cookie' => "a\nb" } =>
383   #   Set-Cookie: a
384   #   Set-Cookie: b
385   def test_escape_header_newlines
386     str = StringIO.new
387     app = lambda { |env| [302, { 'Set-Cookie' => "a\nb" }, [] ] }
388     cl = Clogger.new(app, :logger => str, :format => '$sent_http_set_cookie')
389     cl.call(@req)
390     assert_equal "a\\x0Ab\n", str.string
391   end
393   def test_request_uri_fallback
394     str = StringIO.new
395     app = lambda { |env| [ 200, {}, [] ] }
396     cl = Clogger.new(app, :logger => str, :format => '$request_uri')
397     status, headers, body = cl.call(@req)
398     assert_equal "/hello?goodbye=true\n", str.string
399   end
401   def test_request_uri_set
402     str = StringIO.new
403     app = lambda { |env| [ 200, {}, [] ] }
404     cl = Clogger.new(app, :logger => str, :format => '$request_uri')
405     status, headers, body = cl.call(@req.merge("REQUEST_URI" => '/zzz'))
406     assert_equal "/zzz\n", str.string
407   end
409   def test_cookies
410     str = StringIO.new
411     app = lambda { |env|
412       req = Rack::Request.new(env).cookies
413       [ 302, {}, [] ]
414     }
415     cl = Clogger.new(app,
416         :format => '$cookie_foo $cookie_quux',
417         :logger => str)
418     req = @req.merge('HTTP_COOKIE' => "foo=bar;quux=h&m")
419     status, headers, body = cl.call(req)
420     assert_equal "bar h&m\n", str.string
421   end
423   def test_bogus_app_response
424     str = StringIO.new
425     app = lambda { |env| 302 }
426     cl = Clogger.new(app, :logger => str)
427     assert_raise(TypeError) { cl.call(@req) }
428     str = str.string
429     e = Regexp.quote " \"GET /hello?goodbye=true HTTP/1.0\" 500 -"
430     assert_match %r{#{e}$}m, str
431   end
433   def test_broken_header_response
434     str = StringIO.new
435     app = lambda { |env| [302, [ %w(a) ], []] }
436     cl = Clogger.new(app, :logger => str, :format => '$sent_http_set_cookie')
437     assert_nothing_raised { cl.call(@req) }
438   end
440   def test_subclass_hash
441     str = StringIO.new
442     req = Rack::Utils::HeaderHash.new(@req)
443     app = lambda { |env| [302, [ %w(a) ], []] }
444     cl = Clogger.new(app, :logger => str, :format => Rack_1_0)
445     assert_nothing_raised { cl.call(req).last.each {}.close }
446     assert str.size > 0
447   end
449   def test_subclassed_string_req
450     str = StringIO.new
451     req = {}
452     @req.each { |key,value|
453       req[FooString.new(key)] = value.kind_of?(String) ?
454                                 FooString.new(value) : value
455     }
456     app = lambda { |env| [302, [ %w(a) ], []] }
457     cl = Clogger.new(app, :logger => str, :format => Rack_1_0)
458     assert_nothing_raised { cl.call(req).last.each {}.close }
459     assert str.size > 0
460   end
462   def test_subclassed_string_in_body
463     str = StringIO.new
464     body = "hello"
465     r = nil
466     app = lambda { |env| [302, [ %w(a) ], [FooString.new(body)]] }
467     cl = Clogger.new(app, :logger => str, :format => '$body_bytes_sent')
468     assert_nothing_raised { cl.call(@req).last.each { |x| r = x }.close }
469     assert str.size > 0
470     assert_equal body.size.to_s << "\n", str.string
471     assert_equal r, body
472     assert r.object_id != body.object_id
473   end
475   def test_http_09_request
476     str = StringIO.new
477     app = lambda { |env| [302, [ %w(a) ], []] }
478     cl = Clogger.new(app, :logger => str, :format => '$request')
479     req = @req.dup
480     req.delete 'HTTP_VERSION'
481     cl.call(req)
482     assert_equal "GET /hello?goodbye=true\n", str.string
483   end
485   def test_request_method_only
486     str = StringIO.new
487     app = lambda { |env| [302, [ %w(a) ], []] }
488     cl = Clogger.new(app, :logger => str, :format => '$request_method')
489     cl.call(@req)
490     assert_equal "GET\n", str.string
491   end
493   def test_content_length_null
494     str = StringIO.new
495     app = lambda { |env| [302, [ %w(a) ], []] }
496     cl = Clogger.new(app, :logger => str, :format => '$content_length')
497     cl.call(@req)
498     assert_equal "-\n", str.string
499   end
501   def test_content_length_set
502     str = StringIO.new
503     app = lambda { |env| [302, [ %w(a) ], []] }
504     cl = Clogger.new(app, :logger => str, :format => '$content_length')
505     cl.call(@req.merge('CONTENT_LENGTH' => '5'))
506     assert_equal "5\n", str.string
507   end
509   def test_http_content_type_fallback
510     str = StringIO.new
511     app = lambda { |env| [302, [ %w(a) ], []] }
512     cl = Clogger.new(app, :logger => str, :format => '$http_content_type')
513     cl.call(@req.merge('CONTENT_TYPE' => 'text/plain'))
514     assert_equal "text/plain\n", str.string
515   end
517   def test_clogger_synced
518     io = StringIO.new
519     logger = Struct.new(:sync, :io).new(false, io)
520     assert ! logger.sync
521     def logger.<<(str)
522       io << str
523     end
524     app = lambda { |env| [302, [ %w(a) ], []] }
525     cl = Clogger.new(app, :logger => logger)
526     assert logger.sync
527   end
529   def test_clogger_unsyncable
530     logger = ''
531     assert ! logger.respond_to?('sync=')
532     app = lambda { |env| [302, [ %w(a) ], []] }
533     assert_nothing_raised { Clogger.new(app, :logger => logger) }
534   end
536   def test_clogger_no_ORS
537     s = ''
538     app = lambda { |env| [302, [ %w(a) ], []] }
539     cl = Clogger.new(app, :logger => s, :format => "$request", :ORS => "")
540     cl.call(@req)
541     assert_equal "GET /hello?goodbye=true HTTP/1.0", s
542   end
544   def test_clogger_weird_ORS
545     s = ''
546     app = lambda { |env| [302, [ %w(a) ], []] }
547     cl = Clogger.new(app, :logger => s, :format => "<$request", :ORS => ">")
548     cl.call(@req)
549     assert_equal "<GET /hello?goodbye=true HTTP/1.0>", s
550   end
552   def test_clogger_body_not_closeable
553     s = ''
554     app = lambda { |env| [302, [ %w(a) ], []] }
555     cl = Clogger.new(app, :logger => s)
556     status, headers, body = cl.call(@req)
557     assert_nil body.close
558   end
560   def test_clogger_response_frozen
561     response = [ 200, { "AAAA" => "AAAA"}.freeze, [].freeze ].freeze
562     s = StringIO.new("")
563     app = Rack::Builder.new do
564       use Clogger, :logger => s, :format => "$request_time $http_host"
565       run lambda { |env| response }
566     end
567     assert_nothing_raised do
568       3.times do
569         resp = app.call(@req)
570         assert ! resp.frozen?
571         resp.last.each { |x| }
572       end
573     end
574   end
576   def test_clogger_body_close_return_value
577     s = ''
578     body = []
579     def body.close
580       :foo
581     end
582     app = lambda { |env| [302, [ %w(a) ], body ] }
583     cl = Clogger.new(app, :logger => s)
584     status, headers, body = cl.call(@req)
585     assert_equal :foo, body.close
586   end
588   def test_clogger_auto_reentrant_true
589     s = ''
590     body = []
591     app = lambda { |env| [302, [ %w(a) ], body ] }
592     cl = Clogger.new(app, :logger => s, :format => "$request_time")
593     @req['rack.multithread'] = true
594     status, headers, body = cl.call(@req)
595     assert cl.reentrant?
596   end
598   def test_clogger_auto_reentrant_false
599     s = ''
600     body = []
601     app = lambda { |env| [302, [ %w(a) ], body ] }
602     cl = Clogger.new(app, :logger => s, :format => "$request_time")
603     @req['rack.multithread'] = false
604     status, headers, body = cl.call(@req)
605     assert ! cl.reentrant?
606   end
608   def test_clogger_auto_reentrant_forced_true
609     s = ''
610     body = []
611     app = lambda { |env| [302, [ %w(a) ], body ] }
612     o = { :logger => s, :format => "$request_time", :reentrant => true }
613     cl = Clogger.new(app, o)
614     @req['rack.multithread'] = false
615     status, headers, body = cl.call(@req)
616     assert cl.reentrant?
617   end
619   def test_clogger_auto_reentrant_forced_false
620     s = ''
621     body = []
622     app = lambda { |env| [302, [ %w(a) ], body ] }
623     o = { :logger => s, :format => "$request_time", :reentrant => false }
624     cl = Clogger.new(app, o)
625     @req['rack.multithread'] = true
626     status, headers, body = cl.call(@req)
627     assert ! cl.reentrant?
628   end
630   def test_invalid_status
631     s = []
632     body = []
633     app = lambda { |env| [ env["force.status"], [ %w(a b) ], body ] }
634     o = { :logger => s, :format => "$status" }
635     cl = Clogger.new(app, o)
636     status, headers, body = cl.call(@req.merge("force.status" => -1))
637     assert_equal -1, status
638     assert_equal "-\n", s.last
639     status, headers, body = cl.call(@req.merge("force.status" => 1000))
640     assert_equal 1000, status
641     assert_equal "-\n", s.last
642     u64_max = 0xffffffffffffffff
643     status, headers, body = cl.call(@req.merge("force.status" => u64_max))
644     assert_equal u64_max, status
645     assert_equal "-\n", s.last
646   end
648   # so we don't  care about the portability of this test
649   # if it doesn't leak on Linux, it won't leak anywhere else
650   # unless your C compiler or platform is otherwise broken
651   LINUX_PROC_PID_STATUS = "/proc/self/status"
652   def test_memory_leak
653     app = lambda { |env| [ 0, {}, [] ] }
654     clogger = Clogger.new(app, :logger => $stderr)
655     match_rss = /^VmRSS:\s+(\d+)/
656     if File.read(LINUX_PROC_PID_STATUS) =~ match_rss
657       before = $1.to_i
658       1000000.times { clogger.dup }
659       File.read(LINUX_PROC_PID_STATUS) =~ match_rss
660       after = $1.to_i
661       diff = after - before
662       assert(diff < 10000, "memory grew more than 10M: #{diff}")
663     end
664   end if RUBY_PLATFORM =~ /linux/ && File.readable?(LINUX_PROC_PID_STATUS)
666   def test_path_open_file
667     tmp = Tempfile.new('test_clogger')
668     app = lambda { |env| [ 200, {}, [] ] }
669     app = Clogger.new(app, :format => '$status', :path => tmp.path)
670     assert_kind_of Integer, app.fileno
671     assert app.fileno != tmp.fileno
672     status, headers, body = app.call(@req)
673     assert_equal "200\n", tmp.read
674   end
676   def test_path_logger_conflict
677     tmp = Tempfile.new('test_clogger')
678     app = lambda { |env| [ 200, {}, [] ] }
679     assert_raises(ArgumentError) {
680       Clogger.new(app, :logger=> $stderr, :path => tmp.path)
681     }
682   end
684   def test_request_time
685     s = []
686     app = lambda { |env| sleep(0.1) ; [302, [], [] ] }
687     cl = Clogger.new(app, :logger => s, :format => "$request_time")
688     status, headers, body = cl.call(@req)
689     assert_nothing_raised { body.each { |x| } ; body.close }
690     assert s[-1].to_f >= 0.100
691     assert s[-1].to_f <= 0.110
692   end
694   def test_insanely_long_time_format
695     s = []
696     app = lambda { |env| [200, [], [] ] }
697     fmt = '%Y' * 100
698     expect = Time.now.utc.strftime(fmt) << "\n"
699     assert_equal 100 * 4 + 1, expect.size
700     cl = Clogger.new(app, :logger => s, :format => "$time_utc{#{fmt}}")
701     status, headers, body = cl.call(@req)
702     assert_equal expect, s[0]
703   end
705   def test_time_utc
706     s = []
707     app = lambda { |env| [200, [], [] ] }
708     cl = Clogger.new(app, :logger => s, :format => "$time_utc")
709     status, headers, body = cl.call(@req)
710     assert %r!\A\d+/\w+/\d{4}:\d\d:\d\d:\d\d \+0000\n\z! =~ s[0], s.inspect
711   end
713   def test_time_iso8601
714     s = []
715     app = lambda { |env| [200, [], [] ] }
716     cl = Clogger.new(app, :logger => s, :format => "$time_iso8601")
717     status, headers, body = cl.call(@req)
718     t = Time.parse(s[0])
719     assert_equal t.iso8601, s[0].strip
720   end
722   def test_time_iso8601_pst8pdt
723     ENV["TZ"] = "PST8PDT"
724     s = []
725     app = lambda { |env| [200, [], [] ] }
726     cl = Clogger.new(app, :logger => s, :format => "$time_iso8601")
727     status, headers, body = cl.call(@req)
728     t = Time.parse(s[0])
729     assert_equal t.iso8601, s[0].strip
730   end
732   def test_time_iso8601_utc
733     ENV["TZ"] = "UTC"
734     s = []
735     app = lambda { |env| [200, [], [] ] }
736     cl = Clogger.new(app, :logger => s, :format => "$time_iso8601")
737     status, headers, body = cl.call(@req)
738     t = Time.parse(s[0])
739     assert_equal t.iso8601, s[0].strip
740   end
742   def test_time_local
743     s = []
744     app = lambda { |env| [200, [], [] ] }
745     cl = Clogger.new(app, :logger => s, :format => "$time_local")
746     status, headers, body = cl.call(@req)
747     t = DateTime.strptime(s[0].strip, @nginx_fmt)
748     assert_equal t.strftime(@nginx_fmt), s[0].strip
749   end
751   def test_time_local_pst8pdt
752     orig = ENV["TZ"]
753     ENV["TZ"] = "PST8PDT"
754     s = []
755     app = lambda { |env| [200, [], [] ] }
756     cl = Clogger.new(app, :logger => s, :format => "$time_local")
757     status, headers, body = cl.call(@req)
758     t = DateTime.strptime(s[0].strip, @nginx_fmt)
759     assert_equal t.strftime(@nginx_fmt), s[0].strip
760   end
762   def test_time_local_utc
763     ENV["TZ"] = "UTC"
764     s = []
765     app = lambda { |env| [200, [], [] ] }
766     cl = Clogger.new(app, :logger => s, :format => "$time_local")
767     status, headers, body = cl.call(@req)
768     t = DateTime.strptime(s[0].strip, @nginx_fmt)
769     assert_equal t.strftime(@nginx_fmt), s[0].strip
770   end
772   def test_method_missing
773     s = []
774     body = []
775     def body.foo_bar(foo)
776       [ foo.to_s ]
777     end
778     def body.noargs
779       :hello
780     end
781     def body.omg(&block)
782       yield :PONIES
783     end
784     app = lambda { |env| [200, [], body ] }
785     cl = Clogger.new(app, :logger => s, :format => '$body_bytes_sent')
786     status, headers, body = cl.call(@req)
787     assert_nothing_raised do
788       body.each { |x| s << x }
789       body.close
790     end
791     assert_equal "0\n", s[0], s.inspect
792     assert_kind_of Clogger, body
793     assert_equal %w(1), body.foo_bar(1)
794     assert_equal :hello, body.noargs
795     body.omg { |x| s << x }
796     assert_equal :PONIES, s[1]
797     assert_equal 2, s.size
798   end
800   def test_full_uri
801     s = []
802     format = '"$request_method ' \
803              '$env{rack.url_scheme}://$http_host$request_uri $http_version"'
804     app = lambda { |env| [200, [], [] ] }
805     cl = Clogger.new(app, :logger => s, :format => format)
806     @req["HTTP_HOST"] = "example.com"
807     status, headers, body = cl.call(@req)
808     expect = "\"GET http://example.com/hello?goodbye=true HTTP/1.0\"\n"
809     assert_equal [ expect ], s
810   end