mirror of
https://github.com/github/rails.git
synced 2026-04-04 03:00:58 -04:00
assert_response supports symbolic status codes. Closes #6569.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5466 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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 @@ module ActionController
|
||||
|
||||
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 @@ module ActionController
|
||||
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 @@ module ActionController
|
||||
else
|
||||
expected == rendered
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -132,4 +136,4 @@ module ActionController
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -640,24 +640,29 @@ EOS
|
||||
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,6 +677,7 @@ EOS
|
||||
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
|
||||
@@ -679,5 +685,6 @@ EOS
|
||||
assert_equal 403, @response.response_code
|
||||
assert_equal "Forbidden", @response.message
|
||||
assert_equal "something", @response.headers["X-Custom-Header"]
|
||||
assert_response :forbidden
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user