2008-05-18 06:22:34 +02:00
|
|
|
require 'abstract_unit'
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
class DispatcherTest < Test::Unit::TestCase
|
|
|
|
Dispatcher = ActionController::Dispatcher
|
|
|
|
|
|
|
|
def setup
|
|
|
|
ENV['REQUEST_METHOD'] = 'GET'
|
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
Dispatcher.middleware = ActionController::MiddlewareStack.new do |middleware|
|
|
|
|
middlewares = File.expand_path(File.join(File.dirname(__FILE__), "../../lib/action_controller/middlewares.rb"))
|
|
|
|
middleware.instance_eval(File.read(middlewares))
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks
|
|
|
|
Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
|
|
|
|
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)
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2008-05-18 06:22:34 +02:00
|
|
|
ENV.delete 'REQUEST_METHOD'
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_clears_dependencies_after_dispatch_if_in_loading_mode
|
2008-09-07 07:54:05 +02:00
|
|
|
ActiveSupport::Dependencies.expects(:clear).once
|
2009-08-04 17:16:03 +02:00
|
|
|
# Close the response so dependencies kicks in
|
|
|
|
dispatch(false).last.close
|
2008-10-27 07:47:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_reloads_routes_before_dispatch_if_in_loading_mode
|
|
|
|
ActionController::Routing::Routes.expects(:reload).once
|
2009-02-04 21:26:08 +01:00
|
|
|
dispatch(false)
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
|
|
|
|
ActionController::Routing::Routes.expects(:reload).never
|
2008-09-07 07:54:05 +02:00
|
|
|
ActiveSupport::Dependencies.expects(:clear).never
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
dispatch
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Stub out dispatch error logger
|
|
|
|
class << Dispatcher
|
|
|
|
def log_failsafe_exception(status, exception); end
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_failsafe_response
|
2009-02-04 21:26:08 +01:00
|
|
|
Dispatcher.any_instance.expects(:dispatch).raises('b00m')
|
|
|
|
ActionController::Failsafe.any_instance.expects(:log_failsafe_exception)
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
response = nil
|
2009-02-04 21:26:08 +01:00
|
|
|
assert_nothing_raised do
|
2009-08-04 17:16:03 +02:00
|
|
|
response = dispatch
|
2009-02-04 21:26:08 +01:00
|
|
|
end
|
2009-08-04 17:16:03 +02:00
|
|
|
assert_equal 3, response.size
|
|
|
|
assert_equal 500, response[0]
|
|
|
|
assert_equal({"Content-Type" => "text/html"}, response[1])
|
|
|
|
assert_match /500 Internal Server Error/, response[2].join
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def test_prepare_callbacks
|
2007-12-21 08:48:59 +01:00
|
|
|
a = b = c = nil
|
2008-05-18 06:22:34 +02:00
|
|
|
Dispatcher.to_prepare { |*args| a = b = c = 1 }
|
|
|
|
Dispatcher.to_prepare { |*args| b = c = 2 }
|
|
|
|
Dispatcher.to_prepare { |*args| c = 3 }
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Ensure to_prepare callbacks are not run when defined
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_nil a || b || c
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Run callbacks
|
2009-02-28 02:23:00 +01:00
|
|
|
Dispatcher.run_prepare_callbacks
|
2008-05-18 06:22:34 +02:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal 1, a
|
|
|
|
assert_equal 2, b
|
|
|
|
assert_equal 3, c
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
# Make sure they are only run once
|
2007-12-21 08:48:59 +01:00
|
|
|
a = b = c = nil
|
2009-02-04 21:26:08 +01:00
|
|
|
dispatch
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_nil a || b || c
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_prepare_with_identifier_replaces
|
|
|
|
a = b = nil
|
2008-05-18 06:22:34 +02:00
|
|
|
Dispatcher.to_prepare(:unique_id) { |*args| a = b = 1 }
|
|
|
|
Dispatcher.to_prepare(:unique_id) { |*args| a = 2 }
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2009-02-28 02:23:00 +01:00
|
|
|
Dispatcher.run_prepare_callbacks
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_equal 2, a
|
|
|
|
assert_equal nil, b
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2009-02-04 21:26:08 +01:00
|
|
|
def dispatch(cache_classes = true)
|
|
|
|
ActionController::Routing::RouteSet.any_instance.stubs(:call).returns([200, {}, 'response'])
|
2008-05-18 06:22:34 +02:00
|
|
|
Dispatcher.define_dispatcher_callbacks(cache_classes)
|
2009-08-04 17:16:03 +02:00
|
|
|
Dispatcher.new.call({'rack.input' => StringIO.new('')})
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
|
|
|
|
assert_equal howmany, klass.subclasses.size, message
|
|
|
|
end
|
|
|
|
end
|