2008-01-05 13:32:06 +00:00
|
|
|
require 'abstract_unit'
|
2007-09-23 02:11:44 +00:00
|
|
|
|
|
|
|
uses_mocha 'dispatcher tests' do
|
|
|
|
|
2007-09-26 01:24:07 +00:00
|
|
|
require 'action_controller/dispatcher'
|
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
|
|
|
|
@output = StringIO.new
|
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)
|
|
|
|
|
2007-09-26 01:24:07 +00:00
|
|
|
@dispatcher = Dispatcher.new(@output)
|
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
|
|
|
|
ActionController::Routing::Routes.expects(:reload).once
|
|
|
|
Dependencies.expects(:clear).once
|
2005-06-24 11:57:40 +00:00
|
|
|
|
2008-04-17 22:49:03 +00:00
|
|
|
dispatch(@output, 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
|
|
|
|
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
|
|
|
|
CGI.expects(:new).raises('some multipart parsing failure')
|
2008-04-15 23:04:12 +00:00
|
|
|
Dispatcher.expects(:log_failsafe_exception)
|
2007-09-23 02:11:44 +00: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-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-04-17 22:49:03 +00:00
|
|
|
@dispatcher.send :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-04-17 22:49:03 +00:00
|
|
|
def dispatch(output = @output, cache_classes = true)
|
2007-09-23 02:11:44 +00: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 22:49:03 +00:00
|
|
|
Dispatcher.define_dispatcher_callbacks(cache_classes)
|
2006-08-29 16:16:59 +00:00
|
|
|
Dispatcher.dispatch(nil, {}, output)
|
|
|
|
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
|