log_subscriber_test.rb 7.7 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
    wrap_parameters :person, :include => :name, :format => :json
8

9 10 11 12 13 14 15
    class SpecialException < Exception
    end

    rescue_from SpecialException do
      head :status => 406
    end

16 17 18 19 20
    before_filter :redirector, :only => :never_executed

    def never_executed
    end

21 22 23 24 25 26 27
    def show
      render :nothing => true
    end

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

29 30 31 32
    def filterable_redirector
      redirect_to "http://secret.foo.bar/"
    end

33
    def data_sender
34
      send_data "cool data", :filename => "file.txt"
35 36 37 38 39 40 41 42 43
    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 已提交
44

45 46 47
    def with_fragment_cache_and_percent_in_key
      render :inline => "<%= cache('foo%bar'){ 'Contains % sign in key' } %>"
    end
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    def with_fragment_cache_and_if_true_condition
      render :inline => "<%= cache('foo', :if => true) { 'bar' } %>"
    end

    def with_fragment_cache_and_if_false_condition
      render :inline => "<%= cache('foo', :if => false) { 'bar' } %>"
    end

    def with_fragment_cache_and_unless_false_condition
      render :inline => "<%= cache('foo', :unless => false) { 'bar' } %>"
    end

    def with_fragment_cache_and_unless_true_condition
      render :inline => "<%= cache('foo', :unless => true) { 'bar' } %>"
    end

65 66 67
    def with_exception
      raise Exception
    end
68

69 70 71
    def with_rescued_exception
      raise SpecialException
    end
72 73 74 75

    def with_action_not_found
      raise AbstractController::ActionNotFound
    end
76 77 78
  end
end

79 80
class ACLogSubscriberTest < ActionController::TestCase
  tests Another::LogSubscribersController
81
  include ActiveSupport::LogSubscriber::TestHelper
82 83

  def setup
C
Carlhuda 已提交
84 85
    super

86
    @old_logger = ActionController::Base.logger
87 88

    @cache_path = File.expand_path('../temp/test_cache', File.dirname(__FILE__))
89
    @controller.cache_store = :file_store, @cache_path
90
    ActionController::LogSubscriber.attach_to :action_controller
91 92 93 94
  end

  def teardown
    super
95
    ActiveSupport::LogSubscriber.log_subscribers.clear
96
    FileUtils.rm_rf(@cache_path)
97 98 99 100 101 102 103
    ActionController::Base.logger = @old_logger
  end

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

104 105 106 107
  def test_start_processing
    get :show
    wait
    assert_equal 2, logs.size
108
    assert_equal "Processing by Another::LogSubscribersController#show as HTML", logs.first
109 110
  end

111 112 113 114 115 116 117
  def test_halted_callback
    get :never_executed
    wait
    assert_equal 4, logs.size
    assert_equal "Filter chain halted as :redirector rendered or redirected", logs.third
  end

118 119 120
  def test_process_action
    get :show
    wait
121
    assert_equal 2, logs.size
122 123
    assert_match(/Completed/, logs.last)
    assert_match(/200 OK/, logs.last)
124 125
  end

126 127 128
  def test_process_action_without_parameters
    get :show
    wait
129
    assert_nil logs.detect {|l| l =~ /Parameters/ }
130 131 132 133 134 135
  end

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

136 137
    assert_equal 3, logs.size
    assert_equal 'Parameters: {"id"=>"10"}', logs[1]
138 139
  end

140 141 142 143 144 145 146 147 148
  def test_process_action_with_wrapped_parameters
    @request.env['CONTENT_TYPE'] = 'application/json'
    post :show, :id => '10', :name => 'jose'
    wait

    assert_equal 3, logs.size
    assert_match '"person"=>{"name"=>"jose"}', logs[1]
  end

149 150 151
  def test_process_action_with_view_runtime
    get :show
    wait
152
    assert_match(/\(Views: [\d.]+ms\)/, logs[1])
153 154 155
  end

  def test_process_action_with_filter_parameters
156
    @request.env["action_dispatch.parameter_filter"] = [:lifo, :amount]
157 158 159 160

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

161
    params = logs[1]
162 163 164
    assert_match(/"amount"=>"\[FILTERED\]"/, params)
    assert_match(/"lifo"=>"\[FILTERED\]"/, params)
    assert_match(/"step"=>"1"/, params)
165 166 167 168 169 170
  end

  def test_redirect_to
    get :redirector
    wait

171 172
    assert_equal 3, logs.size
    assert_equal "Redirected to http://foo.bar/", logs[1]
173 174
  end

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  def test_filter_redirect_url_by_string
    @request.env['action_dispatch.redirect_filter'] = ['secret']
    get :filterable_redirector
    wait

    assert_equal 3, logs.size
    assert_equal "Redirected to [FILTERED]", logs[1]
  end

  def test_filter_redirect_url_by_regexp
    @request.env['action_dispatch.redirect_filter'] = [/secret\.foo.+/]
    get :filterable_redirector
    wait

    assert_equal 3, logs.size
    assert_equal "Redirected to [FILTERED]", logs[1]
  end

193 194 195 196
  def test_send_data
    get :data_sender
    wait

197
    assert_equal 3, logs.size
198
    assert_match(/Sent data file\.txt/, logs[1])
199 200 201 202 203 204
  end

  def test_send_file
    get :file_sender
    wait

205
    assert_equal 3, logs.size
206 207
    assert_match(/Sent file/, logs[1])
    assert_match(/test\/fixtures\/company\.rb/, logs[1])
208 209 210
  end

  def test_with_fragment_cache
211
    @controller.config.perform_caching = true
212 213 214
    get :with_fragment_cache
    wait

215
    assert_equal 4, logs.size
216
    assert_match(/Read fragment views\/foo/, logs[1])
217
    assert_match(/Write fragment views\/foo/, logs[2])
218
  ensure
219
    @controller.config.perform_caching = true
220
  end
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  def test_with_fragment_cache_and_if_true
    @controller.config.perform_caching = true
    get :with_fragment_cache_and_if_true_condition
    wait

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

  def test_with_fragment_cache_and_if_false
    @controller.config.perform_caching = true
    get :with_fragment_cache_and_if_false_condition
    wait

    assert_equal 2, logs.size
    assert_no_match(/Read fragment views\/foo/, logs[1])
    assert_no_match(/Write fragment views\/foo/, logs[2])
  ensure
    @controller.config.perform_caching = true
  end

  def test_with_fragment_cache_and_unless_true
    @controller.config.perform_caching = true
    get :with_fragment_cache_and_unless_true_condition
    wait

    assert_equal 2, logs.size
    assert_no_match(/Read fragment views\/foo/, logs[1])
    assert_no_match(/Write fragment views\/foo/, logs[2])
  ensure
    @controller.config.perform_caching = true
  end

  def test_with_fragment_cache_and_unless_false
    @controller.config.perform_caching = true
    get :with_fragment_cache_and_unless_false_condition
    wait

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

270 271 272 273 274 275
  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
276 277
    assert_match(/Read fragment views\/foo/, logs[1])
    assert_match(/Write fragment views\/foo/, logs[2])
278 279 280
  ensure
    @controller.config.perform_caching = true
  end
281

282 283
  def test_process_action_with_exception_includes_http_status_code
    begin
284 285 286 287 288 289
      get :with_exception
      wait
    rescue Exception
    end
    assert_equal 2, logs.size
    assert_match(/Completed 500/, logs.last)
290
  end
291

292 293 294 295 296 297 298 299
  def test_process_action_with_rescued_exception_includes_http_status_code
    get :with_rescued_exception
    wait

    assert_equal 2, logs.size
    assert_match(/Completed 406/, logs.last)
  end

300 301 302 303 304 305 306 307 308 309 310
  def test_process_action_with_with_action_not_found_logs_404
    begin
      get :with_action_not_found
      wait
    rescue AbstractController::ActionNotFound
    end

    assert_equal 2, logs.size
    assert_match(/Completed 404/, logs.last)
  end

311 312
  def logs
    @logs ||= @logger.logged(:info)
313 314
  end
end