Skip to content
Browse files

Merge [5466] from trunk.

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-pre-release@5467 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent ba086a4 commit dfa070aab9d4426876f52e546bb921054b5a87c0 @jeremy jeremy committed Nov 7, 2006
View
5 actionpack/CHANGELOG
@@ -1,5 +1,10 @@
*SVN*
+* assert_response supports symbolic status codes. #6569 [Kevin Clark]
+ assert_response :ok
+ assert_response :not_found
+ assert_response :forbidden
+
* Cache parsed query parameters. #6559 [Stefan Kaes]
* Deprecate JavaScriptHelper#update_element_function, which is superseeded by RJS [Thomas Fuchs]
View
26 actionpack/lib/action_controller/assertions/response_assertions.rb
@@ -5,27 +5,31 @@ module ActionController
module Assertions
module ResponseAssertions
# Asserts that the response is one of the following types:
- #
+ #
# * <tt>:success</tt>: Status code was 200
# * <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
#
- # You can also pass an explicit status code number as the type, like assert_response(501)
+ # You can also pass an explicit status number like assert_response(501)
+ # or its symbolic equivalent assert_response(:not_implemented).
+ # See ActionController::StatusCodes for a full list.
def assert_response(type, message = nil)
clean_backtrace do
if [ :success, :missing, :redirect, :error ].include?(type) && @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 == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type]
+ assert_block("") { true } # to count the assertion
else
assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
- end
+ end
end
end
# Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial,
- # such that assert_redirected_to(:controller => "weblog") will also match the redirection of
+ # such that assert_redirected_to(:controller => "weblog") will also match the redirection of
# redirect_to(:controller => "weblog", :action => "show") and so on.
def assert_redirected_to(options = {}, message=nil)
clean_backtrace do
@@ -66,18 +70,18 @@ def assert_redirected_to(options = {}, message=nil)
if value.respond_to?(:[]) && value['controller']
if key == :actual && value['controller'].first != '/' && !value['controller'].include?('/')
- value['controller'] = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path)
+ value['controller'] = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path)
end
value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash
end
url[key] = value
end
-
+
@response_diff = url[:expected].diff(url[:actual]) if url[:actual]
- msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>), difference: <?>",
+ msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>), difference: <?>",
url[:actual], @response_diff)
-
+
assert_block(msg) do
url[:expected].keys.all? do |k|
if k == :controller then url[:expected][k] == ActionController::Routing.controller_relative_to(url[:actual][k], @controller.class.controller_path)
@@ -94,7 +98,7 @@ def assert_redirected_to(options = {}, message=nil)
end.flatten
assert_equal(eurl, url, msg) if eurl && url
- assert_equal(epath, path, msg) if epath && path
+ assert_equal(epath, path, msg) if epath && path
end
end
end
@@ -110,7 +114,7 @@ def assert_template(expected = nil, message=nil)
else
expected == rendered
end
- end
+ end
end
end
@@ -132,4 +136,4 @@ def parameterize(value)
end
end
end
-end
+end
View
7 actionpack/test/controller/new_render_test.rb
@@ -640,24 +640,29 @@ def test_head_with_location_header
get :head_with_location_header
assert @response.body.blank?
assert_equal "/foo", @response.headers["Location"]
+ assert_response :ok
end
def test_head_with_custom_header
get :head_with_custom_header
assert @response.body.blank?
assert_equal "something", @response.headers["X-Custom-Header"]
+ assert_response :ok
end
def test_head_with_symbolic_status
get :head_with_symbolic_status, :status => "ok"
assert_equal "200 OK", @response.headers["Status"]
+ assert_response :ok
get :head_with_symbolic_status, :status => "not_found"
assert_equal "404 Not Found", @response.headers["Status"]
+ assert_response :not_found
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |status, code|
get :head_with_symbolic_status, :status => status.to_s
assert_equal code, @response.response_code
+ assert_response status
end
end
@@ -672,12 +677,14 @@ def test_head_with_string_status
get :head_with_string_status, :status => "404 Eat Dirt"
assert_equal 404, @response.response_code
assert_equal "Eat Dirt", @response.message
+ assert_response :not_found
end
def test_head_with_status_code_first
get :head_with_status_code_first
assert_equal 403, @response.response_code
assert_equal "Forbidden", @response.message
assert_equal "something", @response.headers["X-Custom-Header"]
+ assert_response :forbidden
end
end

0 comments on commit dfa070a

Please sign in to comment.
Something went wrong with that request. Please try again.