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
This commit is contained in:
Jeremy Kemper
2007-02-25 16:35:24 +00:00
parent c9770d8a5a
commit 781985f7f2
3 changed files with 19 additions and 2 deletions

View File

@@ -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]

View File

@@ -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

View File

@@ -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