Skip to content

Commit

Permalink
Support routing constraints in functional tests
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pixeltrix authored and josevalim committed Aug 20, 2010
1 parent 0420fb5 commit 7c9bf45
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_dispatch/routing/route_set.rb
Expand Up @@ -494,15 +494,15 @@ def call(env)

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

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

req = Rack::Request.new(env)
req = @request_class.new(env)
@set.recognize(req) do |route, matches, params|
params.each do |key, value|
if value.is_a?(String)
Expand Down
51 changes: 37 additions & 14 deletions actionpack/lib/action_dispatch/testing/assertions/routing.rb
@@ -1,3 +1,4 @@
require 'uri'
require 'active_support/core_ext/hash/diff'
require 'active_support/core_ext/hash/indifferent_access'

Expand Down Expand Up @@ -40,14 +41,7 @@ module RoutingAssertions
# # Check a Simply RESTful generated route
# assert_recognizes list_items_url, 'items/list'
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

request = recognized_request_for(path, request_method)
request = recognized_request_for(path)

expected_options = expected_options.clone
extras.each_key { |key| expected_options.delete key } unless extras.nil?
Expand Down Expand Up @@ -77,7 +71,16 @@ def assert_recognizes(expected_options, path, extras={}, message=nil)
# # Asserts that the generated route gives us our custom route
# assert_generates "changesets/12", { :controller => 'scm', :action => 'show_diff', :revision => "12" }
def assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)
expected_path = "/#{expected_path}" unless expected_path[0] == ?/
if expected_path =~ %r{://}
begin
uri = URI.parse(expected_path)
expected_path = uri.path.to_s.empty? ? "/" : uri.path
rescue URI::InvalidURIError => e
raise ActionController::RoutingError, e.message
end
else
expected_path = "/#{expected_path}" unless expected_path.first == '/'
end
# Load routes.rb if it hasn't been loaded.

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

private
# Recognizes the route for a given path.
def recognized_request_for(path, request_method = nil)
path = "/#{path}" unless path.first == '/'
def recognized_request_for(path)
if path.is_a?(Hash)
method = path[:method]
path = path[:path]
else
method = :get
end

# Assume given controller
request = ActionController::TestRequest.new
request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
request.path = path

params = @routes.recognize_path(path, { :method => request.method })
if path =~ %r{://}
begin
uri = URI.parse(path)
request.env["rack.url_scheme"] = uri.scheme || "http"
request.host = uri.host if uri.host
request.port = uri.port if uri.port
request.path = uri.path.to_s.empty? ? "/" : uri.path
rescue URI::InvalidURIError => e
raise ActionController::RoutingError, e.message
end
else
path = "/#{path}" unless path.first == "/"
request.path = path
end

request.request_method = method if method

params = @routes.recognize_path(path, { :method => method })
request.path_parameters = params.with_indifferent_access

request
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/routing_test.rb
Expand Up @@ -956,7 +956,7 @@ def test_recognize_with_conditions
params = set.recognize_path("/people", :method => :put)
assert_equal("update", params[:action])

assert_raise(ActionController::RoutingError) {
assert_raise(ActionController::UnknownHttpMethod) {
set.recognize_path("/people", :method => :bacon)
}

Expand Down

0 comments on commit 7c9bf45

Please sign in to comment.