2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2009-05-13 04:10:37 -04:00
|
|
|
require 'logger'
|
2005-09-20 09:55:27 -04:00
|
|
|
require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
|
2005-02-17 14:00:21 -05:00
|
|
|
|
2010-01-21 10:50:11 -05:00
|
|
|
module Rails
|
|
|
|
end
|
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
# Provide some controller to run the tests on.
|
|
|
|
module Submodule
|
|
|
|
class ContainedEmptyController < ActionController::Base
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2010-01-07 09:26:31 -05:00
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
class ContainedNonEmptyController < ActionController::Base
|
2005-02-17 14:00:21 -05:00
|
|
|
def public_action
|
2008-07-12 18:54:24 -04:00
|
|
|
render :nothing => true
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2005-02-23 07:52:38 -05:00
|
|
|
hide_action :hidden_action
|
2005-02-17 14:00:21 -05:00
|
|
|
def hidden_action
|
2006-08-13 00:15:22 -04:00
|
|
|
raise "Noooo!"
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
def another_hidden_action
|
|
|
|
end
|
|
|
|
hide_action :another_hidden_action
|
|
|
|
end
|
2010-01-07 09:26:31 -05:00
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
class SubclassedController < ContainedNonEmptyController
|
|
|
|
hide_action :public_action # Hiding it here should not affect the superclass.
|
|
|
|
end
|
|
|
|
end
|
2010-01-07 09:26:31 -05:00
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
class EmptyController < ActionController::Base
|
|
|
|
end
|
2010-01-07 09:26:31 -05:00
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
class NonEmptyController < ActionController::Base
|
|
|
|
def public_action
|
2009-05-03 00:02:22 -04:00
|
|
|
render :nothing => true
|
2006-02-26 12:49:09 -05:00
|
|
|
end
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
hide_action :hidden_action
|
|
|
|
def hidden_action
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-13 00:15:22 -04:00
|
|
|
class MethodMissingController < ActionController::Base
|
|
|
|
hide_action :shouldnt_be_called
|
|
|
|
def shouldnt_be_called
|
|
|
|
raise "NO WAY!"
|
|
|
|
end
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2006-08-13 00:15:22 -04:00
|
|
|
protected
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2006-08-13 00:15:22 -04:00
|
|
|
def method_missing(selector)
|
|
|
|
render :text => selector.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-20 00:57:36 -04:00
|
|
|
class DefaultUrlOptionsController < ActionController::Base
|
2010-01-07 09:26:31 -05:00
|
|
|
def from_view
|
|
|
|
render :inline => "<%= #{params[:route]} %>"
|
2008-04-20 00:57:36 -04:00
|
|
|
end
|
|
|
|
|
2008-05-04 11:54:08 -04:00
|
|
|
def default_url_options(options = nil)
|
2010-01-07 09:26:31 -05:00
|
|
|
{ :host => 'www.override.com', :action => 'new', :locale => 'en' }
|
2008-04-20 00:57:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-26 18:34:12 -05:00
|
|
|
class UrlOptionsController < ActionController::Base
|
|
|
|
def from_view
|
|
|
|
render :inline => "<%= #{params[:route]} %>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_options
|
|
|
|
super.merge(:host => 'www.override.com', :action => 'new', :locale => 'en')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-09 13:37:46 -05:00
|
|
|
class RecordIdentifierController < ActionController::Base
|
|
|
|
end
|
|
|
|
|
2010-01-21 10:50:11 -05:00
|
|
|
class ControllerClassTests < ActiveSupport::TestCase
|
2005-02-17 14:00:21 -05:00
|
|
|
def test_controller_path
|
2006-02-26 12:49:09 -05:00
|
|
|
assert_equal 'empty', EmptyController.controller_path
|
2006-08-04 21:56:58 -04:00
|
|
|
assert_equal EmptyController.controller_path, EmptyController.new.controller_path
|
2006-02-26 12:49:09 -05:00
|
|
|
assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
|
2006-08-04 21:56:58 -04:00
|
|
|
assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2010-01-07 09:26:31 -05:00
|
|
|
|
2005-02-17 14:00:21 -05:00
|
|
|
def test_controller_name
|
2006-02-26 12:49:09 -05:00
|
|
|
assert_equal 'empty', EmptyController.controller_name
|
|
|
|
assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
|
2010-01-21 10:50:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_filter_parameter_logging
|
|
|
|
parameters = []
|
|
|
|
config = mock(:config => mock(:filter_parameters => parameters))
|
|
|
|
Rails.expects(:application).returns(config)
|
|
|
|
|
|
|
|
assert_deprecated do
|
|
|
|
Class.new(ActionController::Base) do
|
|
|
|
filter_parameter_logging :password
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal [:password], parameters
|
|
|
|
end
|
2010-03-09 13:37:46 -05:00
|
|
|
|
|
|
|
def test_record_identifier
|
|
|
|
assert_respond_to RecordIdentifierController.new, :dom_id
|
|
|
|
assert_respond_to RecordIdentifierController.new, :dom_class
|
|
|
|
end
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class ControllerInstanceTests < Test::Unit::TestCase
|
|
|
|
def setup
|
2006-02-26 12:49:09 -05:00
|
|
|
@empty = EmptyController.new
|
|
|
|
@contained = Submodule::ContainedEmptyController.new
|
|
|
|
@empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
@non_empty_controllers = [NonEmptyController.new,
|
|
|
|
Submodule::ContainedNonEmptyController.new]
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2005-09-20 03:54:55 -04:00
|
|
|
|
2005-02-17 14:00:21 -05:00
|
|
|
def test_action_methods
|
2005-10-20 17:59:48 -04:00
|
|
|
@empty_controllers.each do |c|
|
2009-06-10 18:27:53 -04:00
|
|
|
assert_equal Set.new, c.class.__send__(:action_methods), "#{c.controller_path} should be empty!"
|
2005-10-20 17:59:48 -04:00
|
|
|
end
|
2010-01-07 09:26:31 -05:00
|
|
|
|
2005-10-20 17:59:48 -04:00
|
|
|
@non_empty_controllers.each do |c|
|
2009-06-10 18:27:53 -04:00
|
|
|
assert_equal Set.new(%w(public_action)), c.class.__send__(:action_methods), "#{c.controller_path} should not be empty!"
|
2005-10-20 17:59:48 -04:00
|
|
|
end
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2010-02-26 05:51:21 -05:00
|
|
|
|
|
|
|
def test_temporary_anonymous_controllers
|
|
|
|
name = 'ExamplesController'
|
|
|
|
klass = Class.new(ActionController::Base)
|
|
|
|
Object.const_set(name, klass)
|
|
|
|
|
|
|
|
controller = klass.new
|
|
|
|
assert_equal "examples", controller.controller_path
|
|
|
|
end
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2006-08-13 00:15:22 -04:00
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class PerformActionTest < ActionController::TestCase
|
2006-08-13 00:15:22 -04:00
|
|
|
def use_controller(controller_class)
|
|
|
|
@controller = controller_class.new
|
|
|
|
|
|
|
|
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
|
|
|
|
# a more accurate simulation of what happens in "real life".
|
|
|
|
@controller.logger = Logger.new(nil)
|
|
|
|
|
2010-01-07 09:26:31 -05:00
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2006-08-13 00:15:22 -04:00
|
|
|
@request.host = "www.nextangle.com"
|
2009-01-07 16:23:10 -05:00
|
|
|
|
|
|
|
rescue_action_in_public!
|
2006-08-13 00:15:22 -04:00
|
|
|
end
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2006-08-13 00:15:22 -04:00
|
|
|
def test_get_on_priv_should_show_selector
|
|
|
|
use_controller MethodMissingController
|
|
|
|
get :shouldnt_be_called
|
|
|
|
assert_response :success
|
|
|
|
assert_equal 'shouldnt_be_called', @response.body
|
|
|
|
end
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2006-08-13 00:15:22 -04:00
|
|
|
def test_method_missing_is_not_an_action_name
|
|
|
|
use_controller MethodMissingController
|
2010-01-07 09:26:31 -05:00
|
|
|
assert !@controller.__send__(:action_method?, 'method_missing')
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2006-08-13 00:15:22 -04:00
|
|
|
get :method_missing
|
|
|
|
assert_response :success
|
|
|
|
assert_equal 'method_missing', @response.body
|
|
|
|
end
|
2009-10-20 17:03:55 -04:00
|
|
|
|
2006-08-13 00:15:22 -04:00
|
|
|
def test_get_on_hidden_should_fail
|
|
|
|
use_controller NonEmptyController
|
2009-05-03 00:02:22 -04:00
|
|
|
assert_raise(ActionController::UnknownAction) { get :hidden_action }
|
|
|
|
assert_raise(ActionController::UnknownAction) { get :another_hidden_action }
|
2006-08-13 00:15:22 -04:00
|
|
|
end
|
2007-01-24 07:04:08 -05:00
|
|
|
end
|
2008-04-20 00:57:36 -04:00
|
|
|
|
2010-02-26 18:34:12 -05:00
|
|
|
class UrlOptionsTest < ActionController::TestCase
|
|
|
|
tests UrlOptionsController
|
2008-04-20 00:57:36 -04:00
|
|
|
|
2009-01-07 16:23:10 -05:00
|
|
|
def setup
|
2009-04-08 20:32:19 -04:00
|
|
|
super
|
2008-04-20 00:57:36 -04:00
|
|
|
@request.host = 'www.example.com'
|
2009-01-07 16:23:10 -05:00
|
|
|
rescue_action_in_public!
|
2008-04-20 00:57:36 -04:00
|
|
|
end
|
|
|
|
|
2010-03-09 00:40:04 -05:00
|
|
|
def test_url_options_override
|
2009-08-16 22:14:26 -04:00
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
2010-02-26 18:34:12 -05:00
|
|
|
match 'from_view', :to => 'url_options#from_view', :as => :from_view
|
2009-12-08 17:52:26 -05:00
|
|
|
match ':controller/:action'
|
2009-08-16 22:14:26 -04:00
|
|
|
end
|
2008-04-20 00:57:36 -04:00
|
|
|
|
2010-01-07 09:26:31 -05:00
|
|
|
get :from_view, :route => "from_view_url"
|
2008-04-20 00:57:36 -04:00
|
|
|
|
2010-01-07 09:26:31 -05:00
|
|
|
assert_equal 'http://www.override.com/from_view?locale=en', @response.body
|
|
|
|
assert_equal 'http://www.override.com/from_view?locale=en', @controller.send(:from_view_url)
|
|
|
|
assert_equal 'http://www.override.com/default_url_options/new?locale=en', @controller.url_for(:controller => 'default_url_options')
|
2009-08-16 22:14:26 -04:00
|
|
|
end
|
2010-02-26 18:34:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DefaultUrlOptionsTest < ActionController::TestCase
|
|
|
|
tests DefaultUrlOptionsController
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@request.host = 'www.example.com'
|
|
|
|
rescue_action_in_public!
|
|
|
|
end
|
|
|
|
|
2010-03-09 00:40:04 -05:00
|
|
|
def test_default_url_options_override
|
2010-02-26 18:34:12 -05:00
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
match 'from_view', :to => 'default_url_options#from_view', :as => :from_view
|
|
|
|
match ':controller/:action'
|
|
|
|
end
|
|
|
|
|
2010-03-09 00:40:04 -05:00
|
|
|
get :from_view, :route => "from_view_url"
|
2010-02-26 18:34:12 -05:00
|
|
|
|
2010-03-09 00:40:04 -05:00
|
|
|
assert_equal 'http://www.override.com/from_view?locale=en', @response.body
|
|
|
|
assert_equal 'http://www.override.com/from_view?locale=en', @controller.send(:from_view_url)
|
|
|
|
assert_equal 'http://www.override.com/default_url_options/new?locale=en', @controller.url_for(:controller => 'default_url_options')
|
2010-02-26 18:34:12 -05:00
|
|
|
end
|
2008-04-20 00:57:36 -04:00
|
|
|
end
|
2010-01-07 09:26:31 -05:00
|
|
|
|
|
|
|
def test_default_url_options_are_used_in_non_positional_parameters
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
|
|
|
scope("/:locale") do
|
|
|
|
resources :descriptions
|
|
|
|
end
|
|
|
|
match ':controller/:action'
|
|
|
|
end
|
|
|
|
|
2010-03-09 00:40:04 -05:00
|
|
|
get :from_view, :route => "description_path(1)"
|
|
|
|
|
|
|
|
assert_equal '/en/descriptions/1', @response.body
|
|
|
|
assert_equal '/en/descriptions', @controller.send(:descriptions_path)
|
|
|
|
assert_equal '/pl/descriptions', @controller.send(:descriptions_path, "pl")
|
|
|
|
assert_equal '/pl/descriptions', @controller.send(:descriptions_path, :locale => "pl")
|
|
|
|
assert_equal '/pl/descriptions.xml', @controller.send(:descriptions_path, "pl", "xml")
|
|
|
|
assert_equal '/en/descriptions.xml', @controller.send(:descriptions_path, :format => "xml")
|
|
|
|
assert_equal '/en/descriptions/1', @controller.send(:description_path, 1)
|
|
|
|
assert_equal '/pl/descriptions/1', @controller.send(:description_path, "pl", 1)
|
|
|
|
assert_equal '/pl/descriptions/1', @controller.send(:description_path, 1, :locale => "pl")
|
|
|
|
assert_equal '/pl/descriptions/1.xml', @controller.send(:description_path, "pl", 1, "xml")
|
|
|
|
assert_equal '/en/descriptions/1.xml', @controller.send(:description_path, 1, :format => "xml")
|
2010-01-07 09:26:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-06 02:42:52 -04:00
|
|
|
end
|
|
|
|
|
2009-01-07 16:23:10 -05:00
|
|
|
class EmptyUrlOptionsTest < ActionController::TestCase
|
|
|
|
tests NonEmptyController
|
2008-06-05 08:20:54 -04:00
|
|
|
|
2009-01-07 16:23:10 -05:00
|
|
|
def setup
|
2009-04-08 20:32:19 -04:00
|
|
|
super
|
2008-06-05 08:20:54 -04:00
|
|
|
@request.host = 'www.example.com'
|
2009-01-07 16:23:10 -05:00
|
|
|
rescue_action_in_public!
|
2008-06-05 08:20:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
|
|
|
|
get :public_action
|
|
|
|
assert_equal "http://www.example.com/non_empty/public_action", @controller.url_for
|
|
|
|
end
|
|
|
|
|
2010-01-07 09:26:31 -05:00
|
|
|
def test_named_routes_with_path_without_doing_a_request_first
|
2010-02-24 19:01:03 -05:00
|
|
|
@controller = EmptyController.new
|
2010-02-25 18:05:10 -05:00
|
|
|
@controller.request = @request
|
2010-02-24 19:01:03 -05:00
|
|
|
|
2009-08-16 22:14:26 -04:00
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
2009-12-08 17:52:26 -05:00
|
|
|
resources :things
|
2009-08-16 22:14:26 -04:00
|
|
|
end
|
2008-05-06 02:42:52 -04:00
|
|
|
|
2010-02-24 19:01:03 -05:00
|
|
|
assert_equal '/things', @controller.send(:things_path)
|
2009-08-16 22:14:26 -04:00
|
|
|
end
|
2008-05-06 02:42:52 -04:00
|
|
|
end
|
2008-06-05 08:20:54 -04:00
|
|
|
end
|