1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
require 'test/spec'
require 'stringio'
require 'rack/commonlogger'
require 'rack/lobster'
require 'rack/mock'
context "Rack::CommonLogger" do
app = lambda { |env|
[200,
{"Content-Type" => "text/html", "Content-Length" => length.to_s},
[obj]]}
app_without_length = lambda { |env|
[200,
{"Content-Type" => "text/html"},
[]]}
app_with_zero_length = lambda { |env|
[200,
{"Content-Type" => "text/html", "Content-Length" => "0"},
[]]}
specify "should log to rack.errors by default" do
res = Rack::MockRequest.new(Rack::CommonLogger.new(app)).get("/")
res.errors.should.not.be.empty
res.errors.should =~ /"GET \/ " 200 #{length} /
end
specify "should log to anything with +write+" do
log = StringIO.new
res = Rack::MockRequest.new(Rack::CommonLogger.new(app, log)).get("/")
log.string.should =~ /"GET \/ " 200 #{length} /
end
specify "should log - content length if header is missing" do
res = Rack::MockRequest.new(Rack::CommonLogger.new(app_without_length)).get("/")
res.errors.should.not.be.empty
res.errors.should =~ /"GET \/ " 200 - /
end
specify "should log - content length if header is zero" do
res = Rack::MockRequest.new(Rack::CommonLogger.new(app_with_zero_length)).get("/")
res.errors.should.not.be.empty
res.errors.should =~ /"GET \/ " 200 - /
end
def length
self.class.length
end
def self.length
123
end
def self.obj
"hello world"
end
end
|