Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / dispatcher_test.rb
1 require 'abstract_unit'
2
3 uses_mocha 'dispatcher tests' do
4
5 require 'action_controller/dispatcher'
6
7 class DispatcherTest < Test::Unit::TestCase
8 Dispatcher = ActionController::Dispatcher
9
10 def setup
11 @output = StringIO.new
12 ENV['REQUEST_METHOD'] = 'GET'
13
14 # Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks
15 Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
16 Dispatcher.instance_variable_set("@before_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
17 Dispatcher.instance_variable_set("@after_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
18
19 Dispatcher.stubs(:require_dependency)
20
21 @dispatcher = Dispatcher.new(@output)
22 end
23
24 def teardown
25 ENV.delete 'REQUEST_METHOD'
26 end
27
28 def test_clears_dependencies_after_dispatch_if_in_loading_mode
29 ActiveSupport::Dependencies.expects(:clear).once
30 dispatch(@output, false)
31 end
32
33 def test_reloads_routes_before_dispatch_if_in_loading_mode
34 ActionController::Routing::Routes.expects(:reload).once
35 dispatch(@output, false)
36 end
37
38 def test_clears_asset_tag_cache_before_dispatch_if_in_loading_mode
39 ActionView::Helpers::AssetTagHelper::AssetTag::Cache.expects(:clear).once
40 dispatch(@output, false)
41 end
42
43 def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
44 ActionController::Routing::Routes.expects(:reload).never
45 ActiveSupport::Dependencies.expects(:clear).never
46
47 dispatch
48 end
49
50 # Stub out dispatch error logger
51 class << Dispatcher
52 def log_failsafe_exception(status, exception); end
53 end
54
55 def test_failsafe_response
56 CGI.expects(:new).raises('some multipart parsing failure')
57 Dispatcher.expects(:log_failsafe_exception)
58
59 assert_nothing_raised { dispatch }
60
61 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
62 end
63
64 def test_prepare_callbacks
65 a = b = c = nil
66 Dispatcher.to_prepare { |*args| a = b = c = 1 }
67 Dispatcher.to_prepare { |*args| b = c = 2 }
68 Dispatcher.to_prepare { |*args| c = 3 }
69
70 # Ensure to_prepare callbacks are not run when defined
71 assert_nil a || b || c
72
73 # Run callbacks
74 @dispatcher.send :run_callbacks, :prepare_dispatch
75
76 assert_equal 1, a
77 assert_equal 2, b
78 assert_equal 3, c
79
80 # Make sure they are only run once
81 a = b = c = nil
82 @dispatcher.send :dispatch
83 assert_nil a || b || c
84 end
85
86 def test_to_prepare_with_identifier_replaces
87 a = b = nil
88 Dispatcher.to_prepare(:unique_id) { |*args| a = b = 1 }
89 Dispatcher.to_prepare(:unique_id) { |*args| a = 2 }
90
91 @dispatcher.send :run_callbacks, :prepare_dispatch
92 assert_equal 2, a
93 assert_equal nil, b
94 end
95
96 private
97 def dispatch(output = @output, cache_classes = true)
98 controller = mock
99 controller.stubs(:process).returns(controller)
100 controller.stubs(:out).with(output).returns('response')
101
102 ActionController::Routing::Routes.stubs(:recognize).returns(controller)
103
104 Dispatcher.define_dispatcher_callbacks(cache_classes)
105 Dispatcher.dispatch(nil, {}, output)
106 end
107
108 def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
109 assert_equal howmany, klass.subclasses.size, message
110 end
111 end
112
113 end