diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 8b46790fd2a45..2a25a8c4eaec9 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -208,6 +208,7 @@ def redirect(*args, &block) uri = URI.parse(path_proc.call(req.params)) uri.scheme ||= req.scheme uri.host ||= req.host + uri.port ||= req.port unless req.port == 80 headers = { 'Location' => uri.to_s, diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 21bd7d5ac0103..a746c7e2edc5e 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -507,6 +507,18 @@ def test_redirect_with_complete_url end end + def test_redirect_with_port + previous_host, self.host = self.host, 'www.example.com:3000' + with_test_routes do + get '/account/login' + assert_equal 301, @response.status + assert_equal 'http://www.example.com:3000/login', @response.headers['Location'] + assert_equal 'Moved Permanently', @response.body + end + ensure + self.host = previous_host + end + private def with_test_routes real_routes, temp_routes = ActionController::Routing::Routes, Routes