From 57313c54bfc3a47f32507cb466df91b5a9688db7 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 7 Nov 2006 21:45:10 +0000 Subject: assert_response supports symbolic status codes. Closes #6569. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5466 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 5 +++++ .../assertions/response_assertions.rb | 26 +++++++++++++--------- actionpack/test/controller/new_render_test.rb | 7 ++++++ 3 files changed, 27 insertions(+), 11 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index dc6bd3130e..5cd3add473 100644 --- a/actionpack/CHANGELOG +++ b/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] diff --git a/actionpack/lib/action_controller/assertions/response_assertions.rb b/actionpack/lib/action_controller/assertions/response_assertions.rb index a3834e3a3e..c0b78552f2 100644 --- a/actionpack/lib/action_controller/assertions/response_assertions.rb +++ b/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: - # + # # * :success: Status code was 200 # * :redirect: Status code was in the 300-399 range # * :missing: Status code was 404 # * :error: 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 \ No newline at end of file +end diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 729119d9fb..16709b4ea6 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -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 -- cgit v1.2.3