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
|
|
|
|
|
|
|
dispatch do |env|
|
|
|
|
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
|
|
|
|
ActionDispatch::Callbacks.to_prepare { prepared = true }
|
|
|
|
ActionDispatch::Callbacks.to_prepare { cleaned = true }
|
2010-12-19 18:58:58 -05:00
|
|
|
|
|
|
|
ActionDispatch::Reloader.prepare!
|
|
|
|
assert prepared
|
2010-12-23 13:20:57 -05:00
|
|
|
|
|
|
|
ActionDispatch::Reloader.cleanup!
|
|
|
|
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
|