2010-01-12 18:41:04 -05:00
|
|
|
require "abstract_unit"
|
2010-06-24 07:23:43 -04:00
|
|
|
require "active_support/log_subscriber/test_helper"
|
|
|
|
require "action_controller/log_subscriber"
|
2010-01-12 18:41:04 -05:00
|
|
|
|
|
|
|
module Another
|
2010-02-15 09:44:30 -05:00
|
|
|
class LogSubscribersController < ActionController::Base
|
2011-05-19 10:33:25 -04:00
|
|
|
wrap_parameters :person, :include => :name, :format => :json
|
2011-05-02 19:03:21 -04:00
|
|
|
|
2011-07-18 13:37:23 -04:00
|
|
|
class SpecialException < Exception
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from SpecialException do
|
|
|
|
head :status => 406
|
|
|
|
end
|
|
|
|
|
2011-11-30 03:52:52 -05:00
|
|
|
before_filter :redirector, :only => :never_executed
|
|
|
|
|
|
|
|
def never_executed
|
|
|
|
end
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
def show
|
|
|
|
render :nothing => true
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirector
|
|
|
|
redirect_to "http://foo.bar/"
|
|
|
|
end
|
2010-01-12 19:04:33 -05:00
|
|
|
|
|
|
|
def data_sender
|
2010-01-31 00:27:24 -05:00
|
|
|
send_data "cool data", :filename => "file.txt"
|
2010-01-12 19:04:33 -05:00
|
|
|
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
|
2010-09-29 14:34:51 -04:00
|
|
|
|
2010-09-22 16:17:49 -04:00
|
|
|
def with_fragment_cache_and_percent_in_key
|
|
|
|
render :inline => "<%= cache('foo%bar'){ 'Contains % sign in key' } %>"
|
|
|
|
end
|
2010-01-12 19:04:33 -05:00
|
|
|
|
|
|
|
def with_page_cache
|
|
|
|
cache_page("Super soaker", "/index.html")
|
|
|
|
render :nothing => true
|
|
|
|
end
|
2011-05-19 10:33:25 -04:00
|
|
|
|
2011-01-18 17:36:08 -05:00
|
|
|
def with_exception
|
|
|
|
raise Exception
|
|
|
|
end
|
2011-05-19 10:33:25 -04:00
|
|
|
|
2011-07-18 13:37:23 -04:00
|
|
|
def with_rescued_exception
|
|
|
|
raise SpecialException
|
|
|
|
end
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-15 09:44:30 -05:00
|
|
|
class ACLogSubscriberTest < ActionController::TestCase
|
|
|
|
tests Another::LogSubscribersController
|
2010-06-24 07:23:43 -04:00
|
|
|
include ActiveSupport::LogSubscriber::TestHelper
|
2010-01-12 18:41:04 -05:00
|
|
|
|
|
|
|
def setup
|
2010-03-02 17:40:59 -05:00
|
|
|
super
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
@old_logger = ActionController::Base.logger
|
2010-01-12 19:04:33 -05:00
|
|
|
|
|
|
|
@cache_path = File.expand_path('../temp/test_cache', File.dirname(__FILE__))
|
|
|
|
ActionController::Base.page_cache_directory = @cache_path
|
2010-03-04 19:50:57 -05:00
|
|
|
@controller.cache_store = :file_store, @cache_path
|
2010-06-24 07:23:43 -04:00
|
|
|
ActionController::LogSubscriber.attach_to :action_controller
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
2010-06-24 07:23:43 -04:00
|
|
|
ActiveSupport::LogSubscriber.log_subscribers.clear
|
2010-01-12 19:04:33 -05:00
|
|
|
FileUtils.rm_rf(@cache_path)
|
2010-01-12 18:41:04 -05:00
|
|
|
ActionController::Base.logger = @old_logger
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_logger(logger)
|
|
|
|
ActionController::Base.logger = logger
|
|
|
|
end
|
|
|
|
|
2010-01-19 06:52:10 -05:00
|
|
|
def test_start_processing
|
|
|
|
get :show
|
|
|
|
wait
|
|
|
|
assert_equal 2, logs.size
|
2010-02-15 09:44:30 -05:00
|
|
|
assert_equal "Processing by Another::LogSubscribersController#show as HTML", logs.first
|
2010-01-19 06:52:10 -05:00
|
|
|
end
|
|
|
|
|
2011-11-30 03:52:52 -05:00
|
|
|
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
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
def test_process_action
|
|
|
|
get :show
|
|
|
|
wait
|
2010-01-19 06:52:10 -05:00
|
|
|
assert_equal 2, logs.size
|
2010-09-22 15:03:39 -04:00
|
|
|
assert_match(/Completed/, logs.last)
|
|
|
|
assert_match(/200 OK/, logs.last)
|
2010-01-14 14:02:42 -05:00
|
|
|
end
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
def test_process_action_without_parameters
|
|
|
|
get :show
|
|
|
|
wait
|
2010-01-12 19:04:33 -05:00
|
|
|
assert_nil logs.detect {|l| l =~ /Parameters/ }
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_action_with_parameters
|
|
|
|
get :show, :id => '10'
|
|
|
|
wait
|
|
|
|
|
2010-01-19 06:52:10 -05:00
|
|
|
assert_equal 3, logs.size
|
|
|
|
assert_equal 'Parameters: {"id"=>"10"}', logs[1]
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
2011-05-02 19:03:21 -04:00
|
|
|
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
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
def test_process_action_with_view_runtime
|
|
|
|
get :show
|
|
|
|
wait
|
2010-10-15 10:31:00 -04:00
|
|
|
assert_match(/\(Views: [\d.]+ms\)/, logs[1])
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_process_action_with_filter_parameters
|
2010-01-21 10:50:11 -05:00
|
|
|
@request.env["action_dispatch.parameter_filter"] = [:lifo, :amount]
|
2010-01-12 18:41:04 -05:00
|
|
|
|
|
|
|
get :show, :lifo => 'Pratik', :amount => '420', :step => '1'
|
|
|
|
wait
|
|
|
|
|
2010-01-19 06:52:10 -05:00
|
|
|
params = logs[1]
|
2010-09-22 15:03:39 -04:00
|
|
|
assert_match(/"amount"=>"\[FILTERED\]"/, params)
|
|
|
|
assert_match(/"lifo"=>"\[FILTERED\]"/, params)
|
|
|
|
assert_match(/"step"=>"1"/, params)
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_redirect_to
|
|
|
|
get :redirector
|
|
|
|
wait
|
|
|
|
|
2010-01-19 06:52:10 -05:00
|
|
|
assert_equal 3, logs.size
|
|
|
|
assert_equal "Redirected to http://foo.bar/", logs[1]
|
2010-01-12 19:04:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_data
|
|
|
|
get :data_sender
|
|
|
|
wait
|
|
|
|
|
2010-01-19 06:52:10 -05:00
|
|
|
assert_equal 3, logs.size
|
2010-09-22 15:03:39 -04:00
|
|
|
assert_match(/Sent data file\.txt/, logs[1])
|
2010-01-12 19:04:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_file
|
|
|
|
get :file_sender
|
|
|
|
wait
|
|
|
|
|
2010-01-19 06:52:10 -05:00
|
|
|
assert_equal 3, logs.size
|
2010-09-22 15:03:39 -04:00
|
|
|
assert_match(/Sent file/, logs[1])
|
|
|
|
assert_match(/test\/fixtures\/company\.rb/, logs[1])
|
2010-01-12 19:04:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_fragment_cache
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.config.perform_caching = true
|
2010-01-12 19:04:33 -05:00
|
|
|
get :with_fragment_cache
|
|
|
|
wait
|
|
|
|
|
2010-01-19 06:52:10 -05:00
|
|
|
assert_equal 4, logs.size
|
2011-02-07 19:44:27 -05:00
|
|
|
assert_match(/Read fragment views\/foo/, logs[1])
|
2010-09-22 15:03:39 -04:00
|
|
|
assert_match(/Write fragment views\/foo/, logs[2])
|
2010-01-12 19:04:33 -05:00
|
|
|
ensure
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.config.perform_caching = true
|
2010-01-12 19:04:33 -05:00
|
|
|
end
|
2010-09-16 12:44:18 -04:00
|
|
|
|
2010-09-22 16:17:49 -04:00
|
|
|
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
|
2011-02-07 19:44:27 -05:00
|
|
|
assert_match(/Read fragment views\/foo/, logs[1])
|
|
|
|
assert_match(/Write fragment views\/foo/, logs[2])
|
2010-09-22 16:17:49 -04:00
|
|
|
ensure
|
|
|
|
@controller.config.perform_caching = true
|
|
|
|
end
|
2010-01-12 19:04:33 -05:00
|
|
|
|
|
|
|
def test_with_page_cache
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.config.perform_caching = true
|
2010-01-12 19:04:33 -05:00
|
|
|
get :with_page_cache
|
|
|
|
wait
|
|
|
|
|
2010-01-19 06:52:10 -05:00
|
|
|
assert_equal 3, logs.size
|
2010-09-22 15:03:39 -04:00
|
|
|
assert_match(/Write page/, logs[1])
|
|
|
|
assert_match(/\/index\.html/, logs[1])
|
2010-01-12 19:04:33 -05:00
|
|
|
ensure
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.config.perform_caching = true
|
2010-01-12 19:04:33 -05:00
|
|
|
end
|
2011-02-07 19:44:27 -05:00
|
|
|
|
2011-01-18 17:36:08 -05:00
|
|
|
def test_process_action_with_exception_includes_http_status_code
|
|
|
|
begin
|
2011-02-07 19:44:27 -05:00
|
|
|
get :with_exception
|
|
|
|
wait
|
|
|
|
rescue Exception
|
|
|
|
end
|
|
|
|
assert_equal 2, logs.size
|
|
|
|
assert_match(/Completed 500/, logs.last)
|
2011-01-18 17:36:08 -05:00
|
|
|
end
|
2010-01-12 19:04:33 -05:00
|
|
|
|
2011-07-18 13:37:23 -04:00
|
|
|
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
|
|
|
|
|
2010-01-12 19:04:33 -05:00
|
|
|
def logs
|
|
|
|
@logs ||= @logger.logged(:info)
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
end
|