提交 a38c749d 编写于 作者: Y Yehuda Katz

Sync 'rails/rails/master'

上级 42b32938
*2.3.0 [Edge]*
* Added :silence option to BenchmarkHelper#benchmark and turned log_level into a hash parameter and deprecated the old use [DHH]
* Fixed the AssetTagHelper cache to use the computed asset host as part of the cache key instead of just assuming the its a string #1299 [DHH]
* Make ActionController#render(string) work as a shortcut for render :file/:template/:action => string. [#1435] [Pratik Naik] Examples:
# Instead of render(:action => 'other_action')
......
......@@ -91,7 +91,6 @@ def reload_application
run_callbacks :prepare_dispatch
Routing::Routes.reload
ActionView::Helpers::AssetTagHelper::AssetTag::Cache.clear
end
# Cleanup the application by clearing out loaded classes so they can
......
......@@ -81,8 +81,8 @@ class MultiPartNeededException < Exception
end
# Create and initialize a new Session instance.
def initialize(app)
@application = app
def initialize(app = nil)
@application = app || ActionController::Dispatcher.new
reset!
end
......@@ -591,7 +591,6 @@ def reset!
# can use this method to open multiple sessions that ought to be tested
# simultaneously.
def open_session(application = nil)
application ||= ActionController::Dispatcher.new
session = Integration::Session.new(application)
# delegate the fixture accessors back to the test instance
......
......@@ -60,8 +60,8 @@ def self.included(base) #:nodoc:
module ClassMethods
def call_with_exception(env, exception) #:nodoc:
request = env["actioncontroller.rescue.request"]
response = env["actioncontroller.rescue.response"]
request = env["actioncontroller.rescue.request"] ||= Request.new(env)
response = env["actioncontroller.rescue.response"] ||= Response.new
new.process(request, response, :rescue_action, exception)
end
end
......
......@@ -18,12 +18,33 @@ module BenchmarkHelper
# That would add something like "Process data files (345.2ms)" to the log,
# which you can then use to compare timings when optimizing your code.
#
# You may give an optional logger level as the second argument
# You may give an optional logger level as the :level option.
# (:debug, :info, :warn, :error); the default value is :info.
def benchmark(message = "Benchmarking", level = :info)
#
# <% benchmark "Low-level files", :level => :debug do %>
# <%= lowlevel_files_operation %>
# <% end %>
#
# Finally, you can pass true as the third argument to silence all log activity
# inside the block. This is great for boiling down a noisy block to just a single statement:
#
# <% benchmark "Process data files", :level => :info, :silence => true do %>
# <%= expensive_and_chatty_files_operation %>
# <% end %>
def benchmark(message = "Benchmarking", options = {})
if controller.logger
ms = Benchmark.ms { yield }
controller.logger.send(level, '%s (%.1fms)' % [message, ms])
if options.is_a?(Symbol)
ActiveSupport::Deprecation.warn("use benchmark('#{message}', :level => :#{options}) instead", caller)
options = { :level => options, :silence => false }
else
options.assert_valid_keys(:level, :silence)
options[:level] ||= :info
end
result = nil
ms = Benchmark.ms { result = options[:silence] ? controller.logger.silence { yield } : yield }
controller.logger.send(options[:level], '%s (%.1fms)' % [ message, ms ])
result
else
yield
end
......
......@@ -32,11 +32,6 @@ def test_reloads_routes_before_dispatch_if_in_loading_mode
dispatch(false)
end
def test_clears_asset_tag_cache_before_dispatch_if_in_loading_mode
ActionView::Helpers::AssetTagHelper::AssetTag::Cache.expects(:clear).once
dispatch(false)
end
def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
ActionController::Routing::Routes.expects(:reload).never
ActiveSupport::Dependencies.expects(:clear).never
......
......@@ -10,6 +10,10 @@ def update
SessionUploadTest.last_request_type = ActionController::Base.param_parsers[request.content_type]
render :text => "got here"
end
def read
render :text => "File: #{params[:uploaded_data].read}"
end
end
class SessionUploadTest < ActionController::IntegrationTest
......@@ -19,21 +23,43 @@ class << self
attr_accessor :last_request_type
end
# def setup
# @session = ActionController::Integration::Session.new
# end
def test_post_with_upload
uses_mocha "test_post_with_upload" do
ActiveSupport::Dependencies.stubs(:load?).returns(false)
def test_upload_and_read_file
with_test_routing do
post '/read', :uploaded_data => fixture_file_upload(FILES_DIR + "/hello.txt", "text/plain")
assert_equal "File: Hello", response.body
end
end
# The lint wrapper is used in integration tests
# instead of a normal StringIO class
InputWrapper = Rack::Lint::InputWrapper
def test_post_with_upload_with_unrewindable_input
InputWrapper.any_instance.expects(:rewind).raises(Errno::ESPIPE)
with_test_routing do
post '/read', :uploaded_data => fixture_file_upload(FILES_DIR + "/hello.txt", "text/plain")
assert_equal "File: Hello", response.body
end
end
def test_post_with_upload_with_params_parsing
with_test_routing do
params = { :uploaded_data => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") }
post '/update', params, :location => 'blah'
assert_equal(:multipart_form, SessionUploadTest.last_request_type)
end
end
private
def with_test_routing
with_routing do |set|
set.draw do |map|
map.update 'update', :controller => "upload_test", :action => "update", :method => :post
map.read 'read', :controller => "upload_test", :action => "read", :method => :post
end
params = { :uploaded_data => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") }
post '/update', params, :location => 'blah'
assert_equal(:multipart_form, SessionUploadTest.last_request_type)
yield
end
end
end
end
......@@ -390,6 +390,13 @@ def test_rescue_dispatcher_exceptions
assert_equal "no way", @response.body
end
def test_rescue_dispatcher_exceptions_without_request_set
@request.env['REQUEST_URI'] = '/no_way'
response = RescueController.call_with_exception(@request.env, ActionController::RoutingError.new("Route not found"))
assert_kind_of ActionController::Response, response
assert_equal "no way", response.body
end
protected
def with_all_requests_local(local = true)
old_local, ActionController::Base.consider_all_requests_local =
......
Hello
\ No newline at end of file
......@@ -38,8 +38,6 @@ def host_with_port() 'localhost' end
@controller.request = @request
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
AssetTag::Cache.clear
AssetCollection::Cache.clear
end
def teardown
......@@ -281,6 +279,26 @@ def test_should_not_modify_source_string
assert_equal copy, source
end
def test_caching_image_path_with_caching_and_proc_asset_host_using_request
ENV['RAILS_ASSET_ID'] = ''
ActionController::Base.asset_host = Proc.new do |source, request|
if request.ssl?
"#{request.protocol}#{request.host_with_port}"
else
"#{request.protocol}assets#{source.length}.example.com"
end
end
ActionController::Base.perform_caching = true
@controller.request.stubs(:ssl?).returns(false)
assert_equal "http://assets15.example.com/images/xml.png", image_path("xml.png")
@controller.request.stubs(:ssl?).returns(true)
assert_equal "http://localhost/images/xml.png", image_path("xml.png")
end
def test_caching_javascript_include_tag_when_caching_on
ENV["RAILS_ASSET_ID"] = ""
ActionController::Base.asset_host = 'http://a0.example.com'
......
......@@ -4,32 +4,25 @@
class BenchmarkHelperTest < ActionView::TestCase
tests ActionView::Helpers::BenchmarkHelper
class MockLogger
attr_reader :logged
def initialize
@logged = []
end
def method_missing(method, *args)
@logged << [method, args]
end
def teardown
controller.logger.send(:clear_buffer)
end
def controller
@controller ||= Struct.new(:logger).new(MockLogger.new)
logger = ActiveSupport::BufferedLogger.new(StringIO.new)
logger.auto_flushing = false
@controller ||= Struct.new(:logger).new(logger)
end
def test_without_block
assert_raise(LocalJumpError) { benchmark }
assert controller.logger.logged.empty?
assert buffer.empty?
end
def test_defaults
i_was_run = false
benchmark { i_was_run = true }
assert i_was_run
assert 1, controller.logger.logged.size
assert_last_logged
end
......@@ -37,24 +30,57 @@ def test_with_message
i_was_run = false
benchmark('test_run') { i_was_run = true }
assert i_was_run
assert 1, controller.logger.logged.size
assert_last_logged 'test_run'
end
def test_with_message_and_level
def test_with_message_and_deprecated_level
i_was_run = false
benchmark('debug_run', :debug) { i_was_run = true }
assert_deprecated do
benchmark('debug_run', :debug) { i_was_run = true }
end
assert i_was_run
assert 1, controller.logger.logged.size
assert_last_logged 'debug_run', :debug
assert_last_logged 'debug_run'
end
def test_within_level
controller.logger.level = ActiveSupport::BufferedLogger::DEBUG
benchmark('included_debug_run', :level => :debug) { }
assert_last_logged 'included_debug_run'
end
def test_outside_level
controller.logger.level = ActiveSupport::BufferedLogger::ERROR
benchmark('skipped_debug_run', :level => :debug) { }
assert_no_match(/skipped_debug_run/, buffer.last)
ensure
controller.logger.level = ActiveSupport::BufferedLogger::DEBUG
end
def test_without_silencing
benchmark('debug_run', :silence => false) do
controller.logger.info "not silenced!"
end
assert_equal 2, buffer.size
end
def test_with_silencing
benchmark('debug_run', :silence => true) do
controller.logger.info "silenced!"
end
assert_equal 1, buffer.size
end
private
def assert_last_logged(message = 'Benchmarking', level = :info)
last = controller.logger.logged.last
assert 2, last.size
assert_equal level, last.first
assert 1, last[1].size
assert last[1][0] =~ /^#{message} \(.*\)$/
def buffer
controller.logger.send(:buffer)
end
def assert_last_logged(message = 'Benchmarking')
assert_match(/^#{message} \(.*\)$/, buffer.last)
end
end
......@@ -14,6 +14,15 @@ class ApplicationController < ActionController::Base; end
Test::Unit.run = false
class ConsoleAppTest < Test::Unit::TestCase
def test_app_method_should_return_integration_session
assert_nothing_thrown do
console_session = app
assert_not_nil console_session
assert_instance_of ActionController::Integration::Session,
console_session
end
end
uses_mocha 'console reload test' do
def test_reload_should_fire_preparation_callbacks
a = b = c = nil
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册