Skip to content

Commit

Permalink
Merge pull request #16637 from Agis-/redirect-with-constraint-route
Browse files Browse the repository at this point in the history
Fix the router ignoring constraints when used together with a redirect route
  • Loading branch information
tenderlove committed Aug 28, 2014
2 parents 8c52480 + d78f3f0 commit 6d86762
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
7 changes: 7 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* Fix bug where the router would ignore any constraints added to redirect
routes.

Fixes #16605.

*Agis Anastasopoulos*

* Allow `config.action_dispatch.trusted_proxies` to accept an IPAddr object.

Example:
Expand Down
2 changes: 0 additions & 2 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ def add_request_method(via, conditions)
end

def app(blocks)
return to if Redirect === to

if to.respond_to?(:call)
Constraints.new(to, blocks, false)
else
Expand Down
24 changes: 24 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def self.matches?(request)
end
end

class GrumpyRestrictor
def self.matches?(request)
false
end
end

class YoutubeFavoritesRedirector
def self.call(params, request)
"http://www.youtube.com/watch?v=#{params[:youtube_id]}"
Expand Down Expand Up @@ -89,6 +95,24 @@ def test_namespace_redirect
verify_redirect 'http://www.example.com/private/index'
end

def test_redirect_with_failing_constraint
draw do
get 'hi', to: redirect("/foo"), constraints: ::TestRoutingMapper::GrumpyRestrictor
end

get '/hi'
assert_equal 404, status
end

def test_redirect_with_passing_constraint
draw do
get 'hi', to: redirect("/foo"), constraints: ->(req) { true }
end

get '/hi'
assert_equal 301, status
end

def test_namespace_with_controller_segment
assert_raise(ArgumentError) do
draw do
Expand Down

0 comments on commit 6d86762

Please sign in to comment.