Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5466

Show
Ignore:
Timestamp:
11/07/06 21:45:10 (2 years ago)
Author:
bitsweat
Message:

assert_response supports symbolic status codes. Closes #6569.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r5442 r5466  
    11*SVN* 
     2 
     3* assert_response supports symbolic status codes.  #6569 [Kevin Clark] 
     4    assert_response :ok 
     5    assert_response :not_found 
     6    assert_response :forbidden 
    27 
    38* Cache parsed query parameters.  #6559 [Stefan Kaes] 
  • trunk/actionpack/lib/action_controller/assertions/response_assertions.rb

    r4935 r5466  
    66    module ResponseAssertions 
    77      # Asserts that the response is one of the following types: 
    8       #  
     8      # 
    99      # * <tt>:success</tt>: Status code was 200 
    1010      # * <tt>:redirect</tt>: Status code was in the 300-399 range 
     
    1212      # * <tt>:error</tt>:  Status code was in the 500-599 range 
    1313      # 
    14       # You can also pass an explicit status code number as the type, like assert_response(501) 
     14      # You can also pass an explicit status number like assert_response(501) 
     15      # or its symbolic equivalent assert_response(:not_implemented). 
     16      # See ActionController::StatusCodes for a full list. 
    1517      def assert_response(type, message = nil) 
    1618        clean_backtrace do 
     
    1921          elsif type.is_a?(Fixnum) && @response.response_code == type 
    2022            assert_block("") { true } # to count the assertion 
     23          elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type] 
     24            assert_block("") { true } # to count the assertion 
    2125          else 
    2226            assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false } 
    23           end                
     27          end 
    2428        end 
    2529      end 
    2630 
    2731      # Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, 
    28       # such that assert_redirected_to(:controller => "weblog") will also match the redirection of  
     32      # such that assert_redirected_to(:controller => "weblog") will also match the redirection of 
    2933      # redirect_to(:controller => "weblog", :action => "show") and so on. 
    3034      def assert_redirected_to(options = {}, message=nil) 
     
    6771              if value.respond_to?(:[]) && value['controller'] 
    6872                if key == :actual && value['controller'].first != '/' && !value['controller'].include?('/') 
    69                   value['controller'] = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path)  
     73                  value['controller'] = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path) 
    7074                end 
    7175                value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash 
     
    7377              url[key] = value 
    7478            end 
    75              
     79 
    7680 
    7781            @response_diff = url[:expected].diff(url[:actual]) if url[:actual] 
    78             msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>), difference: <?>",  
     82            msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>), difference: <?>", 
    7983                                url[:actual], @response_diff) 
    80              
     84 
    8185            assert_block(msg) do 
    8286              url[:expected].keys.all? do |k| 
     
    9599 
    96100            assert_equal(eurl, url, msg) if eurl && url 
    97             assert_equal(epath, path, msg) if epath && path  
     101            assert_equal(epath, path, msg) if epath && path 
    98102          end 
    99103        end 
     
    111115              expected == rendered 
    112116            end 
    113           end                
     117          end 
    114118        end 
    115119      end 
  • trunk/actionpack/test/controller/new_render_test.rb

    r5201 r5466  
    641641    assert @response.body.blank? 
    642642    assert_equal "/foo", @response.headers["Location"] 
     643    assert_response :ok 
    643644  end 
    644645 
     
    647648    assert @response.body.blank? 
    648649    assert_equal "something", @response.headers["X-Custom-Header"] 
     650    assert_response :ok 
    649651  end 
    650652 
     
    652654    get :head_with_symbolic_status, :status => "ok" 
    653655    assert_equal "200 OK", @response.headers["Status"] 
     656    assert_response :ok 
    654657 
    655658    get :head_with_symbolic_status, :status => "not_found" 
    656659    assert_equal "404 Not Found", @response.headers["Status"] 
     660    assert_response :not_found 
    657661 
    658662    ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |status, code| 
    659663      get :head_with_symbolic_status, :status => status.to_s 
    660664      assert_equal code, @response.response_code 
     665      assert_response status 
    661666    end 
    662667  end 
     
    673678    assert_equal 404, @response.response_code 
    674679    assert_equal "Eat Dirt", @response.message 
     680    assert_response :not_found 
    675681  end 
    676682 
     
    680686    assert_equal "Forbidden", @response.message 
    681687    assert_equal "something", @response.headers["X-Custom-Header"] 
     688    assert_response :forbidden 
    682689  end 
    683690end