aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/notifications_test.rb
blob: d834a734ef8c173ddd21e3c3c5cf535191d07496 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'abstract_unit'

class NotificationsMiddlewareTest < ActionController::IntegrationTest
  Boomer = lambda do |env|
    req = ActionDispatch::Request.new(env)
    case req.path
    when "/"
      [200, {}, []]
    else
      raise "puke!"
    end
  end

  App = ActionDispatch::Notifications.new(Boomer)

  def setup
    @queue    = ActiveSupport::Notifications::Fanout.new
    @notifier = ActiveSupport::Notifications::Notifier.new(@queue)
    ActiveSupport::Notifications.notifier = @notifier

    @events = []
    ActiveSupport::Notifications.subscribe do |*args|
      @events << args
    end

    @app = App
  end

  test "publishes notifications" do
    get "/"
    ActiveSupport::Notifications.notifier.wait

    assert_equal 2, @events.size
    before, after = @events

    assert_equal 'action_dispatch.before_dispatch', before[0]
    assert_kind_of Hash, before[4][:env]
    assert_equal 'GET',  before[4][:env]["REQUEST_METHOD"]

    assert_equal 'action_dispatch.after_dispatch', after[0]
    assert_kind_of Hash, after[4][:env]
    assert_equal 'GET',  after[4][:env]["REQUEST_METHOD"]
  end

  test "publishes notifications on failure" do
    begin
      get "/puke"
    rescue
    end

    ActiveSupport::Notifications.notifier.wait

    assert_equal 3, @events.size
    before, after, exception = @events

    assert_equal 'action_dispatch.before_dispatch', before[0]
    assert_kind_of Hash, before[4][:env]
    assert_equal 'GET',  before[4][:env]["REQUEST_METHOD"]

    assert_equal 'action_dispatch.after_dispatch', after[0]
    assert_kind_of Hash, after[4][:env]
    assert_equal 'GET',  after[4][:env]["REQUEST_METHOD"]  

    assert_equal 'action_dispatch.exception', exception[0]
    assert_kind_of Hash, exception[4][:env]
    assert_equal 'GET',  exception[4][:env]["REQUEST_METHOD"]  
    assert_kind_of RuntimeError, exception[4][:exception]
  end
end