rack-flash-session 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,14 @@
1
- Install
1
+ == Install
2
2
 
3
3
  $ gem install rack-flash-session
4
4
 
5
- Rails
5
+ === Rails
6
6
 
7
7
  # config/environment.rb
8
8
  config.gem 'rack-flash-session', :lib => 'rack/flash-session'
9
9
  config.middleware.insert_before(ActionController::Base.session_store, 'Rack::FlashSession');
10
10
 
11
- Middleware
11
+ === Middleware
12
12
 
13
13
  # add a use line to your builder
14
14
  require 'rack/flash-session'
@@ -17,23 +17,27 @@ Middleware
17
17
  run MyApp.new
18
18
  end
19
19
 
20
- Options
20
+ == Options
21
21
 
22
- # The default session key is '_session_id' but you can specify your
23
- # own:
22
+ The default session key is '_session_id' but you can specify your
23
+ own:
24
24
 
25
25
  use Rack::FlashSession, 'sid'
26
26
 
27
- # Or in Rails you can reference the key defined to be used as session key:
27
+ Or in Rails you can reference the key defined to be used as session key:
28
28
 
29
29
  config.middleware.insert_before(ActionController::Base.session_store, 'Rack::FlashSession', config.action_controller.session[:key])
30
30
 
31
- # Note that the session key is used for both the query parameter name
32
- # and the cookie name.
31
+ Note that the session key is used for both the query parameter name
32
+ and the cookie name.
33
33
 
34
- Thanks to:
34
+ You can specify multiple session keys:
35
35
 
36
- Rob Anderton for posting Flash uploaders, Rails, cookie based
37
- sessions and CSRF: Rack Middleware to the rescue!
38
- (http://thewebfellas.com/blog/2008/12/22/flash-uploaders-rails-cookie-based-sessions-and-csrf-rack-middleware-to-the-rescue)
39
- on which this gem is based.
36
+ use Rack::FlashSession, 'sid', 'my_other_session_key'
37
+
38
+ == Thanks to:
39
+
40
+ Rob Anderton for posting Flash uploaders, Rails, cookie based
41
+ sessions and CSRF: Rack Middleware to the rescue!
42
+ (http://thewebfellas.com/blog/2008/12/22/flash-uploaders-rails-cookie-based-sessions-and-csrf-rack-middleware-to-the-rescue)
43
+ on which this gem is based.
@@ -3,15 +3,16 @@ require 'rack/utils'
3
3
  module Rack
4
4
  class FlashSession
5
5
 
6
- def initialize(app, session_key = '_session_id')
6
+ def initialize(app, *args)
7
+ args = ['_session_id'] if args.empty?
7
8
  @app = app
8
- @session_key = session_key
9
+ @session_keys = args
9
10
  end
10
11
 
11
12
  def call(env)
12
13
  if env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/ then
13
14
  params = ::Rack::Request.new(env).params
14
- env['HTTP_COOKIE'] = [@session_key, params[@session_key]].join('=').freeze unless params[@session_key].nil?
15
+ env['HTTP_COOKIE'] = params.select{|k,v| @session_keys.include?(k)}.map{|k,v| [k, ::Rack::Utils.escape(v)].join('=')}.join(';')
15
16
  end
16
17
  @app.call(env)
17
18
  end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::FlashSession do
4
+ def app(*args)
5
+ Rack::Builder.new {
6
+ use Rack::Lint
7
+ use Rack::FlashSession, *args
8
+ run lambda {|env| [200, {'Content-Type' => 'text/html'}, ['']] }
9
+ }.to_app
10
+ end
11
+
12
+ def env_for(params = {}, options={})
13
+ Rack::MockRequest.env_for('', options.merge(:params => params))
14
+ end
15
+
16
+ def do_call(params, options, *args)
17
+ env = env_for(params, options)
18
+ app(*args).call(env)
19
+ Rack::Request.new(env)
20
+ end
21
+
22
+ context 'flash request' do
23
+ def request(params, *args)
24
+ options = {'HTTP_USER_AGENT' => 'Adobe Flash'}
25
+ do_call(params, options, *args)
26
+ end
27
+
28
+ describe 'default _session_id' do
29
+ subject {request({'_session_id' => '12345'})}
30
+ its(:cookies){should == {'_session_id' => '12345'}}
31
+ end
32
+
33
+ describe 'custom session id' do
34
+ subject {request({'my_session_id' => '12345'}, 'my_session_id')}
35
+ its(:cookies){should == {'my_session_id' => '12345'}}
36
+ end
37
+
38
+ describe 'multiple cookies' do
39
+ subject {request({'my_session_id' => '12345', 'my_other_session_id' => '54321'}, 'my_session_id', 'my_other_session_id')}
40
+ its(:cookies){should include({'my_session_id' => '12345'})}
41
+ its(:cookies){should include({'my_other_session_id' => '54321'})}
42
+ its(:cookies){should have(2).items}
43
+ end
44
+ end
45
+
46
+ context 'non flash request' do
47
+ def request(params, *args)
48
+ options = {'HTTP_USER_AGENT' => 'some browser'}
49
+ do_call(params, options, *args)
50
+ end
51
+
52
+ subject {request({'_session_id' => '12345'})}
53
+ its(:cookies){should be_empty}
54
+ end
55
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+
3
+ require 'spec'
4
+ require 'rack'
5
+ require 'rack/mock'
6
+
7
+ require 'rack/flash-session'
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-flash-session
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 19
4
5
  prerelease: false
5
6
  segments:
6
7
  - 1
7
8
  - 0
8
- - 1
9
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
10
11
  platform: ruby
11
12
  authors:
12
13
  - Bart Teeuwisse
@@ -14,22 +15,71 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-03-20 00:00:00 -07:00
18
+ date: 2010-06-13 00:00:00 -07:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: rack
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 15
27
30
  segments:
28
31
  - 1
29
32
  - 0
30
33
  version: "1.0"
31
34
  type: :runtime
32
35
  version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rack
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 13
45
+ segments:
46
+ - 1
47
+ - 1
48
+ version: "1.1"
49
+ type: :development
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: rack-test
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 13
60
+ segments:
61
+ - 0
62
+ - 5
63
+ - 3
64
+ version: 0.5.3
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: rspec
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 27
76
+ segments:
77
+ - 1
78
+ - 3
79
+ - 0
80
+ version: 1.3.0
81
+ type: :development
82
+ version_requirements: *id004
33
83
  description: |+
34
84
 
35
85
  Rack::FlashSession converts a session query parameter to a cookie if the request's user agent is flash.
@@ -45,6 +95,8 @@ extra_rdoc_files:
45
95
  files:
46
96
  - lib/rack/flash-session.rb
47
97
  - README.rdoc
98
+ - spec/flash_session_spec.rb
99
+ - spec/spec_helper.rb
48
100
  has_rdoc: true
49
101
  homepage: http://github.com/bartt/rack-flash-session
50
102
  licenses: []
@@ -55,25 +107,30 @@ rdoc_options:
55
107
  require_paths:
56
108
  - lib
57
109
  required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
58
111
  requirements:
59
112
  - - ">="
60
113
  - !ruby/object:Gem::Version
114
+ hash: 3
61
115
  segments:
62
116
  - 0
63
117
  version: "0"
64
118
  required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
65
120
  requirements:
66
121
  - - ">="
67
122
  - !ruby/object:Gem::Version
123
+ hash: 3
68
124
  segments:
69
125
  - 0
70
126
  version: "0"
71
127
  requirements: []
72
128
 
73
129
  rubyforge_project:
74
- rubygems_version: 1.3.6
130
+ rubygems_version: 1.3.7
75
131
  signing_key:
76
132
  specification_version: 3
77
133
  summary: Rack::FlashSession converts a session query parameter to a cookie if the request's user agent is flash
78
- test_files: []
79
-
134
+ test_files:
135
+ - spec/flash_session_spec.rb
136
+ - spec/spec_helper.rb