2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2009-09-06 22:19:29 -04:00
|
|
|
require 'controller/fake_controllers'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
class ActionPackAssertionsController < ActionController::Base
|
|
|
|
|
2006-11-28 18:41:04 -05:00
|
|
|
def nothing() head :ok end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2007-09-02 20:18:30 -04:00
|
|
|
def hello_world() render :template => "test/hello_world"; end
|
2012-04-06 23:27:47 -04:00
|
|
|
def hello_repeating_in_path() render :template => "test/hello/hello"; end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2007-09-02 20:18:30 -04:00
|
|
|
def hello_xml_world() render :template => "test/hello_xml_world"; end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2009-06-16 19:46:56 -04:00
|
|
|
def hello_xml_world_pdf
|
|
|
|
self.content_type = "application/pdf"
|
|
|
|
render :template => "test/hello_xml_world"
|
|
|
|
end
|
|
|
|
|
|
|
|
def hello_xml_world_pdf_header
|
|
|
|
response.headers["Content-Type"] = "application/pdf; charset=utf-8"
|
|
|
|
render :template => "test/hello_xml_world"
|
|
|
|
end
|
|
|
|
|
2009-05-17 13:48:15 -04:00
|
|
|
def partial() render :partial => 'test/partial'; end
|
|
|
|
|
2005-05-22 03:43:05 -04:00
|
|
|
def redirect_internal() redirect_to "/nothing"; end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2005-02-12 16:54:34 -05:00
|
|
|
def redirect_to_action() redirect_to :action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end
|
2005-02-12 14:35:30 -05:00
|
|
|
|
|
|
|
def redirect_to_controller() redirect_to :controller => "elsewhere", :action => "flash_me"; end
|
|
|
|
|
2007-10-07 15:12:02 -04:00
|
|
|
def redirect_to_controller_with_symbol() redirect_to :controller => :elsewhere, :action => :flash_me; end
|
|
|
|
|
2005-05-19 15:19:58 -04:00
|
|
|
def redirect_to_path() redirect_to '/some/path' end
|
2005-08-06 04:37:25 -04:00
|
|
|
|
2010-09-29 11:55:43 -04:00
|
|
|
def redirect_invalid_external_route() redirect_to 'ht_tp://www.rubyonrails.org' end
|
|
|
|
|
2005-08-06 04:37:25 -04:00
|
|
|
def redirect_to_named_route() redirect_to route_one_url end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2007-03-12 23:58:22 -04:00
|
|
|
def redirect_external() redirect_to "http://www.rubyonrails.org"; end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2013-04-26 14:30:29 -04:00
|
|
|
def redirect_external_protocol_relative() redirect_to "//www.rubyonrails.org"; end
|
|
|
|
|
2006-11-28 18:41:04 -05:00
|
|
|
def response404() head '404 AWOL' end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2006-11-28 18:41:04 -05:00
|
|
|
def response500() head '500 Sorry' end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2006-11-28 18:41:04 -05:00
|
|
|
def response599() head '599 Whoah!' end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def flash_me
|
|
|
|
flash['hello'] = 'my name is inigo montoya...'
|
2007-09-02 20:18:30 -04:00
|
|
|
render :text => "Inconceivable!"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def flash_me_naked
|
|
|
|
flash.clear
|
2007-09-02 20:18:30 -04:00
|
|
|
render :text => "wow!"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def assign_this
|
|
|
|
@howdy = "ho"
|
2005-07-24 12:45:39 -04:00
|
|
|
render :inline => "Mr. Henke"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_based_on_parameters
|
2007-09-02 20:18:30 -04:00
|
|
|
render :text => "Mr. #{params[:name]}"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-06-26 10:36:13 -04:00
|
|
|
def render_url
|
2007-09-02 20:18:30 -04:00
|
|
|
render :text => "<div>#{url_for(:action => 'flash_me', :only_path => true)}</div>"
|
2005-06-26 10:36:13 -04:00
|
|
|
end
|
|
|
|
|
2006-06-01 23:39:56 -04:00
|
|
|
def render_text_with_custom_content_type
|
|
|
|
render :text => "Hello!", :content_type => Mime::RSS
|
|
|
|
end
|
|
|
|
|
2011-12-02 13:13:36 -05:00
|
|
|
def render_with_layout
|
2011-12-17 01:30:28 -05:00
|
|
|
@variable_for_layout = nil
|
2011-12-02 13:13:36 -05:00
|
|
|
render "test/hello_world", :layout => "layouts/standard"
|
|
|
|
end
|
|
|
|
|
2012-04-11 01:28:19 -04:00
|
|
|
def render_with_layout_and_partial
|
|
|
|
@variable_for_layout = nil
|
|
|
|
render "test/hello_world_with_partial", :layout => "layouts/standard"
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def session_stuffing
|
|
|
|
session['xmas'] = 'turkey'
|
2007-09-02 20:18:30 -04:00
|
|
|
render :text => "ho ho ho"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def raise_exception_on_get
|
2006-09-29 03:34:02 -04:00
|
|
|
raise "get" if request.get?
|
2007-09-02 20:18:30 -04:00
|
|
|
render :text => "request method: #{request.env['REQUEST_METHOD']}"
|
2004-12-07 06:10:46 -05:00
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def raise_exception_on_post
|
2006-09-29 03:34:02 -04:00
|
|
|
raise "post" if request.post?
|
2007-09-02 20:18:30 -04:00
|
|
|
render :text => "request method: #{request.env['REQUEST_METHOD']}"
|
2006-09-29 03:34:02 -04:00
|
|
|
end
|
2013-02-27 10:44:25 -05:00
|
|
|
|
|
|
|
def render_file_absolute_path
|
|
|
|
render :file => File.expand_path('../../../README.rdoc', __FILE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_file_relative_path
|
|
|
|
render :file => 'README.rdoc'
|
|
|
|
end
|
2005-06-27 01:36:03 -04:00
|
|
|
end
|
|
|
|
|
2008-01-04 21:19:48 -05:00
|
|
|
# Used to test that assert_response includes the exception message
|
|
|
|
# in the failure message when an action raises and assert_response
|
|
|
|
# is expecting something other than an error.
|
|
|
|
class AssertResponseWithUnexpectedErrorController < ActionController::Base
|
|
|
|
def index
|
|
|
|
raise 'FAIL'
|
|
|
|
end
|
2008-05-16 11:27:09 -04:00
|
|
|
|
|
|
|
def show
|
|
|
|
render :text => "Boom", :status => 500
|
|
|
|
end
|
2008-01-04 21:19:48 -05:00
|
|
|
end
|
|
|
|
|
2005-06-27 01:36:03 -04:00
|
|
|
module Admin
|
|
|
|
class InnerModuleController < ActionController::Base
|
2006-08-13 14:31:58 -04:00
|
|
|
def index
|
|
|
|
render :nothing => true
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_to_index
|
|
|
|
redirect_to admin_inner_module_path
|
|
|
|
end
|
|
|
|
|
2005-06-27 01:36:03 -04:00
|
|
|
def redirect_to_absolute_controller
|
|
|
|
redirect_to :controller => '/content'
|
|
|
|
end
|
2006-08-13 14:31:58 -04:00
|
|
|
|
2005-06-27 01:36:03 -04:00
|
|
|
def redirect_to_fellow_controller
|
|
|
|
redirect_to :controller => 'user'
|
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2006-12-25 04:34:01 -05:00
|
|
|
def redirect_to_top_level_named_route
|
|
|
|
redirect_to top_level_url(:id => "foo")
|
|
|
|
end
|
2005-06-27 01:36:03 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class ActionPackAssertionsControllerTest < ActionController::TestCase
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2013-02-27 10:44:25 -05:00
|
|
|
def test_render_file_absolute_path
|
|
|
|
get :render_file_absolute_path
|
2013-03-07 19:32:51 -05:00
|
|
|
assert_match(/\A= Action Pack/, @response.body)
|
2013-02-27 10:44:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_file_relative_path
|
|
|
|
get :render_file_relative_path
|
2013-03-07 19:32:51 -05:00
|
|
|
assert_match(/\A= Action Pack/, @response.body)
|
2013-02-27 10:44:25 -05:00
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def test_get_request
|
|
|
|
assert_raise(RuntimeError) { get :raise_exception_on_get }
|
|
|
|
get :raise_exception_on_post
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 'request method: GET', @response.body
|
2004-12-07 06:10:46 -05:00
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def test_post_request
|
|
|
|
assert_raise(RuntimeError) { post :raise_exception_on_post }
|
|
|
|
post :raise_exception_on_get
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 'request method: POST', @response.body
|
2010-09-05 22:24:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_get_post_request_switch
|
|
|
|
post :raise_exception_on_get
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 'request method: POST', @response.body
|
2010-09-05 22:24:10 -04:00
|
|
|
get :raise_exception_on_post
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 'request method: GET', @response.body
|
2010-09-05 22:24:10 -04:00
|
|
|
post :raise_exception_on_get
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 'request method: POST', @response.body
|
2010-09-05 22:24:10 -04:00
|
|
|
get :raise_exception_on_post
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 'request method: GET', @response.body
|
2004-12-07 06:10:46 -05:00
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2010-11-15 16:37:05 -05:00
|
|
|
def test_string_constraint
|
|
|
|
with_routing do |set|
|
2010-11-17 16:36:51 -05:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get "photos", :to => 'action_pack_assertions#nothing', :constraints => {:subdomain => "admin"}
|
2010-11-15 16:37:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-04-22 11:08:25 -04:00
|
|
|
def test_assert_redirect_to_named_route_failure
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'route_one', :to => 'action_pack_assertions#nothing', :as => :route_one
|
|
|
|
get 'route_two', :to => 'action_pack_assertions#nothing', :id => 'two', :as => :route_two
|
|
|
|
get ':controller/:action'
|
2006-04-22 11:08:25 -04:00
|
|
|
end
|
|
|
|
process :redirect_to_named_route
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
2006-04-22 11:08:25 -04:00
|
|
|
assert_redirected_to 'http://test.host/route_two'
|
|
|
|
end
|
2012-05-03 09:16:38 -04:00
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_redirected_to %r(^http://test.host/route_two)
|
|
|
|
end
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
2006-04-22 11:08:25 -04:00
|
|
|
assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
|
|
|
|
end
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
2006-04-22 11:08:25 -04:00
|
|
|
assert_redirected_to route_two_url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-08-13 14:31:58 -04:00
|
|
|
def test_assert_redirect_to_nested_named_route
|
2010-02-24 19:01:03 -05:00
|
|
|
@controller = Admin::InnerModuleController.new
|
|
|
|
|
2006-08-13 14:31:58 -04:00
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get 'admin/inner_module', :to => 'admin/inner_module#index', :as => :admin_inner_module
|
|
|
|
get ':controller/:action'
|
2006-08-13 14:31:58 -04:00
|
|
|
end
|
|
|
|
process :redirect_to_index
|
|
|
|
# redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
|
|
|
|
assert_redirected_to admin_inner_module_path
|
|
|
|
end
|
|
|
|
end
|
2008-06-02 18:57:35 -04:00
|
|
|
|
2006-12-25 04:34:01 -05:00
|
|
|
def test_assert_redirected_to_top_level_named_route_from_nested_controller
|
2010-02-24 19:01:03 -05:00
|
|
|
@controller = Admin::InnerModuleController.new
|
|
|
|
|
2006-12-25 04:34:01 -05:00
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/action_pack_assertions/:id', :to => 'action_pack_assertions#index', :as => :top_level
|
|
|
|
get ':controller/:action'
|
2006-12-25 04:34:01 -05:00
|
|
|
end
|
|
|
|
process :redirect_to_top_level_named_route
|
2008-06-02 18:57:35 -04:00
|
|
|
# assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return
|
2006-12-25 04:34:01 -05:00
|
|
|
assert_redirected_to "/action_pack_assertions/foo"
|
2012-05-03 09:16:38 -04:00
|
|
|
assert_redirected_to %r(/action_pack_assertions/foo)
|
2006-12-25 04:34:01 -05:00
|
|
|
end
|
|
|
|
end
|
2006-08-13 14:31:58 -04:00
|
|
|
|
2008-06-02 18:57:35 -04:00
|
|
|
def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces
|
2010-02-24 19:01:03 -05:00
|
|
|
@controller = Admin::InnerModuleController.new
|
|
|
|
|
2008-06-02 18:57:35 -04:00
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw do
|
2008-06-02 18:57:35 -04:00
|
|
|
# this controller exists in the admin namespace as well which is the only difference from previous test
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/user/:id', :to => 'user#index', :as => :top_level
|
|
|
|
get ':controller/:action'
|
2008-06-02 18:57:35 -04:00
|
|
|
end
|
|
|
|
process :redirect_to_top_level_named_route
|
|
|
|
# assert_redirected_to top_level_url('foo') would pass because of exact match early return
|
|
|
|
assert_redirected_to top_level_path('foo')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-26 14:30:29 -04:00
|
|
|
def test_assert_redirect_failure_message_with_protocol_relative_url
|
|
|
|
begin
|
|
|
|
process :redirect_external_protocol_relative
|
|
|
|
assert_redirected_to "/foo"
|
|
|
|
rescue ActiveSupport::TestCase::Assertion => ex
|
|
|
|
assert_no_match(
|
|
|
|
/#{request.protocol}#{request.host}\/\/www.rubyonrails.org/,
|
|
|
|
ex.message,
|
|
|
|
'protocol relative url was incorrectly normalized'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def test_template_objects_exist
|
2004-11-23 20:04:44 -05:00
|
|
|
process :assign_this
|
2010-09-28 16:35:17 -04:00
|
|
|
assert !@controller.instance_variable_defined?(:"@hi")
|
2010-03-17 19:28:05 -04:00
|
|
|
assert @controller.instance_variable_get(:"@howdy")
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def test_template_objects_missing
|
2004-11-23 20:04:44 -05:00
|
|
|
process :nothing
|
2010-09-28 16:35:17 -04:00
|
|
|
assert !@controller.instance_variable_defined?(:@howdy)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def test_empty_flash
|
2004-11-23 20:04:44 -05:00
|
|
|
process :flash_me_naked
|
2010-09-05 21:59:41 -04:00
|
|
|
assert flash.empty?
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def test_flash_exist
|
2004-11-23 20:04:44 -05:00
|
|
|
process :flash_me
|
2010-09-05 21:59:41 -04:00
|
|
|
assert flash.any?
|
2013-01-05 11:59:36 -05:00
|
|
|
assert flash['hello'].present?
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def test_flash_does_not_exist
|
2004-11-23 20:04:44 -05:00
|
|
|
process :nothing
|
2010-09-05 21:59:41 -04:00
|
|
|
assert flash.empty?
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def test_session_exist
|
|
|
|
process :session_stuffing
|
2014-08-18 02:29:21 -04:00
|
|
|
assert_equal 'turkey', session['xmas']
|
2010-09-05 22:24:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def session_does_not_exist
|
|
|
|
process :nothing
|
|
|
|
assert session.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_template_action
|
2004-11-23 20:04:44 -05:00
|
|
|
process :nothing
|
2010-03-17 17:23:00 -04:00
|
|
|
assert_template nil
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2007-09-02 20:18:30 -04:00
|
|
|
process :hello_world
|
2010-03-17 17:23:00 -04:00
|
|
|
assert_template 'hello_world'
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_redirection_location
|
|
|
|
process :redirect_internal
|
2005-05-22 03:43:05 -04:00
|
|
|
assert_equal 'http://test.host/nothing', @response.redirect_url
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
process :redirect_external
|
|
|
|
assert_equal 'http://www.rubyonrails.org', @response.redirect_url
|
2013-04-26 14:30:29 -04:00
|
|
|
|
|
|
|
process :redirect_external_protocol_relative
|
|
|
|
assert_equal '//www.rubyonrails.org', @response.redirect_url
|
2006-11-28 18:41:04 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2006-11-28 18:41:04 -05:00
|
|
|
def test_no_redirect_url
|
2004-11-23 20:04:44 -05:00
|
|
|
process :nothing
|
|
|
|
assert_nil @response.redirect_url
|
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_server_error_response_code
|
|
|
|
process :response500
|
|
|
|
assert @response.server_error?
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
process :response599
|
|
|
|
assert @response.server_error?
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
process :response404
|
|
|
|
assert !@response.server_error?
|
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_missing_response_code
|
|
|
|
process :response404
|
|
|
|
assert @response.missing?
|
|
|
|
end
|
|
|
|
|
2008-11-22 14:10:12 -05:00
|
|
|
def test_client_error_response_code
|
|
|
|
process :response404
|
|
|
|
assert @response.client_error?
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_redirect_url_match
|
|
|
|
process :redirect_external
|
|
|
|
assert @response.redirect?
|
2010-09-22 15:03:39 -04:00
|
|
|
assert_match(/rubyonrails/, @response.redirect_url)
|
2010-09-05 21:59:41 -04:00
|
|
|
assert !/perloffrails/.match(@response.redirect_url)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_redirection
|
|
|
|
process :redirect_internal
|
|
|
|
assert @response.redirect?
|
|
|
|
|
|
|
|
process :redirect_external
|
|
|
|
assert @response.redirect?
|
|
|
|
|
|
|
|
process :nothing
|
|
|
|
assert !@response.redirect?
|
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_successful_response_code
|
|
|
|
process :nothing
|
|
|
|
assert @response.success?
|
2006-09-29 03:34:02 -04:00
|
|
|
end
|
|
|
|
|
2010-09-05 22:24:10 -04:00
|
|
|
def test_response_object
|
2004-11-23 20:04:44 -05:00
|
|
|
process :nothing
|
|
|
|
assert_kind_of ActionController::TestResponse, @response
|
|
|
|
end
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_render_based_on_parameters
|
2012-01-06 06:25:40 -05:00
|
|
|
process :render_based_on_parameters, "GET", "name" => "David"
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal "Mr. David", @response.body
|
|
|
|
end
|
|
|
|
|
2006-04-24 23:54:46 -04:00
|
|
|
def test_assert_redirection_fails_with_incorrect_controller
|
|
|
|
process :redirect_to_controller
|
2008-11-07 15:42:34 -05:00
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
2006-04-24 23:54:46 -04:00
|
|
|
assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_redirection_with_extra_controller_option
|
|
|
|
get :redirect_to_action
|
|
|
|
assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
|
|
|
|
end
|
|
|
|
|
2008-06-23 12:46:15 -04:00
|
|
|
def test_redirected_to_url_leading_slash
|
2005-05-19 15:19:58 -04:00
|
|
|
process :redirect_to_path
|
|
|
|
assert_redirected_to '/some/path'
|
|
|
|
end
|
2008-06-23 12:46:15 -04:00
|
|
|
|
2010-03-02 17:07:42 -05:00
|
|
|
def test_redirected_to_url_no_leading_slash_fails
|
2005-05-19 15:19:58 -04:00
|
|
|
process :redirect_to_path
|
2010-03-02 17:07:42 -05:00
|
|
|
assert_raise ActiveSupport::TestCase::Assertion do
|
|
|
|
assert_redirected_to 'some/path'
|
|
|
|
end
|
2005-05-19 15:19:58 -04:00
|
|
|
end
|
2008-06-23 12:46:15 -04:00
|
|
|
|
2010-09-29 11:55:43 -04:00
|
|
|
def test_redirect_invalid_external_route
|
|
|
|
process :redirect_invalid_external_route
|
|
|
|
assert_redirected_to "http://test.hostht_tp://www.rubyonrails.org"
|
|
|
|
end
|
|
|
|
|
2005-05-19 15:19:58 -04:00
|
|
|
def test_redirected_to_url_full_url
|
|
|
|
process :redirect_to_path
|
|
|
|
assert_redirected_to 'http://test.host/some/path'
|
|
|
|
end
|
2005-06-27 01:36:03 -04:00
|
|
|
|
2007-10-07 15:12:02 -04:00
|
|
|
def test_assert_redirection_with_symbol
|
|
|
|
process :redirect_to_controller_with_symbol
|
|
|
|
assert_nothing_raised {
|
|
|
|
assert_redirected_to :controller => "elsewhere", :action => "flash_me"
|
|
|
|
}
|
|
|
|
process :redirect_to_controller_with_symbol
|
|
|
|
assert_nothing_raised {
|
|
|
|
assert_redirected_to :controller => :elsewhere, :action => :flash_me
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-06-27 01:36:03 -04:00
|
|
|
def test_redirected_to_with_nested_controller
|
|
|
|
@controller = Admin::InnerModuleController.new
|
|
|
|
get :redirect_to_absolute_controller
|
2008-06-23 12:46:15 -04:00
|
|
|
assert_redirected_to :controller => '/content'
|
2006-09-29 03:34:02 -04:00
|
|
|
|
2005-06-27 01:36:03 -04:00
|
|
|
get :redirect_to_fellow_controller
|
|
|
|
assert_redirected_to :controller => 'admin/user'
|
2006-09-29 03:34:02 -04:00
|
|
|
end
|
|
|
|
|
2008-01-04 21:19:48 -05:00
|
|
|
def test_assert_response_uses_exception_message
|
|
|
|
@controller = AssertResponseWithUnexpectedErrorController.new
|
2013-12-19 06:03:39 -05:00
|
|
|
e = assert_raise RuntimeError, 'Expected non-success response' do
|
|
|
|
get :index
|
|
|
|
end
|
2008-01-04 21:19:48 -05:00
|
|
|
assert_response :success
|
2013-12-19 06:03:39 -05:00
|
|
|
assert_includes 'FAIL', e.message
|
2008-01-04 21:19:48 -05:00
|
|
|
end
|
2008-05-16 11:27:09 -04:00
|
|
|
|
|
|
|
def test_assert_response_failure_response_with_no_exception
|
|
|
|
@controller = AssertResponseWithUnexpectedErrorController.new
|
|
|
|
get :show
|
2013-12-19 06:03:39 -05:00
|
|
|
assert_response 500
|
|
|
|
assert_equal 'Boom', response.body
|
2008-05-16 11:27:09 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2010-04-29 10:29:35 -04:00
|
|
|
class AssertTemplateTest < ActionController::TestCase
|
|
|
|
tests ActionPackAssertionsController
|
|
|
|
|
2012-11-25 21:39:08 -05:00
|
|
|
def test_with_invalid_hash_keys_raises_argument_error
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
assert_template foo: "bar"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-29 10:29:35 -04:00
|
|
|
def test_with_partial
|
|
|
|
get :partial
|
|
|
|
assert_template :partial => '_partial'
|
|
|
|
end
|
|
|
|
|
2013-02-27 10:44:25 -05:00
|
|
|
def test_file_with_absolute_path_success
|
|
|
|
get :render_file_absolute_path
|
|
|
|
assert_template :file => File.expand_path('../../../README.rdoc', __FILE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_with_relative_path_success
|
|
|
|
get :render_file_relative_path
|
|
|
|
assert_template :file => 'README.rdoc'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_file_failure
|
|
|
|
get :render_file_absolute_path
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template :file => 'test/hello_world'
|
|
|
|
end
|
2014-07-02 18:36:23 -04:00
|
|
|
|
|
|
|
get :render_file_absolute_path
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template file: nil
|
|
|
|
end
|
2013-02-27 10:44:25 -05:00
|
|
|
end
|
|
|
|
|
2010-04-29 10:29:35 -04:00
|
|
|
def test_with_nil_passes_when_no_template_rendered
|
|
|
|
get :nothing
|
|
|
|
assert_template nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_with_nil_fails_when_template_rendered
|
|
|
|
get :hello_world
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-23 14:13:08 -05:00
|
|
|
def test_with_empty_string_fails_when_template_rendered
|
|
|
|
get :hello_world
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-23 16:10:18 -05:00
|
|
|
def test_with_empty_string_fails_when_no_template_rendered
|
|
|
|
get :nothing
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-29 10:29:35 -04:00
|
|
|
def test_passes_with_correct_string
|
|
|
|
get :hello_world
|
|
|
|
assert_template 'hello_world'
|
|
|
|
assert_template 'test/hello_world'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_passes_with_correct_symbol
|
|
|
|
get :hello_world
|
|
|
|
assert_template :hello_world
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fails_with_incorrect_string
|
|
|
|
get :hello_world
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template 'hello_planet'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-06 23:27:47 -04:00
|
|
|
def test_fails_with_incorrect_string_that_matches
|
|
|
|
get :hello_world
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template 'est/he'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fails_with_repeated_name_in_path
|
|
|
|
get :hello_repeating_in_path
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template 'test/hello'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-29 10:29:35 -04:00
|
|
|
def test_fails_with_incorrect_symbol
|
|
|
|
get :hello_world
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template :hello_planet
|
|
|
|
end
|
|
|
|
end
|
2010-10-18 04:57:51 -04:00
|
|
|
|
2012-04-06 23:27:47 -04:00
|
|
|
def test_fails_with_incorrect_symbol_that_matches
|
|
|
|
get :hello_world
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template :"est/he"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-02 13:13:36 -05:00
|
|
|
def test_fails_with_wrong_layout
|
|
|
|
get :render_with_layout
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template :layout => "application"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-11 01:55:51 -04:00
|
|
|
def test_fails_expecting_no_layout
|
|
|
|
get :render_with_layout
|
|
|
|
assert_raise(ActiveSupport::TestCase::Assertion) do
|
|
|
|
assert_template :layout => nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-02 13:13:36 -05:00
|
|
|
def test_passes_with_correct_layout
|
|
|
|
get :render_with_layout
|
|
|
|
assert_template :layout => "layouts/standard"
|
2012-04-11 01:28:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_passes_with_layout_and_partial
|
|
|
|
get :render_with_layout_and_partial
|
|
|
|
assert_template :layout => "layouts/standard"
|
2011-12-02 13:13:36 -05:00
|
|
|
end
|
|
|
|
|
2012-04-11 01:55:51 -04:00
|
|
|
def test_passed_with_no_layout
|
|
|
|
get :hello_world
|
|
|
|
assert_template :layout => nil
|
|
|
|
end
|
|
|
|
|
2012-04-11 02:45:28 -04:00
|
|
|
def test_passed_with_no_layout_false
|
|
|
|
get :hello_world
|
|
|
|
assert_template :layout => false
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_passes_with_correct_layout_without_layouts_prefix
|
|
|
|
get :render_with_layout
|
|
|
|
assert_template :layout => "standard"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_passes_with_correct_layout_symbol
|
|
|
|
get :render_with_layout
|
|
|
|
assert_template :layout => :standard
|
|
|
|
end
|
|
|
|
|
2010-10-18 04:57:51 -04:00
|
|
|
def test_assert_template_reset_between_requests
|
|
|
|
get :hello_world
|
|
|
|
assert_template 'test/hello_world'
|
|
|
|
|
|
|
|
get :nothing
|
|
|
|
assert_template nil
|
2014-07-02 18:36:23 -04:00
|
|
|
|
|
|
|
get :partial
|
|
|
|
assert_template partial: 'test/_partial'
|
|
|
|
|
|
|
|
get :nothing
|
|
|
|
assert_template partial: nil
|
|
|
|
|
|
|
|
get :render_with_layout
|
|
|
|
assert_template layout: 'layouts/standard'
|
|
|
|
|
|
|
|
get :nothing
|
|
|
|
assert_template layout: nil
|
|
|
|
|
|
|
|
get :render_file_relative_path
|
|
|
|
assert_template file: 'README.rdoc'
|
|
|
|
|
|
|
|
get :nothing
|
|
|
|
assert_template file: nil
|
2010-10-18 04:57:51 -04:00
|
|
|
end
|
2010-04-29 10:29:35 -04:00
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class ActionPackHeaderTest < ActionController::TestCase
|
|
|
|
tests ActionPackAssertionsController
|
2006-03-12 20:33:57 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_rendering_xml_sets_content_type
|
2007-09-02 20:18:30 -04:00
|
|
|
process :hello_xml_world
|
2008-12-19 18:15:22 -05:00
|
|
|
assert_equal('application/xml; charset=utf-8', @response.headers['Content-Type'])
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-03-12 20:33:57 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_rendering_xml_respects_content_type
|
2009-06-16 19:46:56 -04:00
|
|
|
process :hello_xml_world_pdf
|
|
|
|
assert_equal('application/pdf; charset=utf-8', @response.headers['Content-Type'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rendering_xml_respects_content_type_when_set_in_the_header
|
|
|
|
process :hello_xml_world_pdf_header
|
|
|
|
assert_equal('application/pdf; charset=utf-8', @response.headers['Content-Type'])
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2006-06-01 23:39:56 -04:00
|
|
|
|
|
|
|
def test_render_text_with_custom_content_type
|
|
|
|
get :render_text_with_custom_content_type
|
2008-12-19 18:15:22 -05:00
|
|
|
assert_equal 'application/rss+xml; charset=utf-8', @response.headers['Content-Type']
|
2006-06-01 23:39:56 -04:00
|
|
|
end
|
2006-02-09 15:05:11 -05:00
|
|
|
end
|