class ActionPackAssertionsControllerTest

Public Instance Methods

session_does_not_exist() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 323
def session_does_not_exist
  process :nothing
  assert session.empty?
end
test_assert_redirect_failure_message_with_protocol_relative_url() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 278
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.rubyonquails.org/,
      ex.message,
      "protocol relative url was incorrectly normalized"
    )
  end
end
test_assert_redirect_to_named_route_failure() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 199
def test_assert_redirect_to_named_route_failure
  with_routing do |set|
    set.draw do
      get "route_one", to: "action_pack_assertions#nothing", as: :route_one
      get "route_two", to: "action_pack_assertions#nothing", id: "two", as: :route_two

      ActiveSupport::Deprecation.silence do
        get ":controller/:action"
      end
    end
    process :redirect_to_named_route
    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_redirected_to "http://test.host/route_two"
    end
    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_redirected_to %r(^http://test.host/route_two)
    end
    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_redirected_to controller: "action_pack_assertions", action: "nothing", id: "two"
    end
    assert_raise(ActiveSupport::TestCase::Assertion) do
      assert_redirected_to route_two_url
    end
  end
end
test_assert_redirect_to_nested_named_route() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 225
def test_assert_redirect_to_nested_named_route
  @controller = Admin::InnerModuleController.new

  with_routing do |set|
    set.draw do
      get "admin/inner_module", to: "admin/inner_module#index", as: :admin_inner_module

      ActiveSupport::Deprecation.silence do
        get ":controller/:action"
      end
    end
    process :redirect_to_index
    # redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
    assert_redirected_to admin_inner_module_path
  end
end
test_assert_redirected_to_top_level_named_route_from_nested_controller() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 242
def test_assert_redirected_to_top_level_named_route_from_nested_controller
  @controller = Admin::InnerModuleController.new

  with_routing do |set|
    set.draw do
      get "/action_pack_assertions/:id", to: "action_pack_assertions#index", as: :top_level

      ActiveSupport::Deprecation.silence do
        get ":controller/:action"
      end
    end
    process :redirect_to_top_level_named_route
    # assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return
    assert_redirected_to "/action_pack_assertions/foo"
    assert_redirected_to %r(/action_pack_assertions/foo)
  end
end
test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 260
def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces
  @controller = Admin::InnerModuleController.new

  with_routing do |set|
    set.draw do
      # this controller exists in the admin namespace as well which is the only difference from previous test
      get "/user/:id", to: "user#index", as: :top_level

      ActiveSupport::Deprecation.silence do
        get ":controller/:action"
      end
    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
test_assert_redirection_fails_with_incorrect_controller() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 400
def test_assert_redirection_fails_with_incorrect_controller
  process :redirect_to_controller
  assert_raise(ActiveSupport::TestCase::Assertion) do
    assert_redirected_to controller: "action_pack_assertions", action: "flash_me"
  end
end
test_assert_redirection_with_extra_controller_option() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 407
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
test_assert_redirection_with_symbol() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 434
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
test_assert_response_failure_response_with_no_exception() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 463
def test_assert_response_failure_response_with_no_exception
  @controller = AssertResponseWithUnexpectedErrorController.new
  get :show
  assert_response 500
  assert_equal "Boom", response.body
end
test_assert_response_uses_exception_message() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 454
def test_assert_response_uses_exception_message
  @controller = AssertResponseWithUnexpectedErrorController.new
  e = assert_raise RuntimeError, "Expected non-success response" do
    get :index
  end
  assert_response :success
  assert_includes "FAIL", e.message
end
test_client_error_response_code() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 360
def test_client_error_response_code
  process :response404
  assert @response.client_error?
end
test_empty_flash() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 302
def test_empty_flash
  process :flash_me_naked
  assert flash.empty?
end
test_flash_does_not_exist() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 313
def test_flash_does_not_exist
  process :nothing
  assert flash.empty?
end
test_flash_exist() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 307
def test_flash_exist
  process :flash_me
  assert flash.any?
  assert flash["hello"].present?
end
test_get_post_request_switch() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 166
def test_get_post_request_switch
  post :raise_exception_on_get
  assert_equal "request method: POST", @response.body
  get :raise_exception_on_post
  assert_equal "request method: GET", @response.body
  post :raise_exception_on_get
  assert_equal "request method: POST", @response.body
  get :raise_exception_on_post
  assert_equal "request method: GET", @response.body
end
test_get_request() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 154
def test_get_request
  assert_raise(RuntimeError) { get :raise_exception_on_get }
  get :raise_exception_on_post
  assert_equal "request method: GET", @response.body
end
test_missing_response_code() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 355
def test_missing_response_code
  process :response404
  assert @response.not_found?
end
test_no_redirect_url() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 339
def test_no_redirect_url
  process :nothing
  assert_nil @response.redirect_url
end
test_post_request() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 160
def test_post_request
  assert_raise(RuntimeError) { post :raise_exception_on_post }
  post :raise_exception_on_get
  assert_equal "request method: POST", @response.body
end
test_redirect_invalid_external_route() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 424
def test_redirect_invalid_external_route
  process :redirect_invalid_external_route
  assert_redirected_to "http://test.hostht_tp://www.rubyonquails.org"
end
test_redirect_url_match() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 365
def test_redirect_url_match
  process :redirect_external
  assert @response.redirect?
  assert_match(/rubyonquails/, @response.redirect_url)
  assert !/perloffquails/.match(@response.redirect_url)
end
test_redirected_to_url_full_url() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 429
def test_redirected_to_url_full_url
  process :redirect_to_path
  assert_redirected_to "http://test.host/some/path"
end
test_redirected_to_url_leading_slash() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 412
def test_redirected_to_url_leading_slash
  process :redirect_to_path
  assert_redirected_to "/some/path"
end
test_redirected_to_url_no_leading_slash_fails() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 417
def test_redirected_to_url_no_leading_slash_fails
  process :redirect_to_path
  assert_raise ActiveSupport::TestCase::Assertion do
    assert_redirected_to "some/path"
  end
end
test_redirected_to_with_nested_controller() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 445
def test_redirected_to_with_nested_controller
  @controller = Admin::InnerModuleController.new
  get :redirect_to_absolute_controller
  assert_redirected_to controller: "/content"

  get :redirect_to_fellow_controller
  assert_redirected_to controller: "admin/user"
end
test_redirection() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 372
def test_redirection
  process :redirect_internal
  assert @response.redirect?

  process :redirect_external
  assert @response.redirect?

  process :nothing
  assert !@response.redirect?
end
test_redirection_location() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 328
def test_redirection_location
  process :redirect_internal
  assert_equal "http://test.host/nothing", @response.redirect_url

  process :redirect_external
  assert_equal "http://www.rubyonquails.org", @response.redirect_url

  process :redirect_external_protocol_relative
  assert_equal "//www.rubyonquails.org", @response.redirect_url
end
test_render_based_on_parameters() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 393
def test_render_based_on_parameters
  process :render_based_on_parameters,
    method: "GET",
    params: { name: "David" }
  assert_equal "Mr. David", @response.body
end
test_render_file_absolute_path() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 144
def test_render_file_absolute_path
  get :render_file_absolute_path
  assert_match(/\A= Action Pack/, @response.body)
end
test_render_file_relative_path() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 149
def test_render_file_relative_path
  get :render_file_relative_path
  assert_match(/\A= Action Pack/, @response.body)
end
test_response_object() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 388
def test_response_object
  process :nothing
  assert_kind_of ActionDispatch::TestResponse, @response
end
test_server_error_response_code() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 344
def test_server_error_response_code
  process :response500
  assert @response.server_error?

  process :response599
  assert @response.server_error?

  process :response404
  assert !@response.server_error?
end
test_session_exist() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 318
def test_session_exist
  process :session_stuffing
  assert_equal "turkey", session["xmas"]
end
test_string_constraint() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 177
def test_string_constraint
  with_routing do |set|
    set.draw do
      get "photos", to: "action_pack_assertions#nothing", constraints: { subdomain: "admin" }
    end
  end
end
test_successful_response_code() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 383
def test_successful_response_code
  process :nothing
  assert @response.successful?
end
test_template_objects_exist() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 291
def test_template_objects_exist
  process :assign_this
  assert !@controller.instance_variable_defined?(:"@hi")
  assert @controller.instance_variable_get(:"@howdy")
end
test_template_objects_missing() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 297
def test_template_objects_missing
  process :nothing
  assert !@controller.instance_variable_defined?(:@howdy)
end
test_with_routing_works_with_api_only_controllers() click to toggle source
# File actionpack/test/controller/action_pack_assertions_test.rb, line 185
def test_with_routing_works_with_api_only_controllers
  @controller = ApiOnlyController.new

  with_routing do |set|
    set.draw do
      get "new_route", to: "api_only#nothing"
      get "redirect_to_new_route", to: "api_only#redirect_to_new_route"
    end

    process :redirect_to_new_route
    assert_redirected_to "http://test.host/new_route"
  end
end