Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / controller / dispatcher_test.rb
blobec937ebfd26ab72bef8a6924d416e803738f043c
1 require "#{File.dirname(__FILE__)}/../abstract_unit"
3 uses_mocha 'dispatcher tests' do
5 require 'action_controller/dispatcher'
7 class DispatcherTest < Test::Unit::TestCase
8   Dispatcher = ActionController::Dispatcher
10   def setup
11     @output = StringIO.new
12     ENV['REQUEST_METHOD'] = 'GET'
14     Dispatcher.callbacks[:prepare].clear
15     @dispatcher = Dispatcher.new(@output)
16   end
18   def teardown
19     ENV['REQUEST_METHOD'] = nil
20   end
22   def test_clears_dependencies_after_dispatch_if_in_loading_mode
23     Dependencies.stubs(:load?).returns(true)
25     ActionController::Routing::Routes.expects(:reload).once
26     Dependencies.expects(:clear).once
28     dispatch
29   end
31   def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
32     Dependencies.stubs(:load?).returns(false)
34     ActionController::Routing::Routes.expects(:reload).never
35     Dependencies.expects(:clear).never
37     dispatch
38   end
40   def test_failsafe_response
41     CGI.expects(:new).raises('some multipart parsing failure')
43     ActionController::Routing::Routes.stubs(:reload)
44     Dispatcher.stubs(:log_failsafe_exception)
46     assert_nothing_raised { dispatch }
48     assert_equal "Status: 400 Bad Request\r\nContent-Type: text/html\r\n\r\n<html><body><h1>400 Bad Request</h1></body></html>", @output.string
49   end
51   def test_reload_application_sets_unprepared_if_loading_dependencies
52     Dependencies.stubs(:load?).returns(false)
53     ActionController::Routing::Routes.expects(:reload).never
54     @dispatcher.unprepared = false
55     @dispatcher.send!(:reload_application)
56     assert !@dispatcher.unprepared
58     Dependencies.stubs(:load?).returns(true)
59     ActionController::Routing::Routes.expects(:reload).once
60     @dispatcher.send!(:reload_application)
61     assert @dispatcher.unprepared
62   end
64   def test_prepare_application_runs_callbacks_if_unprepared
65     a = b = c = nil
66     Dispatcher.to_prepare { a = b = c = 1 }
67     Dispatcher.to_prepare { b = c = 2 }
68     Dispatcher.to_prepare { c = 3 }
70     # Skip the callbacks when already prepared.
71     @dispatcher.unprepared = false
72     @dispatcher.send! :prepare_application
73     assert_nil a || b || c
75     # Perform the callbacks when unprepared.
76     @dispatcher.unprepared = true
77     @dispatcher.send! :prepare_application
78     assert_equal 1, a
79     assert_equal 2, b
80     assert_equal 3, c
82     # But when not :load, make sure they are only run once
83     a = b = c = nil
84     @dispatcher.send! :prepare_application
85     assert_nil a || b || c
86   end
88   def test_to_prepare_with_identifier_replaces
89     a = b = nil
90     Dispatcher.to_prepare(:unique_id) { a = b = 1 }
91     Dispatcher.to_prepare(:unique_id) { a = 2 }
93     @dispatcher.unprepared = true
94     @dispatcher.send! :prepare_application
95     assert_equal 2, a
96     assert_equal nil, b
97   end
99   def test_to_prepare_only_runs_once_if_not_loading_dependencies
100     Dependencies.stubs(:load?).returns(false)
101     called = 0
102     Dispatcher.to_prepare(:unprepared_test) { called += 1 }
103     2.times { dispatch }
104     assert_equal 1, called
105   end
107   private
108     def dispatch(output = @output)
109       controller = mock
110       controller.stubs(:process).returns(controller)
111       controller.stubs(:out).with(output).returns('response')
113       ActionController::Routing::Routes.stubs(:recognize).returns(controller)
115       Dispatcher.dispatch(nil, {}, output)
116     end
118     def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
119       assert_equal howmany, klass.subclasses.size, message
120     end