2008-01-05 13:32:06 +00:00
|
|
|
require 'abstract_unit'
|
2007-09-23 02:11:44 +00:00
|
|
|
|
|
|
|
uses_mocha 'dispatcher tests' do
|
|
|
|
|
2005-06-24 11:57:40 +00:00
|
|
|
class DispatcherTest < Test::Unit::TestCase
|
2007-09-26 01:24:07 +00:00
|
|
|
Dispatcher = ActionController::Dispatcher
|
|
|
|
|
2005-06-24 11:57:40 +00:00
|
|
|
def setup
|
2007-09-26 01:24:07 +00:00
|
|
|
ENV['REQUEST_METHOD'] = 'GET'
|
2006-08-29 16:16:59 +00:00
|
|
|
|
2008-04-17 22:49:03 +00:00
|
|
|
# Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks
|
2008-03-18 17:56:05 +00:00
|
|
|
Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
|
2008-04-17 22:49:03 +00:00
|
|
|
Dispatcher.instance_variable_set("@before_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
|
|
|
|
Dispatcher.instance_variable_set("@after_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
|
|
|
|
|
|
|
|
Dispatcher.stubs(:require_dependency)
|
|
|
|
|
2008-12-05 02:39:36 +00:00
|
|
|
@dispatcher = Dispatcher.new
|
2005-06-24 11:57:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2007-12-27 11:17:53 +00:00
|
|
|
ENV.delete 'REQUEST_METHOD'
|
2005-06-24 11:57:40 +00:00
|
|
|
end
|
|
|
|
|
2007-09-23 02:11:44 +00:00
|
|
|
def test_clears_dependencies_after_dispatch_if_in_loading_mode
|
2008-06-03 18:32:53 +00:00
|
|
|
ActiveSupport::Dependencies.expects(:clear).once
|
2008-12-05 02:39:36 +00:00
|
|
|
dispatch(false)
|
2008-10-20 18:21:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reloads_routes_before_dispatch_if_in_loading_mode
|
|
|
|
ActionController::Routing::Routes.expects(:reload).once
|
2008-12-05 02:39:36 +00:00
|
|
|
dispatch(false)
|
2008-10-20 18:21:59 +00:00
|
|
|
end
|
2005-06-24 11:57:40 +00:00
|
|
|
|
2008-10-20 18:21:59 +00:00
|
|
|
def test_clears_asset_tag_cache_before_dispatch_if_in_loading_mode
|
|
|
|
ActionView::Helpers::AssetTagHelper::AssetTag::Cache.expects(:clear).once
|
2008-12-05 02:39:36 +00:00
|
|
|
dispatch(false)
|
2007-09-23 02:11:44 +00:00
|
|
|
end
|
|
|
|
|
2007-09-26 01:24:07 +00:00
|
|
|
def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
|
2007-09-23 02:11:44 +00:00
|
|
|
ActionController::Routing::Routes.expects(:reload).never
|
2008-06-03 18:32:53 +00:00
|
|
|
ActiveSupport::Dependencies.expects(:clear).never
|
2005-06-24 11:57:40 +00:00
|
|
|
|
2007-09-23 02:11:44 +00:00
|
|
|
dispatch
|
2005-06-24 11:57:40 +00:00
|
|
|
end
|
|
|
|
|
2008-04-15 23:04:12 +00:00
|
|
|
# Stub out dispatch error logger
|
|
|
|
class << Dispatcher
|
|
|
|
def log_failsafe_exception(status, exception); end
|
|
|
|
end
|
|
|
|
|
2007-09-23 02:11:44 +00:00
|
|
|
def test_failsafe_response
|
2008-12-18 18:00:54 +00:00
|
|
|
Dispatcher.any_instance.expects(:dispatch).raises('b00m')
|
2008-12-05 02:39:36 +00:00
|
|
|
ActionController::Failsafe.any_instance.expects(:log_failsafe_exception)
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
assert_equal [
|
|
|
|
500,
|
|
|
|
{"Content-Type" => "text/html"},
|
|
|
|
"<html><body><h1>500 Internal Server Error</h1></body></html>"
|
|
|
|
], dispatch
|
|
|
|
end
|
2005-11-02 01:20:36 +00:00
|
|
|
end
|
2007-09-23 02:11:44 +00:00
|
|
|
|
2008-04-17 22:49:03 +00:00
|
|
|
def test_prepare_callbacks
|
2006-08-06 02:51:53 +00:00
|
|
|
a = b = c = nil
|
2008-04-01 06:11:56 +00:00
|
|
|
Dispatcher.to_prepare { |*args| a = b = c = 1 }
|
|
|
|
Dispatcher.to_prepare { |*args| b = c = 2 }
|
|
|
|
Dispatcher.to_prepare { |*args| c = 3 }
|
2007-09-26 01:24:07 +00:00
|
|
|
|
2008-04-17 22:49:03 +00:00
|
|
|
# Ensure to_prepare callbacks are not run when defined
|
2007-09-26 01:24:07 +00:00
|
|
|
assert_nil a || b || c
|
|
|
|
|
2008-04-17 22:49:03 +00:00
|
|
|
# Run callbacks
|
|
|
|
@dispatcher.send :run_callbacks, :prepare_dispatch
|
|
|
|
|
2006-08-06 02:51:53 +00:00
|
|
|
assert_equal 1, a
|
|
|
|
assert_equal 2, b
|
|
|
|
assert_equal 3, c
|
2007-09-26 01:24:07 +00:00
|
|
|
|
2008-04-17 22:49:03 +00:00
|
|
|
# Make sure they are only run once
|
2006-08-06 02:51:53 +00:00
|
|
|
a = b = c = nil
|
2008-12-05 02:39:36 +00:00
|
|
|
dispatch
|
2007-09-26 01:24:07 +00:00
|
|
|
assert_nil a || b || c
|
2006-08-06 02:51:53 +00:00
|
|
|
end
|
2007-09-23 02:11:44 +00:00
|
|
|
|
2007-09-26 01:24:07 +00:00
|
|
|
def test_to_prepare_with_identifier_replaces
|
2006-08-06 02:51:53 +00:00
|
|
|
a = b = nil
|
2008-04-01 06:11:31 +00:00
|
|
|
Dispatcher.to_prepare(:unique_id) { |*args| a = b = 1 }
|
|
|
|
Dispatcher.to_prepare(:unique_id) { |*args| a = 2 }
|
2007-09-26 01:24:07 +00:00
|
|
|
|
2008-04-17 22:49:03 +00:00
|
|
|
@dispatcher.send :run_callbacks, :prepare_dispatch
|
2006-08-06 02:51:53 +00:00
|
|
|
assert_equal 2, a
|
|
|
|
assert_equal nil, b
|
|
|
|
end
|
2005-11-02 01:20:36 +00:00
|
|
|
|
2005-06-24 11:57:40 +00:00
|
|
|
private
|
2008-12-05 02:39:36 +00:00
|
|
|
def dispatch(cache_classes = true)
|
2008-12-18 19:14:09 +00:00
|
|
|
controller = mock()
|
|
|
|
controller.stubs(:process).returns([200, {}, 'response'])
|
|
|
|
ActionController::Routing::Routes.stubs(:recognize).returns(controller)
|
2008-04-17 22:49:03 +00:00
|
|
|
Dispatcher.define_dispatcher_callbacks(cache_classes)
|
2008-12-05 02:39:36 +00:00
|
|
|
@dispatcher.call({})
|
2006-08-29 16:16:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
|
|
|
|
assert_equal howmany, klass.subclasses.size, message
|
2005-07-13 02:12:00 +00:00
|
|
|
end
|
2007-03-01 23:29:56 +00:00
|
|
|
end
|
2007-09-23 02:11:44 +00:00
|
|
|
|
2007-09-26 01:24:07 +00:00
|
|
|
end
|