rack-param_to_cookie 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ff789a7ad9f19014e1e5d85d7c8301ec212328d
4
- data.tar.gz: 0b43e39084247e90143fe4b417d1fba6fd31c717
3
+ metadata.gz: 3ad04222bd754f08090d5e8308b648a4aa80afab
4
+ data.tar.gz: 440b3a794b871eaa7175881fa8ced68f8d4b0746
5
5
  SHA512:
6
- metadata.gz: 671d18c5bd162762248287605f391d333899c3332b86ae6966d1e8971faa8ae4fdb0d646aab2453866af4f7986d31f4346a6386a265b1d5f8077b9e9559911a3
7
- data.tar.gz: a990dc602f606d02f6ed6abbc381dd713f90ff77f2a42afc560b86204bdb75007bfd1b7c809c3abf6d387a72726ab82deb12a612ad74043fbb803493a921c637
6
+ metadata.gz: c5ab893233126b3da78e0cbd176eaedf2bb087e463b7967e25cbfdf836c608cdf5e0f470f72d183fcaf659897970a1069a1f53cb295dab86e00bbf817ebd4829
7
+ data.tar.gz: 31e4213b2c2b65ab1198d7c7a1af6b2c5a2b6d6e309ff5209c7d5755e3eb6e9fdef52dea5b5c79fc12e0bbd621fd2e1bc067daa909f795c351b4feac253c4487
data/README.rdoc CHANGED
@@ -20,17 +20,18 @@ Rack::ParamToCookie is a rack middleware that extracts request parameters from r
20
20
  In a Rails app, you can add it as middleware in +config/application.rb+. For a parameter called +ref+, the basic usage is:
21
21
 
22
22
  config.middleware.use 'Rack::ParamToCookie', 'ref' => {}
23
-
23
+
24
24
  This tells Rack::ParamToCookie to capture a request parameter called +ref+, store it in a cookie called +ref+, and make it available in your rails app as <tt>request.env['ref']</tt>. You can specify multiple parameters and configure them. Here's a more in-depth example:
25
25
 
26
26
  config.middleware.use 'Rack::ParamToCookie',
27
27
  'ref' => {cookie_name: 'referral_code',
28
28
  env_name: 'referral.code',
29
- ttl: 14*24*60*60},
29
+ ttl: 14*24*60*60,
30
+ max_length: 12},
30
31
  'aff' => {cookie_name: 'affiliate_code',
31
32
  env_name: 'affiliate.code'}
32
33
 
33
- The first cookie, +referral_code+ for parameter +ref+, has a 14 day time to live and is accessible in +request.env+ <tt>['referral.code']</tt>. The second, for parameter +aff+, has the default time to live, which is 30 days.
34
+ The first cookie, +referral_code+ for parameter +ref+, has a 14 day time to live, can be a maximum of 12 characters and is accessible in +request.env+ <tt>['referral.code']</tt>. The second, for parameter +aff+, has the default time to live, which is 30 days and can be the default maximum length, which is 64 characters.
34
35
 
35
36
  == Installation
36
37
 
@@ -1,6 +1,6 @@
1
1
  module Rack
2
2
  class ParamToCookie
3
- VERSION_MAJOR = 2
3
+ VERSION_MAJOR = 3
4
4
  VERSION_MINOR = 0
5
5
  VERSION_PATCH = 0
6
6
  VERSION = [VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH].join('.')
@@ -19,6 +19,7 @@ module Rack
19
19
  options[:env_name] ||= param
20
20
  options[:ttl] ||= 60*60*24*30 # 30 days
21
21
  options[:set_cookie_options] ||= {}
22
+ options[:max_length] ||= 64 # characters
22
23
  end
23
24
  end
24
25
 
@@ -33,6 +34,10 @@ module Rack
33
34
  # check whether there's a new value for the cookie with this request
34
35
  params_value = req.params[param] rescue nil
35
36
 
37
+ # validate the length of the value
38
+ params_value = nil if
39
+ params_value && params_value.length > options[:max_length]
40
+
36
41
  value = params_value || cookie_value
37
42
  env[options[:env_name]] = value if value
38
43
 
@@ -26,7 +26,7 @@ describe "Rack::ParamToCookie" do
26
26
  it "should do nothing when there is no ref parameter" do
27
27
  get '/'
28
28
 
29
- assert_equal nil, last_request.env['ref']
29
+ assert_nil last_request.env['ref']
30
30
  assert_equal({}, rack_mock_session.cookie_jar.to_hash)
31
31
  end
32
32
 
@@ -41,7 +41,7 @@ describe "Rack::ParamToCookie" do
41
41
  get '/'
42
42
  assert_equal 'abc', last_request.env['ref']
43
43
  assert_equal({'ref' => 'abc'}, rack_mock_session.cookie_jar.to_hash)
44
- assert_equal nil, last_response.headers['Set-Cookie']
44
+ assert_nil last_response.headers['Set-Cookie']
45
45
 
46
46
  # if we set it again, it gets overwritten
47
47
  get '/', ref: '123'
@@ -53,14 +53,15 @@ describe "Rack::ParamToCookie" do
53
53
  get '/'
54
54
  assert_equal '123', last_request.env['ref']
55
55
  assert_equal({'ref' => '123'}, rack_mock_session.cookie_jar.to_hash)
56
- assert_equal nil, last_response.headers['Set-Cookie']
56
+ assert_nil last_response.headers['Set-Cookie']
57
57
  end
58
58
  end
59
59
 
60
60
  describe "with multiple parameters and custom names" do
61
61
  before do
62
62
  make_app \
63
- 'ref' => {cookie_name: 'ref_cookie', env_name: 'ref.env', ttl: 10},
63
+ 'ref' => {cookie_name: 'ref_cookie', env_name: 'ref.env', ttl: 10,
64
+ max_length: 10},
64
65
  'aff' => {cookie_name: 'aff_cookie', env_name: 'aff.env', ttl: 20}
65
66
  clear_cookies
66
67
  end
@@ -68,8 +69,8 @@ describe "Rack::ParamToCookie" do
68
69
  it "should set ref and aff" do
69
70
  # initially no cookies
70
71
  get '/'
71
- assert_equal nil, last_request.env['ref.env']
72
- assert_equal nil, last_request.env['aff.env']
72
+ assert_nil last_request.env['ref.env']
73
+ assert_nil last_request.env['aff.env']
73
74
  assert_equal({}, rack_mock_session.cookie_jar.to_hash)
74
75
 
75
76
  # set both at the same time
@@ -95,7 +96,7 @@ describe "Rack::ParamToCookie" do
95
96
  assert_equal 'bar', last_request.env['aff.env']
96
97
  assert_equal({'ref_cookie' => 'foo', 'aff_cookie' => 'bar'},
97
98
  rack_mock_session.cookie_jar.to_hash)
98
- assert_equal nil, last_response.headers['Set-Cookie']
99
+ assert_nil last_response.headers['Set-Cookie']
99
100
 
100
101
  # update ref
101
102
  get '/', ref: 'baz'
@@ -112,7 +113,7 @@ describe "Rack::ParamToCookie" do
112
113
  assert_equal 'bar', last_request.env['aff.env']
113
114
  assert_equal({'ref_cookie' => 'baz', 'aff_cookie' => 'bar'},
114
115
  rack_mock_session.cookie_jar.to_hash)
115
- assert_equal nil, last_response.headers['Set-Cookie']
116
+ assert_nil last_response.headers['Set-Cookie']
116
117
 
117
118
  # update aff
118
119
  get '/', aff: 'bat'
@@ -129,7 +130,12 @@ describe "Rack::ParamToCookie" do
129
130
  assert_equal 'bat', last_request.env['aff.env']
130
131
  assert_equal({'ref_cookie' => 'baz', 'aff_cookie' => 'bat'},
131
132
  rack_mock_session.cookie_jar.to_hash)
132
- assert_equal nil, last_response.headers['Set-Cookie']
133
+ assert_nil last_response.headers['Set-Cookie']
134
+ end
135
+
136
+ it "should not set cookies longer than the max length" do
137
+ get '/', ref: 'abcdefghijklmnopqrstuvwxyz'
138
+ assert_equal({}, rack_mock_session.cookie_jar.to_hash)
133
139
  end
134
140
  end
135
141
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-param_to_cookie
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Lees-Miller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-11 00:00:00.000000000 Z
11
+ date: 2017-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -74,7 +74,7 @@ rdoc_options:
74
74
  - "--main"
75
75
  - README.rdoc
76
76
  - "--title"
77
- - rack-param_to_cookie-2.0.0 Documentation
77
+ - rack-param_to_cookie-3.0.0 Documentation
78
78
  require_paths:
79
79
  - lib
80
80
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -89,10 +89,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  requirements: []
91
91
  rubyforge_project:
92
- rubygems_version: 2.4.5.1
92
+ rubygems_version: 2.6.12
93
93
  signing_key:
94
94
  specification_version: 4
95
95
  summary: Store selected request parameters to cookies.
96
96
  test_files:
97
97
  - test/rack/param_to_cookie/param_to_cookie_test.rb
98
- has_rdoc: