routing.rb 7.7 KB
Newer Older
J
Jeremy Kemper 已提交
1 2
require 'active_support/core_ext/hash/diff'

3
module ActionDispatch
4
  module Assertions
5
    # Suite of assertions to test routes generated by Rails and the handling of requests made to them.
6
    module RoutingAssertions
J
Joshua Peek 已提交
7
      # Asserts that the routing of the given +path+ was handled correctly and that the parsed options (given in the +expected_options+ hash)
8
      # match +path+.  Basically, it asserts that Rails recognizes the route given by +expected_options+.
9
      #
10
      # Pass a hash in the second argument (+path+) to specify the request method.  This is useful for routes
11 12
      # requiring a specific HTTP method.  The hash should contain a :path with the incoming request path
      # and a :method containing the required HTTP verb.
13
      #
14
      #   # assert that POSTing to /items will call the create action on ItemsController
P
Pratik Naik 已提交
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
J
Joshua Peek 已提交
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
P
Pratik Naik 已提交
22
      #   assert_recognizes {:controller => 'items', :action => 'list', :id => '1', :view => 'print'}, 'items/list/1', { :view => "print" }
23
      #
J
Joshua Peek 已提交
24
      # The +message+ parameter allows you to pass in an error message that is displayed upon failure.
25 26 27
      #
      # ==== Examples
      #   # Check the default route (i.e., the index action)
P
Pratik Naik 已提交
28
      #   assert_recognizes {:controller => 'items', :action => 'index'}, 'items'
29 30
      #
      #   # Test a specific action
P
Pratik Naik 已提交
31
      #   assert_recognizes {:controller => 'items', :action => 'list'}, 'items/list'
32 33
      #
      #   # Test an action with a parameter
P
Pratik Naik 已提交
34
      #   assert_recognizes {:controller => 'items', :action => 'destroy', :id => '1'}, 'items/destroy/1'
35 36
      #
      #   # Test a custom route
P
Pratik Naik 已提交
37
      #   assert_recognizes {:controller => 'items', :action => 'show', :id => '1'}, 'view/item1'
38 39
      #
      #   # Check a Simply RESTful generated route
P
Pratik Naik 已提交
40
      #   assert_recognizes list_items_url, 'items/list'
41 42 43 44 45 46 47 48
      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

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

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

55 56 57 58 59
        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 }
60 61
      end

P
Pratik Naik 已提交
62
      # Asserts that the provided options can be used to generate the provided path.  This is the inverse of +assert_recognizes+.
63 64
      # 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.
65
      #
66
      # The +defaults+ parameter is unused.
J
Joshua Peek 已提交
67
      #
68 69
      # ==== Examples
      #   # Asserts that the default action is generated for a route with no action
P
Pratik Naik 已提交
70
      #   assert_generates "/items", :controller => "items", :action => "index"
71 72
      #
      #   # Tests that the list action is properly routed
P
Pratik Naik 已提交
73
      #   assert_generates "/items/list", :controller => "items", :action => "list"
74 75
      #
      #   # Tests the generation of a route with a parameter
P
Pratik Naik 已提交
76
      #   assert_generates "/items/list/1", { :controller => "items", :action => "list", :id => "1" }
77 78 79
      #
      #   # Asserts that the generated route gives us our custom route
      #   assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }
80
      def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
81 82 83
        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 已提交
84

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

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

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

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

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

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

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

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

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