File: failsafe_test.rb

package info (click to toggle)
rails 2.3.5-1.2%2Bsqueeze8
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 18,248 kB
  • ctags: 20,944
  • sloc: ruby: 122,413; makefile: 72; sql: 43; sh: 1
file content (60 lines) | stat: -rw-r--r-- 2,057 bytes parent folder | download | duplicates (4)
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
require 'abstract_unit'
require 'stringio'
require 'logger'

class FailsafeTest < ActionController::TestCase
  FIXTURE_PUBLIC = "#{File.dirname(__FILE__)}/../fixtures/failsafe".freeze
  
  def setup
    @old_error_file_path = ActionController::Failsafe.error_file_path
    ActionController::Failsafe.error_file_path = FIXTURE_PUBLIC
    @app = mock
    @log_io = StringIO.new
    @logger = Logger.new(@log_io)
    @failsafe = ActionController::Failsafe.new(@app)
    @failsafe.stubs(:failsafe_logger).returns(@logger)
  end
  
  def teardown
    ActionController::Failsafe.error_file_path = @old_error_file_path
  end
  
  def app_will_raise_error!
    @app.expects(:call).then.raises(RuntimeError.new("Printer on fire"))
  end
  
  def test_calls_app_and_returns_its_return_value
    @app.expects(:call).returns([200, { "Content-Type" => "text/html" }, "ok"])
    assert_equal [200, { "Content-Type" => "text/html" }, "ok"], @failsafe.call({})
  end
  
  def test_writes_to_log_file_on_exception
    app_will_raise_error!
    @failsafe.call({})
    assert_match /Printer on fire/, @log_io.string     # Logs exception message.
    assert_match /failsafe_test\.rb/, @log_io.string   # Logs backtrace.
  end
  
  def test_returns_500_internal_server_error_on_exception
    app_will_raise_error!
    response = @failsafe.call({})
    assert_equal 3, response.size    # It is a valid Rack response.
    assert_equal 500, response[0]    # Status is 500.
  end
  
  def test_renders_error_page_file_with_erb
    app_will_raise_error!
    response = @failsafe.call({})
    assert_equal 500, response[0]
    assert_equal "hello my world", response[2].join
  end
  
  def test_returns_a_default_message_if_erb_rendering_failed
    app_will_raise_error!
    @failsafe.expects(:render_template).raises(RuntimeError.new("Harddisk is crashing"))
    response = @failsafe.call({})
    assert_equal 500, response[0]
    assert_match /500 Internal Server Error/, response[2].join
    assert_match %r(please read this web application's log file), response[2].join
  end
end