Repository: rails/rails
File: actionpack/lib/action_dispatch/testing/assertions/response.rb (Ruby)

Code before:
      #
      #     # Asserts that the redirection matches the regular expression
      #     assert_redirected_to %r(\Ahttp://example.org)
      #
      #     # Asserts that the redirection has the HTTP status code 301 (Moved
      #     # Permanently).
      #     assert_redirected_to "/some/path", status: :moved_permanently
      def assert_redirected_to(url_options = {}, options = {}, message = nil)
        options, message = {}, options unless options.is_a?(Hash)

        status = options[:status] || :redirect
        assert_response(status, message)
        return true if url_options === @response.location

        redirect_is       = normalize_argument_to_redirection(@response.location)
        redirect_expected = normalize_argument_to_redirection(url_options)

        message ||= "Expected response to be a redirect to <#{redirect_expected}> but was a redirect to <#{redirect_is}>"
        assert_operator redirect_expected, :===, redirect_is, message
      end

      # Asserts that the given +text+ is present somewhere in the response body.
      #
      #     assert_in_body fixture(:name).description
      def assert_in_body(text)
        assert_match(/#{text}/, @response.body)
      end

      # Asserts that the given +text+ is not present anywhere in the response body.
      #
      #     assert_not_in_body fixture(:name).description
      def assert_not_in_body(text)
        assert_no_match(/#{text}/, @response.body)
      end

      private
        # Proxy to to_param if the object will respond to it.
        def parameterize(value)
          value.respond_to?(:to_param) ? value.to_param : value
        end

        def normalize_argument_to_redirection(fragment)
          if Regexp === fragment
            fragment
          else
            handle = @controller || ActionController::Redirecting
            handle._compute_redirect_to_location(@request, fragment)
          end
        end

        def generate_response_message(expected, actual = @response.response_code)

Review comment (security):
Maybe we can escape the Regexp? I can see people using something like `assert_in_body("Name (optional)")` which wouldn't match because of the unescaped parenthesis.

Code after:
      #
      #     # Asserts that the redirection matches the regular expression
      #     assert_redirected_to %r(\Ahttp://example.org)
      #
      #     # Asserts that the redirection has the HTTP status code 301 (Moved
      #     # Permanently).
      #     assert_redirected_to "/some/path", status: :moved_permanently
      def assert_redirected_to(url_options = {}, options = {}, message = nil)
        options, message = {}, options unless options.is_a?(Hash)

        status = options[:status] || :redirect
        assert_response(status, message)
        return true if url_options === @response.location

        redirect_is       = normalize_argument_to_redirection(@response.location)
        redirect_expected = normalize_argument_to_redirection(url_options)

        message ||= "Expected response to be a redirect to <#{redirect_expected}> but was a redirect to <#{redirect_is}>"
        assert_operator redirect_expected, :===, redirect_is, message
      end

      # Asserts that the given +text+ is present somewhere in the response body.
      #
      #     assert_in_body fixture(:name).description
      def assert_in_body(text)
        assert_match(/#{Regexp.escape(text)}/, @response.body)
      end

      # Asserts that the given +text+ is not present anywhere in the response body.
      #
      #     assert_not_in_body fixture(:name).description
      def assert_not_in_body(text)
        assert_no_match(/#{Regexp.escape(text)}/, @response.body)
      end

      private
        # Proxy to to_param if the object will respond to it.
        def parameterize(value)
          value.respond_to?(:to_param) ? value.to_param : value
        end

        def normalize_argument_to_redirection(fragment)
          if Regexp === fragment
            fragment
          else
            handle = @controller || ActionController::Redirecting
            handle._compute_redirect_to_location(@request, fragment)
          end
        end

        def generate_response_message(expected, actual = @response.response_code)