File: spec_runtime.rb

package info (click to toggle)
ruby-rack 2.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,492 kB
  • sloc: ruby: 14,778; sh: 12; makefile: 6; javascript: 1
file content (52 lines) | stat: -rw-r--r-- 1,656 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require 'minitest/global_expectations/autorun'
require 'rack/lint'
require 'rack/mock'
require 'rack/runtime'

describe Rack::Runtime do
  def runtime_app(app, *args)
    Rack::Lint.new Rack::Runtime.new(app, *args)
  end

  def request
    Rack::MockRequest.env_for
  end

  it "sets X-Runtime is none is set" do
    app = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, "Hello, World!"] }
    response = runtime_app(app).call(request)
    response[1]['X-Runtime'].must_match(/[\d\.]+/)
  end

  it "doesn't set the X-Runtime if it is already set" do
    app = lambda { |env| [200, { 'Content-Type' => 'text/plain', "X-Runtime" => "foobar" }, "Hello, World!"] }
    response = runtime_app(app).call(request)
    response[1]['X-Runtime'].must_equal "foobar"
  end

  it "allow a suffix to be set" do
    app = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, "Hello, World!"] }
    response = runtime_app(app, "Test").call(request)
    response[1]['X-Runtime-Test'].must_match(/[\d\.]+/)
  end

  it "allow multiple timers to be set" do
    app = lambda { |env| sleep 0.1; [200, { 'Content-Type' => 'text/plain' }, "Hello, World!"] }
    runtime = runtime_app(app, "App")

    # wrap many times to guarantee a measurable difference
    100.times do |i|
      runtime = Rack::Runtime.new(runtime, i.to_s)
    end
    runtime = Rack::Runtime.new(runtime, "All")

    response = runtime.call(request)

    response[1]['X-Runtime-App'].must_match(/[\d\.]+/)
    response[1]['X-Runtime-All'].must_match(/[\d\.]+/)

    Float(response[1]['X-Runtime-All']).must_be :>, Float(response[1]['X-Runtime-App'])
  end
end