Skip to content

Commit 7c9bf45

Browse files
pixeltrixjosevalim
authored andcommitted
Support routing constraints in functional tests
Extend assert_recognizes and assert_generates to support passing full urls as the path argument. This allows testing of routing constraints such as subdomain and host within functional tests. [#5005 state:resolved] Signed-off-by: José Valim <jose.valim@gmail.com>
1 parent 0420fb5 commit 7c9bf45

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

actionpack/lib/action_dispatch/routing/route_set.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,15 +494,15 @@ def call(env)
494494

495495
def recognize_path(path, environment = {})
496496
method = (environment[:method] || "GET").to_s.upcase
497-
path = Rack::Mount::Utils.normalize_path(path)
497+
path = Rack::Mount::Utils.normalize_path(path) unless path =~ %r{://}
498498

499499
begin
500500
env = Rack::MockRequest.env_for(path, {:method => method})
501501
rescue URI::InvalidURIError => e
502502
raise ActionController::RoutingError, e.message
503503
end
504504

505-
req = Rack::Request.new(env)
505+
req = @request_class.new(env)
506506
@set.recognize(req) do |route, matches, params|
507507
params.each do |key, value|
508508
if value.is_a?(String)

actionpack/lib/action_dispatch/testing/assertions/routing.rb

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'uri'
12
require 'active_support/core_ext/hash/diff'
23
require 'active_support/core_ext/hash/indifferent_access'
34

@@ -40,14 +41,7 @@ module RoutingAssertions
4041
# # Check a Simply RESTful generated route
4142
# assert_recognizes list_items_url, 'items/list'
4243
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)
5145

5246
expected_options = expected_options.clone
5347
extras.each_key { |key| expected_options.delete key } unless extras.nil?
@@ -77,7 +71,16 @@ def assert_recognizes(expected_options, path, extras={}, message=nil)
7771
# # Asserts that the generated route gives us our custom route
7872
# assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }
7973
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
8184
# Load routes.rb if it hasn't been loaded.
8285

8386
generated_path, extra_keys = @routes.generate_extras(options, defaults)
@@ -177,15 +180,35 @@ def method_missing(selector, *args, &block)
177180

178181
private
179182
# 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
182190

183191
# Assume given controller
184192
request = ActionController::TestRequest.new
185-
request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
186-
request.path = path
187193

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 })
189212
request.path_parameters = params.with_indifferent_access
190213

191214
request

actionpack/test/controller/routing_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ def test_recognize_with_conditions
956956
params = set.recognize_path("/people", :method => :put)
957957
assert_equal("update", params[:action])
958958

959-
assert_raise(ActionController::RoutingError) {
959+
assert_raise(ActionController::UnknownHttpMethod) {
960960
set.recognize_path("/people", :method => :bacon)
961961
}
962962

0 commit comments

Comments
 (0)