From 781985f7f229eb665b3eed693eb9642caebc44a0 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 25 Feb 2007 16:35:24 +0000 Subject: Cookie session store: empty and unchanged sessions don't write a cookie. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6226 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/session/cookie_store.rb | 3 ++- actionpack/test/controller/session/cookie_store_test.rb | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 20258b536e..396fa98a76 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Cookie session store: empty and unchanged sessions don't write a cookie. [Jeremy Kemper] + * Added helper(:all) as a way to include all helpers from app/helpers/**/*.rb in ApplicationController [DHH] * Integration tests: introduce methods for other HTTP methods. #6353 [caboose] diff --git a/actionpack/lib/action_controller/session/cookie_store.rb b/actionpack/lib/action_controller/session/cookie_store.rb index 8763a4933a..1754eb34b1 100644 --- a/actionpack/lib/action_controller/session/cookie_store.rb +++ b/actionpack/lib/action_controller/session/cookie_store.rb @@ -65,7 +65,7 @@ class CGI::Session::CookieStore # Write the session data cookie if it was loaded and has changed. def close - if defined? @data + if defined?(@data) && !@data.blank? updated = marshal(@data) raise CookieOverflow if updated.size > MAX write_cookie('value' => updated) unless updated == @original @@ -74,6 +74,7 @@ class CGI::Session::CookieStore # Delete the session data by setting an expired cookie with no data. def delete + @data = nil write_cookie('value' => '', 'expires' => 1.year.ago) end diff --git a/actionpack/test/controller/session/cookie_store_test.rb b/actionpack/test/controller/session/cookie_store_test.rb index 7a12fb4650..353785825b 100755 --- a/actionpack/test/controller/session/cookie_store_test.rb +++ b/actionpack/test/controller/session/cookie_store_test.rb @@ -68,11 +68,20 @@ class CookieStoreTest < Test::Unit::TestCase end end + def test_close_doesnt_write_cookie_if_data_is_blank + new_session do |session| + assert_nil session.cgi.output_cookies, session.cgi.output_cookies.inspect + session.close + assert_nil session.cgi.output_cookies, session.cgi.output_cookies.inspect + end + end + def test_close_doesnt_write_cookie_if_data_is_unchanged set_cookie! Cookies::TYPICAL.first new_session do |session| assert_nil session.cgi.output_cookies, session.cgi.output_cookies.inspect session['user_id'] = session['user_id'] + session.close assert_nil session.cgi.output_cookies, session.cgi.output_cookies.inspect end end @@ -91,7 +100,7 @@ class CookieStoreTest < Test::Unit::TestCase end end - def test_delete_writes_expired_empty_cookie + def test_delete_writes_expired_empty_cookie_and_sets_data_to_nil set_cookie! Cookies::TYPICAL.first new_session do |session| assert_nil session.cgi.output_cookies, session.cgi.output_cookies.inspect @@ -100,6 +109,11 @@ class CookieStoreTest < Test::Unit::TestCase cookie = session.cgi.output_cookies.first assert_equal ['_myapp_session', [], 1.year.ago.to_date], [cookie.name, cookie.value, cookie.expires.to_date] + + # @data is set to nil so #close doesn't send another cookie. + session.close + assert_equal ['_myapp_session', [], 1.year.ago.to_date], + [cookie.name, cookie.value, cookie.expires.to_date] end end -- cgit v1.2.3