aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/session/abstract_store_test.rb
blob: c9ce5cad4291942db29203c633ae41d3a5ea0335 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 find_session(env, sid)
          sid ||= 1
          session = @sessions[sid] ||= {}
          [sid, session]
        end

        def write_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 ActionDispatch::Request.new @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 ActionDispatch::Request.new @env
        session['foo'] = 'bar'

        as.call(@env)
        session1 = Request::Session.find ActionDispatch::Request.new @env

        assert_not_equal session, session1
        assert_equal session.to_hash, session1.to_hash
      end

      def test_previous_session_has_indifferent_access
        env = {}
        as = MemoryStore.new app
        as.call(env)

        assert @env
        session = Request::Session.find ActionDispatch::Request.new @env
        session[:foo] = { bar: "baz" }

        as.call(@env)
        session = Request::Session.find ActionDispatch::Request.new @env

        assert_equal session[:foo][:bar], "baz"
        assert_equal session[:foo]["bar"], "baz"
      end

      private
      def app(&block)
        @env = nil
        lambda { |env| @env = env }
      end
    end
  end
end