aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/subscriber_test.rb
blob: e2c6150c5e564b6e1b3083dd5a75402c2e439c23 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require "abstract_unit"
require "rails/subscriber/test_helper"
require "action_controller/railties/subscriber"

module Another
  class SubscribersController < ActionController::Base
    def show
      render :nothing => true
    end

    def redirector
      redirect_to "http://foo.bar/"
    end
  end
end

module ActionControllerSubscriberTest
  Rails::Subscriber.add(:action_controller, ActionController::Railties::Subscriber.new)

  def self.included(base)
    base.tests Another::SubscribersController
  end

  def wait
    sleep(0.01)
    super
  end

  def setup
    @old_logger = ActionController::Base.logger
    super
  end

  def teardown
    super
    ActionController::Base.logger = @old_logger
  end

  def set_logger(logger)
    ActionController::Base.logger = logger
  end

  def test_process_action
    get :show
    wait
    assert_equal 3, @logger.logged(:info).size
    assert_match /Processed\sAnother::SubscribersController#show/, @logger.logged(:info)[0]
  end

  def test_process_action_without_parameters
    get :show
    wait
    assert_nil @logger.logged(:info).detect {|l| l =~ /Parameters/ }
  end

  def test_process_action_with_parameters
    get :show, :id => '10'
    wait

    assert_equal 4, @logger.logged(:info).size
    assert_equal 'Parameters: {"id"=>"10"}', @logger.logged(:info)[1]
  end

  def test_process_action_with_view_runtime
    get :show
    wait
    assert_match /View runtime/, @logger.logged(:info)[1]
  end

  def test_process_action_with_status_and_request_uri
    get :show
    wait
    last = @logger.logged(:info).last
    assert_match /Completed/, last
    assert_match /200/, last
    assert_match /another\/subscribers\/show/, last
  end

  def test_process_action_with_filter_parameters
    Another::SubscribersController.filter_parameter_logging(:lifo, :amount)

    get :show, :lifo => 'Pratik', :amount => '420', :step => '1'
    wait

    params = @logger.logged(:info)[1]
    assert_match /"amount"=>"\[FILTERED\]"/, params
    assert_match /"lifo"=>"\[FILTERED\]"/, params
    assert_match /"step"=>"1"/, params
  end

  def test_redirect_to
    get :redirector
    wait

    assert_equal 3, @logger.logged(:info).size
    assert_equal "Redirected to http://foo.bar/ with status 302", @logger.logged(:info)[0]
  end

  class SyncSubscriberTest < ActionController::TestCase
    include Rails::Subscriber::SyncTestHelper
    include ActionControllerSubscriberTest
  end

  class AsyncSubscriberTest < ActionController::TestCase
    include Rails::Subscriber::AsyncTestHelper
    include ActionControllerSubscriberTest
  end
end