spec_commonlogger.rb 1.4 KB

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