aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/testing/assertions/response.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_dispatch/testing/assertions/response.rb')
-rw-r--r--actionpack/lib/action_dispatch/testing/assertions/response.rb34
1 files changed, 13 insertions, 21 deletions
diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb
index 7381617dd7..40d38c59d6 100644
--- a/actionpack/lib/action_dispatch/testing/assertions/response.rb
+++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb
@@ -4,11 +4,9 @@ module ActionDispatch
module Assertions
# A small suite of assertions that test responses from \Rails applications.
module ResponseAssertions
- extend ActiveSupport::Concern
-
# Asserts that the response is one of the following types:
#
- # * <tt>:success</tt> - Status code was 200
+ # * <tt>:success</tt> - Status code was in the 200-299 range
# * <tt>:redirect</tt> - Status code was in the 300-399 range
# * <tt>:missing</tt> - Status code was 404
# * <tt>:error</tt> - Status code was in the 500-599 range
@@ -26,16 +24,17 @@ module ActionDispatch
# assert_response 401
#
def assert_response(type, message = nil)
- validate_request!
+ message ||= "Expected response to be a <#{type}>, but was <#{@response.response_code}>"
- if type.in?([:success, :missing, :redirect, :error]) && @response.send("#{type}?")
- assert_block("") { true } # to count the assertion
- elsif type.is_a?(Fixnum) && @response.response_code == type
- assert_block("") { true } # to count the assertion
- elsif type.is_a?(Symbol) && @response.response_code == Rack::Utils::SYMBOL_TO_STATUS_CODE[type]
- assert_block("") { true } # to count the assertion
+ if Symbol === type
+ if [:success, :missing, :redirect, :error].include?(type)
+ assert @response.send("#{type}?"), message
+ else
+ code = Rack::Utils::SYMBOL_TO_STATUS_CODE[type]
+ assert_equal @response.response_code, code, message
+ end
else
- flunk(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code))
+ assert_equal type, @response.response_code, message
end
end
@@ -61,9 +60,8 @@ module ActionDispatch
redirect_is = normalize_argument_to_redirection(@response.location)
redirect_expected = normalize_argument_to_redirection(options)
- if redirect_is != redirect_expected
- flunk "Expected response to be a redirect to <#{redirect_expected}> but was a redirect to <#{redirect_is}>"
- end
+ message ||= "Expected response to be a redirect to <#{redirect_expected}> but was a redirect to <#{redirect_is}>"
+ assert_equal redirect_expected, redirect_is, message
end
private
@@ -83,13 +81,7 @@ module ActionDispatch
refer
else
@controller.url_for(fragment)
- end.gsub(/[\r\n]/, '')
- end
-
- def validate_request!
- unless @request.is_a?(ActionDispatch::Request)
- raise ArgumentError, "@request must be an ActionDispatch::Request"
- end
+ end.delete("\0\r\n")
end
end
end