2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2007-09-22 22:11:44 -04:00
|
|
|
|
|
|
|
uses_mocha 'dispatcher tests' do
|
|
|
|
|
2007-09-25 21:24:07 -04:00
|
|
|
require 'action_controller/dispatcher'
|
2005-06-24 07:57:40 -04:00
|
|
|
|
|
|
|
class DispatcherTest < Test::Unit::TestCase
|
2007-09-25 21:24:07 -04:00
|
|
|
Dispatcher = ActionController::Dispatcher
|
|
|
|
|
2005-06-24 07:57:40 -04:00
|
|
|
def setup
|
|
|
|
@output = StringIO.new
|
2007-09-25 21:24:07 -04:00
|
|
|
ENV['REQUEST_METHOD'] = 'GET'
|
2006-08-29 12:16:59 -04:00
|
|
|
|
2008-04-17 18:49:03 -04:00
|
|
|
# Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks
|
2008-03-18 13:56:05 -04:00
|
|
|
Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
|
2008-04-17 18:49:03 -04: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)
|
|
|
|
|
2007-09-25 21:24:07 -04:00
|
|
|
@dispatcher = Dispatcher.new(@output)
|
2005-06-24 07:57:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2007-12-27 06:17:53 -05:00
|
|
|
ENV.delete 'REQUEST_METHOD'
|
2005-06-24 07:57:40 -04:00
|
|
|
end
|
|
|
|
|
2007-09-22 22:11:44 -04:00
|
|
|
def test_clears_dependencies_after_dispatch_if_in_loading_mode
|
|
|
|
ActionController::Routing::Routes.expects(:reload).once
|
2008-06-03 14:32:53 -04:00
|
|
|
ActiveSupport::Dependencies.expects(:clear).once
|
2005-06-24 07:57:40 -04:00
|
|
|
|
2008-04-17 18:49:03 -04:00
|
|
|
dispatch(@output, false)
|
2007-09-22 22:11:44 -04:00
|
|
|
end
|
|
|
|
|
2007-09-25 21:24:07 -04:00
|
|
|
def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
|
2007-09-22 22:11:44 -04:00
|
|
|
ActionController::Routing::Routes.expects(:reload).never
|
2008-06-03 14:32:53 -04:00
|
|
|
ActiveSupport::Dependencies.expects(:clear).never
|
2005-06-24 07:57:40 -04:00
|
|
|
|
2007-09-22 22:11:44 -04:00
|
|
|
dispatch
|
2005-06-24 07:57:40 -04:00
|
|
|
end
|
|
|
|
|
2008-04-15 19:04:12 -04:00
|
|
|
# Stub out dispatch error logger
|
|
|
|
class << Dispatcher
|
|
|
|
def log_failsafe_exception(status, exception); end
|
|
|
|
end
|
|
|
|
|
2007-09-22 22:11:44 -04:00
|
|
|
def test_failsafe_response
|
|
|
|
CGI.expects(:new).raises('some multipart parsing failure')
|
2008-04-15 19:04:12 -04:00
|
|
|
Dispatcher.expects(:log_failsafe_exception)
|
2007-09-22 22:11:44 -04:00
|
|
|
|
|
|
|
assert_nothing_raised { dispatch }
|
|
|
|
|
|
|
|
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
|
2005-11-01 20:20:36 -05:00
|
|
|
end
|
2007-09-22 22:11:44 -04:00
|
|
|
|
2008-04-17 18:49:03 -04:00
|
|
|
def test_prepare_callbacks
|
2006-08-05 22:51:53 -04:00
|
|
|
a = b = c = nil
|
2008-04-01 02:11:56 -04:00
|
|
|
Dispatcher.to_prepare { |*args| a = b = c = 1 }
|
|
|
|
Dispatcher.to_prepare { |*args| b = c = 2 }
|
|
|
|
Dispatcher.to_prepare { |*args| c = 3 }
|
2007-09-25 21:24:07 -04:00
|
|
|
|
2008-04-17 18:49:03 -04:00
|
|
|
# Ensure to_prepare callbacks are not run when defined
|
2007-09-25 21:24:07 -04:00
|
|
|
assert_nil a || b || c
|
|
|
|
|
2008-04-17 18:49:03 -04:00
|
|
|
# Run callbacks
|
|
|
|
@dispatcher.send :run_callbacks, :prepare_dispatch
|
|
|
|
|
2006-08-05 22:51:53 -04:00
|
|
|
assert_equal 1, a
|
|
|
|
assert_equal 2, b
|
|
|
|
assert_equal 3, c
|
2007-09-25 21:24:07 -04:00
|
|
|
|
2008-04-17 18:49:03 -04:00
|
|
|
# Make sure they are only run once
|
2006-08-05 22:51:53 -04:00
|
|
|
a = b = c = nil
|
2008-04-17 18:49:03 -04:00
|
|
|
@dispatcher.send :dispatch
|
2007-09-25 21:24:07 -04:00
|
|
|
assert_nil a || b || c
|
2006-08-05 22:51:53 -04:00
|
|
|
end
|
2007-09-22 22:11:44 -04:00
|
|
|
|
2007-09-25 21:24:07 -04:00
|
|
|
def test_to_prepare_with_identifier_replaces
|
2006-08-05 22:51:53 -04:00
|
|
|
a = b = nil
|
2008-04-01 02:11:31 -04:00
|
|
|
Dispatcher.to_prepare(:unique_id) { |*args| a = b = 1 }
|
|
|
|
Dispatcher.to_prepare(:unique_id) { |*args| a = 2 }
|
2007-09-25 21:24:07 -04:00
|
|
|
|
2008-04-17 18:49:03 -04:00
|
|
|
@dispatcher.send :run_callbacks, :prepare_dispatch
|
2006-08-05 22:51:53 -04:00
|
|
|
assert_equal 2, a
|
|
|
|
assert_equal nil, b
|
|
|
|
end
|
2005-11-01 20:20:36 -05:00
|
|
|
|
2005-06-24 07:57:40 -04:00
|
|
|
private
|
2008-04-17 18:49:03 -04:00
|
|
|
def dispatch(output = @output, cache_classes = true)
|
2007-09-22 22:11:44 -04:00
|
|
|
controller = mock
|
|
|
|
controller.stubs(:process).returns(controller)
|
|
|
|
controller.stubs(:out).with(output).returns('response')
|
|
|
|
|
|
|
|
ActionController::Routing::Routes.stubs(:recognize).returns(controller)
|
|
|
|
|
2008-04-17 18:49:03 -04:00
|
|
|
Dispatcher.define_dispatcher_callbacks(cache_classes)
|
2006-08-29 12:16:59 -04:00
|
|
|
Dispatcher.dispatch(nil, {}, output)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
|
|
|
|
assert_equal howmany, klass.subclasses.size, message
|
2005-07-12 22:12:00 -04:00
|
|
|
end
|
2007-03-01 18:29:56 -05:00
|
|
|
end
|
2007-09-22 22:11:44 -04:00
|
|
|
|
2007-09-25 21:24:07 -04:00
|
|
|
end
|