Skip to content
This repository

Rack middleware that provides access to rack.session environment

branch: master

Merge pull request #11 from arthurnn/patch-1

Small fixes on
latest commit 15cc5ae467
Andriy Yanko ayanko authored January 28, 2014
Octocat-spinner-32 apps Added tests against rails4 March 07, 2013
Octocat-spinner-32 lib v0.1.1 March 07, 2013
Octocat-spinner-32 spec Fixed specs February 18, 2013
Octocat-spinner-32 .gitignore Added tests against rails4 March 07, 2013
Octocat-spinner-32 Gemfile Update versions in Gemfile March 07, 2013
Octocat-spinner-32 Gemfile.rails4 Added tests against rails4 March 07, 2013
Octocat-spinner-32 LICENSE Added LICENSE March 07, 2013
Octocat-spinner-32 Small fixes on January 27, 2014
Octocat-spinner-32 Rakefile Initial commit January 07, 2012
Octocat-spinner-32 rack_session_access.gemspec Initial commit January 07, 2012


RackSessionAccess provides rack middleware for 'rack.session' environment management.


Acceptance testing assumes that you can't directly access application session. For example if you use capybara with selenium webdriver you can't change some session value because your test use browser that access application via backend server.


But if you still want to change session values? Possible solution is inject into application some code that will manage session. If you use rack based framework this gem does it!


gem install rack_session_access

Using with Rails 3 or 4

Add to Gemfile:

gem 'rack_session_access'

Add RackSessionAccess middleware to rails middleware stack. Add the following inconfig/environments/test.rb:

[MyRailsApp]::Application.configure do |config|
  # Access to rack session
  config.middleware.use RackSessionAccess::Middleware

Note Ensure you include rack_session_access middleware only for test environment otherwise you will have security issue.

Using with Sinatra

Add to your sinatra application:

class MySinatraApplication < Sinatra::Base
  enable :sessions
  use RackSessionAccess if environment == :test

If you use rspec you may prefer to inject middleware only for rspec tests: Put into spec/spec_helper:

  MySinatraApplication.configure do |app|
    app.use RackSessionAccess::Middleware

Using with Rack builder do
  use Rack::Session::Cookie
  use RackSessionAccess::Middleware
  use MyRackApplication

Testing with Capybara

Add to spec/spec_helper.rb

require "rack_session_access/capybara"


  • page.set_rack_session to set your desired session data
  • page.get_rack_session to obtain your application session data
  • page.get_rack_session_key to obtain certain key if your application session data


require 'spec_helper'

feature "My feature" do
  background do
    @user = Factory(:user, :email => '')

  scenario "logged in user access profile page" do
    page.set_rack_session(:user_id =>
    page.visit "/profile"
    page.should have_content("Hi,")

  scenario "visit landing page" do
    page.visit "/landing?ref=123"
    page.get_rack_session_key('ref').should == "123"

Authlogic integration

page.set_rack_session("user_credentials" => @user.persistence_token)

Devise integration

page.set_rack_session("warden.user.user.key" => User.serialize_into_session(@user).unshift("User"))


Thus we use marshalized data it's possible to set any ruby object into application session hash.


Running rack_session_access tests

Against Rails3, Sinatra, rack applications

BUNDLE_GEMFILE=Gemfile bundle install
BUNDLE_GEMFILE=Gemfile bundle exec rspec -fs -c spec/

Against Rails4, Sinatra, rack applications

BUNDLE_GEMFILE=Gemfile.rails4 bundle install
BUNDLE_GEMFILE=Gemfile.rails4 bundle exec rspec -fs -c spec/


Andriy Yanko



Something went wrong with that request. Please try again.