diff options
Diffstat (limited to 'actionpack/lib/action_dispatch/testing/assertions/response.rb')
| -rw-r--r-- | actionpack/lib/action_dispatch/testing/assertions/response.rb | 51 | 
1 files changed, 32 insertions, 19 deletions
diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb index b6e21b0d28..cd55b7d975 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/response.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb @@ -1,4 +1,3 @@ -  module ActionDispatch    module Assertions      # A small suite of assertions that test responses from \Rails applications. @@ -21,41 +20,35 @@ module ActionDispatch        # or its symbolic equivalent <tt>assert_response(:not_implemented)</tt>.        # See Rack::Utils::SYMBOL_TO_STATUS_CODE for a full list.        # -      #   # assert that the response was a redirection +      #   # Asserts that the response was a redirection        #   assert_response :redirect        # -      #   # assert that the response code was status code 401 (unauthorized) +      #   # Asserts that the response code was status code 401 (unauthorized)        #   assert_response 401        def assert_response(type, message = nil) -        if Symbol === type -          if [:success, :missing, :redirect, :error].include?(type) -            assert_predicate @response, RESPONSE_PREDICATES[type], message -          else -            code = Rack::Utils::SYMBOL_TO_STATUS_CODE[type] -            if code.nil? -              raise ArgumentError, "Invalid response type :#{type}" -            end -            assert_equal code, @response.response_code, message -          end +        message ||= generate_response_message(type) + +        if RESPONSE_PREDICATES.keys.include?(type) +          assert @response.send(RESPONSE_PREDICATES[type]), message          else -          assert_equal type, @response.response_code, message +          assert_equal AssertionResponse.new(type).code, @response.response_code, message          end        end -      # Assert that the redirection options passed in match those of the redirect called in the latest action. +      # Asserts that the redirection options passed in match those of the redirect called in the latest action.        # This match can be partial, such that <tt>assert_redirected_to(controller: "weblog")</tt> will also        # match the redirection of <tt>redirect_to(controller: "weblog", action: "show")</tt> and so on.        # -      #   # assert that the redirection was to the "index" action on the WeblogController +      #   # Asserts that the redirection was to the "index" action on the WeblogController        #   assert_redirected_to controller: "weblog", action: "index"        # -      #   # assert that the redirection was to the named route login_url +      #   # Asserts that the redirection was to the named route login_url        #   assert_redirected_to login_url        # -      #   # assert that the redirection was to the url for @customer +      #   # Asserts that the redirection was to the url for @customer        #   assert_redirected_to @customer        # -      #   # asserts that the redirection matches the regular expression +      #   # Asserts that the redirection matches the regular expression        #   assert_redirected_to %r(\Ahttp://example.org)        def assert_redirected_to(options = {}, message=nil)          assert_response(:redirect, message) @@ -82,6 +75,26 @@ module ActionDispatch              handle._compute_redirect_to_location(@request, fragment)            end          end + +        def generate_response_message(expected, actual = @response.response_code) +          "Expected response to be a <#{code_with_name(expected)}>,"\ +          " but was a <#{code_with_name(actual)}>" +          .concat location_if_redirected +        end + +        def location_if_redirected +          return '' unless @response.redirection? && @response.location.present? +          location = normalize_argument_to_redirection(@response.location) +          " redirect to <#{location}>" +        end + +        def code_with_name(code_or_name) +          if RESPONSE_PREDICATES.values.include?("#{code_or_name}?".to_sym) +            code_or_name = RESPONSE_PREDICATES.invert["#{code_or_name}?".to_sym] +          end + +          AssertionResponse.new(code_or_name).code_and_name +        end      end    end  end  | 
