mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #3262 from subdigital/master
Add optional host option to force_ssl
This commit is contained in:
commit
2ed66feae0
2 changed files with 26 additions and 3 deletions
|
@ -24,12 +24,15 @@ module ActionController
|
|||
# * <tt>only</tt> - The callback should be run only for this action
|
||||
# * <tt>except<tt> - The callback should be run for all actions except this action
|
||||
def force_ssl(options = {})
|
||||
host = options.delete(:host)
|
||||
before_filter(options) do
|
||||
if !request.ssl? && !Rails.env.development?
|
||||
redirect_to :protocol => 'https://', :status => :moved_permanently
|
||||
redirect_options = {:protocol => 'https://', :status => :moved_permanently}
|
||||
redirect_options.merge!(:host => host) if host
|
||||
redirect_to redirect_options
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,6 +14,10 @@ class ForceSSLControllerLevel < ForceSSLController
|
|||
force_ssl
|
||||
end
|
||||
|
||||
class ForceSSLCustomDomain < ForceSSLController
|
||||
force_ssl :host => "secure.test.host"
|
||||
end
|
||||
|
||||
class ForceSSLOnlyAction < ForceSSLController
|
||||
force_ssl :only => :cheeseburger
|
||||
end
|
||||
|
@ -38,6 +42,22 @@ class ForceSSLControllerLevelTest < ActionController::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
class ForceSSLCustomDomainTest < ActionController::TestCase
|
||||
tests ForceSSLCustomDomain
|
||||
|
||||
def test_banana_redirects_to_https_with_custom_host
|
||||
get :banana
|
||||
assert_response 301
|
||||
assert_equal "https://secure.test.host/force_ssl_custom_domain/banana", redirect_to_url
|
||||
end
|
||||
|
||||
def test_cheeseburger_redirects_to_https_with_custom_host
|
||||
get :cheeseburger
|
||||
assert_response 301
|
||||
assert_equal "https://secure.test.host/force_ssl_custom_domain/cheeseburger", redirect_to_url
|
||||
end
|
||||
end
|
||||
|
||||
class ForceSSLOnlyActionTest < ActionController::TestCase
|
||||
tests ForceSSLOnlyAction
|
||||
|
||||
|
@ -80,4 +100,4 @@ class ForceSSLExcludeDevelopmentTest < ActionController::TestCase
|
|||
get :banana
|
||||
assert_response 200
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue