From 5fdd1d8cab05734bef75222196b7d69bf75abe6d Mon Sep 17 00:00:00 2001 From: Michael King Date: Thu, 13 Nov 2008 14:12:16 -0600 Subject: [PATCH] added functionality and tests for discarding sessions --- actionpack/lib/action_controller/base.rb | 6 +++++ actionpack/lib/action_controller/cgi_process.rb | 5 ++++ actionpack/lib/action_controller/test_process.rb | 4 +++ .../test/controller/session_management_test.rb | 22 ++++++++++++++++++++ 4 files changed, 37 insertions(+), 0 deletions(-) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 43f6c1b..589322a 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -1167,6 +1167,12 @@ module ActionController #:nodoc: response.session = @_session end + # Discards the session by clearing out all the objects stored within and initializing a new hash object. + def discard_session #:doc: + request.discard_session + @_session = request.session + response.session = @_session + end private def render_for_file(template_path, status = nil, layout = nil, locals = {}) #:nodoc: diff --git a/actionpack/lib/action_controller/cgi_process.rb b/actionpack/lib/action_controller/cgi_process.rb index fabacd9..f39c948 100644 --- a/actionpack/lib/action_controller/cgi_process.rb +++ b/actionpack/lib/action_controller/cgi_process.rb @@ -107,6 +107,11 @@ module ActionController #:nodoc: @session = new_session end + def discard_session + @session.delete if defined?(@session) && @session.is_a?(CGI::Session) + @session = Hash.new + end + def method_missing(method_id, *arguments) @cgi.__send__(method_id, *arguments) rescue super end diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index 1e3a646..4b13500 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -48,6 +48,10 @@ module ActionController #:nodoc: @session = TestSession.new end + def discard_session + @session = Hash.new + end + # Wraps raw_post in a StringIO. def body_stream #:nodoc: StringIO.new(raw_post) diff --git a/actionpack/test/controller/session_management_test.rb b/actionpack/test/controller/session_management_test.rb index 592b0b5..c04ef5f 100644 --- a/actionpack/test/controller/session_management_test.rb +++ b/actionpack/test/controller/session_management_test.rb @@ -13,6 +13,17 @@ class SessionManagementTest < Test::Unit::TestCase end end + class SessionOnController < ActionController::Base + def show + render :text => "done" + end + + def tell + discard_session + render :text => "done" + end + end + class SessionOffOnController < ActionController::Base session :off session :on, :only => :tell @@ -175,4 +186,15 @@ class SessionManagementTest < Test::Unit::TestCase get :tell assert @controller.session_enabled? end + + def test_session_is_discarded + @controller = SessionOnController.new + get :show +# assert_instance_of Hash, @request.session_options + assert_instance_of ActionController::TestSession, @request.session +# assert_equal false, @request.session_options[:disabled] + get :tell +# assert_equal false, @request.session_options[:disabled] + assert_instance_of Hash, @request.session, 'session wrong type' + end end -- 1.5.6.3