|
| 1 | +require 'uri' |
1 | 2 | require 'active_support/core_ext/hash/diff'
|
2 | 3 | require 'active_support/core_ext/hash/indifferent_access'
|
3 | 4 |
|
@@ -40,14 +41,7 @@ module RoutingAssertions
|
40 | 41 | # # Check a Simply RESTful generated route
|
41 | 42 | # assert_recognizes list_items_url, 'items/list'
|
42 | 43 | def assert_recognizes(expected_options, path, extras={}, message=nil)
|
43 |
| - if path.is_a? Hash |
44 |
| - request_method = path[:method] |
45 |
| - path = path[:path] |
46 |
| - else |
47 |
| - request_method = nil |
48 |
| - end |
49 |
| - |
50 |
| - request = recognized_request_for(path, request_method) |
| 44 | + request = recognized_request_for(path) |
51 | 45 |
|
52 | 46 | expected_options = expected_options.clone
|
53 | 47 | extras.each_key { |key| expected_options.delete key } unless extras.nil?
|
@@ -77,7 +71,16 @@ def assert_recognizes(expected_options, path, extras={}, message=nil)
|
77 | 71 | # # Asserts that the generated route gives us our custom route
|
78 | 72 | # assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }
|
79 | 73 | def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
|
80 |
| - expected_path = "/#{expected_path}" unless expected_path[0] == ?/ |
| 74 | + if expected_path =~ %r{://} |
| 75 | + begin |
| 76 | + uri = URI.parse(expected_path) |
| 77 | + expected_path = uri.path.to_s.empty? ? "/" : uri.path |
| 78 | + rescue URI::InvalidURIError => e |
| 79 | + raise ActionController::RoutingError, e.message |
| 80 | + end |
| 81 | + else |
| 82 | + expected_path = "/#{expected_path}" unless expected_path.first == '/' |
| 83 | + end |
81 | 84 | # Load routes.rb if it hasn't been loaded.
|
82 | 85 |
|
83 | 86 | generated_path, extra_keys = @routes.generate_extras(options, defaults)
|
@@ -177,15 +180,35 @@ def method_missing(selector, *args, &block)
|
177 | 180 |
|
178 | 181 | private
|
179 | 182 | # Recognizes the route for a given path.
|
180 |
| - def recognized_request_for(path, request_method = nil) |
181 |
| - path = "/#{path}" unless path.first == '/' |
| 183 | + def recognized_request_for(path) |
| 184 | + if path.is_a?(Hash) |
| 185 | + method = path[:method] |
| 186 | + path = path[:path] |
| 187 | + else |
| 188 | + method = :get |
| 189 | + end |
182 | 190 |
|
183 | 191 | # Assume given controller
|
184 | 192 | request = ActionController::TestRequest.new
|
185 |
| - request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method |
186 |
| - request.path = path |
187 | 193 |
|
188 |
| - params = @routes.recognize_path(path, { :method => request.method }) |
| 194 | + if path =~ %r{://} |
| 195 | + begin |
| 196 | + uri = URI.parse(path) |
| 197 | + request.env["rack.url_scheme"] = uri.scheme || "http" |
| 198 | + request.host = uri.host if uri.host |
| 199 | + request.port = uri.port if uri.port |
| 200 | + request.path = uri.path.to_s.empty? ? "/" : uri.path |
| 201 | + rescue URI::InvalidURIError => e |
| 202 | + raise ActionController::RoutingError, e.message |
| 203 | + end |
| 204 | + else |
| 205 | + path = "/#{path}" unless path.first == "/" |
| 206 | + request.path = path |
| 207 | + end |
| 208 | + |
| 209 | + request.request_method = method if method |
| 210 | + |
| 211 | + params = @routes.recognize_path(path, { :method => method }) |
189 | 212 | request.path_parameters = params.with_indifferent_access
|
190 | 213 |
|
191 | 214 | request
|
|
0 commit comments