routing.rb 7.6 KB
Newer Older
1
module ActionDispatch
2
  module Assertions
3
    # Suite of assertions to test routes generated by Rails and the handling of requests made to them.
4
    module RoutingAssertions
J
Joshua Peek 已提交
5
      # Asserts that the routing of the given +path+ was handled correctly and that the parsed options (given in the +expected_options+ hash)
6
      # match +path+.  Basically, it asserts that Rails recognizes the route given by +expected_options+.
7
      #
8
      # Pass a hash in the second argument (+path+) to specify the request method.  This is useful for routes
9 10
      # requiring a specific HTTP method.  The hash should contain a :path with the incoming request path
      # and a :method containing the required HTTP verb.
11
      #
12
      #   # assert that POSTing to /items will call the create action on ItemsController
P
Pratik Naik 已提交
13
      #   assert_recognizes {:controller => 'items', :action => 'create'}, {:path => 'items', :method => :post}
14
      #
15
      # You can also pass in +extras+ with a hash containing URL parameters that would normally be in the query string.  This can be used
16
      # 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
J
Joshua Peek 已提交
17
      # extras argument, appending the query string on the path directly will not work.  For example:
18 19
      #
      #   # assert that a path of '/items/list/1?view=print' returns the correct options
P
Pratik Naik 已提交
20
      #   assert_recognizes {:controller => 'items', :action => 'list', :id => '1', :view => 'print'}, 'items/list/1', { :view => "print" }
21
      #
J
Joshua Peek 已提交
22
      # The +message+ parameter allows you to pass in an error message that is displayed upon failure.
23 24 25
      #
      # ==== Examples
      #   # Check the default route (i.e., the index action)
P
Pratik Naik 已提交
26
      #   assert_recognizes {:controller => 'items', :action => 'index'}, 'items'
27 28
      #
      #   # Test a specific action
P
Pratik Naik 已提交
29
      #   assert_recognizes {:controller => 'items', :action => 'list'}, 'items/list'
30 31
      #
      #   # Test an action with a parameter
P
Pratik Naik 已提交
32
      #   assert_recognizes {:controller => 'items', :action => 'destroy', :id => '1'}, 'items/destroy/1'
33 34
      #
      #   # Test a custom route
P
Pratik Naik 已提交
35
      #   assert_recognizes {:controller => 'items', :action => 'show', :id => '1'}, 'view/item1'
36 37
      #
      #   # Check a Simply RESTful generated route
P
Pratik Naik 已提交
38
      #   assert_recognizes list_items_url, 'items/list'
39 40 41 42 43 44 45 46
      def assert_recognizes(expected_options, path, extras={}, message=nil)
        if path.is_a? Hash
          request_method = path[:method]
          path           = path[:path]
        else
          request_method = nil
        end

47 48
        ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
        request = recognized_request_for(path, request_method)
J
Joshua Peek 已提交
49

50 51
        expected_options = expected_options.clone
        extras.each_key { |key| expected_options.delete key } unless extras.nil?
J
Joshua Peek 已提交
52

53 54 55 56 57
        expected_options.stringify_keys!
        routing_diff = expected_options.diff(request.path_parameters)
        msg = build_message(message, "The recognized options <?> did not match <?>, difference: <?>",
            request.path_parameters, expected_options, expected_options.diff(request.path_parameters))
        assert_block(msg) { request.path_parameters == expected_options }
58 59
      end

P
Pratik Naik 已提交
60
      # Asserts that the provided options can be used to generate the provided path.  This is the inverse of +assert_recognizes+.
61 62
      # The +extras+ parameter is used to tell the request the names and values of additional request parameters that would be in
      # a query string. The +message+ parameter allows you to specify a custom error message for assertion failures.
63
      #
64
      # The +defaults+ parameter is unused.
J
Joshua Peek 已提交
65
      #
66 67
      # ==== Examples
      #   # Asserts that the default action is generated for a route with no action
P
Pratik Naik 已提交
68
      #   assert_generates "/items", :controller => "items", :action => "index"
69 70
      #
      #   # Tests that the list action is properly routed
P
Pratik Naik 已提交
71
      #   assert_generates "/items/list", :controller => "items", :action => "list"
72 73
      #
      #   # Tests the generation of a route with a parameter
P
Pratik Naik 已提交
74
      #   assert_generates "/items/list/1", { :controller => "items", :action => "list", :id => "1" }
75 76 77
      #
      #   # Asserts that the generated route gives us our custom route
      #   assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }
78
      def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
79 80 81
        expected_path = "/#{expected_path}" unless expected_path[0] == ?/
        # Load routes.rb if it hasn't been loaded.
        ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
J
Joshua Peek 已提交
82

83 84
        generated_path, extra_keys = ActionController::Routing::Routes.generate_extras(options, defaults)
        found_extras = options.reject {|k, v| ! extra_keys.include? k}
85

86 87
        msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)
        assert_block(msg) { found_extras == extras }
J
Joshua Peek 已提交
88

89 90 91
        msg = build_message(message, "The generated path <?> did not match <?>", generated_path,
            expected_path)
        assert_block(msg) { expected_path == generated_path }
92 93
      end

J
Joshua Peek 已提交
94
      # Asserts that path and options match both ways; in other words, it verifies that <tt>path</tt> generates
P
Pratik Naik 已提交
95 96
      # <tt>options</tt> and then that <tt>options</tt> generates <tt>path</tt>.  This essentially combines +assert_recognizes+
      # and +assert_generates+ into one step.
97 98
      #
      # The +extras+ hash allows you to specify options that would normally be provided as a query string to the action.  The
J
Joshua Peek 已提交
99
      # +message+ parameter allows you to specify a custom error message to display upon failure.
100 101 102
      #
      # ==== Examples
      #  # Assert a basic route: a controller with the default action (index)
P
Pratik Naik 已提交
103
      #  assert_routing '/home', :controller => 'home', :action => 'index'
104 105
      #
      #  # Test a route generated with a specific controller, action, and parameter (id)
P
Pratik Naik 已提交
106
      #  assert_routing '/entries/show/23', :controller => 'entries', :action => 'show', id => 23
107 108
      #
      #  # Assert a basic route (controller + default action), with an error message if it fails
P
Pratik Naik 已提交
109
      #  assert_routing '/store', { :controller => 'store', :action => 'index' }, {}, {}, 'Route for store index not generated properly'
110 111 112
      #
      #  # Tests a route, providing a defaults hash
      #  assert_routing 'controller/action/9', {:id => "9", :item => "square"}, {:controller => "controller", :action => "action"}, {}, {:item => "square"}
113 114
      #
      #  # Tests a route with a HTTP method
P
Pratik Naik 已提交
115
      #  assert_routing { :method => 'put', :path => '/product/321' }, { :controller => "product", :action => "update", :id => "321" }
116 117
      def assert_routing(path, options, defaults={}, extras={}, message=nil)
        assert_recognizes(options, path, extras, message)
J
Joshua Peek 已提交
118 119

        controller, default_controller = options[:controller], defaults[:controller]
120 121 122
        if controller && controller.include?(?/) && default_controller && default_controller.include?(?/)
          options[:controller] = "/#{controller}"
        end
J
Joshua Peek 已提交
123

124
        assert_generates(path.is_a?(Hash) ? path[:path] : path, options, defaults, extras, message)
125 126 127
      end

      private
128
        # Recognizes the route for a given path.
129 130 131 132
        def recognized_request_for(path, request_method = nil)
          path = "/#{path}" unless path.first == '/'

          # Assume given controller
133
          request = ActionController::TestRequest.new
134 135 136 137 138 139 140 141
          request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
          request.path   = path

          ActionController::Routing::Routes.recognize(request)
          request
        end
    end
  end
142
end