diff --git a/actionpack/lib/action_controller/session/cookie_store.rb b/actionpack/lib/action_controller/session/cookie_store.rb index a2543c1..eed437a 100644 --- a/actionpack/lib/action_controller/session/cookie_store.rb +++ b/actionpack/lib/action_controller/session/cookie_store.rb @@ -97,21 +97,24 @@ module ActionController if !session_data.is_a?(AbstractStore::SessionHash) || session_data.send(:loaded?) || options[:expire_after] session_data.send(:load!) if session_data.is_a?(AbstractStore::SessionHash) && !session_data.send(:loaded?) - session_data = marshal(session_data.to_hash) - raise CookieOverflow if session_data.size > MAX + unless session_data.keys.reject {|k| k == :session_id }.empty? + marshaled_session_data = marshal(session_data.to_hash) - cookie = Hash.new - cookie[:value] = session_data - unless options[:expire_after].nil? - cookie[:expires] = Time.now + options[:expire_after] - end + raise CookieOverflow if marshaled_session_data.size > MAX - cookie = build_cookie(@key, cookie.merge(options)) - unless headers[HTTP_SET_COOKIE].blank? - headers[HTTP_SET_COOKIE] << "\n#{cookie}" - else - headers[HTTP_SET_COOKIE] = cookie + cookie = Hash.new + cookie[:value] = marshaled_session_data + unless options[:expire_after].nil? + cookie[:expires] = Time.now + options[:expire_after] + end + + cookie = build_cookie(@key, cookie.merge(options)) + unless headers[HTTP_SET_COOKIE].blank? + headers[HTTP_SET_COOKIE] << "\n#{cookie}" + else + headers[HTTP_SET_COOKIE] = cookie + end end end diff --git a/actionpack/test/controller/session/cookie_store_test.rb b/actionpack/test/controller/session/cookie_store_test.rb index 5ef8eec..ff96b90 100644 --- a/actionpack/test/controller/session/cookie_store_test.rb +++ b/actionpack/test/controller/session/cookie_store_test.rb @@ -17,6 +17,11 @@ class CookieStoreTest < ActionController::IntegrationTest head :ok end + def session_access_buy_empty + session[:value] + render :text => '' + end + def persistent_session_id render :text => session[:session_id] end @@ -110,6 +115,14 @@ class CookieStoreTest < ActionController::IntegrationTest end end + def test_empty_session_shouldnt_generate_cookie + with_test_route_set do + get '/session_access_buy_empty' + assert_response :success + assert_nil headers['Set-Cookie'] + end + end + def test_getting_session_id with_test_route_set do cookies[SessionKey] = SignedBar