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-03-18 17:56:05 +00:00
|
|
|
Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
|
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
|
|
|
|
Dependencies.stubs(:load?).returns(true)
|
2005-06-24 11:57:40 +00:00
|
|
|
|
2007-09-23 02:11:44 +00:00
|
|
|
ActionController::Routing::Routes.expects(:reload).once
|
|
|
|
Dependencies.expects(:clear).once
|
2005-06-24 11:57:40 +00:00
|
|
|
|
2005-07-13 02:12:00 +00:00
|
|
|
dispatch
|
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
|
|
|
Dependencies.stubs(:load?).returns(false)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2007-09-23 02:11:44 +00:00
|
|
|
def test_failsafe_response
|
|
|
|
CGI.expects(:new).raises('some multipart parsing failure')
|
2005-11-02 01:20:36 +00:00
|
|
|
|
2007-09-23 02:11:44 +00:00
|
|
|
ActionController::Routing::Routes.stubs(:reload)
|
2008-04-11 23:44:42 +00:00
|
|
|
Dispatcher.any_instance.stubs(: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
|
|
|
|
2007-09-26 01:24:07 +00:00
|
|
|
def test_reload_application_sets_unprepared_if_loading_dependencies
|
|
|
|
Dependencies.stubs(:load?).returns(false)
|
|
|
|
ActionController::Routing::Routes.expects(:reload).never
|
|
|
|
@dispatcher.unprepared = false
|
2007-10-02 05:32:14 +00:00
|
|
|
@dispatcher.send!(:reload_application)
|
2007-09-26 01:24:07 +00:00
|
|
|
assert !@dispatcher.unprepared
|
|
|
|
|
|
|
|
Dependencies.stubs(:load?).returns(true)
|
|
|
|
ActionController::Routing::Routes.expects(:reload).once
|
2007-10-02 05:32:14 +00:00
|
|
|
@dispatcher.send!(:reload_application)
|
2007-09-26 01:24:07 +00:00
|
|
|
assert @dispatcher.unprepared
|
|
|
|
end
|
2007-09-23 02:11:44 +00:00
|
|
|
|
2007-09-26 01:24:07 +00:00
|
|
|
def test_prepare_application_runs_callbacks_if_unprepared
|
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
|
|
|
|
|
|
|
# Skip the callbacks when already prepared.
|
|
|
|
@dispatcher.unprepared = false
|
2007-10-02 05:32:14 +00:00
|
|
|
@dispatcher.send! :prepare_application
|
2007-09-26 01:24:07 +00:00
|
|
|
assert_nil a || b || c
|
|
|
|
|
|
|
|
# Perform the callbacks when unprepared.
|
|
|
|
@dispatcher.unprepared = true
|
2007-10-02 05:32:14 +00:00
|
|
|
@dispatcher.send! :prepare_application
|
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
|
|
|
|
2006-08-06 02:51:53 +00:00
|
|
|
# But when not :load, make sure they are only run once
|
|
|
|
a = b = c = nil
|
2007-10-02 05:32:14 +00:00
|
|
|
@dispatcher.send! :prepare_application
|
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
|
|
|
|
|
|
|
@dispatcher.unprepared = true
|
2007-10-02 05:32:14 +00:00
|
|
|
@dispatcher.send! :prepare_application
|
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
|
|
|
|
2007-10-16 16:36:52 +00:00
|
|
|
def test_to_prepare_only_runs_once_if_not_loading_dependencies
|
|
|
|
Dependencies.stubs(:load?).returns(false)
|
|
|
|
called = 0
|
2008-04-01 06:11:56 +00:00
|
|
|
Dispatcher.to_prepare(:unprepared_test) { |*args| called += 1 }
|
2007-10-16 16:36:52 +00:00
|
|
|
2.times { dispatch }
|
|
|
|
assert_equal 1, called
|
|
|
|
end
|
|
|
|
|
2005-06-24 11:57:40 +00:00
|
|
|
private
|
2006-08-29 16:16:59 +00:00
|
|
|
def dispatch(output = @output)
|
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)
|
|
|
|
|
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
|