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

Code before:
    end

    def test_fragment_cache
      original_enable_fragment_cache_logging = ActionController::Base.enable_fragment_cache_logging
      ActionController::Base.enable_fragment_cache_logging = true
      cache_path = Dir.mktmpdir(%w[tmp cache])
      @controller.cache_store = :file_store, cache_path

      assert_event_reported("action_controller.fragment_cache", payload: {
        method: "read_fragment",
        key: "views/foo"
      }) do
        assert_event_reported("action_controller.fragment_cache", payload: {
          method: "write_fragment",
          key: "views/foo"
        }) do
          get :with_fragment_cache
        end
      end
    ensure
      ActionController::Base.enable_fragment_cache_logging = original_enable_fragment_cache_logging
      FileUtils.rm_rf(cache_path)
    end

    def test_open_redirect
      with_action_on_open_redirect(:notify) do
        assert_event_reported("action_controller.open_redirect_detected", payload: {
          location: "http://test.hostexample.com",
          request_method: "GET",
          request_path: "/action_controller/structured_event_subscriber_test/another/structured_event_subscribers/open_redirector"
        }) do
          get :open_redirector
        end
      end
    end

    private
      def with_action_on_open_redirect(action)
        old_action = ActionController::Base.action_on_open_redirect
        ActionController::Base.action_on_open_redirect = action
        yield
      ensure
        ActionController::Base.action_on_open_redirect = old_action
      end
  end
end


Review comment (suggestion):
```suggestion
      ActionController::Base.with(action_on_open_redirect: :notify) do
```

I'd require the `with` extension instead of creating a method

Code after:
        get :raise_error
      end
    end

    def test_fragment_cache
      original_enable_fragment_cache_logging = ActionController::Base.enable_fragment_cache_logging
      ActionController::Base.enable_fragment_cache_logging = true
      cache_path = Dir.mktmpdir(%w[tmp cache])
      @controller.cache_store = :file_store, cache_path

      assert_event_reported("action_controller.fragment_cache", payload: {
        method: "read_fragment",
        key: "views/foo"
      }) do
        assert_event_reported("action_controller.fragment_cache", payload: {
          method: "write_fragment",
          key: "views/foo"
        }) do
          get :with_fragment_cache
        end
      end
    ensure
      ActionController::Base.enable_fragment_cache_logging = original_enable_fragment_cache_logging
      FileUtils.rm_rf(cache_path)
    end

    def test_open_redirect
      ActionController::Base.with(action_on_open_redirect: :notify) do
        event = assert_event_reported("action_controller.open_redirect", payload: {
          location: "http://test.hostexample.com",
          request_method: "GET",
          request_path: "/action_controller/structured_event_subscriber_test/another/structured_event_subscribers/open_redirector"
        }) do
          get :open_redirector
        end

        assert(event[:payload][:stacktrace].find { |line| line.include?("structured_event_subscriber_test.rb:51") })
      end
    end
  end
end