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
|
2016-08-06 13:35:13 -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
|
2015-06-15 16:53:45 -04:00
|
|
|
head 406
|
2011-07-18 13:37:23 -04:00
|
|
|
end
|
|
|
|
|
2012-12-07 14:46:06 -05:00
|
|
|
before_action :redirector, only: :never_executed
|
2011-11-30 03:52:52 -05:00
|
|
|
|
|
|
|
def never_executed
|
|
|
|
end
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
def show
|
2015-05-28 08:13:32 -04:00
|
|
|
head :ok
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def redirector
|
|
|
|
redirect_to "http://foo.bar/"
|
|
|
|
end
|
2010-01-12 19:04:33 -05:00
|
|
|
|
2012-09-30 18:00:44 -04:00
|
|
|
def filterable_redirector
|
|
|
|
redirect_to "http://secret.foo.bar/"
|
|
|
|
end
|
|
|
|
|
2010-01-12 19:04:33 -05:00
|
|
|
def data_sender
|
2016-08-06 13:35:13 -04: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
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= cache('foo'){ 'bar' } %>"
|
2010-01-12 19:04:33 -05:00
|
|
|
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
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= cache('foo%bar'){ 'Contains % sign in key' } %>"
|
2010-09-22 16:17:49 -04:00
|
|
|
end
|
2010-01-12 19:04:33 -05:00
|
|
|
|
2012-12-12 12:26:38 -05:00
|
|
|
def with_fragment_cache_if_with_true_condition
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= cache_if(true, 'foo') { 'bar' } %>"
|
2012-11-29 10:16:18 -05:00
|
|
|
end
|
|
|
|
|
2012-12-12 12:26:38 -05:00
|
|
|
def with_fragment_cache_if_with_false_condition
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= cache_if(false, 'foo') { 'bar' } %>"
|
2012-11-29 10:16:18 -05:00
|
|
|
end
|
|
|
|
|
2012-12-12 12:26:38 -05:00
|
|
|
def with_fragment_cache_unless_with_false_condition
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= cache_unless(false, 'foo') { 'bar' } %>"
|
2012-11-29 10:16:18 -05:00
|
|
|
end
|
|
|
|
|
2012-12-12 12:26:38 -05:00
|
|
|
def with_fragment_cache_unless_with_true_condition
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= cache_unless(true, 'foo') { 'bar' } %>"
|
2012-11-29 10:16:18 -05:00
|
|
|
end
|
|
|
|
|
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
|
2012-09-16 15:48:24 -04:00
|
|
|
|
|
|
|
def with_action_not_found
|
|
|
|
raise AbstractController::ActionNotFound
|
|
|
|
end
|
2014-12-09 04:55:58 -05:00
|
|
|
|
|
|
|
def append_info_to_payload(payload)
|
|
|
|
super
|
|
|
|
payload[:test_key] = "test_value"
|
|
|
|
@last_payload = payload
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_payload
|
|
|
|
@last_payload
|
|
|
|
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
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@cache_path = File.join Dir.tmpdir, Dir::Tmpname.make_tmpname("tmp", "cache")
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
get :show, params: { id: "10" }
|
2010-01-12 18:41:04 -05:00
|
|
|
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
|
|
|
|
|
2014-01-27 04:08:56 -05:00
|
|
|
def test_multiple_process_with_parameters
|
2016-08-06 12:54:50 -04:00
|
|
|
get :show, params: { id: "10" }
|
|
|
|
get :show, params: { id: "20" }
|
2014-01-27 04:08:56 -05:00
|
|
|
|
|
|
|
wait
|
|
|
|
|
|
|
|
assert_equal 6, logs.size
|
|
|
|
assert_equal 'Parameters: {"id"=>"10"}', logs[1]
|
|
|
|
assert_equal 'Parameters: {"id"=>"20"}', logs[4]
|
|
|
|
end
|
|
|
|
|
2011-05-02 19:03:21 -04:00
|
|
|
def test_process_action_with_wrapped_parameters
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["CONTENT_TYPE"] = "application/json"
|
|
|
|
post :show, params: { id: "10", name: "jose" }
|
2011-05-02 19:03:21 -04:00
|
|
|
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
|
2015-11-04 16:40:19 -05:00
|
|
|
assert_match(/Completed 200 OK in \d+ms/, logs[1])
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
2014-12-09 04:55:58 -05:00
|
|
|
def test_append_info_to_payload_is_called_even_with_exception
|
|
|
|
begin
|
|
|
|
get :with_exception
|
|
|
|
wait
|
|
|
|
rescue Exception
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "test_value", @controller.last_payload[:test_key]
|
|
|
|
end
|
|
|
|
|
2016-03-08 22:18:49 -05:00
|
|
|
def test_process_action_headers
|
|
|
|
get :show
|
|
|
|
wait
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "Rails Testing", @controller.last_payload[:headers]["User-Agent"]
|
2016-03-08 22:18:49 -05:00
|
|
|
end
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
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
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get :show, params: {
|
2016-08-06 12:54:50 -04:00
|
|
|
lifo: "Pratik", amount: "420", step: "1"
|
2015-01-04 04:35:06 -05:00
|
|
|
}
|
2010-01-12 18:41:04 -05:00
|
|
|
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
|
|
|
|
|
2012-09-30 18:00:44 -04:00
|
|
|
def test_filter_redirect_url_by_string
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["action_dispatch.redirect_filter"] = ["secret"]
|
2012-09-30 18:00:44 -04:00
|
|
|
get :filterable_redirector
|
|
|
|
wait
|
|
|
|
|
|
|
|
assert_equal 3, logs.size
|
|
|
|
assert_equal "Redirected to [FILTERED]", logs[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_filter_redirect_url_by_regexp
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["action_dispatch.redirect_filter"] = [/secret\.foo.+/]
|
2012-09-30 18:00:44 -04:00
|
|
|
get :filterable_redirector
|
|
|
|
wait
|
|
|
|
|
|
|
|
assert_equal 3, logs.size
|
|
|
|
assert_equal "Redirected to [FILTERED]", logs[1]
|
|
|
|
end
|
|
|
|
|
2010-01-12 19:04:33 -05:00
|
|
|
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
|
|
|
|
2012-12-12 12:26:38 -05:00
|
|
|
def test_with_fragment_cache_if_with_true
|
2012-11-29 10:16:18 -05:00
|
|
|
@controller.config.perform_caching = true
|
2012-12-12 12:26:38 -05:00
|
|
|
get :with_fragment_cache_if_with_true_condition
|
2012-11-29 10:16:18 -05:00
|
|
|
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
|
|
|
|
|
2012-12-12 12:26:38 -05:00
|
|
|
def test_with_fragment_cache_if_with_false
|
2012-11-29 10:16:18 -05:00
|
|
|
@controller.config.perform_caching = true
|
2012-12-12 12:26:38 -05:00
|
|
|
get :with_fragment_cache_if_with_false_condition
|
2012-11-29 10:16:18 -05:00
|
|
|
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
|
|
|
|
|
2012-12-12 12:26:38 -05:00
|
|
|
def test_with_fragment_cache_unless_with_true
|
2012-11-29 10:16:18 -05:00
|
|
|
@controller.config.perform_caching = true
|
2012-12-12 12:26:38 -05:00
|
|
|
get :with_fragment_cache_unless_with_true_condition
|
2012-11-29 10:16:18 -05:00
|
|
|
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
|
|
|
|
|
2012-12-12 12:26:38 -05:00
|
|
|
def test_with_fragment_cache_unless_with_false
|
2012-11-29 10:16:18 -05:00
|
|
|
@controller.config.perform_caching = true
|
2012-12-12 12:26:38 -05:00
|
|
|
get :with_fragment_cache_unless_with_false_condition
|
2012-11-29 10:16:18 -05:00
|
|
|
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
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2012-09-16 15:48:24 -04:00
|
|
|
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
|
|
|
|
|
2010-01-12 19:04:33 -05:00
|
|
|
def logs
|
|
|
|
@logs ||= @logger.logged(:info)
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
end
|