2010-01-14 13:53:07 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
2010-12-19 18:58:58 -05:00
|
|
|
class DispatcherTest < ActiveSupport::TestCase
|
2010-01-14 13:53:07 -05:00
|
|
|
class Foo
|
|
|
|
cattr_accessor :a, :b
|
|
|
|
end
|
|
|
|
|
|
|
|
class DummyApp
|
|
|
|
def call(env)
|
|
|
|
[200, {}, 'response']
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
end
|
2010-01-14 13:53:07 -05:00
|
|
|
|
|
|
|
def setup
|
|
|
|
Foo.a, Foo.b = 0, 0
|
|
|
|
ActionDispatch::Callbacks.reset_callbacks(:call)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_before_and_after_callbacks
|
|
|
|
ActionDispatch::Callbacks.before { |*args| Foo.a += 1; Foo.b += 1 }
|
|
|
|
ActionDispatch::Callbacks.after { |*args| Foo.a += 1; Foo.b += 1 }
|
|
|
|
|
|
|
|
dispatch
|
|
|
|
assert_equal 2, Foo.a
|
|
|
|
assert_equal 2, Foo.b
|
|
|
|
|
|
|
|
dispatch
|
|
|
|
assert_equal 4, Foo.a
|
|
|
|
assert_equal 4, Foo.b
|
2012-02-16 08:58:58 -05:00
|
|
|
|
2015-08-25 00:01:12 -04:00
|
|
|
dispatch do
|
2012-02-16 08:58:58 -05:00
|
|
|
raise "error"
|
|
|
|
end rescue nil
|
|
|
|
assert_equal 6, Foo.a
|
|
|
|
assert_equal 6, Foo.b
|
2010-01-14 13:53:07 -05:00
|
|
|
end
|
|
|
|
|
2010-12-23 13:20:57 -05:00
|
|
|
def test_to_prepare_and_cleanup_delegation
|
|
|
|
prepared = cleaned = false
|
2016-02-21 20:25:52 -05:00
|
|
|
assert_deprecated do
|
|
|
|
ActionDispatch::Callbacks.to_prepare { prepared = true }
|
|
|
|
ActionDispatch::Callbacks.to_prepare { cleaned = true }
|
|
|
|
end
|
2010-12-19 18:58:58 -05:00
|
|
|
|
2016-02-21 20:25:52 -05:00
|
|
|
assert_deprecated do
|
|
|
|
ActionDispatch::Reloader.prepare!
|
|
|
|
end
|
2010-12-19 18:58:58 -05:00
|
|
|
assert prepared
|
2010-12-23 13:20:57 -05:00
|
|
|
|
2016-02-21 20:25:52 -05:00
|
|
|
assert_deprecated do
|
|
|
|
ActionDispatch::Reloader.cleanup!
|
|
|
|
end
|
2010-12-23 13:20:57 -05:00
|
|
|
assert cleaned
|
2010-12-19 18:58:58 -05:00
|
|
|
end
|
|
|
|
|
2010-01-14 13:53:07 -05:00
|
|
|
private
|
|
|
|
|
2010-12-19 18:58:58 -05:00
|
|
|
def dispatch(&block)
|
2012-02-16 08:58:58 -05:00
|
|
|
ActionDispatch::Callbacks.new(block || DummyApp.new).call(
|
|
|
|
{'rack.input' => StringIO.new('')}
|
|
|
|
)
|
2010-01-14 13:53:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|