Repository: rails/rails
File: actionpack/test/controller/redirect_test.rb (Ruby)

Code before:
  end

  def test_redirect_to_absolute_url_does_not_notify
    old_config = ActionController::Base.relative_redirects_without_leading_slash
    ActionController::Base.relative_redirects_without_leading_slash = :notify

    events = []
    ActiveSupport::Notifications.subscribe("unsafe_redirect.action_controller") do |*args|
      events << ActiveSupport::Notifications::Event.new(*args)
    end

    get :redirect_to_url
    assert_response :redirect
    assert_equal "http://www.rubyonrails.org/", redirect_to_url
    assert_empty events

    get :relative_url_redirect_with_status
    assert_response :redirect
    assert_equal "http://test.host/things/stuff", redirect_to_url
    assert_empty events
  ensure
    ActiveSupport::Notifications.unsubscribe("unsafe_redirect.action_controller")
    ActionController::Base.relative_redirects_without_leading_slash = old_config
  end

  def test_redirect_to_absolute_url_does_not_raise
    old_config = ActionController::Base.relative_redirects_without_leading_slash
    ActionController::Base.relative_redirects_without_leading_slash = :raise

    get :redirect_to_url
    assert_response :redirect
    assert_equal "http://www.rubyonrails.org/", redirect_to_url

    get :relative_url_redirect_with_status
    assert_response :redirect
    assert_equal "http://test.host/things/stuff", redirect_to_url

    get :redirect_to_url_with_network_path_reference
    assert_response :redirect
    assert_equal "//www.rubyonrails.org/", redirect_to_url
  ensure
    ActionController::Base.relative_redirects_without_leading_slash = old_config
  end

  private
    def with_raise_on_open_redirects
      old_raise_on_open_redirects = ActionController::Base.raise_on_open_redirects
      ActionController::Base.raise_on_open_redirects = true
      yield
    ensure
      ActionController::Base.raise_on_open_redirects = old_raise_on_open_redirects

Review comment (suggestion):
I am not sure we want the counter tests for log AND raise but I added them for now to make sure/prove that it does not break flows we expect to work. Happy to remove/update them though

Code after:
  end

  def test_redirect_to_absolute_url_does_not_notify
    old_config = ActionController::Base.action_on_path_relative_redirect
    ActionController::Base.action_on_path_relative_redirect = :notify

    events = []
    ActiveSupport::Notifications.subscribe("unsafe_redirect.action_controller") do |*args|
      events << ActiveSupport::Notifications::Event.new(*args)
    end

    get :redirect_to_url
    assert_response :redirect
    assert_equal "http://www.rubyonrails.org/", redirect_to_url
    assert_empty events

    get :relative_url_redirect_with_status
    assert_response :redirect
    assert_equal "http://test.host/things/stuff", redirect_to_url
    assert_empty events
  ensure
    ActiveSupport::Notifications.unsubscribe("unsafe_redirect.action_controller")
    ActionController::Base.action_on_path_relative_redirect = old_config
  end

  def test_redirect_to_absolute_url_does_not_raise
    old_config = ActionController::Base.action_on_path_relative_redirect
    ActionController::Base.action_on_path_relative_redirect = :raise

    get :redirect_to_url
    assert_response :redirect
    assert_equal "http://www.rubyonrails.org/", redirect_to_url

    get :relative_url_redirect_with_status
    assert_response :redirect
    assert_equal "http://test.host/things/stuff", redirect_to_url

    get :redirect_to_url_with_network_path_reference
    assert_response :redirect
    assert_equal "//www.rubyonrails.org/", redirect_to_url
  ensure
    ActionController::Base.action_on_path_relative_redirect = old_config
  end

  private
    def with_raise_on_open_redirects
      old_raise_on_open_redirects = ActionController::Base.raise_on_open_redirects
      ActionController::Base.raise_on_open_redirects = true
      yield
    ensure
      ActionController::Base.raise_on_open_redirects = old_raise_on_open_redirects