diff --git a/lib/redis-session-store.rb b/lib/redis-session-store.rb index 9e3514a..187ccb1 100644 --- a/lib/redis-session-store.rb +++ b/lib/redis-session-store.rb @@ -4,14 +4,6 @@ # the MemCacheStore code, simply dropping in Redis instead. class RedisSessionStore < ActionDispatch::Session::AbstractSecureStore VERSION = '0.11.5'.freeze - # Rails 3.1 and beyond defines the constant elsewhere - unless defined?(ENV_SESSION_OPTIONS_KEY) - ENV_SESSION_OPTIONS_KEY = if Rack.release.split('.').first.to_i > 1 - Rack::RACK_SESSION_OPTIONS - else - Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY - end - end USE_INDIFFERENT_ACCESS = defined?(ActiveSupport).freeze # ==== Options @@ -156,7 +148,7 @@ def set_session(env, sid, session_data, options = nil) alias write_session set_session def get_expiry(env, options) - session_storage_options = options || env.fetch(ENV_SESSION_OPTIONS_KEY, {}) + session_storage_options = options || env.fetch(Rack::RACK_SESSION_OPTIONS, {}) session_storage_options[:ttl] || session_storage_options[:expire_after] end diff --git a/spec/redis_session_store_spec.rb b/spec/redis_session_store_spec.rb index 51dee2f..35a2770 100644 --- a/spec/redis_session_store_spec.rb +++ b/spec/redis_session_store_spec.rb @@ -151,71 +151,6 @@ end end - describe 'rack 1.45 compatibility' do - # Rack 1.45 (which Rails 3.2.x depends on) uses the return value of - # set_session to set the cookie value. See: - # https://github.com/rack/rack/blob/1.4.5/lib/rack/session/abstract/id.rb - - let(:env) { double('env') } - let(:session_id) { Rack::Session::SessionId.new('12 345') } - let(:session_data) { double('session_data') } - let(:options) { { expire_after: 123 } } - - context 'when successfully persisting the session' do - it 'returns the session id' do - expect(store.send(:set_session, env, session_id, session_data, options)) - .to eq(session_id) - end - end - - context 'when unsuccessfully persisting the session' do - before do - allow(store).to receive(:redis).and_raise(Redis::CannotConnectError) - end - - it 'returns false' do - expect(store.send(:set_session, env, session_id, session_data, options)) - .to eq(false) - end - end - - context 'when no expire_after option is given' do - let(:options) { {} } - - it 'sets the session value without expiry' do - expect(store.send(:set_session, env, session_id, session_data, options)) - .to eq(session_id) - end - end - - context 'when redis is down' do - before do - allow(store).to receive(:redis).and_raise(Redis::CannotConnectError) - store.on_redis_down = ->(*_a) { @redis_down_handled = true } - end - - it 'returns false' do - expect(store.send(:set_session, env, session_id, session_data, options)) - .to eq(false) - end - - it 'calls the on_redis_down handler' do - store.send(:set_session, env, session_id, session_data, options) - expect(@redis_down_handled).to eq(true) - end - - context 'when :on_redis_down re-raises' do - before { store.on_redis_down = ->(e, *) { raise e } } - - it 'explodes' do - expect do - store.send(:set_session, env, session_id, session_data, options) - end.to raise_error(Redis::CannotConnectError) - end - end - end - end - describe 'checking for session existence' do let(:public_id) { 'foo' } let(:session_id) { Rack::Session::SessionId.new(public_id) } diff --git a/spec/support.rb b/spec/support.rb index 1d009e6..cfdd2e6 100644 --- a/spec/support.rb +++ b/spec/support.rb @@ -1,86 +1,5 @@ -unless defined?(Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY) - module Rack - module Session - module Abstract - ENV_SESSION_OPTIONS_KEY = 'rack.session.options'.freeze - end - end - end -end -unless defined?(Rack::Session::SessionId) - module Rack - module Session - class SessionId - ID_VERSION = 2 +require "rack/session/abstract/id" - attr_reader :public_id - - def initialize(public_id) - @public_id = public_id - end - - alias to_s public_id - - def empty? - false - end - - def inspect - public_id.inspect - end - - def private_id - "#{ID_VERSION}::#{hash_sid(public_id)}" - end - - private - - def hash_sid(value) - "test_hash_from:#{value}" - end - end - end - end -end - -unless defined?(ActionDispatch::Session::AbstractSecureStore) - module ActionDispatch - module Session - class AbstractSecureStore - ENV_SESSION_OPTIONS_KEY = 'rack.session.options'.freeze - DEFAULT_OPTIONS = { - key: '_session_id', - path: '/', - domain: nil, - expire_after: nil, - secure: false, - httponly: true, - cookie_only: true - }.freeze - - def initialize(app, options = {}) - @app = app - @default_options = DEFAULT_OPTIONS.dup.merge(options) - @key = @default_options[:key] - @cookie_only = @default_options[:cookie_only] - end - - private - - def generate_sid - Rack::Session::SessionId.new(rand(999..9999).to_s(16)) - end - end - end - end -end - -unless defined?(Rails) - require 'logger' - - module Rails - def self.logger - @logger ||= Logger.new('/dev/null') - end - end -end +require "active_support/message_encryptor" +require "active_support/core_ext/hash/indifferent_access" +require "action_dispatch/middleware/session/abstract_store"