2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2010-01-14 13:53:07 -05:00
|
|
|
|
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)
|
2016-08-06 12:54:50 -04:00
|
|
|
[200, {}, "response"]
|
2010-01-14 13:53:07 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
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(
|
2016-08-06 13:44:11 -04:00
|
|
|
"rack.input" => StringIO.new("")
|
2012-02-16 08:58:58 -05:00
|
|
|
)
|
2010-01-14 13:53:07 -05:00
|
|
|
end
|
|
|
|
end
|