diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-05-31 09:43:55 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-05-31 09:43:55 -0700 |
commit | da048de773cea8a970768f04cecd0b3dbb015772 (patch) | |
tree | 013621b9f50005f40402acc0080d8139f167e48b | |
parent | 67b94799c27cc3214017627aede759c5c5a39189 (diff) | |
parent | 4791822368bf0b1238a096c353cd408c6f1fe8c4 (diff) | |
download | rails-da048de773cea8a970768f04cecd0b3dbb015772.tar.gz rails-da048de773cea8a970768f04cecd0b3dbb015772.tar.bz2 rails-da048de773cea8a970768f04cecd0b3dbb015772.zip |
Merge pull request #6571 from jeremyf/extract-force-ssl-filter-logic-to-separate-method
Extracted redirect logic from ActionController::Force::ClassMethods.forc...
-rw-r--r-- | actionpack/CHANGELOG.md | 4 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/force_ssl.rb | 22 | ||||
-rw-r--r-- | actionpack/test/controller/force_ssl_test.rb | 31 |
3 files changed, 50 insertions, 7 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index b907eea3c1..b4d50b7072 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Extracted redirect logic from `ActionController::ForceSSL::ClassMethods.force_ssl` into `ActionController::ForceSSL#force_ssl_redirect` + + *Jeremy Friesen* + * Make possible to use a block in button_to helper if button text is hard to fit into the name parameter, e.g.: diff --git a/actionpack/lib/action_controller/metal/force_ssl.rb b/actionpack/lib/action_controller/metal/force_ssl.rb index ac12cbb625..77d799a38a 100644 --- a/actionpack/lib/action_controller/metal/force_ssl.rb +++ b/actionpack/lib/action_controller/metal/force_ssl.rb @@ -40,15 +40,23 @@ module ActionController def force_ssl(options = {}) host = options.delete(:host) before_filter(options) do - unless request.ssl? - redirect_options = {:protocol => 'https://', :status => :moved_permanently} - redirect_options.merge!(:host => host) if host - redirect_options.merge!(:params => request.query_parameters) - flash.keep if respond_to?(:flash) - redirect_to redirect_options - end + force_ssl_redirect(host) end end end + + # Redirect the existing request to use the HTTPS protocol. + # + # ==== Parameters + # * <tt>host</tt> - Redirect to a different host name + def force_ssl_redirect(host = nil) + unless request.ssl? + redirect_options = {:protocol => 'https://', :status => :moved_permanently} + redirect_options.merge!(:host => host) if host + redirect_options.merge!(:params => request.query_parameters) + flash.keep if respond_to?(:flash) + redirect_to redirect_options + end + end end end diff --git a/actionpack/test/controller/force_ssl_test.rb b/actionpack/test/controller/force_ssl_test.rb index 5b423c8151..6758668b7a 100644 --- a/actionpack/test/controller/force_ssl_test.rb +++ b/actionpack/test/controller/force_ssl_test.rb @@ -49,6 +49,15 @@ class ForceSSLFlash < ForceSSLController end end +class RedirectToSSL < ForceSSLController + def banana + force_ssl_redirect || render(:text => 'monkey') + end + def cheeseburger + force_ssl_redirect('secure.cheeseburger.host') || render(:text => 'ihaz') + end +end + class ForceSSLControllerLevelTest < ActionController::TestCase tests ForceSSLControllerLevel @@ -149,3 +158,25 @@ class ForceSSLFlashTest < ActionController::TestCase assert_equal "hello", assigns["flashy"] end end + +class RedirectToSSLTest < ActionController::TestCase + tests RedirectToSSL + def test_banana_redirects_to_https_if_not_https + get :banana + assert_response 301 + assert_equal "https://test.host/redirect_to_ssl/banana", redirect_to_url + end + + def test_cheeseburgers_redirects_to_https_with_new_host_if_not_https + get :cheeseburger + assert_response 301 + assert_equal "https://secure.cheeseburger.host/redirect_to_ssl/cheeseburger", redirect_to_url + end + + def test_banana_does_not_redirect_if_already_https + request.env['HTTPS'] = 'on' + get :cheeseburger + assert_response 200 + assert_equal 'ihaz', response.body + end +end
\ No newline at end of file |