Changeset 4248
- Timestamp:
- 04/22/06 15:08:25 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/assertions.rb (modified) (5 diffs)
- trunk/actionpack/test/controller/action_pack_assertions_test.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/redirect_test.rb (modified) (1 diff)
- trunk/actionpack/test/controller/test_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r4242 r4248 1 1 *SVN* 2 3 * Diff compared options with #assert_redirected_to [Rick] 2 4 3 5 * Add support in routes for semicolon delimited "subpaths", like /books/:id;:action [Jamis Buck] trunk/actionpack/lib/action_controller/assertions.rb
r4004 r4248 74 74 clean_backtrace do 75 75 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. 78 115 msg = build_message(message, "expected a redirect to <?>, found one to <?>", options, @response.redirect_url) 79 116 url_regexp = %r{^(\w+://.*?(/|$|\?))(.*)$} … … 85 122 assert_equal(eurl, url, msg) if eurl && url 86 123 assert_equal(epath, path, msg) if epath && path 87 else88 @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) do93 if options.is_a?(Symbol)94 @response.redirected_to == options95 else96 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 end100 end101 end102 end103 124 end 104 125 end … … 123 144 def assert_recognizes(expected_options, path, extras={}, message=nil) 124 145 clean_backtrace do 125 path = "/#{path}" unless path[0..0] == '/'126 # Load routes.rb if it hasn't been loaded.127 146 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) 133 148 134 149 expected_options = expected_options.clone … … 297 312 clean_backtrace do 298 313 expected_dom = HTML::Document.new(expected).root 299 actual_dom = HTML::Document.new(actual).root314 actual_dom = HTML::Document.new(actual).root 300 315 full_message = build_message(message, "<?> expected to be != to\n<?>.", expected_dom.to_s, actual_dom.to_s) 301 316 assert_block(full_message) { expected_dom != actual_dom } … … 316 331 raise AssertionFailedError, e.message, e.backtrace.reject { |line| File.expand_path(line) =~ /#{path}/ } 317 332 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 318 348 end 319 349 end trunk/actionpack/test/controller/action_pack_assertions_test.rb
r3852 r4248 227 227 # test the redirection to a named route 228 228 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 232 238 end 233 239 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 235 264 # test the flash-based assertions with something is in the flash 236 265 def test_flash_assertions_full … … 322 351 end 323 352 324 325 353 # check if we were rendered by a file-based template? 326 354 def test_rendered_action trunk/actionpack/test/controller/redirect_test.rb
r3969 r4248 65 65 rescue Test::Unit::AssertionFailedError => err 66 66 redirection_msg, diff_msg = err.message.scan(/<\{[^\}]+\}>/).collect { |s| s[2..-3] } 67 assert_match %r( :only_path=>false), redirection_msg68 assert_match %r( :host=>"other.test.host"), redirection_msg69 assert_match %r( :action=>"other_host"), redirection_msg70 assert_match %r( :only_path=>true), diff_msg71 assert_match %r( :host=>"other.test.host"), diff_msg67 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 72 72 end 73 73 end trunk/actionpack/test/controller/test_test.rb
r4004 r4248 406 406 407 407 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 410 417 end 411 418 end