mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Test fragment/page cache and send data/file notifications.
This commit is contained in:
parent
da5978c223
commit
b0d35ad00c
3 changed files with 100 additions and 17 deletions
|
@ -24,11 +24,11 @@ module ActionController
|
|||
def send_file(event)
|
||||
message = if event.payload[:x_sendfile]
|
||||
header = ActionController::Streaming::X_SENDFILE_HEADER
|
||||
"Sending #{header} header %s"
|
||||
"Sent #{header} header %s"
|
||||
elsif event.payload[:stream]
|
||||
"Streaming file %s"
|
||||
"Streamed file %s"
|
||||
else
|
||||
"Sending file %s"
|
||||
"Sent file %s"
|
||||
end
|
||||
|
||||
message << " (%.1fms)"
|
||||
|
@ -40,7 +40,7 @@ module ActionController
|
|||
end
|
||||
|
||||
def send_data(event)
|
||||
info("Sending data %s (%.1fms)" % [event.payload[:filename], event.duration])
|
||||
info("Sent data %s (%.1fms)" % [event.payload[:filename], event.duration])
|
||||
end
|
||||
|
||||
%w(write_fragment read_fragment exist_fragment?
|
||||
|
@ -49,7 +49,7 @@ module ActionController
|
|||
def #{method}(event)
|
||||
key_or_path = event.payload[:key] || event.payload[:path]
|
||||
human_name = #{method.to_s.humanize.inspect}
|
||||
info("\#{human_name} \#{key_or_path.inspect} (%.1fms)" % event.duration)
|
||||
info("\#{human_name} \#{key_or_path} (%.1fms)" % event.duration)
|
||||
end
|
||||
METHOD
|
||||
end
|
||||
|
|
|
@ -11,11 +11,31 @@ module Another
|
|||
def redirector
|
||||
redirect_to "http://foo.bar/"
|
||||
end
|
||||
|
||||
def data_sender
|
||||
send_data "cool data", :filename => "omg.txt"
|
||||
end
|
||||
|
||||
def xfile_sender
|
||||
send_file File.expand_path("company.rb", FIXTURE_LOAD_PATH), :x_sendfile => true
|
||||
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
|
||||
|
||||
def with_page_cache
|
||||
cache_page("Super soaker", "/index.html")
|
||||
render :nothing => true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module ActionControllerSubscriberTest
|
||||
Rails::Subscriber.add(:action_controller, ActionController::Railties::Subscriber.new)
|
||||
|
||||
def self.included(base)
|
||||
base.tests Another::SubscribersController
|
||||
|
@ -28,11 +48,19 @@ module ActionControllerSubscriberTest
|
|||
|
||||
def setup
|
||||
@old_logger = ActionController::Base.logger
|
||||
|
||||
@cache_path = File.expand_path('../temp/test_cache', File.dirname(__FILE__))
|
||||
ActionController::Base.page_cache_directory = @cache_path
|
||||
ActionController::Base.cache_store = :file_store, @cache_path
|
||||
|
||||
Rails::Subscriber.add(:action_controller, ActionController::Railties::Subscriber.new)
|
||||
super
|
||||
end
|
||||
|
||||
def teardown
|
||||
super
|
||||
Rails::Subscriber.subscribers.clear
|
||||
FileUtils.rm_rf(@cache_path)
|
||||
ActionController::Base.logger = @old_logger
|
||||
end
|
||||
|
||||
|
@ -43,34 +71,34 @@ module ActionControllerSubscriberTest
|
|||
def test_process_action
|
||||
get :show
|
||||
wait
|
||||
assert_equal 3, @logger.logged(:info).size
|
||||
assert_match /Processed\sAnother::SubscribersController#show/, @logger.logged(:info)[0]
|
||||
assert_equal 3, logs.size
|
||||
assert_match /Processed\sAnother::SubscribersController#show/, logs[0]
|
||||
end
|
||||
|
||||
def test_process_action_without_parameters
|
||||
get :show
|
||||
wait
|
||||
assert_nil @logger.logged(:info).detect {|l| l =~ /Parameters/ }
|
||||
assert_nil logs.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]
|
||||
assert_equal 4, logs.size
|
||||
assert_equal 'Parameters: {"id"=>"10"}', logs[1]
|
||||
end
|
||||
|
||||
def test_process_action_with_view_runtime
|
||||
get :show
|
||||
wait
|
||||
assert_match /View runtime/, @logger.logged(:info)[1]
|
||||
assert_match /View runtime/, logs[1]
|
||||
end
|
||||
|
||||
def test_process_action_with_status_and_request_uri
|
||||
get :show
|
||||
wait
|
||||
last = @logger.logged(:info).last
|
||||
last = logs.last
|
||||
assert_match /Completed/, last
|
||||
assert_match /200/, last
|
||||
assert_match /another\/subscribers\/show/, last
|
||||
|
@ -82,7 +110,7 @@ module ActionControllerSubscriberTest
|
|||
get :show, :lifo => 'Pratik', :amount => '420', :step => '1'
|
||||
wait
|
||||
|
||||
params = @logger.logged(:info)[1]
|
||||
params = logs[1]
|
||||
assert_match /"amount"=>"\[FILTERED\]"/, params
|
||||
assert_match /"lifo"=>"\[FILTERED\]"/, params
|
||||
assert_match /"step"=>"1"/, params
|
||||
|
@ -92,8 +120,62 @@ module ActionControllerSubscriberTest
|
|||
get :redirector
|
||||
wait
|
||||
|
||||
assert_equal 3, @logger.logged(:info).size
|
||||
assert_equal "Redirected to http://foo.bar/ with status 302", @logger.logged(:info)[0]
|
||||
assert_equal 3, logs.size
|
||||
assert_equal "Redirected to http://foo.bar/ with status 302", logs[0]
|
||||
end
|
||||
|
||||
def test_send_data
|
||||
get :data_sender
|
||||
wait
|
||||
|
||||
assert_equal 4, logs.size
|
||||
assert_match /Sent data omg\.txt/, logs[0]
|
||||
end
|
||||
|
||||
def test_send_file
|
||||
get :file_sender
|
||||
wait
|
||||
|
||||
assert_equal 4, logs.size
|
||||
assert_match /Sent file/, logs[0]
|
||||
assert_match /test\/fixtures\/company\.rb/, logs[0]
|
||||
end
|
||||
|
||||
def test_send_xfile
|
||||
get :xfile_sender
|
||||
wait
|
||||
|
||||
assert_equal 3, logs.size
|
||||
assert_match /Sent X\-Sendfile header/, logs[0]
|
||||
assert_match /test\/fixtures\/company\.rb/, logs[0]
|
||||
end
|
||||
|
||||
def test_with_fragment_cache
|
||||
ActionController::Base.perform_caching = true
|
||||
get :with_fragment_cache
|
||||
wait
|
||||
|
||||
assert_equal 5, logs.size
|
||||
assert_match /Exist fragment\? views\/foo/, logs[0]
|
||||
assert_match /Write fragment views\/foo/, logs[1]
|
||||
ensure
|
||||
ActionController::Base.perform_caching = true
|
||||
end
|
||||
|
||||
def test_with_page_cache
|
||||
ActionController::Base.perform_caching = true
|
||||
get :with_page_cache
|
||||
wait
|
||||
|
||||
assert_equal 4, logs.size
|
||||
assert_match /Write page/, logs[0]
|
||||
assert_match /\/index\.html/, logs[0]
|
||||
ensure
|
||||
ActionController::Base.perform_caching = true
|
||||
end
|
||||
|
||||
def logs
|
||||
@logs ||= @logger.logged(:info)
|
||||
end
|
||||
|
||||
class SyncSubscriberTest < ActionController::TestCase
|
||||
|
|
|
@ -4,17 +4,18 @@ require "action_view/railties/subscriber"
|
|||
require "controller/fake_models"
|
||||
|
||||
module ActionViewSubscriberTest
|
||||
Rails::Subscriber.add(:action_view, ActionView::Railties::Subscriber.new)
|
||||
|
||||
def setup
|
||||
@old_logger = ActionController::Base.logger
|
||||
@view = ActionView::Base.new(ActionController::Base.view_paths, {})
|
||||
Rails.stubs(:root).returns(File.expand_path(FIXTURE_LOAD_PATH))
|
||||
Rails::Subscriber.add(:action_view, ActionView::Railties::Subscriber.new)
|
||||
super
|
||||
end
|
||||
|
||||
def teardown
|
||||
super
|
||||
Rails::Subscriber.subscribers.clear
|
||||
ActionController::Base.logger = @old_logger
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue