1 require 'abstract_unit'
3 uses_mocha
'dispatcher tests' do
5 require 'action_controller/dispatcher'
7 class DispatcherTest
< Test
::Unit::TestCase
8 Dispatcher
= ActionController
::Dispatcher
11 @output = StringIO
.new
12 ENV['REQUEST_METHOD'] = 'GET'
14 # Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks
15 Dispatcher
.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport
::Callbacks::CallbackChain.new
)
16 Dispatcher
.instance_variable_set("@before_dispatch_callbacks", ActiveSupport
::Callbacks::CallbackChain.new
)
17 Dispatcher
.instance_variable_set("@after_dispatch_callbacks", ActiveSupport
::Callbacks::CallbackChain.new
)
19 Dispatcher
.stubs(:require_dependency)
21 @dispatcher = Dispatcher
.new(@output)
25 ENV.delete
'REQUEST_METHOD'
28 def test_clears_dependencies_after_dispatch_if_in_loading_mode
29 ActiveSupport
::Dependencies.expects(:clear).once
30 dispatch(@output, false)
33 def test_reloads_routes_before_dispatch_if_in_loading_mode
34 ActionController
::Routing::Routes.expects(:reload).once
35 dispatch(@output, false)
38 def test_clears_asset_tag_cache_before_dispatch_if_in_loading_mode
39 ActionView
::Helpers::AssetTagHelper::AssetTag::Cache.expects(:clear).once
40 dispatch(@output, false)
43 def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
44 ActionController
::Routing::Routes.expects(:reload).never
45 ActiveSupport
::Dependencies.expects(:clear).never
50 # Stub out dispatch error logger
52 def log_failsafe_exception(status
, exception
); end
55 def test_failsafe_response
56 CGI
.expects(:new).raises('some multipart parsing failure')
57 Dispatcher
.expects(:log_failsafe_exception)
59 assert_nothing_raised
{ dispatch
}
61 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
64 def test_prepare_callbacks
66 Dispatcher
.to_prepare
{ |*args
| a
= b
= c
= 1 }
67 Dispatcher
.to_prepare
{ |*args
| b
= c
= 2 }
68 Dispatcher
.to_prepare
{ |*args
| c
= 3 }
70 # Ensure to_prepare callbacks are not run when defined
71 assert_nil a
|| b
|| c
74 @dispatcher.send
:run_callbacks, :prepare_dispatch
80 # Make sure they are only run once
82 @dispatcher.send
:dispatch
83 assert_nil a
|| b
|| c
86 def test_to_prepare_with_identifier_replaces
88 Dispatcher
.to_prepare(:unique_id) { |*args
| a
= b
= 1 }
89 Dispatcher
.to_prepare(:unique_id) { |*args
| a
= 2 }
91 @dispatcher.send
:run_callbacks, :prepare_dispatch
97 def dispatch(output
= @output, cache_classes
= true)
99 controller
.stubs(:process).returns(controller
)
100 controller
.stubs(:out).with(output
).returns('response')
102 ActionController
::Routing::Routes.stubs(:recognize).returns(controller
)
104 Dispatcher
.define_dispatcher_callbacks(cache_classes
)
105 Dispatcher
.dispatch(nil, {}, output
)
108 def assert_subclasses(howmany
, klass
, message
= klass
.subclasses
.inspect
)
109 assert_equal howmany
, klass
.subclasses
.size
, message