提交 b0d35ad0 编写于 作者: J José Valim

Test fragment/page cache and send data/file notifications.

上级 da5978c2
...@@ -24,11 +24,11 @@ def process_action(event) ...@@ -24,11 +24,11 @@ def process_action(event)
def send_file(event) def send_file(event)
message = if event.payload[:x_sendfile] message = if event.payload[:x_sendfile]
header = ActionController::Streaming::X_SENDFILE_HEADER header = ActionController::Streaming::X_SENDFILE_HEADER
"Sending #{header} header %s" "Sent #{header} header %s"
elsif event.payload[:stream] elsif event.payload[:stream]
"Streaming file %s" "Streamed file %s"
else else
"Sending file %s" "Sent file %s"
end end
message << " (%.1fms)" message << " (%.1fms)"
...@@ -40,7 +40,7 @@ def redirect_to(event) ...@@ -40,7 +40,7 @@ def redirect_to(event)
end end
def send_data(event) 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 end
%w(write_fragment read_fragment exist_fragment? %w(write_fragment read_fragment exist_fragment?
...@@ -49,7 +49,7 @@ def send_data(event) ...@@ -49,7 +49,7 @@ def send_data(event)
def #{method}(event) def #{method}(event)
key_or_path = event.payload[:key] || event.payload[:path] key_or_path = event.payload[:key] || event.payload[:path]
human_name = #{method.to_s.humanize.inspect} 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 end
METHOD METHOD
end end
......
...@@ -11,11 +11,31 @@ def show ...@@ -11,11 +11,31 @@ def show
def redirector def redirector
redirect_to "http://foo.bar/" redirect_to "http://foo.bar/"
end 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
end end
module ActionControllerSubscriberTest module ActionControllerSubscriberTest
Rails::Subscriber.add(:action_controller, ActionController::Railties::Subscriber.new)
def self.included(base) def self.included(base)
base.tests Another::SubscribersController base.tests Another::SubscribersController
...@@ -28,11 +48,19 @@ def wait ...@@ -28,11 +48,19 @@ def wait
def setup def setup
@old_logger = ActionController::Base.logger @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 super
end end
def teardown def teardown
super super
Rails::Subscriber.subscribers.clear
FileUtils.rm_rf(@cache_path)
ActionController::Base.logger = @old_logger ActionController::Base.logger = @old_logger
end end
...@@ -43,34 +71,34 @@ def set_logger(logger) ...@@ -43,34 +71,34 @@ def set_logger(logger)
def test_process_action def test_process_action
get :show get :show
wait wait
assert_equal 3, @logger.logged(:info).size assert_equal 3, logs.size
assert_match /Processed\sAnother::SubscribersController#show/, @logger.logged(:info)[0] assert_match /Processed\sAnother::SubscribersController#show/, logs[0]
end end
def test_process_action_without_parameters def test_process_action_without_parameters
get :show get :show
wait wait
assert_nil @logger.logged(:info).detect {|l| l =~ /Parameters/ } assert_nil logs.detect {|l| l =~ /Parameters/ }
end end
def test_process_action_with_parameters def test_process_action_with_parameters
get :show, :id => '10' get :show, :id => '10'
wait wait
assert_equal 4, @logger.logged(:info).size assert_equal 4, logs.size
assert_equal 'Parameters: {"id"=>"10"}', @logger.logged(:info)[1] assert_equal 'Parameters: {"id"=>"10"}', logs[1]
end end
def test_process_action_with_view_runtime def test_process_action_with_view_runtime
get :show get :show
wait wait
assert_match /View runtime/, @logger.logged(:info)[1] assert_match /View runtime/, logs[1]
end end
def test_process_action_with_status_and_request_uri def test_process_action_with_status_and_request_uri
get :show get :show
wait wait
last = @logger.logged(:info).last last = logs.last
assert_match /Completed/, last assert_match /Completed/, last
assert_match /200/, last assert_match /200/, last
assert_match /another\/subscribers\/show/, last assert_match /another\/subscribers\/show/, last
...@@ -82,7 +110,7 @@ def test_process_action_with_filter_parameters ...@@ -82,7 +110,7 @@ def test_process_action_with_filter_parameters
get :show, :lifo => 'Pratik', :amount => '420', :step => '1' get :show, :lifo => 'Pratik', :amount => '420', :step => '1'
wait wait
params = @logger.logged(:info)[1] params = logs[1]
assert_match /"amount"=>"\[FILTERED\]"/, params assert_match /"amount"=>"\[FILTERED\]"/, params
assert_match /"lifo"=>"\[FILTERED\]"/, params assert_match /"lifo"=>"\[FILTERED\]"/, params
assert_match /"step"=>"1"/, params assert_match /"step"=>"1"/, params
...@@ -92,8 +120,62 @@ def test_redirect_to ...@@ -92,8 +120,62 @@ def test_redirect_to
get :redirector get :redirector
wait wait
assert_equal 3, @logger.logged(:info).size assert_equal 3, logs.size
assert_equal "Redirected to http://foo.bar/ with status 302", @logger.logged(:info)[0] 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 end
class SyncSubscriberTest < ActionController::TestCase class SyncSubscriberTest < ActionController::TestCase
......
...@@ -4,17 +4,18 @@ ...@@ -4,17 +4,18 @@
require "controller/fake_models" require "controller/fake_models"
module ActionViewSubscriberTest module ActionViewSubscriberTest
Rails::Subscriber.add(:action_view, ActionView::Railties::Subscriber.new)
def setup def setup
@old_logger = ActionController::Base.logger @old_logger = ActionController::Base.logger
@view = ActionView::Base.new(ActionController::Base.view_paths, {}) @view = ActionView::Base.new(ActionController::Base.view_paths, {})
Rails.stubs(:root).returns(File.expand_path(FIXTURE_LOAD_PATH)) Rails.stubs(:root).returns(File.expand_path(FIXTURE_LOAD_PATH))
Rails::Subscriber.add(:action_view, ActionView::Railties::Subscriber.new)
super super
end end
def teardown def teardown
super super
Rails::Subscriber.subscribers.clear
ActionController::Base.logger = @old_logger ActionController::Base.logger = @old_logger
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册