spec_runtime.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. require 'rack/runtime'
  2. describe Rack::Runtime do
  3. it "sets X-Runtime is none is set" do
  4. app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
  5. response = Rack::Runtime.new(app).call({})
  6. response[1]['X-Runtime'].should =~ /[\d\.]+/
  7. end
  8. it "doesn't set the X-Runtime if it is already set" do
  9. app = lambda { |env| [200, {'Content-Type' => 'text/plain', "X-Runtime" => "foobar"}, "Hello, World!"] }
  10. response = Rack::Runtime.new(app).call({})
  11. response[1]['X-Runtime'].should == "foobar"
  12. end
  13. should "allow a suffix to be set" do
  14. app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
  15. response = Rack::Runtime.new(app, "Test").call({})
  16. response[1]['X-Runtime-Test'].should =~ /[\d\.]+/
  17. end
  18. should "allow multiple timers to be set" do
  19. app = lambda { |env| sleep 0.1; [200, {'Content-Type' => 'text/plain'}, "Hello, World!"] }
  20. runtime = Rack::Runtime.new(app, "App")
  21. # wrap many times to guarantee a measurable difference
  22. 100.times do |i|
  23. runtime = Rack::Runtime.new(runtime, i.to_s)
  24. end
  25. runtime = Rack::Runtime.new(runtime, "All")
  26. response = runtime.call({})
  27. response[1]['X-Runtime-App'].should =~ /[\d\.]+/
  28. response[1]['X-Runtime-All'].should =~ /[\d\.]+/
  29. Float(response[1]['X-Runtime-All']).should > Float(response[1]['X-Runtime-App'])
  30. end
  31. end