From d78f3f0ec33d57e78249d5b96f38401a1c239410 Mon Sep 17 00:00:00 2001 From: Agis- Date: Fri, 22 Aug 2014 16:52:48 +0300 Subject: Don't ignore constraints in redirect routes https://github.com/rails/rails/commit/402c2af55053c2f29319091ad21fd6fa6b90ee89 introduced a regression that caused any constraints added to redirect routes to be ignored. Fixes #16605 --- actionpack/CHANGELOG.md | 7 +++++++ actionpack/lib/action_dispatch/routing/mapper.rb | 2 -- actionpack/test/dispatch/routing_test.rb | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 050ec5e649..dbb80bb901 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -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: diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index e92baa5aa7..fc28740828 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -241,8 +241,6 @@ module ActionDispatch end def app(blocks) - return to if Redirect === to - if to.respond_to?(:call) Constraints.new(to, blocks, false) else diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index b8e20c52a0..d141210ad9 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -14,6 +14,12 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest 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]}" @@ -89,6 +95,24 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest 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 -- cgit v1.2.3