Skip to content
Browse files

testing session store behavior

  • Loading branch information...
1 parent dc2352c commit 5b360dbb9b968ea1389a278a8ae10328fb273ea2 @tenderlove tenderlove committed May 2, 2012
View
26 actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
@@ -68,16 +68,10 @@ class AbstractStore < Rack::Session::Abstract::ID
include Compatibility
include StaleSessionCheck
- ENV_SESSION_KEY = Rack::Session::Abstract::ENV_SESSION_KEY # :nodoc:
- ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY # :nodoc:
-
private
def prepare_session(env)
- session_was = env[ENV_SESSION_KEY]
- env[ENV_SESSION_KEY] = Request::Session.new(self, env)
- env[ENV_SESSION_OPTIONS_KEY] = Request::Session::Options.new(self, env, @default_options)
- env[ENV_SESSION_KEY].merge! session_was if session_was
+ Request::Session.create(self, env, @default_options)
end
def loaded_session?(session)
@@ -97,6 +91,19 @@ class Session # :nodoc:
ENV_SESSION_KEY = Rack::Session::Abstract::ENV_SESSION_KEY # :nodoc:
ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY # :nodoc:
+ def self.create(store, env, default_options)
+ session_was = env[ENV_SESSION_KEY]
+ session = Request::Session.new(store, env)
+ env[ENV_SESSION_KEY] = session
+ env[ENV_SESSION_OPTIONS_KEY] = Request::Session::Options.new(store, env, default_options)
+ env[ENV_SESSION_KEY].merge! session_was if session_was
+ session
+ end
+
+ def self.find(env)
+ env[ENV_SESSION_KEY]
+ end
+
class Options #:nodoc:
def initialize(by, env, default_options)
@by = by
@@ -195,6 +202,11 @@ def empty?
@delegate.empty?
end
+ def merge!(other)
+ load_for_write!
+ @delegate.merge!(other)
+ end
+
private
def load_for_read!
View
48 actionpack/test/dispatch/request/session_test.rb
@@ -0,0 +1,48 @@
+require 'abstract_unit'
+require 'action_dispatch/middleware/session/abstract_store'
+
+module ActionDispatch
+ class Request
+ class SessionTest < ActiveSupport::TestCase
+ def test_create_adds_itself_to_env
+ env = {}
+ s = Session.create(store, env, {})
+ assert_equal s, env[Rack::Session::Abstract::ENV_SESSION_KEY]
+ end
+
+ def test_to_hash
+ env = {}
+ s = Session.create(store, env, {})
+ s['foo'] = 'bar'
+ assert_equal 'bar', s['foo']
+ assert_equal({'foo' => 'bar'}, s.to_hash)
+ end
+
+ def test_create_merges_old
+ env = {}
+ s = Session.create(store, env, {})
+ s['foo'] = 'bar'
+
+ s1 = Session.create(store, env, {})
+ refute_equal s, s1
+ assert_equal 'bar', s1['foo']
+ end
+
+ def test_find
+ env = {}
+ assert_nil Session.find(env)
+
+ s = Session.create(store, env, {})
+ assert_equal s, Session.find(env)
+ end
+
+ private
+ def store
+ Class.new {
+ def load_session(env); [1, {}]; end
+ def session_exists?(env); true; end
+ }.new
+ end
+ end
+ end
+end
View
56 actionpack/test/dispatch/session/abstract_store_test.rb
@@ -0,0 +1,56 @@
+require 'abstract_unit'
+require 'action_dispatch/middleware/session/abstract_store'
+
+module ActionDispatch
+ module Session
+ class AbstractStoreTest < ActiveSupport::TestCase
+ class MemoryStore < AbstractStore
+ def initialize(app)
+ @sessions = {}
+ super
+ end
+
+ def get_session(env, sid)
+ sid ||= 1
+ session = @sessions[sid] ||= {}
+ [sid, session]
+ end
+
+ def set_session(env, sid, session, options)
+ @sessions[sid] = session
+ end
+ end
+
+ def test_session_is_set
+ env = {}
+ as = MemoryStore.new app
+ as.call(env)
+
+ assert @env
+ assert Request::Session.find @env
+ end
+
+ def test_new_session_object_is_merged_with_old
+ env = {}
+ as = MemoryStore.new app
+ as.call(env)
+
+ assert @env
+ session = Request::Session.find @env
+ session['foo'] = 'bar'
+
+ as.call(@env)
+ session1 = Request::Session.find @env
+
+ refute_equal session, session1
+ assert_equal session.to_hash, session1.to_hash
+ end
+
+ private
+ def app(&block)
+ @env = nil
+ lambda { |env| @env = env }
+ end
+ end
+ end
+end

0 comments on commit 5b360db

Please sign in to comment.
Something went wrong with that request. Please try again.