Skip to content

Commit

Permalink
Fix the assert_recognizes test method so that it works when there are
Browse files Browse the repository at this point in the history
constraints on the querystring. Issue rails#2781
  • Loading branch information
mattfawcett committed Feb 24, 2012
1 parent 335fac5 commit d7bf930
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions actionpack/lib/action_dispatch/routing/route_set.rb
Expand Up @@ -594,6 +594,7 @@ def call(env)
def recognize_path(path, environment = {})
method = (environment[:method] || "GET").to_s.upcase
path = Journey::Router::Utils.normalize_path(path) unless path =~ %r{://}
extras = environment[:extras] || {}

begin
env = Rack::MockRequest.env_for(path, {:method => method})
Expand All @@ -603,6 +604,7 @@ def recognize_path(path, environment = {})

req = @request_class.new(env)
@router.recognize(req) do |route, matches, params|
params.merge!(extras)
params.each do |key, value|
if value.is_a?(String)
value = value.dup.force_encoding(Encoding::BINARY)
Expand Down
7 changes: 3 additions & 4 deletions actionpack/lib/action_dispatch/testing/assertions/routing.rb
Expand Up @@ -39,10 +39,9 @@ module RoutingAssertions
# # Test a custom route
# assert_recognizes({:controller => 'items', :action => 'show', :id => '1'}, 'view/item1')
def assert_recognizes(expected_options, path, extras={}, message=nil)
request = recognized_request_for(path)
request = recognized_request_for(path, extras)

expected_options = expected_options.clone
extras.each_key { |key| expected_options.delete key } unless extras.nil?

expected_options.stringify_keys!

Expand Down Expand Up @@ -181,7 +180,7 @@ def method_missing(selector, *args, &block)

private
# Recognizes the route for a given path.
def recognized_request_for(path)
def recognized_request_for(path, extras = {})
if path.is_a?(Hash)
method = path[:method]
path = path[:path]
Expand Down Expand Up @@ -209,7 +208,7 @@ def recognized_request_for(path)

request.request_method = method if method

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

request
Expand Down
12 changes: 12 additions & 0 deletions actionpack/test/dispatch/routing_assertions_test.rb
Expand Up @@ -3,6 +3,7 @@

class SecureArticlesController < ArticlesController; end
class BlockArticlesController < ArticlesController; end
class QueryArticlesController < ArticlesController; end

class RoutingAssertionsTest < ActionController::TestCase

Expand All @@ -18,6 +19,10 @@ def setup
scope 'block', :constraints => lambda { |r| r.ssl? } do
resources :articles, :controller => 'block_articles'
end

scope 'query', :constraints => lambda { |r| r.params[:use_query] == 'true' } do
resources :articles, :controller => 'query_articles'
end
end
end

Expand Down Expand Up @@ -62,6 +67,13 @@ def test_assert_recognizes_with_block_constraint
assert_recognizes({ :controller => 'block_articles', :action => 'index' }, 'https://test.host/block/articles')
end

def test_assert_recognizes_with_query_constraint
assert_raise(ActionController::RoutingError) do
assert_recognizes({ :controller => 'query_articles', :action => 'index', :use_query => 'false' }, '/query/articles', { :use_query => 'false' })
end
assert_recognizes({ :controller => 'query_articles', :action => 'index', :use_query => 'true' }, '/query/articles', { :use_query => 'true' })
end

def test_assert_routing
assert_routing('/articles', :controller => 'articles', :action => 'index')
end
Expand Down

0 comments on commit d7bf930

Please sign in to comment.