redis-session-store 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/AUTHORS.md +1 -0
- data/LICENSE +1 -1
- data/lib/redis-session-store.rb +14 -13
- data/spec/redis_session_store_spec.rb +56 -4
- data/spec/spec_helper.rb +1 -1
- data/spec/{fake_action_dispatch_session_abstract_store.rb → support.rb} +11 -0
- metadata +3 -3
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 6f3683b0cfb27fd4e78c5639909af2234bb5bbfb
         | 
| 4 | 
            +
              data.tar.gz: 269d6ab5865c26a9e7f6fb03a467c823569eda6d
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 896d45ca2a1aa9234bef55dae1170e265bc608a1d300aea69add6a7df1a0de580655017f90804fb4d36d82e5c4beccf8daaafb6a23a0d6702f3b43d0c82eab38
         | 
| 7 | 
            +
              data.tar.gz: 78bf2f881ecfc5da216115e34f71dbe117b91b99b35a9bd29e030d0bee9380816802b2691b4468539a651dd3ddcc6533bf69a96a9950b9eb8ac389445a586aa3
         | 
    
        data/AUTHORS.md
    CHANGED
    
    
    
        data/LICENSE
    CHANGED
    
    
    
        data/lib/redis-session-store.rb
    CHANGED
    
    | @@ -4,7 +4,7 @@ require 'redis' | |
| 4 4 | 
             
            # Redis session storage for Rails, and for Rails only. Derived from
         | 
| 5 5 | 
             
            # the MemCacheStore code, simply dropping in Redis instead.
         | 
| 6 6 | 
             
            class RedisSessionStore < ActionDispatch::Session::AbstractStore
         | 
| 7 | 
            -
              VERSION = '0. | 
| 7 | 
            +
              VERSION = '0.4.0'
         | 
| 8 8 |  | 
| 9 9 | 
             
              # ==== Options
         | 
| 10 10 | 
             
              # * +:key+ - Same as with the other cookie stores, key name
         | 
| @@ -36,11 +36,12 @@ class RedisSessionStore < ActionDispatch::Session::AbstractStore | |
| 36 36 | 
             
                @default_options.merge!(namespace: 'rack:session')
         | 
| 37 37 | 
             
                @default_options.merge!(redis_options)
         | 
| 38 38 | 
             
                @redis = Redis.new(redis_options)
         | 
| 39 | 
            +
                @raise_errors = !!options[:raise_errors]
         | 
| 39 40 | 
             
              end
         | 
| 40 41 |  | 
| 41 42 | 
             
              private
         | 
| 42 43 |  | 
| 43 | 
            -
              attr_reader :redis, :key, :default_options
         | 
| 44 | 
            +
              attr_reader :redis, :key, :default_options, :raise_errors
         | 
| 44 45 |  | 
| 45 46 | 
             
              def prefixed(sid)
         | 
| 46 47 | 
             
                "#{default_options[:key_prefix]}#{sid}"
         | 
| @@ -51,26 +52,29 @@ class RedisSessionStore < ActionDispatch::Session::AbstractStore | |
| 51 52 | 
             
                begin
         | 
| 52 53 | 
             
                  data = redis.get(prefixed(sid))
         | 
| 53 54 | 
             
                  session = data.nil? ? {} : Marshal.load(data)
         | 
| 54 | 
            -
                rescue Errno::ECONNREFUSED
         | 
| 55 | 
            +
                rescue Errno::ECONNREFUSED => e
         | 
| 56 | 
            +
                  raise e if raise_errors
         | 
| 55 57 | 
             
                  session = {}
         | 
| 56 58 | 
             
                end
         | 
| 57 59 | 
             
                [sid, session]
         | 
| 58 60 | 
             
              end
         | 
| 59 61 |  | 
| 60 | 
            -
              def set_session(env, sid, session_data, options)
         | 
| 61 | 
            -
                expiry | 
| 62 | 
            +
              def set_session(env, sid, session_data, options = nil)
         | 
| 63 | 
            +
                expiry = (options || env[ENV_SESSION_OPTIONS_KEY])[:expire_after]
         | 
| 62 64 | 
             
                if expiry
         | 
| 63 65 | 
             
                  redis.setex(prefixed(sid), expiry, Marshal.dump(session_data))
         | 
| 64 66 | 
             
                else
         | 
| 65 67 | 
             
                  redis.set(prefixed(sid), Marshal.dump(session_data))
         | 
| 66 68 | 
             
                end
         | 
| 67 69 | 
             
                return sid
         | 
| 68 | 
            -
              rescue Errno::ECONNREFUSED
         | 
| 70 | 
            +
              rescue Errno::ECONNREFUSED => e
         | 
| 71 | 
            +
                raise e if raise_errors
         | 
| 69 72 | 
             
                return false
         | 
| 70 73 | 
             
              end
         | 
| 71 74 |  | 
| 72 75 | 
             
              def destroy_session(env, sid, options)
         | 
| 73 76 | 
             
                redis.del(prefixed(sid))
         | 
| 77 | 
            +
                return nil if (options || {})[:drop]
         | 
| 74 78 | 
             
                generate_sid
         | 
| 75 79 | 
             
              end
         | 
| 76 80 |  | 
| @@ -78,12 +82,9 @@ class RedisSessionStore < ActionDispatch::Session::AbstractStore | |
| 78 82 | 
             
                if env['rack.request.cookie_hash'] && env['rack.request.cookie_hash'][key]
         | 
| 79 83 | 
             
                  redis.del(prefixed(env['rack.request.cookie_hash'][key]))
         | 
| 80 84 | 
             
                end
         | 
| 81 | 
            -
              rescue Errno::ECONNREFUSED
         | 
| 82 | 
            -
                if  | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
                else
         | 
| 86 | 
            -
                  warn('RedisSessionStore#destroy: Connection to redis refused')
         | 
| 87 | 
            -
                end
         | 
| 85 | 
            +
              rescue Errno::ECONNREFUSED => e
         | 
| 86 | 
            +
                raise e if raise_errors
         | 
| 87 | 
            +
                Rails.logger.warn("RedisSessionStore#destroy: #{e.message}")
         | 
| 88 | 
            +
                false
         | 
| 88 89 | 
             
              end
         | 
| 89 90 | 
             
            end
         | 
| @@ -108,12 +108,10 @@ describe RedisSessionStore do | |
| 108 108 | 
             
                let(:options)      { { expire_after: 123 } }
         | 
| 109 109 |  | 
| 110 110 | 
             
                context 'when successfully persisting the session' do
         | 
| 111 | 
            -
             | 
| 112 111 | 
             
                  it 'returns the session id' do
         | 
| 113 112 | 
             
                    store.send(:set_session, env, session_id, session_data, options)
         | 
| 114 113 | 
             
                      .should eq(session_id)
         | 
| 115 114 | 
             
                  end
         | 
| 116 | 
            -
             | 
| 117 115 | 
             
                end
         | 
| 118 116 |  | 
| 119 117 | 
             
                context 'when unsuccessfully persisting the session' do
         | 
| @@ -127,6 +125,33 @@ describe RedisSessionStore do | |
| 127 125 | 
             
                  end
         | 
| 128 126 | 
             
                end
         | 
| 129 127 |  | 
| 128 | 
            +
                context 'when no expire_after option is given' do
         | 
| 129 | 
            +
                  let(:options) { {} }
         | 
| 130 | 
            +
             | 
| 131 | 
            +
                  it 'sets the session value without expiry' do
         | 
| 132 | 
            +
                    store.send(:set_session, env, session_id, session_data, options)
         | 
| 133 | 
            +
                      .should eq(session_id)
         | 
| 134 | 
            +
                  end
         | 
| 135 | 
            +
                end
         | 
| 136 | 
            +
             | 
| 137 | 
            +
                context 'when redis is down' do
         | 
| 138 | 
            +
                  before { store.stub(:redis).and_raise(Errno::ECONNREFUSED) }
         | 
| 139 | 
            +
             | 
| 140 | 
            +
                  it 'returns false' do
         | 
| 141 | 
            +
                    store.send(:set_session, env, session_id, session_data, options)
         | 
| 142 | 
            +
                      .should eq(false)
         | 
| 143 | 
            +
                  end
         | 
| 144 | 
            +
             | 
| 145 | 
            +
                  context 'when :raise_errors option is truthy' do
         | 
| 146 | 
            +
                    let(:options) { { raise_errors: true } }
         | 
| 147 | 
            +
             | 
| 148 | 
            +
                    it 'explodes' do
         | 
| 149 | 
            +
                      expect do
         | 
| 150 | 
            +
                        store.send(:set_session, env, session_id, session_data, options)
         | 
| 151 | 
            +
                      end.to raise_error(Errno::ECONNREFUSED)
         | 
| 152 | 
            +
                    end
         | 
| 153 | 
            +
                  end
         | 
| 154 | 
            +
                end
         | 
| 130 155 | 
             
              end
         | 
| 131 156 |  | 
| 132 157 | 
             
              describe 'fetching a session' do
         | 
| @@ -146,13 +171,22 @@ describe RedisSessionStore do | |
| 146 171 | 
             
                end
         | 
| 147 172 |  | 
| 148 173 | 
             
                context 'when redis is down' do
         | 
| 174 | 
            +
                  before { store.stub(:redis).and_raise(Errno::ECONNREFUSED) }
         | 
| 149 175 |  | 
| 150 176 | 
             
                  it 'should return an empty session hash' do
         | 
| 151 | 
            -
                    store.stub(:redis).and_raise(Errno::ECONNREFUSED)
         | 
| 152 | 
            -
             | 
| 153 177 | 
             
                    expect(store.send(:get_session, double('env'), fake_key))
         | 
| 154 178 | 
             
                      .to eq([fake_key, {}])
         | 
| 155 179 | 
             
                  end
         | 
| 180 | 
            +
             | 
| 181 | 
            +
                  context 'when :raise_errors option is truthy' do
         | 
| 182 | 
            +
                    let(:options) { { raise_errors: true } }
         | 
| 183 | 
            +
             | 
| 184 | 
            +
                    it 'explodes' do
         | 
| 185 | 
            +
                      expect do
         | 
| 186 | 
            +
                        store.send(:get_session, double('env'), fake_key)
         | 
| 187 | 
            +
                      end.to raise_error(Errno::ECONNREFUSED)
         | 
| 188 | 
            +
                    end
         | 
| 189 | 
            +
                  end
         | 
| 156 190 | 
             
                end
         | 
| 157 191 | 
             
              end
         | 
| 158 192 |  | 
| @@ -174,6 +208,24 @@ describe RedisSessionStore do | |
| 174 208 |  | 
| 175 209 | 
             
                    store.send(:destroy, env)
         | 
| 176 210 | 
             
                  end
         | 
| 211 | 
            +
             | 
| 212 | 
            +
                  context 'when redis is down' do
         | 
| 213 | 
            +
                    before { store.stub(:redis).and_raise(Errno::ECONNREFUSED) }
         | 
| 214 | 
            +
             | 
| 215 | 
            +
                    it 'should return false' do
         | 
| 216 | 
            +
                      expect(store.send(:destroy, env)).to be_false
         | 
| 217 | 
            +
                    end
         | 
| 218 | 
            +
             | 
| 219 | 
            +
                    context 'when :raise_errors option is truthy' do
         | 
| 220 | 
            +
                      let(:options) { { raise_errors: true } }
         | 
| 221 | 
            +
             | 
| 222 | 
            +
                      it 'explodes' do
         | 
| 223 | 
            +
                        expect do
         | 
| 224 | 
            +
                          store.send(:destroy, env)
         | 
| 225 | 
            +
                        end.to raise_error(Errno::ECONNREFUSED)
         | 
| 226 | 
            +
                      end
         | 
| 227 | 
            +
                    end
         | 
| 228 | 
            +
                  end
         | 
| 177 229 | 
             
                end
         | 
| 178 230 |  | 
| 179 231 | 
             
                context 'when destroyed via #destroy_session' do
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    
| @@ -4,6 +4,7 @@ unless defined?(ActionDispatch::Session::AbstractStore) | |
| 4 4 | 
             
              module ActionDispatch # rubocop:disable Documentation
         | 
| 5 5 | 
             
                module Session
         | 
| 6 6 | 
             
                  class AbstractStore
         | 
| 7 | 
            +
                    ENV_SESSION_OPTIONS_KEY = 'rack.session.options'.freeze
         | 
| 7 8 | 
             
                    DEFAULT_OPTIONS = {
         | 
| 8 9 | 
             
                      key: '_session_id',
         | 
| 9 10 | 
             
                      path: '/',
         | 
| @@ -30,3 +31,13 @@ unless defined?(ActionDispatch::Session::AbstractStore) | |
| 30 31 | 
             
                end
         | 
| 31 32 | 
             
              end
         | 
| 32 33 | 
             
            end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
            unless defined?(Rails)
         | 
| 36 | 
            +
              require 'logger'
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              module Rails
         | 
| 39 | 
            +
                def self.logger
         | 
| 40 | 
            +
                  @logger ||= Logger.new('/dev/null')
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
              end
         | 
| 43 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: redis-session-store
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.4.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Mathias Meyer
         | 
| @@ -118,9 +118,9 @@ files: | |
| 118 118 | 
             
            - Rakefile
         | 
| 119 119 | 
             
            - lib/redis-session-store.rb
         | 
| 120 120 | 
             
            - redis-session-store.gemspec
         | 
| 121 | 
            -
            - spec/fake_action_dispatch_session_abstract_store.rb
         | 
| 122 121 | 
             
            - spec/redis_session_store_spec.rb
         | 
| 123 122 | 
             
            - spec/spec_helper.rb
         | 
| 123 | 
            +
            - spec/support.rb
         | 
| 124 124 | 
             
            homepage: https://github.com/roidrage/redis-session-store
         | 
| 125 125 | 
             
            licenses:
         | 
| 126 126 | 
             
            - MIT
         | 
| @@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement | |
| 141 141 | 
             
                  version: '0'
         | 
| 142 142 | 
             
            requirements: []
         | 
| 143 143 | 
             
            rubyforge_project: 
         | 
| 144 | 
            -
            rubygems_version: 2.0. | 
| 144 | 
            +
            rubygems_version: 2.0.14
         | 
| 145 145 | 
             
            signing_key: 
         | 
| 146 146 | 
             
            specification_version: 4
         | 
| 147 147 | 
             
            summary: A drop-in replacement for e.g. MemCacheStore to store Rails sessions (and
         |