2007-03-01 18:29:56 -05:00
|
|
|
require "#{File.dirname(__FILE__)}/abstract_unit"
|
2007-09-22 22:11:44 -04:00
|
|
|
|
|
|
|
uses_mocha 'dispatcher tests' do
|
|
|
|
|
2005-06-24 07:57:40 -04:00
|
|
|
$:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"
|
|
|
|
|
|
|
|
require 'stringio'
|
|
|
|
require 'cgi'
|
|
|
|
|
|
|
|
require 'dispatcher'
|
|
|
|
require 'action_controller'
|
|
|
|
require 'action_mailer'
|
|
|
|
|
|
|
|
|
|
|
|
class DispatcherTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@output = StringIO.new
|
|
|
|
ENV['REQUEST_METHOD'] = "GET"
|
2006-08-29 12:16:59 -04:00
|
|
|
|
2006-08-05 22:51:53 -04:00
|
|
|
Dispatcher.send(:preparation_callbacks).clear
|
|
|
|
Dispatcher.send(:preparation_callbacks_run=, false)
|
2006-08-29 12:16:59 -04:00
|
|
|
|
2007-09-22 22:11:44 -04:00
|
|
|
Object.const_set 'ApplicationController', nil
|
2005-06-24 07:57:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
ENV['REQUEST_METHOD'] = nil
|
2007-09-22 22:11:44 -04:00
|
|
|
Object.send :remove_const, 'ApplicationController'
|
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
|
|
|
|
Dependencies.stubs(:load?).returns(true)
|
2005-06-24 07:57:40 -04:00
|
|
|
|
2007-09-22 22:11:44 -04:00
|
|
|
ActionController::Routing::Routes.expects(:reload).once
|
|
|
|
Dependencies.expects(:clear).once
|
2005-06-24 07:57:40 -04:00
|
|
|
|
2005-07-12 22:12:00 -04:00
|
|
|
dispatch
|
2007-09-22 22:11:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_clears_dependencies_after_dispatch_if_not_in_loading_mode
|
|
|
|
Dependencies.stubs(:load?).returns(false)
|
|
|
|
|
|
|
|
ActionController::Routing::Routes.expects(:reload).never
|
|
|
|
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
|
|
|
|
|
2007-09-22 22:11:44 -04:00
|
|
|
def test_failsafe_response
|
|
|
|
CGI.expects(:new).raises('some multipart parsing failure')
|
2005-11-01 20:20:36 -05:00
|
|
|
|
2007-09-22 22:11:44 -04:00
|
|
|
ActionController::Routing::Routes.stubs(:reload)
|
|
|
|
Dispatcher.stubs(:log_failsafe_exception)
|
|
|
|
|
|
|
|
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
|
|
|
|
2006-08-05 22:51:53 -04:00
|
|
|
def test_preparation_callbacks
|
2007-09-22 22:11:44 -04:00
|
|
|
ActionController::Routing::Routes.stubs(:reload)
|
|
|
|
|
2006-08-05 22:51:53 -04:00
|
|
|
old_mechanism = Dependencies.mechanism
|
|
|
|
|
|
|
|
a = b = c = nil
|
|
|
|
Dispatcher.to_prepare { a = b = c = 1 }
|
|
|
|
Dispatcher.to_prepare { b = c = 2 }
|
|
|
|
Dispatcher.to_prepare { c = 3 }
|
|
|
|
|
|
|
|
Dispatcher.send :prepare_application
|
|
|
|
|
|
|
|
assert_equal 1, a
|
|
|
|
assert_equal 2, b
|
|
|
|
assert_equal 3, c
|
|
|
|
|
|
|
|
# When mechanism is :load, perform the callbacks each request:
|
|
|
|
Dependencies.mechanism = :load
|
|
|
|
a = b = c = nil
|
|
|
|
Dispatcher.send :prepare_application
|
|
|
|
assert_equal 1, a
|
|
|
|
assert_equal 2, b
|
|
|
|
assert_equal 3, c
|
|
|
|
|
|
|
|
# But when not :load, make sure they are only run once
|
|
|
|
a = b = c = nil
|
|
|
|
Dependencies.mechanism = :not_load
|
|
|
|
Dispatcher.send :prepare_application
|
|
|
|
assert_equal nil, a || b || c
|
|
|
|
ensure
|
|
|
|
Dependencies.mechanism = old_mechanism
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_prepare_with_identifier_replaces
|
2007-09-22 22:11:44 -04:00
|
|
|
ActionController::Routing::Routes.stubs(:reload)
|
|
|
|
|
2006-08-05 22:51:53 -04:00
|
|
|
a = b = nil
|
|
|
|
Dispatcher.to_prepare(:unique_id) { a = b = 1 }
|
|
|
|
Dispatcher.to_prepare(:unique_id) { a = 2 }
|
|
|
|
|
|
|
|
Dispatcher.send :prepare_application
|
|
|
|
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
|
2006-08-29 12:16:59 -04:00
|
|
|
def dispatch(output = @output)
|
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)
|
|
|
|
|
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
|
|
|
|
|
|
|
end # uses_mocha
|