Lots going on
[lyrix.git] / vendor / rails / actionpack / lib / action_controller / assertions / routing_assertions.rb
blob6cd4d48b9e75411c054ed241ee1666d21e55fc64
1 module ActionController
2   module Assertions
3     module RoutingAssertions
4       # Asserts that the routing of the given path was handled correctly and that the parsed options match.
5       #
6       #   assert_recognizes({:controller => 'items', :action => 'index'}, 'items') # check the default action
7       #   assert_recognizes({:controller => 'items', :action => 'list'}, 'items/list') # check a specific action
8       #   assert_recognizes({:controller => 'items', :action => 'list', :id => '1'}, 'items/list/1') # check an action with a parameter
9       #
10       # Pass a hash in the second argument to specify the request method.  This is useful for routes
11       # requiring a specific HTTP method.  The hash should contain a :path with the incoming request path
12       # and a :method containing the required HTTP verb.
13       #
14       #   # assert that POSTing to /items will call the create action on ItemsController
15       #   assert_recognizes({:controller => 'items', :action => 'create'}, {:path => 'items', :method => :post})
16       #
17       # You can also pass in "extras" with a hash containing URL parameters that would normally be in the query string.  This can be used
18       # to assert that values in the query string string will end up in the params hash correctly.  To test query strings you must use the
19       # extras argument, appending the query string on the path directly will not work.  For example: 
20       #
21       #   # assert that a path of '/items/list/1?view=print' returns the correct options
22       #   assert_recognizes({:controller => 'items', :action => 'list', :id => '1', :view => 'print'}, 'items/list/1', { :view => "print" }) 
23       def assert_recognizes(expected_options, path, extras={}, message=nil)
24         if path.is_a? Hash
25           request_method = path[:method]
26           path           = path[:path]
27         else
28           request_method = nil
29         end
31         clean_backtrace do 
32           ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? 
33           request = recognized_request_for(path, request_method)
34       
35           expected_options = expected_options.clone
36           extras.each_key { |key| expected_options.delete key } unless extras.nil?
37       
38           expected_options.stringify_keys!
39           routing_diff = expected_options.diff(request.path_parameters)
40           msg = build_message(message, "The recognized options <?> did not match <?>, difference: <?>", 
41               request.path_parameters, expected_options, expected_options.diff(request.path_parameters))
42           assert_block(msg) { request.path_parameters == expected_options }
43         end
44       end
46       # Asserts that the provided options can be used to generate the provided path.  This is the inverse of assert_recognizes.
47       # For example:
48       #
49       #   assert_generates("/items", :controller => "items", :action => "index")
50       #   assert_generates("/items/list", :controller => "items", :action => "list")
51       #   assert_generates("/items/list/1", { :controller => "items", :action => "list", :id => "1" }) 
52       def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
53         clean_backtrace do 
54           expected_path = "/#{expected_path}" unless expected_path[0] == ?/
55           # Load routes.rb if it hasn't been loaded.
56           ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? 
57       
58           generated_path, extra_keys = ActionController::Routing::Routes.generate_extras(options, defaults)
59           found_extras = options.reject {|k, v| ! extra_keys.include? k}
61           msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
62           assert_block(msg) { found_extras == extras }
63       
64           msg = build_message(message, "The generated path <?> did not match <?>", generated_path, 
65               expected_path)
66           assert_block(msg) { expected_path == generated_path }
67         end
68       end
70       # Asserts that path and options match both ways; in other words, the URL generated from 
71       # options is the same as path, and also that the options recognized from path are the same as options.  This
72       # essentially combines assert_recognizes and assert_generates into one step.
73       def assert_routing(path, options, defaults={}, extras={}, message=nil)
74         assert_recognizes(options, path, extras, message)
75         
76         controller, default_controller = options[:controller], defaults[:controller] 
77         if controller && controller.include?(?/) && default_controller && default_controller.include?(?/)
78           options[:controller] = "/#{controller}"
79         end
80          
81         assert_generates(path, options, defaults, extras, message)
82       end
84       private
85         # Recognizes the route for a given path.
86         def recognized_request_for(path, request_method = nil)
87           path = "/#{path}" unless path.first == '/'
89           # Assume given controller
90           request = ActionController::TestRequest.new({}, {}, nil)
91           request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
92           request.path   = path
94           ActionController::Routing::Routes.recognize(request)
95           request
96         end
97     end
98   end
99 end