log_subscriber_test.rb 4.4 KB
Newer Older
1
require "abstract_unit"
2 3
require "active_support/log_subscriber/test_helper"
require "action_controller/log_subscriber"
4 5

module Another
6
  class LogSubscribersController < ActionController::Base
7 8 9 10 11 12 13
    def show
      render :nothing => true
    end

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

    def data_sender
16
      send_data "cool data", :filename => "file.txt"
17 18 19 20 21 22 23 24 25
    end

    def file_sender
      send_file File.expand_path("company.rb", FIXTURE_LOAD_PATH)
    end

    def with_fragment_cache
      render :inline => "<%= cache('foo'){ 'bar' } %>"
    end
A
Aaron Patterson 已提交
26

27 28 29
    def with_fragment_cache_and_percent_in_key
      render :inline => "<%= cache('foo%bar'){ 'Contains % sign in key' } %>"
    end
30 31 32 33 34

    def with_page_cache
      cache_page("Super soaker", "/index.html")
      render :nothing => true
    end
35 36 37 38 39
    
    def with_exception
      raise Exception
    end
    
40 41 42
  end
end

43 44
class ACLogSubscriberTest < ActionController::TestCase
  tests Another::LogSubscribersController
45
  include ActiveSupport::LogSubscriber::TestHelper
46 47

  def setup
C
Carlhuda 已提交
48 49
    super

50
    @old_logger = ActionController::Base.logger
51 52 53

    @cache_path = File.expand_path('../temp/test_cache', File.dirname(__FILE__))
    ActionController::Base.page_cache_directory = @cache_path
54
    @controller.cache_store = :file_store, @cache_path
55
    ActionController::LogSubscriber.attach_to :action_controller
56 57 58 59
  end

  def teardown
    super
60
    ActiveSupport::LogSubscriber.log_subscribers.clear
61
    FileUtils.rm_rf(@cache_path)
62 63 64 65 66 67 68
    ActionController::Base.logger = @old_logger
  end

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

69 70 71 72
  def test_start_processing
    get :show
    wait
    assert_equal 2, logs.size
73
    assert_equal "Processing by Another::LogSubscribersController#show as HTML", logs.first
74 75
  end

76 77 78
  def test_process_action
    get :show
    wait
79
    assert_equal 2, logs.size
80 81
    assert_match(/Completed/, logs.last)
    assert_match(/200 OK/, logs.last)
82 83
  end

84 85 86
  def test_process_action_without_parameters
    get :show
    wait
87
    assert_nil logs.detect {|l| l =~ /Parameters/ }
88 89 90 91 92 93
  end

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

94 95
    assert_equal 3, logs.size
    assert_equal 'Parameters: {"id"=>"10"}', logs[1]
96 97 98 99 100
  end

  def test_process_action_with_view_runtime
    get :show
    wait
101
    assert_match(/\(Views: [\d.]+ms\)/, logs[1])
102 103 104
  end

  def test_process_action_with_filter_parameters
105
    @request.env["action_dispatch.parameter_filter"] = [:lifo, :amount]
106 107 108 109

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

110
    params = logs[1]
111 112 113
    assert_match(/"amount"=>"\[FILTERED\]"/, params)
    assert_match(/"lifo"=>"\[FILTERED\]"/, params)
    assert_match(/"step"=>"1"/, params)
114 115 116 117 118 119
  end

  def test_redirect_to
    get :redirector
    wait

120 121
    assert_equal 3, logs.size
    assert_equal "Redirected to http://foo.bar/", logs[1]
122 123 124 125 126 127
  end

  def test_send_data
    get :data_sender
    wait

128
    assert_equal 3, logs.size
129
    assert_match(/Sent data file\.txt/, logs[1])
130 131 132 133 134 135
  end

  def test_send_file
    get :file_sender
    wait

136
    assert_equal 3, logs.size
137 138
    assert_match(/Sent file/, logs[1])
    assert_match(/test\/fixtures\/company\.rb/, logs[1])
139 140 141
  end

  def test_with_fragment_cache
142
    @controller.config.perform_caching = true
143 144 145
    get :with_fragment_cache
    wait

146
    assert_equal 4, logs.size
147
    assert_match /Read fragment views\/foo/, logs[1]
148
    assert_match(/Write fragment views\/foo/, logs[2])
149
  ensure
150
    @controller.config.perform_caching = true
151
  end
152

153 154 155 156 157 158
  def test_with_fragment_cache_and_percent_in_key
    @controller.config.perform_caching = true
    get :with_fragment_cache_and_percent_in_key
    wait

    assert_equal 4, logs.size
159 160
    assert_match /Read fragment views\/foo/, logs[1]
    assert_match /Write fragment views\/foo/, logs[2]
161 162 163
  ensure
    @controller.config.perform_caching = true
  end
164 165

  def test_with_page_cache
166
    @controller.config.perform_caching = true
167 168 169
    get :with_page_cache
    wait

170
    assert_equal 3, logs.size
171 172
    assert_match(/Write page/, logs[1])
    assert_match(/\/index\.html/, logs[1])
173
  ensure
174
    @controller.config.perform_caching = true
175
  end
176 177 178 179 180 181 182 183 184 185
  
  def test_process_action_with_exception_includes_http_status_code
    begin
     get :with_exception
     wait
   rescue Exception => e      
   end
   assert_equal 2, logs.size
   assert_match(/Completed 500/, logs.last)
  end
186 187 188

  def logs
    @logs ||= @logger.logged(:info)
189 190
  end
end