Skip to content

Instantly share code, notes, and snippets.

@kares
Created October 26, 2010 09:39
Show Gist options
  • Save kares/646602 to your computer and use it in GitHub Desktop.
Save kares/646602 to your computer and use it in GitHub Desktop.
making sessions work in integration tests (broken since Rails 2.3.8)
# session is not preserved between requests in integration tests (since 2.3.8)
# https://rails.lighthouseapp.com/projects/8994/tickets/4941-session-is-not-preserved-between-requests-in-integration-tests
# this monkey-fix deals with the issue and is based on the provided patch from
# the above ticket. require in your test_helper or integration_test_helper ...
require 'action_controller/session/abstract_store'
module ActionController
module Session
class AbstractStore
unless instance_methods.include?('prepare!')
def prepare!(env) # prepare! not-available in Rails <= 2.3.8
session = SessionHash.new(self, env)
env[ENV_SESSION_KEY] = session
env[ENV_SESSION_OPTIONS_KEY] = @default_options.dup
end
end
def call(env)
prepare!(env)
response = @app.call(env)
session_data = env[ENV_SESSION_KEY]
options = env[ENV_SESSION_OPTIONS_KEY]
if !session_data.is_a?(AbstractStore::SessionHash) || session_data.send(:loaded?) || options[:expire_after]
request = ActionController::Request.new(env)
return response if (options[:secure] && !request.ssl?)
session_data.send(:load!) if session_data.is_a?(AbstractStore::SessionHash) && !session_data.send(:loaded?)
sid = options[:id] || generate_sid
unless set_session(env, sid, session_data.to_hash)
return response
end
request_cookies = env["rack.request.cookie_hash"]
if (request_cookies.nil? || request_cookies[@key] != sid) || options[:expire_after]
cookie = { :value => sid }
Rack::Utils.set_cookie_header!(response[1], @key, cookie.merge(options))
end
end
response
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment