rack_session_manipulation 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/README.md +80 -4
- data/lib/rack_session_manipulation/capybara.rb +4 -1
- data/lib/rack_session_manipulation/middleware.rb +64 -37
- data/lib/rack_session_manipulation/version.rb +1 -1
- 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: 61e70f7fa7fadbe40c3e4bf885a1a998fced1c10
|
4
|
+
data.tar.gz: 8c9736d1644eae68a05b9b4655a539bd4235543b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8377740e66ebb1fb1b921472d91a18ce41640d9178eaef77153ef4c0a9ad46d05b2b792db897bde45caa125ffbb0bf46f366ac28b4dd2e18b4f4fdb11636697
|
7
|
+
data.tar.gz: f7c2522ebd7c448f347b082d11529a49583f2d7a02fbcbb80e323dc0f35dc5e402202be13c6d120d4708a2fda5e758690132586f31b039bf944e3913e7b233aa
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -8,9 +8,10 @@
|
|
8
8
|
[![Yard Docs](http://img.shields.io/badge/yard-docs-green.svg)](http://www.rubydoc.info/gems/rack_session_manipulation)
|
9
9
|
|
10
10
|
RackSessionManipulation is rack middleware designed to expose internal session
|
11
|
-
information to be read and modified from within tests
|
12
|
-
assist in treating routes as modular units of
|
13
|
-
isolation without the dependency of the rest of your
|
11
|
+
information to be read and modified from within tests (primarily for use within
|
12
|
+
Capybara). This is intended to assist in treating routes as modular units of
|
13
|
+
work that can be tested in isolation without the dependency of the rest of your
|
14
|
+
application.
|
14
15
|
|
15
16
|
This is not intended to replace full set of 'behavior' tests, but can be used
|
16
17
|
to speed up getting to a 'Given' state in place of helpers that previously
|
@@ -37,6 +38,18 @@ gem solved the updating of state using multiple requests, and marshalled
|
|
37
38
|
objects as well as unecessary use of builder. The approach seemed more
|
38
39
|
complicated, slower and more dangerous than necessary.
|
39
40
|
|
41
|
+
If you are looking to test a specific session state from a pure rack/test unit
|
42
|
+
perspective, you don't need this gem. The native rack/test HTTP methods provide
|
43
|
+
the mechanism to directly set and test state like so:
|
44
|
+
|
45
|
+
```
|
46
|
+
session_data = { 'user_id' => 5 }
|
47
|
+
get '/test_path', {}, { 'rack.session' => session_data }
|
48
|
+
```
|
49
|
+
|
50
|
+
The above will make a get request to '/test_path' with the session state
|
51
|
+
contained within the `session_data` variable.
|
52
|
+
|
40
53
|
## Installation
|
41
54
|
|
42
55
|
Add this line to your application's Gemfile:
|
@@ -55,6 +68,64 @@ Or install it yourself as:
|
|
55
68
|
|
56
69
|
## Usage
|
57
70
|
|
71
|
+
The middleware is useful on it's own, but this gem's primary power comes from
|
72
|
+
it's integration with Capybara. On it's own or with capybara, the first step is
|
73
|
+
to inject the middleware. This varies from framework to framework, the most
|
74
|
+
common examples I've provided below.
|
75
|
+
|
76
|
+
### Rack
|
77
|
+
|
78
|
+
The following is a super simple rack app that at a minimum can be used as a
|
79
|
+
fallback definition for any Ruby web application by replacing the `YourApp`
|
80
|
+
constant with your application (generally this is what is passed to `run` in
|
81
|
+
your 'config.ru' file. At the same time you'll want to replace the value in
|
82
|
+
your 'config.ru' file with the constant `YourAwesomeRackStack` (though you can
|
83
|
+
change that as you'd like).
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
YourAwesomeRackStack = Rack::Builder.new do
|
87
|
+
# To access sessions they must first exist. Configure the appropriate Rack
|
88
|
+
# session mechanism prior to using this middleware.
|
89
|
+
use Rack::Session::Cookie, secret: 'some secret string'
|
90
|
+
|
91
|
+
# Add this middleware to the stack
|
92
|
+
use RackSessionManipulation::Middleware
|
93
|
+
|
94
|
+
# Finally, running your own App at the top of the stack
|
95
|
+
run YourApp
|
96
|
+
end.to_app
|
97
|
+
```
|
98
|
+
|
99
|
+
### Sinatra
|
100
|
+
|
101
|
+
TODO
|
102
|
+
|
103
|
+
### Rails
|
104
|
+
|
105
|
+
TODO
|
106
|
+
|
107
|
+
### Capybara Usage
|
108
|
+
|
109
|
+
After setting up the middleware and Capybara as you would normally, the only
|
110
|
+
additional setup is requiring the Capybara helpers (you generally want to add
|
111
|
+
this line after the 'capybara' require):
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
require 'rack_session_access/capybara'
|
115
|
+
```
|
116
|
+
|
117
|
+
After that anywhere you are using Capybara you will have three additional
|
118
|
+
methods available to you:
|
119
|
+
|
120
|
+
* reset_session: Clear the entire contents of the existing session.
|
121
|
+
* session: Get a hash representing the current session state.
|
122
|
+
* session=: additional data into the current session.
|
123
|
+
|
124
|
+
When setting the session, you'll want to provide a hash. The values at the top
|
125
|
+
level keys of the hash will replace the equivalent keys in the current session.
|
126
|
+
Any keys that existed before but aren't getting set will continue to exist. It
|
127
|
+
works like a simple Hash merge. The other methods are fairly self explanatory.
|
128
|
+
|
58
129
|
## Contributing
|
59
130
|
|
60
131
|
I welcome new ideas, bug fixes and comments from anyone and strive to take no
|
@@ -72,10 +143,15 @@ contribution guide:
|
|
72
143
|
|
73
144
|
1. Review the [Contributor Code of Conduct][2]
|
74
145
|
2. Fork it ( https://github.com/sstelfox/rack_session_manipulation/fork )
|
75
|
-
3. Create your feature branch (`git checkout -b my-new-feature`)
|
146
|
+
3. Create your feature branch (`git checkout -b my-new-feature develop`)
|
76
147
|
4. Commit your changes (`git commit -am 'Add some feature'`)
|
77
148
|
5. Push to the branch (`git push origin my-new-feature`)
|
78
149
|
6. Create a new Pull Request against this repo's develop branch
|
79
150
|
|
151
|
+
Code in the master branch is the latest released version. All code that has
|
152
|
+
made it into the develop branch is staged for the next release. All tests must
|
153
|
+
be passing before the code will get merged into develop. Along those lines,
|
154
|
+
please write tests to cover bugs found or features added!
|
155
|
+
|
80
156
|
[1]: https://github.com/railsware/rack_session_access
|
81
157
|
[2]: CODE_OF_CONDUCT.md
|
@@ -1,4 +1,3 @@
|
|
1
|
-
# Parent namespace for the Rack Session Manipulation middleware.
|
2
1
|
module RackSessionManipulation
|
3
2
|
# This module exposes the session state helpers to Capybara, allowing feature
|
4
3
|
# tests to quickly access the session mechanisms.
|
@@ -34,6 +33,10 @@ module RackSessionManipulation
|
|
34
33
|
driver_method_fallback(:put, session_manipulation_config.path, data)
|
35
34
|
end
|
36
35
|
|
36
|
+
# Expose an instance of the middleware's configuration object to Capybara
|
37
|
+
# to allow configuration alongside the DSL.
|
38
|
+
#
|
39
|
+
# @return [RackSessionManipulation::Config]
|
37
40
|
def session_manipulation_config
|
38
41
|
@rsm_config ||= Config.new
|
39
42
|
end
|
@@ -1,10 +1,60 @@
|
|
1
|
-
# Parent namespace for the Rack Session Manipulation middleware.
|
2
1
|
module RackSessionManipulation
|
3
2
|
# Rack middleware that handles the accessing and modification of session
|
4
3
|
# state.
|
4
|
+
#
|
5
|
+
# @attr_reader [Object] app The core Rack application running on top of this
|
6
|
+
# middleware.
|
7
|
+
# @attr_reader [RackSessionManipulation::Config] config An instance of the
|
8
|
+
# configuration provided to this middleware.
|
9
|
+
# @attr_reader [Hash<String=>Symbol>] routes Mapping of HTTP methods to
|
10
|
+
# local method handler.
|
5
11
|
class Middleware
|
6
12
|
attr_reader :app, :config, :routes
|
7
13
|
|
14
|
+
# @!group Action Handlers
|
15
|
+
|
16
|
+
# Handle requests to entirely reset the session state.
|
17
|
+
#
|
18
|
+
# @param [Rack::Request] request
|
19
|
+
# @return [Array<Fixnum, Hash, String>]
|
20
|
+
def reset(request)
|
21
|
+
request.env['rack.session'].clear
|
22
|
+
[204, headers(0), '']
|
23
|
+
end
|
24
|
+
|
25
|
+
# Retrieve the entire contents of the session and properly encode it
|
26
|
+
# before returning.
|
27
|
+
#
|
28
|
+
# @param [Rack::Request] request
|
29
|
+
# @return [Array<Fixnum, Hash, String>]
|
30
|
+
def retrieve(request)
|
31
|
+
session_hash = request.env['rack.session'].to_hash
|
32
|
+
content = config.encoder.encode(session_hash)
|
33
|
+
|
34
|
+
[200, headers(content.length), content]
|
35
|
+
end
|
36
|
+
|
37
|
+
# Update the current state of the session with the provided data. This
|
38
|
+
# works effectively like a hash merge on the current session only setting
|
39
|
+
# and overriding keys in the session data provided.
|
40
|
+
#
|
41
|
+
# @param [Rack::Request] request
|
42
|
+
# @return [Array<Fixnum, Hash, String>]
|
43
|
+
def update(request)
|
44
|
+
session_data = request.params['session_data']
|
45
|
+
config.encoder.decode(session_data).each do |k, v|
|
46
|
+
request.env['rack.session'][k] = v
|
47
|
+
end
|
48
|
+
|
49
|
+
loc_hdr = { 'Location' => config.path }
|
50
|
+
[303, headers(0).merge(loc_hdr), '']
|
51
|
+
rescue JSON::ParserError
|
52
|
+
[400, headers(0), '']
|
53
|
+
end
|
54
|
+
|
55
|
+
# @!endgroup
|
56
|
+
# @!group Standard Middleware Methods
|
57
|
+
|
8
58
|
# Primary entry point of this middleware. Every request that makes it this
|
9
59
|
# far into the stack will be parsed and when it matches something this
|
10
60
|
# middleware is designed to handle it will stop the chain and process it
|
@@ -18,7 +68,7 @@ module RackSessionManipulation
|
|
18
68
|
request = Rack::Request.new(env)
|
19
69
|
|
20
70
|
action = get_action(request)
|
21
|
-
action.nil? ? app.call(env) :
|
71
|
+
action.nil? ? app.call(env) : safe_handle(action, request)
|
22
72
|
end
|
23
73
|
|
24
74
|
# Setup the middleware with the primary application passed in, anything we
|
@@ -38,6 +88,9 @@ module RackSessionManipulation
|
|
38
88
|
}
|
39
89
|
end
|
40
90
|
|
91
|
+
# @!endgroup
|
92
|
+
# @!group Request Handling Helpers
|
93
|
+
|
41
94
|
# Look up what HTTP method was used for this request. In the event the
|
42
95
|
# client doesn't support all HTTP methods, the standard '_method' parameter
|
43
96
|
# fall back is made available to override the method actually used.
|
@@ -82,43 +135,17 @@ module RackSessionManipulation
|
|
82
135
|
}
|
83
136
|
end
|
84
137
|
|
85
|
-
#
|
138
|
+
# Safely handle the middleware requests so we can properly handle errors in
|
139
|
+
# the middleware by returning the correct HTTP status code and message.
|
86
140
|
#
|
87
|
-
# @param [
|
88
|
-
# @
|
89
|
-
def
|
90
|
-
request
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
# Retrieve the entire contents of the session and properly encode it
|
95
|
-
# before returning.
|
96
|
-
#
|
97
|
-
# @param [Rack::Request] request
|
98
|
-
# @return [Array<Fixnum, Hash, String>]
|
99
|
-
def retrieve(request)
|
100
|
-
session_hash = request.env['rack.session'].to_hash
|
101
|
-
content = config.encoder.encode(session_hash)
|
102
|
-
|
103
|
-
[200, headers(content.length), content]
|
141
|
+
# @param [Symbol] action The local method that will handle the request.
|
142
|
+
# @param [Rack::Request] request The request that needs to be processed.
|
143
|
+
def safe_handle(action, request)
|
144
|
+
send(action, request)
|
145
|
+
rescue => e
|
146
|
+
[500, headers(e.message.length), e.message]
|
104
147
|
end
|
105
148
|
|
106
|
-
#
|
107
|
-
# works effectively like a hash merge on the current session only setting
|
108
|
-
# and overriding keys in the session data provided.
|
109
|
-
#
|
110
|
-
# @param [Rack::Request] request
|
111
|
-
# @return [Array<Fixnum, Hash, String>]
|
112
|
-
def update(request)
|
113
|
-
session_data = request.params['session_data']
|
114
|
-
config.encoder.decode(session_data).each do |k, v|
|
115
|
-
request.env['rack.session'][k] = v
|
116
|
-
end
|
117
|
-
|
118
|
-
loc_hdr = { 'Location' => config.path }
|
119
|
-
[303, headers(0).merge(loc_hdr), '']
|
120
|
-
rescue JSON::ParserError
|
121
|
-
[400, headers(0), '']
|
122
|
-
end
|
149
|
+
# @!endgroup
|
123
150
|
end
|
124
151
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack_session_manipulation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Stelfox
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -236,7 +236,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
236
236
|
version: '0'
|
237
237
|
requirements: []
|
238
238
|
rubyforge_project:
|
239
|
-
rubygems_version: 2.
|
239
|
+
rubygems_version: 2.4.3
|
240
240
|
signing_key:
|
241
241
|
specification_version: 4
|
242
242
|
summary: A rack middleware for exposing session state to tests.
|