2009-12-19 03:16:58 +01:00
|
|
|
require 'rack/commonlogger'
|
|
|
|
require 'rack/mock'
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
describe Rack::CommonLogger do
|
|
|
|
obj = 'foobar'
|
|
|
|
length = obj.size
|
|
|
|
|
2009-12-19 03:16:58 +01:00
|
|
|
app = lambda { |env|
|
2009-12-26 21:00:18 +01:00
|
|
|
[200,
|
|
|
|
{"Content-Type" => "text/html", "Content-Length" => length.to_s},
|
|
|
|
[obj]]}
|
|
|
|
app_without_length = lambda { |env|
|
2009-12-19 03:16:58 +01:00
|
|
|
[200,
|
|
|
|
{"Content-Type" => "text/html"},
|
2009-12-26 21:00:18 +01:00
|
|
|
[]]}
|
|
|
|
app_with_zero_length = lambda { |env|
|
|
|
|
[200,
|
|
|
|
{"Content-Type" => "text/html", "Content-Length" => "0"},
|
|
|
|
[]]}
|
2009-12-19 03:16:58 +01:00
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "log to rack.errors by default" do
|
2009-12-19 03:16:58 +01:00
|
|
|
res = Rack::MockRequest.new(Rack::CommonLogger.new(app)).get("/")
|
|
|
|
|
|
|
|
res.errors.should.not.be.empty
|
2009-12-26 21:00:18 +01:00
|
|
|
res.errors.should =~ /"GET \/ " 200 #{length} /
|
2009-12-19 03:16:58 +01:00
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "log to anything with +write+" do
|
2009-12-26 21:00:18 +01:00
|
|
|
log = StringIO.new
|
2009-12-19 03:16:58 +01:00
|
|
|
res = Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/")
|
|
|
|
|
2009-12-26 21:00:18 +01:00
|
|
|
log.string.should =~ /"GET \/ " 200 #{length} /
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "log - content length if header is missing" do
|
2009-12-26 21:00:18 +01:00
|
|
|
res = Rack::MockRequest.new(Rack::CommonLogger.new(app_without_length)).get("/")
|
|
|
|
|
|
|
|
res.errors.should.not.be.empty
|
|
|
|
res.errors.should =~ /"GET \/ " 200 - /
|
|
|
|
end
|
|
|
|
|
2010-06-14 06:09:24 +02:00
|
|
|
should "log - content length if header is zero" do
|
2009-12-26 21:00:18 +01:00
|
|
|
res = Rack::MockRequest.new(Rack::CommonLogger.new(app_with_zero_length)).get("/")
|
|
|
|
|
|
|
|
res.errors.should.not.be.empty
|
|
|
|
res.errors.should =~ /"GET \/ " 200 - /
|
|
|
|
end
|
2010-06-14 06:09:24 +02:00
|
|
|
|
2009-12-26 21:00:18 +01:00
|
|
|
def length
|
|
|
|
123
|
|
|
|
end
|
2010-06-14 06:09:24 +02:00
|
|
|
|
2009-12-26 21:00:18 +01:00
|
|
|
def self.obj
|
|
|
|
"hello world"
|
2009-12-19 03:16:58 +01:00
|
|
|
end
|
|
|
|
end
|