Permalink
Browse files

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>
  • Loading branch information...
1 parent 660fc93 commit f209d3898fbd866e1405861319b85c97674a0508 @Manfred Manfred committed with josh Apr 7, 2009
@@ -3,7 +3,7 @@ def properties
if consider_all_requests_local || local_request?
render :inline => Rails::Info.to_html
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
@@ -2,51 +2,45 @@
require 'action_controller'
require 'action_controller/test_process'
-module Rails; end
require 'rails/info'
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|
map.connect ':controller/:action/:id'
end
-class Rails::InfoControllerTest < ActionController::TestCase
+class InfoControllerTest < ActionController::TestCase
+ tests Rails::InfoController
+
def setup
- @controller = Rails::InfoController.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
+ @controller.stubs(:consider_all_requests_local => false, :local_request? => true)
+ end
+
+ test "info controller does not allow remote requests" do
+ @controller.stubs(:consider_all_requests_local => false, :local_request? => false)
+ get :properties
+ assert_response :forbidden
+ end
- ActionController::Base.consider_all_requests_local = true
+ 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
+ assert_response :success
end
- def test_rails_info_properties_table_rendered_for_local_request
- Rails::InfoController.local_request = true
+ test "info controller allows local requests" do
get :properties
- assert_tag :tag => 'table'
assert_response :success
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 renders a table with properties" do
get :properties
- assert_tag :tag => 'p'
- assert_response 500
+ assert_select 'table'
end
end
@@ -1,17 +1,20 @@
$:.unshift File.dirname(__FILE__) + "/../lib"
$:.unshift File.dirname(__FILE__) + "/../builtin/rails_info"
$:.unshift File.dirname(__FILE__) + "/../../activesupport/lib"
+$:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
require 'test/unit'
require 'active_support'
+require 'active_support/test_case'
+require 'action_controller'
unless defined?(Rails) && defined?(Rails::Info)
module Rails
class Info; end
end
end
-class InfoTest < Test::Unit::TestCase
+class InfoTest < ActiveSupport::TestCase
def setup
Rails.send :remove_const, :Info
silence_warnings { load 'rails/info.rb' }
@@ -72,6 +75,18 @@ def test_frameworks_exist
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
def svn_info=(info)
Rails::Info.module_eval do

0 comments on commit f209d38

Please sign in to comment.