session_off 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright (c) 2011 Karol Bucek
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,76 @@
1
+ SessionOff
2
+ ==========
3
+
4
+ If You're old enough in Rails age, You should remember the times when sessions
5
+ where not (that) lazy loaded and one could have turned them off declaratively.
6
+
7
+ I really liked this explicit approach and sure started missing it ever since it
8
+ got redesigned. Now in theory it sounds great that if You do not touch the session
9
+ it won't be loaded, but in practice enforcing Your code to not invoke the session
10
+ method (and thus not send a session cookie) for a (session-less) mobile client
11
+ while sharing the same code for "normal" (session-aware) clients, is just *hard*.
12
+ You need to come up with a session state management and even so it's non-trivial
13
+ to track down "bugs" where You access the session while not really wanting to !
14
+
15
+ How about we bring the love back and allow (once again) for a session to be off :
16
+
17
+ session :off # turn session off for all actions
18
+ session :off, :only => :bar # only for :bar action
19
+ session :off, :except => :bar # except for :bar action
20
+ session :only => :foo, # on for :foo when doing HTTPS
21
+ :session_secure => true
22
+ session :off, :only => :foo, # off for :foo, if :bar parameter present
23
+ :if => Proc.new { |req| req.params[:bar].present? }
24
+
25
+ **NOTE:** Keep in mind that after installing the plugin `session` might be nil !
26
+ But only while using the `session` (instance) method in controllers, it does
27
+ leave the `request.session` as is. Accessing the `request.session` directly will
28
+ work (and load the session) no matter if the session is off. This is intentional
29
+ as the goal of the plugin is to provide session management for controller code
30
+ and not the whole middleware stack !
31
+ I also highly discourage against using `session` inside model classes, as it is a
32
+ lack of good desing, after all session's just an abstracted request "extension".
33
+
34
+ If in a need to check for a "disabled" session state from a request use :
35
+
36
+ request.session_options[:disabled]
37
+
38
+ If unhappy with what happens while the session is turned off for an action :
39
+
40
+ class ApplicationController < ActionController::Base
41
+
42
+ def disable_session
43
+ super # sets @_session = false (disabled)
44
+ send_mama_a_kiss # or whatever You want !
45
+ end
46
+
47
+ end
48
+
49
+
50
+ Install
51
+ =======
52
+
53
+ gem 'session_off'
54
+
55
+ or as a plain-old rails plugin :
56
+
57
+ script/plugin install git://github.com/kares/session_off.git
58
+
59
+ The code has been tested and should work on Rails **3.x** as well as **2.3**.
60
+
61
+ Example
62
+ =======
63
+
64
+ or how to be nice to robots :
65
+
66
+ class RobotsController < ApplicationController
67
+
68
+ # http://gurge.com/blog/2007/01/08/turn-off-rails-sessions-for-robots/
69
+ ROBOTS = /\b(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg)\b/i
70
+
71
+ # turn off sessions if this is a request from a robot
72
+ session :off, :if => proc { |request| request.user_agent =~ ROBOTS }
73
+
74
+ end
75
+
76
+ <http://log.kares.org/search/label/session_off>
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the session_off plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the session_off plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'SessionOff'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,177 @@
1
+ module SessionOff
2
+
3
+ def self.included(base)
4
+ base.class_eval do
5
+
6
+ extend ClassMethods
7
+
8
+ if instance_methods(false).include?('session_enabled?')
9
+ # remove the "deprecation" Rails 2.3 (can't override) :
10
+ remove_method 'session_enabled?'
11
+ end
12
+ if instance_methods(false).include?('reset_session')
13
+ # we'll have our own that respects session_enabled?
14
+ # Rails 2.3 has this method in ActionController::Base
15
+ remove_method 'reset_session'
16
+ end
17
+ # attr_internal on ActionController::Base for Rails 2.3.x
18
+ # 3+ : `delegate :session, :to => "@_request"` from Metal
19
+ clazz = self
20
+ while clazz && ! clazz.instance_methods(false).include?('session')
21
+ clazz = clazz.superclass
22
+ end
23
+ clazz.send(:remove_method, :'session') if clazz
24
+
25
+ include InstanceMethods
26
+
27
+ alias_method_chain(:process, :session_off)
28
+ # yet another Rails 2.3 specific hook :
29
+ alias_method_chain(:assign_shortcuts, :session_off) rescue NameError
30
+
31
+ end
32
+ end
33
+
34
+ module ClassMethods
35
+
36
+ # Specify how sessions ought to be managed for a subset of the actions on
37
+ # the controller. Like filters, you can specify <tt>:only</tt> and
38
+ # <tt>:except</tt> clauses to restrict the subset, otherwise options
39
+ # apply to all actions on this controller.
40
+ #
41
+ # The session options are inheritable, as well, so if you specify them in
42
+ # a parent controller, they apply to controllers that extend the parent.
43
+ #
44
+ # Usage:
45
+ #
46
+ # # turn off session management for all actions.
47
+ # session :off
48
+ #
49
+ # # turn off session management for all actions _except_ foo and bar.
50
+ # session :off, :except => %w(foo bar)
51
+ #
52
+ # # turn off session management for only the foo and bar actions.
53
+ # session :off, :only => %w(foo bar)
54
+ #
55
+ # # the session will only work over HTTPS, but only for the foo action
56
+ # session :only => :foo, :session_secure => true
57
+ #
58
+ # # the session by default uses HttpOnly sessions for security reasons.
59
+ # # this can be switched off.
60
+ # session :only => :foo, :session_http_only => false
61
+ #
62
+ # # the session will only be disabled for 'foo', and only if it is
63
+ # # requested as a web service
64
+ # session :off, :only => :foo,
65
+ # :if => Proc.new { |req| req.parameters[:ws] }
66
+ #
67
+ # # the session will be disabled for non html/ajax requests
68
+ # session :off,
69
+ # :if => Proc.new { |req| !(req.format.html? || req.format.js?) }
70
+ #
71
+ # # turn the session back on, useful when it was turned off in the
72
+ # # application controller, and you need it on in another controller
73
+ # session :on
74
+ #
75
+ # All session options described for ActionController::Base.process_cgi
76
+ # are valid arguments.
77
+ def session(*args)
78
+ options = args.extract_options!
79
+
80
+ options[:disabled] = false if args.delete(:on)
81
+ options[:disabled] = true unless args.empty?
82
+ options[:only] = [*options[:only]].map { |o| o.to_s } if options[:only]
83
+ options[:except] = [*options[:except]].map { |o| o.to_s } if options[:except]
84
+ if options[:only] && options[:except]
85
+ raise ArgumentError, "only one of either :only or :except are allowed"
86
+ end
87
+
88
+ write_inheritable_array(:session_options, [ options ])
89
+ end
90
+
91
+ def session_options_for(request, action)
92
+ session_options_array = read_inheritable_attribute(:session_options)
93
+ session_options =
94
+ defined?(ActionController::Base.session_options) ?
95
+ ActionController::Base.session_options.dup : {}
96
+
97
+ if session_options_array.blank?
98
+ session_options
99
+ else
100
+ options = session_options
101
+
102
+ action = action.to_s
103
+ session_options_array.each do |opts|
104
+ next if opts[:if] && ! opts[:if].call(request)
105
+ if opts[:only] && opts[:only].include?(action)
106
+ options.merge!(opts)
107
+ elsif opts[:except] && ! opts[:except].include?(action)
108
+ options.merge!(opts)
109
+ elsif ! opts[:only] && ! opts[:except]
110
+ options.merge!(opts)
111
+ end
112
+ end
113
+
114
+ options.delete(:only)
115
+ options.delete(:except)
116
+ options.delete(:if)
117
+ options
118
+ end
119
+ end
120
+
121
+ end
122
+
123
+ module InstanceMethods
124
+
125
+ def session_enabled?
126
+ @_session != false
127
+ end
128
+
129
+ def session
130
+ @_session == false ? nil : @_session ||= request.session
131
+ end
132
+
133
+ def reset_session
134
+ if session_enabled?
135
+ request.reset_session
136
+ @_session = nil
137
+ end
138
+ end
139
+
140
+ def disable_session
141
+ @_session = false
142
+ end
143
+
144
+ if defined?(AbstractController::Base) # Rails 3+
145
+
146
+ def process_with_session_off(action, *args)
147
+ session_options = self.class.session_options_for(request, action)
148
+ request.session_options.merge! session_options
149
+ disable_session if session_options[:disabled]
150
+ process_without_session_off(action, *args)
151
+ end
152
+
153
+ else # Rails 2.3.x
154
+
155
+ def process_with_session_off(request, response, method = :perform_action, *args)
156
+ action = request.parameters["action"] || :index
157
+ session_options = self.class.session_options_for(request, action)
158
+ request.session_options.merge! session_options
159
+ #@_session = false if session_options[:disabled]
160
+ process_without_session_off(request, response, method, *args)
161
+ end
162
+
163
+ private
164
+
165
+ def assign_shortcuts_with_session_off(request, response)
166
+ assign_shortcuts_without_session_off(request, response)
167
+ disable_session if request.session_options[:disabled]
168
+ end
169
+
170
+ end
171
+
172
+ end
173
+
174
+ end
175
+
176
+ require 'action_controller/base'
177
+ ActionController::Base.send :include, SessionOff
@@ -0,0 +1,168 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ # inspired by Rails 2.2.3's session_management_test !
4
+ class SessionManagementTest < ActionController::TestCase # Test::Unit::TestCase
5
+
6
+ def setup
7
+ @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
8
+ end
9
+
10
+ class SessionOffController < ActionController::Base
11
+ session :off
12
+
13
+ def show
14
+ render :text => "done"
15
+ end
16
+
17
+ def tell
18
+ render :text => "done"
19
+ end
20
+ end
21
+
22
+ test 'session_off_globally for action' do
23
+ @controller = SessionOffController.new
24
+
25
+ get :show
26
+ assert_no_session
27
+ end
28
+
29
+ test 'session_off_globally for another action' do
30
+ @controller = SessionOffController.new
31
+
32
+ get :tell
33
+ assert_no_session
34
+ end
35
+
36
+ class SessionOffOnController < ActionController::Base
37
+ session :off
38
+ session :on, :only => :tell
39
+
40
+ def show
41
+ render :text => "done"
42
+ end
43
+
44
+ def tell
45
+ render :text => "done"
46
+ end
47
+ end
48
+
49
+ test 'session_off_globally_then_on for non-matched action' do
50
+ @controller = SessionOffOnController.new
51
+
52
+ get :show
53
+ assert_no_session
54
+ end
55
+
56
+ test 'session_off_globally_then_on for matched action' do
57
+ @controller = SessionOffOnController.new
58
+
59
+ get :tell
60
+ assert_session
61
+ assert_equal false, @request.session_options[:disabled]
62
+ end
63
+
64
+ class SpecializedController < SessionOffController
65
+ session :disabled => false, :only => :something
66
+
67
+ def something
68
+ render :text => "done"
69
+ end
70
+
71
+ def another
72
+ render :text => "done"
73
+ end
74
+ end
75
+
76
+ test 'controller_specialization_overrides_settings 1' do
77
+ @controller = SpecializedController.new
78
+
79
+ get :something
80
+ assert_session
81
+ end
82
+
83
+ test 'controller_specialization_overrides_settings 2' do
84
+ @controller = SpecializedController.new
85
+
86
+ get :another
87
+ assert_no_session
88
+ end
89
+
90
+ class TestController < ActionController::Base
91
+ session :off, :only => :show
92
+ session :session_secure => true, :except => :show
93
+ session :off, :only => :conditional,
94
+ :if => Proc.new { |r| r.parameters[:ws] }
95
+
96
+ def show
97
+ render :text => "done"
98
+ end
99
+
100
+ def tell
101
+ render :text => "done"
102
+ end
103
+
104
+ def conditional
105
+ render :text => ">>>#{params[:ws]}<<<"
106
+ end
107
+ end
108
+
109
+ test 'multiple session offs - off for (non-conditional) action' do
110
+ @controller = TestController.new
111
+
112
+ get :show
113
+ assert_no_session
114
+ end
115
+
116
+ test 'multiple session offs - except (non-conditional) action' do
117
+ @controller = TestController.new
118
+
119
+ get :tell
120
+ assert_session
121
+ assert @request.session_options[:session_secure]
122
+ end
123
+
124
+ test 'multiple session offs - on if condition not met' do
125
+ @controller = TestController.new
126
+
127
+ get :conditional
128
+ assert_session
129
+ end
130
+
131
+ test 'multiple session offs - off if condition met' do
132
+ @controller = TestController.new
133
+
134
+ get :conditional, :ws => "ws"
135
+ assert_no_session
136
+ end
137
+
138
+ test 'session_is_not_enabled' do
139
+ @controller = TestController.new
140
+
141
+ get :show
142
+ assert_nothing_raised do
143
+ assert_equal false, @controller.session_enabled?
144
+ end
145
+ end
146
+
147
+ test 'session_is_enabled' do
148
+ @controller = TestController.new
149
+
150
+ get :tell
151
+ assert @controller.session_enabled?
152
+ end
153
+
154
+ private
155
+
156
+ def assert_session
157
+ # 2.2.3 behavior :
158
+ # assert_instance_of Hash, @request.session_options
159
+ assert_not_nil @controller.session
160
+ end
161
+
162
+ def assert_no_session
163
+ # 2.2.3 behavior :
164
+ # assert_equal false, @request.session_options
165
+ assert_nil @controller.session
166
+ end
167
+
168
+ end
@@ -0,0 +1,120 @@
1
+ require File.expand_path('test_helper', File.dirname(__FILE__))
2
+
3
+ class SessionOffTest < ActionController::TestCase
4
+
5
+ class TestController < ActionController::Base
6
+
7
+ session :off, :secure => false, :except => [ :on ],
8
+ :if => Proc.new { |request| ! request.parameters[:force_session] }
9
+
10
+ def on
11
+ session[:foo] = 'bar on' if session
12
+ head :ok
13
+ end
14
+
15
+ def off
16
+ if session
17
+ session[:foo] = 'bar off'
18
+ head 201
19
+ else
20
+ head 200
21
+ end
22
+ end
23
+
24
+ def reset
25
+ reset_session
26
+ head :ok
27
+ end
28
+
29
+ def disable_session
30
+ super
31
+ @disable_session = true
32
+ end
33
+
34
+ end
35
+
36
+ tests TestController
37
+
38
+ setup :save_session_options
39
+ teardown :restore_session_options
40
+
41
+ test "session is on" do
42
+ get :on
43
+ assert session
44
+ assert_equal 'bar on', session[:foo]
45
+ end
46
+
47
+ test "session is off" do
48
+ get :off
49
+ assert_not_equal 'bar off', session[:foo]
50
+ assert_response 200
51
+ end
52
+
53
+ test "session is off unless condition met" do
54
+ get :off, :force_session => '1'
55
+ assert_equal 'bar off', session[:foo]
56
+ assert_response 201
57
+ end
58
+
59
+ test "session is not reset if off" do
60
+ request.expects(:reset_session).never
61
+ get :reset
62
+ end
63
+
64
+ test "session is reset if it's on" do
65
+ request.expects(:reset_session).once
66
+ get :reset, :force_session => '1'
67
+ end
68
+
69
+ test "request session options are merged" do
70
+ session_options = request.session_options.dup
71
+ begin
72
+ request.session_options[:bar] = 'foo'
73
+ get :on
74
+ assert request.session_options
75
+ assert_equal 'foo', request.session_options[:bar]
76
+ ensure
77
+ request.session_options.clear
78
+ request.session_options.merge!(session_options)
79
+ end
80
+ end
81
+
82
+ if defined?(ActionController::Base.session_options)
83
+
84
+ test "global session options are available" do
85
+ session_options = ActionController::Base.session_options.dup
86
+ begin
87
+ ActionController::Base.session_options.merge!({ :domain => ".test.host" })
88
+ get :on
89
+ assert request.session_options
90
+ assert_equal '.test.host', request.session_options[:domain]
91
+ ensure
92
+ ActionController::Base.session_options.replace(session_options)
93
+ end
94
+ end
95
+
96
+ test "global session options get merged" do
97
+ session_options = ActionController::Base.session_options.dup
98
+ begin
99
+ ActionController::Base.session_options.merge!({ :secure => true })
100
+ get :on
101
+ assert request.session_options
102
+ assert_equal true, request.session_options[:secure]
103
+ ensure
104
+ ActionController::Base.session_options.replace(session_options)
105
+ end
106
+ end
107
+
108
+ end
109
+
110
+ test "disable session is called when turning session off" do
111
+ get :off
112
+ assert_equal true, @controller.instance_variable_get(:@disable_session)
113
+ end
114
+
115
+ test "disable session is not called when session on" do
116
+ get :on
117
+ assert_equal nil, @controller.instance_variable_get(:@disable_session)
118
+ end
119
+
120
+ end
@@ -0,0 +1,165 @@
1
+ require 'rubygems'
2
+
3
+ require 'yaml'
4
+ require 'stringio'
5
+ require 'test/unit'
6
+ require 'mocha'
7
+
8
+ version =
9
+ if ARGV.find { |opt| /RAILS_VERSION=([\d\.]+)/ =~ opt }
10
+ $~[1]
11
+ else
12
+ ENV['RAILS_VERSION'] # rake test RAILS_VERSION=2.3.5
13
+ end
14
+
15
+ if version
16
+ RAILS_VERSION = version
17
+ gem 'activesupport', "= #{RAILS_VERSION}"
18
+ gem 'activerecord', "= #{RAILS_VERSION}"
19
+ gem 'actionpack', "= #{RAILS_VERSION}"
20
+ gem 'actionmailer', "= #{RAILS_VERSION}"
21
+ gem 'rails', "= #{RAILS_VERSION}"
22
+ else
23
+ gem 'activesupport'
24
+ gem 'activerecord'
25
+ gem 'actionpack'
26
+ gem 'actionmailer'
27
+ gem 'rails'
28
+ end
29
+
30
+ require 'active_support'
31
+ require 'active_support/test_case'
32
+ require 'action_controller'
33
+ require 'action_controller/test_case'
34
+
35
+ require 'rails/version'
36
+ puts "emulating Rails.version = #{Rails::VERSION::STRING}"
37
+
38
+ require 'action_controller/integration' if Rails::VERSION::MAJOR < 3
39
+ require 'action_controller/session_management' if Rails::VERSION::MAJOR < 3
40
+ require 'action_dispatch' if Rails::VERSION::MAJOR >= 3
41
+ require 'action_dispatch/routing' if Rails::VERSION::MAJOR >= 3
42
+
43
+ if Rails::VERSION::MAJOR >= 3
44
+ ActiveSupport::Deprecation.behavior = :stderr
45
+ else
46
+ ActiveSupport::Deprecation.debug = true
47
+ end
48
+
49
+ ActionController::Base.logger = nil
50
+ ActionController::Routing::Routes.reload rescue nil
51
+
52
+ if Rails::VERSION::MAJOR >= 3
53
+ require 'rails'
54
+ # a minimal require 'rails/all' :
55
+ require "action_controller/railtie"
56
+ require "rails/test_unit/railtie"
57
+ require 'rails/test_help'
58
+ else
59
+ module Rails
60
+ class << self
61
+
62
+ def initialized?
63
+ @initialized || false
64
+ end
65
+
66
+ def initialized=(initialized)
67
+ @initialized ||= initialized
68
+ end
69
+
70
+ def backtrace_cleaner
71
+ @@backtrace_cleaner ||= begin
72
+ require 'rails/gem_dependency' # backtrace_cleaner depends on this !
73
+ require 'rails/backtrace_cleaner'
74
+ Rails::BacktraceCleaner.new
75
+ end
76
+ end
77
+
78
+ def root
79
+ Pathname.new(RAILS_ROOT) if defined?(RAILS_ROOT)
80
+ end
81
+
82
+ def env
83
+ @_env ||= begin
84
+ ActiveSupport::StringInquirer.new(RAILS_ENV)
85
+ end
86
+ end
87
+
88
+ def cache
89
+ RAILS_CACHE
90
+ end
91
+
92
+ def version
93
+ VERSION::STRING
94
+ end
95
+
96
+ def public_path
97
+ @@public_path ||= self.root ? File.join(self.root, "public") : "public"
98
+ end
99
+
100
+ def public_path=(path)
101
+ @@public_path = path
102
+ end
103
+
104
+ end
105
+ end
106
+ end
107
+
108
+ silence_warnings { RAILS_ROOT = File.expand_path( File.dirname(__FILE__) ) }
109
+
110
+ # Make double-sure the RAILS_ENV is set to test,
111
+ # so fixtures are loaded to the right database
112
+ silence_warnings { RAILS_ENV = "test" }
113
+
114
+ Rails.backtrace_cleaner.remove_silencers! if Rails.backtrace_cleaner
115
+
116
+ module Rails # make sure we can set the logger
117
+ class << self
118
+ attr_accessor :logger
119
+ end
120
+ end
121
+
122
+ File.open(File.join(File.dirname(__FILE__), 'test.log'), 'w') do |file|
123
+ Rails.logger = Logger.new(file.path)
124
+ end
125
+
126
+ if ActionController::Base.respond_to? :session_options # Rails 2.x
127
+
128
+ skey = Rails.version < '2.3' ? :session_key : :key
129
+ ActionController::Base.session_options[skey] = '_session_off_test'
130
+ ActionController::Base.session_options[:secret] = 'x' * 30
131
+
132
+ else # since Rails 3.0.0 :
133
+
134
+ module SessionOff
135
+ class Application < Rails::Application
136
+ config.secret_token = 'x' * 30
137
+ end
138
+ end
139
+
140
+ # Initialize the rails application
141
+ SessionOff::Application.initialize!
142
+ SessionOff::Application.routes.draw do
143
+ match '/:controller(/:action(/:id))'
144
+ end
145
+
146
+ end
147
+
148
+ # call the plugin's init.rb - thus it's setup as it would in a rails app :
149
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib')
150
+ require File.join(File.dirname(__FILE__), '../init')
151
+
152
+ ActionController::TestCase.class_eval do
153
+
154
+ attr_reader :request, :response
155
+ @@session_options = nil
156
+
157
+ def save_session_options
158
+ @@session_options = request.session_options.dup
159
+ end
160
+
161
+ def restore_session_options
162
+ request.session_options = @@session_options
163
+ end
164
+
165
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: session_off
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 3
8
+ version: "0.3"
9
+ platform: ruby
10
+ authors:
11
+ - Karol Bucek
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2011-08-30 00:00:00 +02:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: actionpack
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ version: "2.3"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: mocha
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ description: `session :off, :only => :foo, :if => Proc.new { |req| req.params[:bar] }`
47
+ email:
48
+ - self@kares.org
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files:
54
+ - README.md
55
+ files:
56
+ - lib/session_off.rb
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - test/test_helper.rb
61
+ - test/session_off_test.rb
62
+ - test/session_management_test.rb
63
+ has_rdoc: true
64
+ homepage: http://github.com/kares/session_off
65
+ licenses: []
66
+
67
+ post_install_message:
68
+ rdoc_options: []
69
+
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project: "[none]"
91
+ rubygems_version: 1.3.7
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: declarative session :off from Rails 2.2 'backported'
95
+ test_files:
96
+ - test/test_helper.rb
97
+ - test/session_off_test.rb
98
+ - test/session_management_test.rb