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

Changeset 4248

Show
Ignore:
Timestamp:
04/22/06 15:08:25 (2 years ago)
Author:
rick
Message:

Diff compared options with #assert_redirected_to [Rick]

Files:

Legend:

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

    r4242 r4248  
    11*SVN* 
     2 
     3* Diff compared options with #assert_redirected_to [Rick] 
    24 
    35* Add support in routes for semicolon delimited "subpaths", like /books/:id;:action [Jamis Buck] 
  • trunk/actionpack/lib/action_controller/assertions.rb

    r4004 r4248  
    7474        clean_backtrace do 
    7575          assert_response(:redirect, message) 
    76  
    77           if options.is_a?(String) 
     76          ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?  
     77           
     78          begin 
     79            url = {} 
     80            f={ 
     81              :expected => options.is_a?(Symbol)                 ?  @controller.send("hash_for_#{options}_url")                 : options.dup,  
     82              :actual   => @response.redirected_to.is_a?(Symbol) ?  @controller.send("hash_for_#{@response.redirected_to}_url") : @response.redirected_to.dup  
     83            }.each do |key, value| 
     84              unless value.is_a?(Hash) 
     85                request = case value 
     86                  when NilClass    then nil 
     87                  when /^\w+:\/\// then recognized_request_for(%r{^(\w+://.*?(/|$|\?))(.*)$} =~ value ? $3 : nil) 
     88                  else                  recognized_request_for(value) 
     89                end 
     90                value = request.path_parameters if request 
     91              end 
     92               
     93              value.stringify_keys! if value.is_a?(Hash) 
     94              if value.respond_to?(:[]) && value['controller'] 
     95                if key == :actual && value['controller'][0..0] != '/' 
     96                  value['controller'] = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path)  
     97                end 
     98                value['controller'] = value['controller'][1..-1] if value['controller'][0..0] == '/' # strip leading hash 
     99              end 
     100              url[key] = value 
     101            end 
     102 
     103            @response_diff = url[:expected].diff(url[:actual]) if url[:actual] 
     104            msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>), difference: <?>",  
     105                                url[:actual], @response_diff) 
     106             
     107            assert_block(msg) do 
     108              url[:expected].keys.all? do |k| 
     109                if k == :controller then url[:expected][k] == ActionController::Routing.controller_relative_to(url[:actual][k], @controller.class.controller_path) 
     110                else parameterize(url[:expected][k]) == parameterize(url[:actual][k]) 
     111                end 
     112              end 
     113            end 
     114          rescue ActionController::RoutingError # routing failed us, so match the strings only. 
    78115            msg = build_message(message, "expected a redirect to <?>, found one to <?>", options, @response.redirect_url) 
    79116            url_regexp = %r{^(\w+://.*?(/|$|\?))(.*)$} 
     
    85122            assert_equal(eurl, url, msg) if eurl && url 
    86123            assert_equal(epath, path, msg) if epath && path  
    87           else 
    88             @response_diff = options.diff(@response.redirected_to) if options.is_a?(Hash) && @response.redirected_to.is_a?(Hash) 
    89             msg = build_message(message, "response is not a redirection to all of the options supplied (redirection is <?>)#{', difference: <?>' if @response_diff}",  
    90                                 @response.redirected_to || @response.redirect_url, @response_diff) 
    91  
    92             assert_block(msg) do 
    93               if options.is_a?(Symbol) 
    94                 @response.redirected_to == options 
    95               else 
    96                 options.keys.all? do |k| 
    97                   if k == :controller then options[k] == ActionController::Routing.controller_relative_to(@response.redirected_to[k], @controller.class.controller_path) 
    98                   else options[k] == (@response.redirected_to[k].respond_to?(:to_param) ? @response.redirected_to[k].to_param : @response.redirected_to[k] unless @response.redirected_to[k].nil?) 
    99                   end 
    100                 end 
    101               end 
    102             end 
    103124          end 
    104125        end 
     
    123144      def assert_recognizes(expected_options, path, extras={}, message=nil) 
    124145        clean_backtrace do  
    125           path = "/#{path}" unless path[0..0] == '/' 
    126           # Load routes.rb if it hasn't been loaded. 
    127146          ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?  
    128        
    129           # Assume given controller 
    130           request = ActionController::TestRequest.new({}, {}, nil) 
    131           request.path = path 
    132           ActionController::Routing::Routes.recognize!(request) 
     147          request = recognized_request_for(path) 
    133148       
    134149          expected_options = expected_options.clone 
     
    297312        clean_backtrace do 
    298313          expected_dom = HTML::Document.new(expected).root 
    299           actual_dom = HTML::Document.new(actual).root 
     314          actual_dom   = HTML::Document.new(actual).root 
    300315          full_message = build_message(message, "<?> expected to be != to\n<?>.", expected_dom.to_s, actual_dom.to_s) 
    301316          assert_block(full_message) { expected_dom != actual_dom } 
     
    316331        raise AssertionFailedError, e.message, e.backtrace.reject { |line| File.expand_path(line) =~ /#{path}/ } 
    317332      end 
     333       
     334      private 
     335        def recognized_request_for(path) 
     336          path = "/#{path}" unless path[0..0] == '/' 
     337 
     338          # Assume given controller 
     339          request = ActionController::TestRequest.new({}, {}, nil) 
     340          request.path = path 
     341          ActionController::Routing::Routes.recognize!(request) 
     342          request 
     343        end 
     344         
     345        def parameterize(value) 
     346          value.respond_to?(:to_param) ? value.to_param : value 
     347        end 
    318348    end 
    319349  end 
  • trunk/actionpack/test/controller/action_pack_assertions_test.rb

    r3852 r4248  
    227227  # test the redirection to a named route 
    228228  def test_assert_redirect_to_named_route 
    229     process :redirect_to_named_route 
    230     assert_raise(Test::Unit::AssertionFailedError) do 
    231       assert_redirected_to 'http://test.host/route_two' 
     229    with_routing do |set| 
     230      set.draw do 
     231        set.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing' 
     232        set.connect   ':controller/:action/:id' 
     233      end 
     234      process :redirect_to_named_route 
     235      assert_redirected_to 'http://test.host/route_one' 
     236      assert_redirected_to route_one_url 
     237      assert_redirected_to :route_one 
    232238    end 
    233239  end 
    234    
     240 
     241  def test_assert_redirect_to_named_route_failure 
     242    with_routing do |set| 
     243      set.draw do 
     244        set.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'one' 
     245        set.route_two 'route_two', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two' 
     246        set.connect   ':controller/:action/:id' 
     247      end 
     248      process :redirect_to_named_route 
     249      assert_raise(Test::Unit::AssertionFailedError) do 
     250        assert_redirected_to 'http://test.host/route_two' 
     251      end 
     252      assert_raise(Test::Unit::AssertionFailedError) do 
     253        assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two' 
     254      end 
     255      assert_raise(Test::Unit::AssertionFailedError) do 
     256        assert_redirected_to route_two_url 
     257      end 
     258      assert_raise(Test::Unit::AssertionFailedError) do 
     259        assert_redirected_to :route_two 
     260      end 
     261    end 
     262  end 
     263 
    235264  # test the flash-based assertions with something is in the flash 
    236265  def test_flash_assertions_full 
     
    322351  end 
    323352   
    324    
    325353  # check if we were rendered by a file-based template?  
    326354  def test_rendered_action 
  • trunk/actionpack/test/controller/redirect_test.rb

    r3969 r4248  
    6565    rescue Test::Unit::AssertionFailedError => err 
    6666      redirection_msg, diff_msg = err.message.scan(/<\{[^\}]+\}>/).collect { |s| s[2..-3] } 
    67       assert_match %r(:only_path=>false),        redirection_msg 
    68       assert_match %r(:host=>"other.test.host"), redirection_msg 
    69       assert_match %r(:action=>"other_host"),    redirection_msg 
    70       assert_match %r(:only_path=>true),         diff_msg 
    71       assert_match %r(:host=>"other.test.host"), diff_msg 
     67      assert_match %r("only_path"=>false),        redirection_msg 
     68      assert_match %r("host"=>"other.test.host"), redirection_msg 
     69      assert_match %r("action"=>"other_host"),    redirection_msg 
     70      assert_match %r("only_path"=>true),         diff_msg 
     71      assert_match %r("host"=>"other.test.host"), diff_msg 
    7272    end 
    7373  end 
  • trunk/actionpack/test/controller/test_test.rb

    r4004 r4248  
    406406 
    407407  def test_assert_redirected_to_symbol 
    408     get :redirect_to_symbol 
    409     assert_redirected_to :generate_url 
     408    with_routing do |set| 
     409      set.draw do 
     410        set.generate_url 'foo', :controller => 'test' 
     411        set.connect      ':controller/:action/:id' 
     412      end 
     413       
     414      get :redirect_to_symbol 
     415      assert_redirected_to :generate_url 
     416    end 
    410417  end 
    411418end