From a0f281985c87022087500e80acf69df1af926d03 Mon Sep 17 00:00:00 2001 From: Michael Lovitt Date: Tue, 13 Jul 2010 23:23:12 -0400 Subject: [PATCH] The expire_after session option shouldn't cause sessions to be eagerly loaded. [#4450 state:resolved] --- .../middleware/session/abstract_store.rb | 2 +- .../test/dispatch/session/cookie_store_test.rb | 28 ++++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb index 64f4d1d..40de8a7 100644 --- a/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb +++ b/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb @@ -151,7 +151,7 @@ module ActionDispatch session_data = env[ENV_SESSION_KEY] options = env[ENV_SESSION_OPTIONS_KEY] - if !session_data.is_a?(AbstractStore::SessionHash) || session_data.loaded? || options[:expire_after] + if !session_data.is_a?(AbstractStore::SessionHash) || session_data.loaded? session_data.send(:load!) if session_data.is_a?(AbstractStore::SessionHash) && !session_data.loaded? sid = options[:id] || generate_sid diff --git a/actionpack/test/dispatch/session/cookie_store_test.rb b/actionpack/test/dispatch/session/cookie_store_test.rb index f0e01bf..33b5106 100644 --- a/actionpack/test/dispatch/session/cookie_store_test.rb +++ b/actionpack/test/dispatch/session/cookie_store_test.rb @@ -214,30 +214,36 @@ class CookieStoreTest < ActionController::IntegrationTest def test_session_store_with_expire_after with_test_route_set(:expire_after => 5.hours) do - # First request accesses the session + get '/get_session_value' + assert_response :success + assert_equal 'foo: nil', response.body + assert_nil headers['Set-Cookie'], "Should only create session on write, not read" + + cookies[SessionKey] = SignedBar + time = Time.local(2008, 4, 24) Time.stubs(:now).returns(time) expected_expiry = (time + 5.hours).gmtime.strftime("%a, %d-%b-%Y %H:%M:%S GMT") - cookies[SessionKey] = SignedBar - get '/set_session_value' assert_response :success - cookie_body = response.body - assert_equal "_myapp_session=#{cookie_body}; path=/; expires=#{expected_expiry}; HttpOnly", - headers['Set-Cookie'] + assert_equal "_myapp_session=#{cookie_body}; path=/; expires=#{expected_expiry}; HttpOnly", headers['Set-Cookie'], + "Session was written to; session cookie should be sent with appropriate expiration date" - # Second request does not access the session + get '/no_session_access' + assert_response :success + assert_nil headers['Set-Cookie'], + "Session was not accessed; no session cookie should be sent" + time = Time.local(2008, 4, 25) Time.stubs(:now).returns(time) expected_expiry = (time + 5.hours).gmtime.strftime("%a, %d-%b-%Y %H:%M:%S GMT") - get '/no_session_access' + get '/get_session_value' assert_response :success - - assert_equal "_myapp_session=#{cookie_body}; path=/; expires=#{expected_expiry}; HttpOnly", - headers['Set-Cookie'] + assert_equal "_myapp_session=#{cookie_body}; path=/; expires=#{expected_expiry}; HttpOnly", headers['Set-Cookie'], + "Session was accessed; session cookie should be sent with appropriate expiration date" end end -- 1.7.1