2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/logger"
|
|
|
|
require "controller/fake_models"
|
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
|
2006-02-26 12:49:09 -05:00
|
|
|
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
|
|
|
|
2017-03-29 17:36:54 -04:00
|
|
|
class SimpleController < ActionController::Base
|
|
|
|
def hello
|
|
|
|
self.response_body = "hello"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-02-26 12:49:09 -05:00
|
|
|
class NonEmptyController < ActionController::Base
|
|
|
|
def public_action
|
2015-05-28 08:13:32 -04:00
|
|
|
head :ok
|
2006-02-26 12:49:09 -05:00
|
|
|
end
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
|
|
|
|
2008-04-20 00:57:36 -04:00
|
|
|
class DefaultUrlOptionsController < ActionController::Base
|
2010-01-07 09:26:31 -05:00
|
|
|
def from_view
|
2016-08-06 13:35:13 -04:00
|
|
|
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
|
2016-08-06 13:35:13 -04:00
|
|
|
{ host: "www.override.com", action: "new", locale: "en" }
|
2008-04-20 00:57:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-21 10:40:02 -05:00
|
|
|
class OptionalDefaultUrlOptionsController < ActionController::Base
|
|
|
|
def show
|
2015-05-28 08:13:32 -04:00
|
|
|
head :ok
|
2015-01-21 10:40:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_url_options
|
2016-08-06 12:54:50 -04:00
|
|
|
{ format: "atom", id: "default-id" }
|
2015-01-21 10:40:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-26 18:34:12 -05:00
|
|
|
class UrlOptionsController < ActionController::Base
|
|
|
|
def from_view
|
2016-08-06 13:35:13 -04:00
|
|
|
render inline: "<%= #{params[:route]} %>"
|
2010-02-26 18:34:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def url_options
|
2016-08-06 13:35:13 -04: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)
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: "Response for #{action}"
|
2013-03-19 07:55:26 -04:00
|
|
|
end
|
|
|
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "empty", EmptyController.controller_path
|
2006-08-04 21:56:58 -04:00
|
|
|
assert_equal EmptyController.controller_path, EmptyController.new.controller_path
|
2016-08-06 12:54:50 -04: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
|
2016-08-06 12:54:50 -04: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
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "comment_1", dom_id
|
2012-08-25 07:40:56 -04:00
|
|
|
|
|
|
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "comment", dom_class
|
2012-08-25 07:40:56 -04:00
|
|
|
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
|
2015-12-01 14:28:01 -05:00
|
|
|
@empty.set_request!(ActionDispatch::Request.empty)
|
2015-08-25 21:35:44 -04:00
|
|
|
@empty.set_response!(EmptyController.make_response!(@empty.request))
|
2006-02-26 12:49:09 -05:00
|
|
|
@contained = Submodule::ContainedEmptyController.new
|
2015-01-06 15:33:31 -05:00
|
|
|
@empty_controllers = [@empty, @contained]
|
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?
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate @empty, :performed?
|
2012-01-19 13:52:10 -05:00
|
|
|
@empty.response_body = ["sweet"]
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate @empty, :performed?
|
2012-01-19 13:52:10 -05:00
|
|
|
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
|
2005-02-17 14:00:21 -05:00
|
|
|
end
|
2010-02-26 05:51:21 -05:00
|
|
|
|
|
|
|
def test_temporary_anonymous_controllers
|
2016-08-06 12:54:50 -04:00
|
|
|
name = "ExamplesController"
|
2010-02-26 05:51:21 -05:00
|
|
|
klass = Class.new(ActionController::Base)
|
|
|
|
Object.const_set(name, klass)
|
|
|
|
|
|
|
|
controller = klass.new
|
|
|
|
assert_equal "examples", controller.controller_path
|
|
|
|
end
|
2017-03-29 17:36:54 -04:00
|
|
|
|
|
|
|
def test_response_has_default_headers
|
|
|
|
original_default_headers = ActionDispatch::Response.default_headers
|
|
|
|
|
|
|
|
ActionDispatch::Response.default_headers = {
|
|
|
|
"X-Frame-Options" => "DENY",
|
|
|
|
"X-Content-Type-Options" => "nosniff",
|
|
|
|
"X-XSS-Protection" => "1;"
|
|
|
|
}
|
|
|
|
|
|
|
|
response_headers = SimpleController.action("hello").call(
|
|
|
|
"REQUEST_METHOD" => "GET",
|
|
|
|
"rack.input" => -> {}
|
|
|
|
)[1]
|
|
|
|
|
|
|
|
assert response_headers.key?("X-Frame-Options")
|
|
|
|
assert response_headers.key?("X-Content-Type-Options")
|
|
|
|
assert response_headers.key?("X-XSS-Protection")
|
|
|
|
ensure
|
|
|
|
ActionDispatch::Response.default_headers = original_default_headers
|
|
|
|
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
|
|
|
|
|
|
|
@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
|
|
|
|
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
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.host = "www.example.com"
|
2008-04-20 00:57:36 -04:00
|
|
|
end
|
|
|
|
|
2012-02-07 11:56:26 -05:00
|
|
|
def test_url_for_query_params_included
|
|
|
|
rs = ActionDispatch::Routing::RouteSet.new
|
|
|
|
rs.draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "home" => "pages#home"
|
2012-02-07 11:56:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
options = {
|
2016-08-06 13:35:13 -04:00
|
|
|
action: "home",
|
|
|
|
controller: "pages",
|
|
|
|
only_path: true,
|
|
|
|
params: { "token" => "secret" }
|
2012-02-07 11:56:26 -05:00
|
|
|
}
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/home?token=secret", rs.url_for(options)
|
2012-02-07 11:56:26 -05: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|
|
2010-03-22 18:57:06 -04:00
|
|
|
set.draw do
|
2016-08-06 13:35:13 -04:00
|
|
|
get "from_view", to: "url_options#from_view", as: :from_view
|
2016-03-01 03:48:53 -05:00
|
|
|
|
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 12:54:50 -04:00
|
|
|
get ":controller/:action"
|
2016-03-01 03:48:53 -05:00
|
|
|
end
|
2009-08-16 22:14:26 -04:00
|
|
|
end
|
2008-04-20 00:57:36 -04:00
|
|
|
|
2015-01-29 09:19:41 -05:00
|
|
|
get :from_view, params: { route: "from_view_url" }
|
2008-04-20 00:57:36 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "http://www.override.com/from_view", @response.body
|
|
|
|
assert_equal "http://www.override.com/from_view", @controller.send(:from_view_url)
|
2016-08-06 13:35:13 -04:00
|
|
|
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
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes @controller.class.action_methods, "account_overview_path"
|
2010-03-22 18:57:06 -04:00
|
|
|
end
|
|
|
|
end
|
2010-02-26 18:34:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class DefaultUrlOptionsTest < ActionController::TestCase
|
|
|
|
tests DefaultUrlOptionsController
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.host = "www.example.com"
|
2010-02-26 18:34:12 -05:00
|
|
|
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
|
2016-08-06 13:35:13 -04:00
|
|
|
get "from_view", to: "default_url_options#from_view", as: :from_view
|
2016-03-01 03:48:53 -05:00
|
|
|
|
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 12:54:50 -04:00
|
|
|
get ":controller/:action"
|
2016-03-01 03:48:53 -05:00
|
|
|
end
|
2010-02-26 18:34:12 -05:00
|
|
|
end
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get :from_view, params: { route: "from_view_url" }
|
2010-02-26 18:34:12 -05:00
|
|
|
|
2016-08-06 12:54:50 -04: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)
|
2016-08-06 13:35:13 -04:00
|
|
|
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
|
2016-03-01 03:48:53 -05:00
|
|
|
|
|
|
|
ActiveSupport::Deprecation.silence do
|
2016-08-06 12:54:50 -04:00
|
|
|
get ":controller/:action"
|
2016-03-01 03:48:53 -05:00
|
|
|
end
|
2010-01-07 09:26:31 -05:00
|
|
|
end
|
|
|
|
|
2015-01-04 04:35:06 -05:00
|
|
|
get :from_view, params: { route: "description_path(1)" }
|
2010-03-09 00:40:04 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/en/descriptions/1", @response.body
|
|
|
|
assert_equal "/en/descriptions", @controller.send(:descriptions_path)
|
|
|
|
assert_equal "/pl/descriptions", @controller.send(:descriptions_path, "pl")
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal "/pl/descriptions", @controller.send(:descriptions_path, locale: "pl")
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/pl/descriptions.xml", @controller.send(:descriptions_path, "pl", "xml")
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal "/en/descriptions.xml", @controller.send(:descriptions_path, format: "xml")
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/en/descriptions/1", @controller.send(:description_path, 1)
|
|
|
|
assert_equal "/pl/descriptions/1", @controller.send(:description_path, "pl", 1)
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal "/pl/descriptions/1", @controller.send(:description_path, 1, locale: "pl")
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/pl/descriptions/1.xml", @controller.send(:description_path, "pl", 1, "xml")
|
2016-08-06 13:35:13 -04:00
|
|
|
assert_equal "/en/descriptions/1.xml", @controller.send(:description_path, 1, format: "xml")
|
2010-01-07 09:26:31 -05:00
|
|
|
end
|
|
|
|
end
|
2015-01-21 10:40:02 -05:00
|
|
|
end
|
2010-01-07 09:26:31 -05:00
|
|
|
|
2015-01-21 10:40:02 -05:00
|
|
|
class OptionalDefaultUrlOptionsControllerTest < ActionController::TestCase
|
|
|
|
def test_default_url_options_override_missing_positional_arguments
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/things/:id(.:format)" => "things#show", :as => :thing
|
2015-01-21 10:40:02 -05:00
|
|
|
end
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "/things/1.atom", thing_path("1")
|
|
|
|
assert_equal "/things/default-id.atom", thing_path
|
2015-01-21 10:40:02 -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
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.host = "www.example.com"
|
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|
|
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
|
|
|
|
2016-08-06 12:54:50 -04: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
|