1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Improve tests and code for Rails::InfoController and Rails::Info [#2411 state:resolved]

- Refactor Rails::InfoController tests.
- Return forbidden status from the InfoController for remote requests instead of a 500.
- Add tests for displaying middleware in Rails::Info.

Signed-off-by: Joshua Peek <josh@joshpeek.com>
This commit is contained in:
Manfred Stienstra 2009-04-07 09:16:48 -05:00 committed by Joshua Peek
parent 660fc93942
commit f209d3898f
3 changed files with 42 additions and 33 deletions

View file

@ -3,7 +3,7 @@ class Rails::InfoController < ActionController::Base
if consider_all_requests_local || local_request? if consider_all_requests_local || local_request?
render :inline => Rails::Info.to_html render :inline => Rails::Info.to_html
else else
render :text => '<p>For security purposes, this information is only available to local requests.</p>', :status => 500 render :text => '<p>For security purposes, this information is only available to local requests.</p>', :status => :forbidden
end end
end end
end end

View file

@ -2,51 +2,45 @@ require 'abstract_unit'
require 'action_controller' require 'action_controller'
require 'action_controller/test_process' require 'action_controller/test_process'
module Rails; end
require 'rails/info' require 'rails/info'
require 'rails/info_controller' require 'rails/info_controller'
class Rails::InfoController < ActionController::Base
@local_request = false
class << self
cattr_accessor :local_request
end
# Re-raise errors caught by the controller.
def rescue_action(e) raise e end;
protected
def local_request?
self.class.local_request
end
end
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id' map.connect ':controller/:action/:id'
end end
class Rails::InfoControllerTest < ActionController::TestCase class InfoControllerTest < ActionController::TestCase
def setup tests Rails::InfoController
@controller = Rails::InfoController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
ActionController::Base.consider_all_requests_local = true def setup
@controller.stubs(:consider_all_requests_local => false, :local_request? => true)
end end
def test_rails_info_properties_table_rendered_for_local_request test "info controller does not allow remote requests" do
Rails::InfoController.local_request = true @controller.stubs(:consider_all_requests_local => false, :local_request? => false)
get :properties
assert_response :forbidden
end
test "info controller renders an error message when request was forbidden" do
@controller.stubs(:consider_all_requests_local => false, :local_request? => false)
get :properties
assert_select 'p'
end
test "info controller allows requests when all requests are considered local" do
@controller.stubs(:consider_all_requests_local => true, :local_request? => false)
get :properties get :properties
assert_tag :tag => 'table'
assert_response :success assert_response :success
end end
def test_rails_info_properties_error_rendered_for_non_local_request
Rails::InfoController.local_request = false
ActionController::Base.consider_all_requests_local = false
test "info controller allows local requests" do
get :properties get :properties
assert_tag :tag => 'p' assert_response :success
assert_response 500 end
test "info controller renders a table with properties" do
get :properties
assert_select 'table'
end end
end end

View file

@ -1,9 +1,12 @@
$:.unshift File.dirname(__FILE__) + "/../lib" $:.unshift File.dirname(__FILE__) + "/../lib"
$:.unshift File.dirname(__FILE__) + "/../builtin/rails_info" $:.unshift File.dirname(__FILE__) + "/../builtin/rails_info"
$:.unshift File.dirname(__FILE__) + "/../../activesupport/lib" $:.unshift File.dirname(__FILE__) + "/../../activesupport/lib"
$:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
require 'test/unit' require 'test/unit'
require 'active_support' require 'active_support'
require 'active_support/test_case'
require 'action_controller'
unless defined?(Rails) && defined?(Rails::Info) unless defined?(Rails) && defined?(Rails::Info)
module Rails module Rails
@ -11,7 +14,7 @@ unless defined?(Rails) && defined?(Rails::Info)
end end
end end
class InfoTest < Test::Unit::TestCase class InfoTest < ActiveSupport::TestCase
def setup def setup
Rails.send :remove_const, :Info Rails.send :remove_const, :Info
silence_warnings { load 'rails/info.rb' } silence_warnings { load 'rails/info.rb' }
@ -72,6 +75,18 @@ EOS
end end
end end
def test_middleware_property
assert property_defined?('Middleware')
end
def test_html_includes_middleware
html = Rails::Info.to_html
assert html.include?('<tr><td class="name">Middleware</td>')
properties.value_for('Middleware').each do |value|
assert html.include?("<li>#{CGI.escapeHTML(value)}</li>")
end
end
protected protected
def svn_info=(info) def svn_info=(info)
Rails::Info.module_eval do Rails::Info.module_eval do