2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2011-12-23 13:47:21 -05:00
|
|
|
require 'active_support/logger'
|
2012-08-25 07:40:56 -04:00
|
|
|
require 'controller/fake_models'
|
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
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2012-04-08 22:42:02 -04:00
|
|
|
def default_url_options
|
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
|
2012-03-02 09:01:20 -05:00
|
|
|
super.merge(:host => 'www.override.com')
|
2010-02-26 18:34:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-01 01:07:59 -04:00
|
|
|
class RecordIdentifierIncludedController < ActionController::Base
|
2012-08-25 07:40:56 -04:00
|
|
|
include ActionView::RecordIdentifier
|
|
|
|
end
|
|
|
|
|
2013-03-19 07:55:26 -04:00
|
|
|
class ActionMissingController < ActionController::Base
|
|
|
|
def action_missing(action)
|
|
|
|
render :text => "Response for #{action}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-21 10:50:11 -05:00
|
|
|
class ControllerClassTests < ActiveSupport::TestCase
|
2010-08-14 01:13:00 -04:00
|
|
|
|
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
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2012-08-25 07:40:56 -04:00
|
|
|
def test_no_deprecation_when_action_view_record_identifier_is_included
|
|
|
|
record = Comment.new
|
|
|
|
record.save
|
|
|
|
|
|
|
|
dom_id = nil
|
|
|
|
assert_not_deprecated do
|
2013-06-01 01:07:59 -04:00
|
|
|
dom_id = RecordIdentifierIncludedController.new.dom_id(record)
|
2012-08-25 07:40:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'comment_1', dom_id
|
|
|
|
|
|
|
|
dom_class = nil
|
|
|
|
assert_not_deprecated do
|
2013-06-01 01:07:59 -04:00
|
|
|
dom_class = RecordIdentifierIncludedController.new.dom_class(record)
|
2012-08-25 07:40:56 -04:00
|
|
|
end
|
|
|
|
assert_equal 'comment', dom_class
|
|
|
|
end
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
|
|
|
|
2012-01-05 20:01:45 -05:00
|
|
|
class ControllerInstanceTests < ActiveSupport::TestCase
|
2005-02-17 14:00:21 -05:00
|
|
|
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
|
|
|
|
2012-01-19 13:52:10 -05:00
|
|
|
def test_performed?
|
|
|
|
assert !@empty.performed?
|
|
|
|
@empty.response_body = ["sweet"]
|
|
|
|
assert @empty.performed?
|
|
|
|
end
|
|
|
|
|
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|
|
2010-03-22 18:57:06 -04:00
|
|
|
assert_equal Set.new, c.class.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|
|
2010-03-22 18:57:06 -04:00
|
|
|
assert_equal Set.new(%w(public_action)), c.class.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".
|
2011-12-23 13:47:21 -05:00
|
|
|
@controller.logger = ActiveSupport::Logger.new(nil)
|
2006-08-13 00:15:22 -04:00
|
|
|
|
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"
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-04-18 22:48:35 -04:00
|
|
|
def test_process_should_be_precise
|
|
|
|
use_controller EmptyController
|
|
|
|
exception = assert_raise AbstractController::ActionNotFound do
|
|
|
|
get :non_existent
|
|
|
|
end
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal "The action 'non_existent' could not be found for EmptyController", exception.message
|
2010-04-18 22:48:35 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-08-13 00:15:22 -04:00
|
|
|
def test_get_on_hidden_should_fail
|
|
|
|
use_controller NonEmptyController
|
2012-01-14 15:25:11 -05:00
|
|
|
assert_raise(AbstractController::ActionNotFound) { get :hidden_action }
|
|
|
|
assert_raise(AbstractController::ActionNotFound) { get :another_hidden_action }
|
2006-08-13 00:15:22 -04:00
|
|
|
end
|
2013-03-19 07:55:26 -04:00
|
|
|
|
|
|
|
def test_action_missing_should_work
|
|
|
|
use_controller ActionMissingController
|
|
|
|
get :arbitrary_action
|
|
|
|
assert_equal "Response for arbitrary_action", @response.body
|
|
|
|
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'
|
|
|
|
end
|
|
|
|
|
2012-02-07 11:56:26 -05:00
|
|
|
def test_url_for_query_params_included
|
|
|
|
rs = ActionDispatch::Routing::RouteSet.new
|
|
|
|
rs.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'home' => 'pages#home'
|
2012-02-07 11:56:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
options = {
|
|
|
|
:action => "home",
|
|
|
|
:controller => "pages",
|
|
|
|
:only_path => true,
|
|
|
|
:params => { "token" => "secret" }
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal '/home?token=secret', rs.url_for(options)
|
|
|
|
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|
|
2010-03-22 18:57:06 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'from_view', :to => 'url_options#from_view', :as => :from_view
|
|
|
|
get ':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
|
|
|
|
2012-03-02 09:01:20 -05:00
|
|
|
assert_equal 'http://www.override.com/from_view', @response.body
|
|
|
|
assert_equal 'http://www.override.com/from_view', @controller.send(:from_view_url)
|
|
|
|
assert_equal 'http://www.override.com/default_url_options/index', @controller.url_for(:controller => 'default_url_options')
|
2009-08-16 22:14:26 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
end
|
2010-03-22 18:57:06 -04:00
|
|
|
|
|
|
|
def test_url_helpers_does_not_become_actions
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "account/overview"
|
2010-03-22 18:57:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert !@controller.class.action_methods.include?("account_overview_path")
|
|
|
|
end
|
|
|
|
end
|
2010-02-26 18:34:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class DefaultUrlOptionsTest < ActionController::TestCase
|
|
|
|
tests DefaultUrlOptionsController
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@request.host = 'www.example.com'
|
|
|
|
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|
|
2010-03-22 18:57:06 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'from_view', :to => 'default_url_options#from_view', :as => :from_view
|
|
|
|
get ':controller/:action'
|
2010-02-26 18:34:12 -05:00
|
|
|
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|
|
2010-03-22 18:57:06 -04:00
|
|
|
set.draw do
|
2010-01-07 09:26:31 -05:00
|
|
|
scope("/:locale") do
|
|
|
|
resources :descriptions
|
|
|
|
end
|
2012-04-24 23:32:09 -04:00
|
|
|
get ':controller/:action'
|
2010-01-07 09:26:31 -05:00
|
|
|
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'
|
|
|
|
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|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
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
|