File: test_helper.rb

package info (click to toggle)
ruby-rack-timeout 0.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212 kB
  • sloc: ruby: 515; makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,114 bytes parent folder | download
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
require "test/unit"
require "rack"
require "rack/test"
require "rack/builder"
require "rack/null_logger"
require "rack-timeout"

class RackTimeoutTest < Test::Unit::TestCase
  include Rack::Test::Methods

  attr_accessor :settings

  def initialize(*args)
    self.settings ||= {}
    super(*args)
  end

  def app
    settings = self.settings
    Rack::Builder.new do
      use Rack::Timeout, **settings

      map "/" do
        run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }
      end

      map "/sleep" do
        run lambda { |env| sleep }
      end
    end
  end

  # runs the test with the given environment, but doesn't restore the original
  # environment afterwards. This should be sufficient for rack-timeout testing.
  def with_env(hash)
    hash.each_pair do |k, v|
      ENV[k.to_s] = v.to_s
    end
    yield
    hash.each_key do |k|
      ENV[k.to_s] = nil
    end
  end

  def time_in_msec(t = Time.now)
    "#{t.tv_sec}#{t.tv_usec/1000}"
  end

  def time_in_usec(t = Time.now)
    # time in microseconds, currently 16 digits
    "%d%06d" % [t.tv_sec, t.tv_usec]
  end
end