Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 106 lines (68 sloc) 2.524 kb
35fd021 @ayanko Initial commit
ayanko authored
1 # rack_session_access
2
3 RackSessionAccess provides rack middleware for 'rack.session' environment management.
4
5 ## Problem
6
7 Acceptance testing assumes that you can't directly access application session.
8 For example if you use capybara with selenium webdriver you can't change some session value
9 because your test use browser that access application via backend server.
10
11 ## Solution
12
13 But if you still want to change session values?
14 Possible solution is inject into application some code that will manage session.
15 If you use rack based framework this gem does it!
16
17 ## Installation
18
19 gem install rack_session_access
20
21 ## Using with Rails3
22
23 Add to `Gemfile`:
24
25 gem 'rack_session_access'
26
27 Add to `config/application.rb`
28
29 module MyRailsApplication
30 class Application < Rails::Application
31 config.middleware.use RackSessionAccess::Middleware if Rails.env.test?
32 end
33 end
34
35 *Note* Ensure you include rack_session_access middleware only for test environment
36 otherwise you will have security issue.
37
38 If you use rspec you may prefer to inject middleware only for rspec tests:
39 Put into `spec/spec_helper`:
40
41 Rails.application.configure do
42 config.middleware.use RackSessionAccess::Middleware
43 end
44
45 ## Using with Sinatra
46
47 Add to your sinatra application:
48
49 class MySinatraApplication < Sinatra::Base
50 enable :sessions
51 use RackSessionAccess if environment == :test
52 ...
53 end
54
55 If you use rspec you may prefer to inject middleware only for rspec tests:
56 Put into `spec/spec_helper`:
57
58 MySinatraApplication.configure do
59 use RackSessionAccess::Middleware
60 end
61
62 ## Using with Rack builder
63
64 Rack::Builder.new do
65 ...
66 use Rack::Session::Cookie
67 use RackSessionAccess::Middleware
68 use MyRackApplication
69 end.to_app
70
71 ## Testing with Capybara
72
73 Add to `spec/spec_helper.rb`
74
75 require "rack_session_access/capybara"
76
77 And use `page.set_rack_session` to set your desired session data!
78
79 Example:
80
81 require 'spec_helper'
82
83 feature "My feature" do
84 background do
85 @user = Factory(:user, :email => 'jack@daniels.com')
86 end
87
88 scenario "logged in user access profile page" do
89 page.set_rack_session(:user_id => user.id)
90 page.visit "/profile"
91 page.should have_content("Hi, jack@daniels.com")
92 end
93 end
94
95
96 ## Notes
97
98 Thus we use marshalized data it's possible to set any ruby object into application session hash.
99
100 Enjoy!
101
102
103 ## References
104
105 * [capybara](https://github.com/jnicklas/capybara)
Something went wrong with that request. Please try again.