cookiejar 0.3.2 → 0.3.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.
- checksums.yaml +4 -4
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/.travis.yml +17 -0
- data/Gemfile +2 -0
- data/README.markdown +15 -1
- data/Rakefile +11 -12
- data/cookiejar.gemspec +27 -0
- data/lib/cookiejar.rb +2 -1
- data/lib/cookiejar/cookie.rb +57 -63
- data/lib/cookiejar/cookie_validation.rb +102 -99
- data/lib/cookiejar/jar.rb +50 -48
- data/lib/cookiejar/version.rb +4 -0
- data/spec/cookie_spec.rb +90 -90
- data/spec/cookie_validation_spec.rb +147 -155
- data/spec/jar_spec.rb +107 -110
- data/spec/spec_helper.rb +5 -0
- metadata +50 -11
@@ -1,244 +1,236 @@
|
|
1
|
-
require '
|
2
|
-
require 'rubygems'
|
1
|
+
require 'spec_helper'
|
3
2
|
|
4
3
|
include CookieJar
|
5
4
|
describe CookieValidation do
|
6
|
-
describe
|
5
|
+
describe '#validate_cookie' do
|
7
6
|
localaddr = 'http://localhost/foo/bar/'
|
8
|
-
it
|
9
|
-
lambda do
|
7
|
+
it 'should fail if version unset' do
|
8
|
+
expect(lambda do
|
10
9
|
unversioned = Cookie.from_set_cookie localaddr, 'foo=bar'
|
11
10
|
unversioned.instance_variable_set :@version, nil
|
12
11
|
CookieValidation.validate_cookie localaddr, unversioned
|
13
|
-
end.
|
14
|
-
end
|
15
|
-
it
|
16
|
-
lambda do
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end.
|
44
|
-
end
|
45
|
-
it
|
46
|
-
lambda do
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
it "should fail for setting subdomain cookies" do
|
52
|
-
lambda do
|
53
|
-
subdomain = Cookie.from_set_cookie 'http://foo.com/', 'foo=bar;domain=auth.foo.com'
|
54
|
-
# validate_cookie 'http://foo.com/', subdomain
|
55
|
-
end.should raise_error InvalidCookieError
|
56
|
-
end
|
57
|
-
it "should handle a normal implicit internet cookie" do
|
12
|
+
end).to raise_error InvalidCookieError
|
13
|
+
end
|
14
|
+
it 'should fail if the path is more specific' do
|
15
|
+
expect(lambda do
|
16
|
+
Cookie.from_set_cookie localaddr, 'foo=bar;path=/foo/bar/baz'
|
17
|
+
end).to raise_error InvalidCookieError
|
18
|
+
end
|
19
|
+
it 'should fail if the path is different than the request' do
|
20
|
+
expect(lambda do
|
21
|
+
Cookie.from_set_cookie localaddr, 'foo=bar;path=/baz/'
|
22
|
+
end).to raise_error InvalidCookieError
|
23
|
+
end
|
24
|
+
it 'should fail if the domain has no dots' do
|
25
|
+
expect(lambda do
|
26
|
+
Cookie.from_set_cookie 'http://zero/', 'foo=bar;domain=zero'
|
27
|
+
end).to raise_error InvalidCookieError
|
28
|
+
end
|
29
|
+
it 'should fail for explicit localhost' do
|
30
|
+
expect(lambda do
|
31
|
+
Cookie.from_set_cookie localaddr, 'foo=bar;domain=localhost'
|
32
|
+
end).to raise_error InvalidCookieError
|
33
|
+
end
|
34
|
+
it 'should fail for mismatched domains' do
|
35
|
+
expect(lambda do
|
36
|
+
Cookie.from_set_cookie 'http://www.foo.com/', 'foo=bar;domain=bar.com'
|
37
|
+
end).to raise_error InvalidCookieError
|
38
|
+
end
|
39
|
+
it 'should fail for domains more than one level up' do
|
40
|
+
expect(lambda do
|
41
|
+
Cookie.from_set_cookie 'http://x.y.z.com/', 'foo=bar;domain=z.com'
|
42
|
+
end).to raise_error InvalidCookieError
|
43
|
+
end
|
44
|
+
it 'should fail for setting subdomain cookies' do
|
45
|
+
expect(lambda do
|
46
|
+
Cookie.from_set_cookie 'http://foo.com/', 'foo=bar;domain=auth.foo.com'
|
47
|
+
end).to raise_error InvalidCookieError
|
48
|
+
end
|
49
|
+
it 'should handle a normal implicit internet cookie' do
|
58
50
|
normal = Cookie.from_set_cookie 'http://foo.com/', 'foo=bar'
|
59
|
-
CookieValidation.validate_cookie('http://foo.com/', normal).
|
51
|
+
expect(CookieValidation.validate_cookie('http://foo.com/', normal)).to be_truthy
|
60
52
|
end
|
61
|
-
it
|
53
|
+
it 'should handle a normal implicit localhost cookie' do
|
62
54
|
localhost = Cookie.from_set_cookie 'http://localhost/', 'foo=bar'
|
63
|
-
CookieValidation.validate_cookie('http://localhost/', localhost).
|
55
|
+
expect(CookieValidation.validate_cookie('http://localhost/', localhost)).to be_truthy
|
64
56
|
end
|
65
|
-
it
|
66
|
-
ipaddr =
|
67
|
-
CookieValidation.validate_cookie('http://127.0.0.1/', ipaddr).
|
57
|
+
it 'should handle an implicit IP address cookie' do
|
58
|
+
ipaddr = Cookie.from_set_cookie 'http://127.0.0.1/', 'foo=bar'
|
59
|
+
expect(CookieValidation.validate_cookie('http://127.0.0.1/', ipaddr)).to be_truthy
|
68
60
|
end
|
69
|
-
it
|
61
|
+
it 'should handle an explicit domain on an internet site' do
|
70
62
|
explicit = Cookie.from_set_cookie 'http://foo.com/', 'foo=bar;domain=.foo.com'
|
71
|
-
CookieValidation.validate_cookie('http://foo.com/', explicit).
|
63
|
+
expect(CookieValidation.validate_cookie('http://foo.com/', explicit)).to be_truthy
|
72
64
|
end
|
73
|
-
it
|
65
|
+
it 'should handle setting a cookie explicitly on a superdomain' do
|
74
66
|
superdomain = Cookie.from_set_cookie 'http://auth.foo.com/', 'foo=bar;domain=.foo.com'
|
75
|
-
CookieValidation.validate_cookie('http://foo.com/', superdomain).
|
67
|
+
expect(CookieValidation.validate_cookie('http://foo.com/', superdomain)).to be_truthy
|
76
68
|
end
|
77
|
-
it
|
69
|
+
it 'should handle explicitly setting a cookie' do
|
78
70
|
explicit = Cookie.from_set_cookie 'http://foo.com/bar/', 'foo=bar;path=/bar/'
|
79
71
|
CookieValidation.validate_cookie('http://foo.com/bar/', explicit)
|
80
72
|
end
|
81
|
-
it
|
73
|
+
it 'should handle setting a cookie on a higher path' do
|
82
74
|
higher = Cookie.from_set_cookie 'http://foo.com/bar/baz/', 'foo=bar;path=/bar/'
|
83
75
|
CookieValidation.validate_cookie('http://foo.com/bar/baz/', higher)
|
84
|
-
end
|
76
|
+
end
|
85
77
|
end
|
86
78
|
describe '#cookie_base_path' do
|
87
79
|
it "should leave '/' alone" do
|
88
|
-
CookieValidation.cookie_base_path('/').
|
80
|
+
expect(CookieValidation.cookie_base_path('/')).to eq '/'
|
89
81
|
end
|
90
82
|
it "should strip off everything after the last '/'" do
|
91
|
-
CookieValidation.cookie_base_path('/foo/bar/baz').
|
83
|
+
expect(CookieValidation.cookie_base_path('/foo/bar/baz')).to eq '/foo/bar/'
|
92
84
|
end
|
93
|
-
it
|
94
|
-
CookieValidation.cookie_base_path('/foo/bar?query=a/b/c#fragment/b/c').
|
85
|
+
it 'should handle query parameters and fragments with slashes' do
|
86
|
+
expect(CookieValidation.cookie_base_path('/foo/bar?query=a/b/c#fragment/b/c')).to eq '/foo/'
|
95
87
|
end
|
96
|
-
it
|
97
|
-
CookieValidation.cookie_base_path(URI.parse('http://www.foo.com/bar/')).
|
88
|
+
it 'should handle URI objects' do
|
89
|
+
expect(CookieValidation.cookie_base_path(URI.parse('http://www.foo.com/bar/'))).to eq '/bar/'
|
98
90
|
end
|
99
|
-
it
|
100
|
-
CookieValidation.cookie_base_path(
|
91
|
+
it 'should preserve case' do
|
92
|
+
expect(CookieValidation.cookie_base_path('/BaR/')).to eq '/BaR/'
|
101
93
|
end
|
102
94
|
end
|
103
95
|
describe '#determine_cookie_path' do
|
104
|
-
it
|
105
|
-
CookieValidation.determine_cookie_path('http://foo.com/', nil).
|
106
|
-
CookieValidation.determine_cookie_path('http://foo.com/bar/baz', '').
|
96
|
+
it 'should use the requested path when none is specified for the cookie' do
|
97
|
+
expect(CookieValidation.determine_cookie_path('http://foo.com/', nil)).to eq '/'
|
98
|
+
expect(CookieValidation.determine_cookie_path('http://foo.com/bar/baz', '')).to eq '/bar/'
|
107
99
|
end
|
108
|
-
it
|
109
|
-
CookieValidation.determine_cookie_path(URI.parse('http://foo.com/bar/'), '').
|
100
|
+
it 'should handle URI objects' do
|
101
|
+
expect(CookieValidation.determine_cookie_path(URI.parse('http://foo.com/bar/'), '')).to eq '/bar/'
|
110
102
|
end
|
111
|
-
it
|
112
|
-
cookie = Cookie.from_set_cookie('http://foo.com/',
|
113
|
-
CookieValidation.determine_cookie_path('http://foo.com/', cookie).
|
103
|
+
it 'should handle Cookie objects' do
|
104
|
+
cookie = Cookie.from_set_cookie('http://foo.com/', 'name=value;path=/')
|
105
|
+
expect(CookieValidation.determine_cookie_path('http://foo.com/', cookie)).to eq '/'
|
114
106
|
end
|
115
|
-
it
|
116
|
-
CookieValidation.determine_cookie_path('http://foo.com/ignorable/path', '/path/').
|
107
|
+
it 'should ignore the request when a path is specified' do
|
108
|
+
expect(CookieValidation.determine_cookie_path('http://foo.com/ignorable/path', '/path/')).to eq '/path/'
|
117
109
|
end
|
118
110
|
end
|
119
111
|
describe '#compute_search_domains' do
|
120
|
-
it
|
121
|
-
CookieValidation.compute_search_domains('http://www.auth.foo.com/').
|
122
|
-
|
112
|
+
it 'should handle subdomains' do
|
113
|
+
expect(CookieValidation.compute_search_domains('http://www.auth.foo.com/')).to eq(
|
114
|
+
['www.auth.foo.com', '.www.auth.foo.com', '.auth.foo.com'])
|
123
115
|
end
|
124
|
-
it
|
125
|
-
CookieValidation.compute_search_domains('http://foo.com/').
|
126
|
-
|
116
|
+
it 'should handle root domains' do
|
117
|
+
expect(CookieValidation.compute_search_domains('http://foo.com/')).to eq(
|
118
|
+
['foo.com', '.foo.com'])
|
127
119
|
end
|
128
|
-
it
|
129
|
-
CookieValidation.compute_search_domains('http://tiny.cc/').
|
130
|
-
|
120
|
+
it 'should handle hexadecimal TLDs' do
|
121
|
+
expect(CookieValidation.compute_search_domains('http://tiny.cc/')).to eq(
|
122
|
+
['tiny.cc', '.tiny.cc'])
|
131
123
|
end
|
132
|
-
it
|
133
|
-
CookieValidation.compute_search_domains('http://127.0.0.1/').
|
134
|
-
|
124
|
+
it 'should handle IP addresses' do
|
125
|
+
expect(CookieValidation.compute_search_domains('http://127.0.0.1/')).to eq(
|
126
|
+
['127.0.0.1'])
|
135
127
|
end
|
136
|
-
it
|
137
|
-
CookieValidation.compute_search_domains('http://zero/').
|
138
|
-
|
128
|
+
it 'should handle local addresses' do
|
129
|
+
expect(CookieValidation.compute_search_domains('http://zero/')).to eq(
|
130
|
+
['zero.local', '.zero.local', '.local'])
|
139
131
|
end
|
140
132
|
end
|
141
133
|
describe '#determine_cookie_domain' do
|
142
|
-
it
|
143
|
-
CookieValidation.determine_cookie_domain('http://foo.com/', 'foo.com').
|
134
|
+
it 'should add a dot to the front of domains' do
|
135
|
+
expect(CookieValidation.determine_cookie_domain('http://foo.com/', 'foo.com')).to eq '.foo.com'
|
144
136
|
end
|
145
|
-
it
|
146
|
-
CookieValidation.determine_cookie_domain('http://foo.com/', '.foo.com').
|
137
|
+
it 'should not add a second dot if one present' do
|
138
|
+
expect(CookieValidation.determine_cookie_domain('http://foo.com/', '.foo.com')).to eq '.foo.com'
|
147
139
|
end
|
148
|
-
it
|
149
|
-
c = Cookie.from_set_cookie('http://foo.com/',
|
150
|
-
CookieValidation.determine_cookie_domain('http://foo.com/', c).
|
140
|
+
it 'should handle Cookie objects' do
|
141
|
+
c = Cookie.from_set_cookie('http://foo.com/', 'foo=bar;domain=foo.com')
|
142
|
+
expect(CookieValidation.determine_cookie_domain('http://foo.com/', c)).to eq '.foo.com'
|
151
143
|
end
|
152
|
-
it
|
153
|
-
CookieValidation.determine_cookie_domain(URI.parse('http://foo.com/'), '.foo.com').
|
144
|
+
it 'should handle URI objects' do
|
145
|
+
expect(CookieValidation.determine_cookie_domain(URI.parse('http://foo.com/'), '.foo.com')).to eq '.foo.com'
|
154
146
|
end
|
155
|
-
it
|
156
|
-
CookieValidation.determine_cookie_domain('http://foo.com/', '').
|
147
|
+
it 'should use an exact hostname when no domain specified' do
|
148
|
+
expect(CookieValidation.determine_cookie_domain('http://foo.com/', '')).to eq 'foo.com'
|
157
149
|
end
|
158
|
-
it
|
159
|
-
CookieValidation.determine_cookie_domain('http://127.0.0.1/', '127.0.0.1').
|
150
|
+
it 'should leave IPv4 addresses alone' do
|
151
|
+
expect(CookieValidation.determine_cookie_domain('http://127.0.0.1/', '127.0.0.1')).to eq '127.0.0.1'
|
160
152
|
end
|
161
|
-
it
|
153
|
+
it 'should leave IPv6 addresses alone' do
|
162
154
|
['2001:db8:85a3::8a2e:370:7334', '::ffff:192.0.2.128'].each do |value|
|
163
|
-
CookieValidation.determine_cookie_domain("http://[#{value}]/", value).
|
155
|
+
expect(CookieValidation.determine_cookie_domain("http://[#{value}]/", value)).to eq value
|
164
156
|
end
|
165
157
|
end
|
166
158
|
end
|
167
|
-
describe
|
168
|
-
it
|
159
|
+
describe '#effective_host' do
|
160
|
+
it 'should leave proper domains the same' do
|
169
161
|
['google.com', 'www.google.com', 'google.com.'].each do |value|
|
170
|
-
CookieValidation.effective_host(value).
|
162
|
+
expect(CookieValidation.effective_host(value)).to eq value
|
171
163
|
end
|
172
164
|
end
|
173
|
-
it
|
174
|
-
CookieValidation.effective_host(URI.parse('http://example.com/')).
|
175
|
-
end
|
176
|
-
it
|
177
|
-
CookieValidation.effective_host('localhost').
|
165
|
+
it 'should handle a URI object' do
|
166
|
+
expect(CookieValidation.effective_host(URI.parse('http://example.com/'))).to eq 'example.com'
|
167
|
+
end
|
168
|
+
it 'should add a local suffix on unqualified hosts' do
|
169
|
+
expect(CookieValidation.effective_host('localhost')).to eq 'localhost.local'
|
178
170
|
end
|
179
|
-
it
|
180
|
-
CookieValidation.effective_host('127.0.0.1').
|
171
|
+
it 'should leave IPv4 addresses alone' do
|
172
|
+
expect(CookieValidation.effective_host('127.0.0.1')).to eq '127.0.0.1'
|
181
173
|
end
|
182
|
-
it
|
174
|
+
it 'should leave IPv6 addresses alone' do
|
183
175
|
['2001:db8:85a3::8a2e:370:7334', ':ffff:192.0.2.128'].each do |value|
|
184
|
-
CookieValidation.effective_host(value).
|
176
|
+
expect(CookieValidation.effective_host(value)).to eq value
|
185
177
|
end
|
186
178
|
end
|
187
|
-
it
|
188
|
-
CookieValidation.effective_host('FOO.COM').
|
179
|
+
it 'should lowercase addresses' do
|
180
|
+
expect(CookieValidation.effective_host('FOO.COM')).to eq 'foo.com'
|
189
181
|
end
|
190
182
|
end
|
191
183
|
describe '#match_domains' do
|
192
|
-
it
|
193
|
-
CookieValidation.domains_match('localhost.local', 'localhost.local').
|
194
|
-
CookieValidation.domains_match('foo.com', 'foo.com').
|
195
|
-
CookieValidation.domains_match('127.0.0.1', '127.0.0.1').
|
196
|
-
CookieValidation.domains_match('::ffff:192.0.2.128', '::ffff:192.0.2.128').
|
184
|
+
it 'should handle exact matches' do
|
185
|
+
expect(CookieValidation.domains_match('localhost.local', 'localhost.local')).to eq 'localhost.local'
|
186
|
+
expect(CookieValidation.domains_match('foo.com', 'foo.com')).to eq 'foo.com'
|
187
|
+
expect(CookieValidation.domains_match('127.0.0.1', '127.0.0.1')).to eq '127.0.0.1'
|
188
|
+
expect(CookieValidation.domains_match('::ffff:192.0.2.128', '::ffff:192.0.2.128')).to eq '::ffff:192.0.2.128'
|
197
189
|
end
|
198
|
-
it
|
199
|
-
CookieValidation.domains_match('.foo.com', 'auth.foo.com').
|
200
|
-
CookieValidation.domains_match('.y.z.foo.com', 'x.y.z.foo.com').
|
190
|
+
it 'should handle matching a superdomain' do
|
191
|
+
expect(CookieValidation.domains_match('.foo.com', 'auth.foo.com')).to eq '.foo.com'
|
192
|
+
expect(CookieValidation.domains_match('.y.z.foo.com', 'x.y.z.foo.com')).to eq '.y.z.foo.com'
|
201
193
|
end
|
202
|
-
it
|
203
|
-
CookieValidation.domains_match('.z.foo.com', 'x.y.z.foo.com').
|
204
|
-
CookieValidation.domains_match('foo.com', 'com').
|
194
|
+
it 'should not match superdomains, or illegal domains' do
|
195
|
+
expect(CookieValidation.domains_match('.z.foo.com', 'x.y.z.foo.com')).to be_nil
|
196
|
+
expect(CookieValidation.domains_match('foo.com', 'com')).to be_nil
|
205
197
|
end
|
206
|
-
it
|
207
|
-
CookieValidation.domains_match('foo.com.', 'foo.com').
|
198
|
+
it 'should not match domains with and without a dot suffix together' do
|
199
|
+
expect(CookieValidation.domains_match('foo.com.', 'foo.com')).to be_nil
|
208
200
|
end
|
209
201
|
end
|
210
202
|
describe '#hostname_reach' do
|
211
|
-
it
|
212
|
-
{'www.google.com' => 'google.com', 'auth.corp.companyx.com' => 'corp.companyx.com'}.each do |entry|
|
213
|
-
CookieValidation.hostname_reach(entry[0]).
|
203
|
+
it 'should find the next highest subdomain' do
|
204
|
+
{ 'www.google.com' => 'google.com', 'auth.corp.companyx.com' => 'corp.companyx.com' }.each do |entry|
|
205
|
+
expect(CookieValidation.hostname_reach(entry[0])).to eq entry[1]
|
214
206
|
end
|
215
207
|
end
|
216
|
-
it
|
217
|
-
CookieValidation.hostname_reach('www.google.com.').
|
208
|
+
it 'should handle domains with suffixed dots' do
|
209
|
+
expect(CookieValidation.hostname_reach('www.google.com.')).to eq 'google.com.'
|
218
210
|
end
|
219
|
-
it
|
220
|
-
CookieValidation.hostname_reach('github.com').
|
211
|
+
it 'should return nil for a root domain' do
|
212
|
+
expect(CookieValidation.hostname_reach('github.com')).to be_nil
|
221
213
|
end
|
222
214
|
it "should return 'local' for a local domain" do
|
223
215
|
['foo.local', 'foo.local.'].each do |hostname|
|
224
|
-
CookieValidation.hostname_reach(hostname).
|
216
|
+
expect(CookieValidation.hostname_reach(hostname)).to eq 'local'
|
225
217
|
end
|
226
218
|
end
|
227
219
|
it "should handle mixed-case '.local'" do
|
228
|
-
CookieValidation.hostname_reach('foo.LOCAL').
|
220
|
+
expect(CookieValidation.hostname_reach('foo.LOCAL')).to eq 'local'
|
229
221
|
end
|
230
|
-
it
|
231
|
-
CookieValidation.hostname_reach('127.0.0.1').
|
222
|
+
it 'should return nil for an IPv4 address' do
|
223
|
+
expect(CookieValidation.hostname_reach('127.0.0.1')).to be_nil
|
232
224
|
end
|
233
|
-
it
|
225
|
+
it 'should return nil for IPv6 addresses' do
|
234
226
|
['2001:db8:85a3::8a2e:370:7334', '::ffff:192.0.2.128'].each do |value|
|
235
|
-
CookieValidation.hostname_reach(value).
|
227
|
+
expect(CookieValidation.hostname_reach(value)).to be_nil
|
236
228
|
end
|
237
229
|
end
|
238
230
|
end
|
239
231
|
describe '#parse_set_cookie' do
|
240
|
-
it
|
241
|
-
CookieValidation.parse_set_cookie(
|
232
|
+
it 'should max out at 2038 on 32bit systems' do
|
233
|
+
expect(CookieValidation.parse_set_cookie('TRACK_USER_P=98237480810003948000782774;expires=Sat, 30-Jun-2040 05:39:49 GMT;path=/')[:expires_at].to_i).to be >= 0x7FFFFFFF
|
242
234
|
end
|
243
|
-
end
|
244
|
-
end
|
235
|
+
end
|
236
|
+
end
|
data/spec/jar_spec.rb
CHANGED
@@ -1,235 +1,232 @@
|
|
1
|
-
require '
|
2
|
-
require 'yaml'
|
3
|
-
require 'rubygems'
|
1
|
+
require 'spec_helper'
|
4
2
|
|
5
3
|
include CookieJar
|
6
4
|
|
7
5
|
describe Jar do
|
8
6
|
describe '.setCookie' do
|
9
|
-
it
|
10
|
-
jar = Jar.new
|
7
|
+
it 'should allow me to set a cookie' do
|
8
|
+
jar = Jar.new
|
11
9
|
jar.set_cookie 'http://foo.com/', 'foo=bar'
|
12
10
|
end
|
13
|
-
it
|
14
|
-
jar = Jar.new
|
11
|
+
it 'should allow me to set multiple cookies' do
|
12
|
+
jar = Jar.new
|
15
13
|
jar.set_cookie 'http://foo.com/', 'foo=bar'
|
16
14
|
jar.set_cookie 'http://foo.com/', 'bar=baz'
|
17
15
|
jar.set_cookie 'http://auth.foo.com/', 'foo=bar'
|
18
|
-
jar.set_cookie 'http://auth.foo.com/', 'auth=135121...;domain=foo.com'
|
16
|
+
jar.set_cookie 'http://auth.foo.com/', 'auth=135121...;domain=foo.com'
|
19
17
|
end
|
20
|
-
it
|
18
|
+
it 'should allow me to set multiple cookies in 1 header' do
|
21
19
|
jar = Jar.new
|
22
20
|
jar.set_cookie 'http://foo.com/', 'my_cookie=123456; Domain=foo.com; expires=Thu, 31 Dec 2037 23:59:59 GMT; Path=/, other_cookie=helloworld; Domain=foo.com; expires=Thu, 31 Dec 2037 23:59:59 GMT, last_cookie=098765'
|
23
21
|
end
|
24
22
|
end
|
25
23
|
describe '.get_cookies' do
|
26
|
-
it
|
27
|
-
jar = Jar.new
|
24
|
+
it 'should let me read back cookies which are set' do
|
25
|
+
jar = Jar.new
|
28
26
|
jar.set_cookie 'http://foo.com/', 'foo=bar'
|
29
27
|
jar.set_cookie 'http://foo.com/', 'bar=baz'
|
30
28
|
jar.set_cookie 'http://auth.foo.com/', 'foo=bar'
|
31
29
|
jar.set_cookie 'http://auth.foo.com/', 'auth=135121...;domain=foo.com'
|
32
|
-
jar.get_cookies('http://foo.com/').
|
30
|
+
expect(jar.get_cookies('http://foo.com/')).to have(3).items
|
33
31
|
end
|
34
|
-
it
|
32
|
+
it 'should let me read back a multiple cookies from 1 header' do
|
35
33
|
jar = Jar.new
|
36
34
|
jar.set_cookie 'http://foo.com/', 'my_cookie=123456; Domain=foo.com; expires=Thu, 31 Dec 2037 23:59:59 GMT; Path=/, other_cookie=helloworld; Domain=foo.com; expires=Thu, 31 Dec 2037 23:59:59 GMT, last_cookie=098765'
|
37
|
-
jar.get_cookie_header('http://foo.com/').
|
35
|
+
expect(jar.get_cookie_header('http://foo.com/')).to eq 'last_cookie=098765;my_cookie=123456;other_cookie=helloworld'
|
38
36
|
end
|
39
|
-
it
|
37
|
+
it 'should return cookies longest path first' do
|
40
38
|
jar = Jar.new
|
41
|
-
uri = 'http://foo.com/a/b/c/d'
|
39
|
+
uri = 'http://foo.com/a/b/c/d'
|
42
40
|
jar.set_cookie uri, 'a=bar'
|
43
41
|
jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
|
44
42
|
jar.set_cookie uri, 'c=bar;path=/a/b'
|
45
43
|
jar.set_cookie uri, 'd=bar;path=/a/'
|
46
44
|
cookies = jar.get_cookies(uri)
|
47
|
-
cookies.
|
48
|
-
cookies[0].name.
|
49
|
-
cookies[1].name.
|
50
|
-
cookies[2].name.
|
51
|
-
cookies[3].name.
|
45
|
+
expect(cookies).to have(4).items
|
46
|
+
expect(cookies[0].name).to eq 'b'
|
47
|
+
expect(cookies[1].name).to eq 'a'
|
48
|
+
expect(cookies[2].name).to eq 'c'
|
49
|
+
expect(cookies[3].name).to eq 'd'
|
52
50
|
end
|
53
|
-
it
|
51
|
+
it 'should not return expired cookies' do
|
54
52
|
jar = Jar.new
|
55
53
|
uri = 'http://localhost/'
|
56
54
|
jar.set_cookie uri, 'foo=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
|
57
55
|
cookies = jar.get_cookies(uri)
|
58
|
-
cookies.
|
56
|
+
expect(cookies).to have(0).items
|
59
57
|
end
|
60
58
|
end
|
61
59
|
describe '.get_cookie_headers' do
|
62
|
-
it
|
60
|
+
it 'should return cookie headers' do
|
63
61
|
jar = Jar.new
|
64
|
-
uri = 'http://foo.com/a/b/c/d'
|
62
|
+
uri = 'http://foo.com/a/b/c/d'
|
65
63
|
jar.set_cookie uri, 'a=bar'
|
66
64
|
jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
|
67
65
|
cookie_headers = jar.get_cookie_header uri
|
68
|
-
cookie_headers.
|
66
|
+
expect(cookie_headers).to eq 'b=baz;a=bar'
|
69
67
|
end
|
70
|
-
it
|
68
|
+
it 'should handle a version 1 cookie' do
|
71
69
|
jar = Jar.new
|
72
|
-
uri = 'http://foo.com/a/b/c/d'
|
70
|
+
uri = 'http://foo.com/a/b/c/d'
|
73
71
|
jar.set_cookie uri, 'a=bar'
|
74
72
|
jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
|
75
73
|
jar.set_cookie2 uri, 'c=baz;Version=1;path="/"'
|
76
74
|
cookie_headers = jar.get_cookie_header uri
|
77
|
-
cookie_headers.
|
75
|
+
expect(cookie_headers).to eq '$Version=0;b=baz;$Path="/a/b/c/d";a=bar;$Path="/a/b/c/",$Version=1;c=baz;$Path="/"'
|
78
76
|
end
|
79
77
|
end
|
80
78
|
describe '.add_cookie' do
|
81
|
-
it
|
79
|
+
it 'should let me add a pre-existing cookie' do
|
82
80
|
jar = Jar.new
|
83
81
|
cookie = Cookie.from_set_cookie 'http://localhost/', 'foo=bar'
|
84
82
|
jar.add_cookie cookie
|
85
83
|
end
|
86
84
|
end
|
87
85
|
describe '.to_a' do
|
88
|
-
it
|
89
|
-
uri = 'http://foo.com/a/b/c/d'
|
86
|
+
it 'should return me an array of all cookie objects' do
|
87
|
+
uri = 'http://foo.com/a/b/c/d'
|
90
88
|
jar = Jar.new
|
91
89
|
jar.set_cookie uri, 'a=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
|
92
90
|
jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
|
93
91
|
jar.set_cookie uri, 'c=bar;path=/a/b'
|
94
92
|
jar.set_cookie uri, 'd=bar;path=/a/'
|
95
93
|
jar.set_cookie 'http://localhost/', 'foo=bar'
|
96
|
-
jar.to_a.
|
94
|
+
expect(jar.to_a).to have(5).items
|
97
95
|
end
|
98
96
|
end
|
99
97
|
describe '.expire_cookies' do
|
100
|
-
it
|
101
|
-
uri = 'http://foo.com/a/b/c/d'
|
98
|
+
it 'should expire cookies which are no longer valid' do
|
99
|
+
uri = 'http://foo.com/a/b/c/d'
|
102
100
|
jar = Jar.new
|
103
101
|
jar.set_cookie uri, 'a=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
|
104
102
|
jar.set_cookie uri, 'b=baz;path=/a/b/c/d;expires=Wednesday, 01-Nov-2028 12:00:00 GMT'
|
105
103
|
jar.set_cookie uri, 'c=bar;path=/a/b'
|
106
104
|
jar.set_cookie uri, 'd=bar;path=/a/'
|
107
105
|
jar.set_cookie 'http://localhost/', 'foo=bar'
|
108
|
-
jar.to_a.
|
106
|
+
expect(jar.to_a).to have(5).items
|
109
107
|
jar.expire_cookies
|
110
|
-
jar.to_a.
|
108
|
+
expect(jar.to_a).to have(4).items
|
111
109
|
end
|
112
|
-
it
|
113
|
-
uri = 'http://foo.com/a/b/c/d'
|
110
|
+
it 'should let me expire all session cookies' do
|
111
|
+
uri = 'http://foo.com/a/b/c/d'
|
114
112
|
jar = Jar.new
|
115
113
|
jar.set_cookie uri, 'a=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
|
116
114
|
jar.set_cookie uri, 'b=baz;path=/a/b/c/d;expires=Wednesday, 01-Nov-2028 12:00:00 GMT'
|
117
115
|
jar.set_cookie uri, 'c=bar;path=/a/b'
|
118
116
|
jar.set_cookie uri, 'd=bar;path=/a/'
|
119
117
|
jar.set_cookie 'http://localhost/', 'foo=bar'
|
120
|
-
jar.to_a.
|
118
|
+
expect(jar.to_a).to have(5).items
|
121
119
|
jar.expire_cookies true
|
122
|
-
jar.to_a.
|
120
|
+
expect(jar.to_a).to have(1).items
|
123
121
|
end
|
124
122
|
end
|
125
123
|
describe '#set_cookies_from_headers' do
|
126
|
-
it
|
124
|
+
it 'should handle a Set-Cookie header' do
|
127
125
|
jar = Jar.new
|
128
|
-
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
129
|
-
|
130
|
-
cookies.
|
131
|
-
jar.to_a.
|
126
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
127
|
+
'Set-Cookie' => 'foo=bar'
|
128
|
+
expect(cookies).to have(1).items
|
129
|
+
expect(jar.to_a).to have(1).items
|
132
130
|
end
|
133
|
-
it
|
131
|
+
it 'should handle a set-cookie header' do
|
134
132
|
jar = Jar.new
|
135
|
-
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
136
|
-
|
137
|
-
cookies.
|
138
|
-
jar.to_a.
|
133
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
134
|
+
'set-cookie' => 'foo=bar'
|
135
|
+
expect(cookies).to have(1).items
|
136
|
+
expect(jar.to_a).to have(1).items
|
139
137
|
end
|
140
|
-
it
|
138
|
+
it 'should handle multiple Set-Cookie headers' do
|
141
139
|
jar = Jar.new
|
142
|
-
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
143
|
-
|
144
|
-
cookies.
|
145
|
-
jar.to_a.
|
140
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
141
|
+
'Set-Cookie' => ['foo=bar', 'bar=baz']
|
142
|
+
expect(cookies).to have(2).items
|
143
|
+
expect(jar.to_a).to have(2).items
|
146
144
|
end
|
147
|
-
it
|
145
|
+
it 'should handle a Set-Cookie2 header' do
|
148
146
|
jar = Jar.new
|
149
|
-
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
150
|
-
|
151
|
-
cookies.
|
152
|
-
jar.to_a.
|
147
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
148
|
+
'Set-Cookie2' => 'foo=bar;Version=1'
|
149
|
+
expect(cookies).to have(1).items
|
150
|
+
expect(jar.to_a).to have(1).items
|
153
151
|
end
|
154
|
-
it
|
152
|
+
it 'should handle a set-cookie2 header' do
|
155
153
|
jar = Jar.new
|
156
|
-
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
157
|
-
|
158
|
-
cookies.
|
159
|
-
jar.to_a.
|
154
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
155
|
+
'set-cookie2' => 'foo=bar;Version=1'
|
156
|
+
expect(cookies).to have(1).items
|
157
|
+
expect(jar.to_a).to have(1).items
|
160
158
|
end
|
161
|
-
it
|
159
|
+
it 'should handle multiple Set-Cookie2 headers' do
|
162
160
|
jar = Jar.new
|
163
|
-
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
164
|
-
|
165
|
-
cookies.
|
166
|
-
jar.to_a.
|
161
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
162
|
+
'Set-Cookie2' => ['foo=bar;Version=1', 'bar=baz;Version=1']
|
163
|
+
expect(cookies).to have(2).items
|
164
|
+
expect(jar.to_a).to have(2).items
|
167
165
|
end
|
168
|
-
it
|
166
|
+
it 'should handle mixed distinct Set-Cookie and Set-Cookie2 headers' do
|
169
167
|
jar = Jar.new
|
170
|
-
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
171
|
-
|
172
|
-
|
173
|
-
cookies.
|
174
|
-
jar.to_a.
|
168
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
169
|
+
'Set-Cookie' => 'foo=bar',
|
170
|
+
'Set-Cookie2' => 'bar=baz;Version=1'
|
171
|
+
expect(cookies).to have(2).items
|
172
|
+
expect(jar.to_a).to have(2).items
|
175
173
|
end
|
176
|
-
it
|
174
|
+
it 'should handle overlapping Set-Cookie and Set-Cookie2 headers' do
|
177
175
|
jar = Jar.new
|
178
|
-
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
179
|
-
|
180
|
-
|
181
|
-
cookies.
|
182
|
-
jar.to_a.
|
176
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
177
|
+
'Set-Cookie' => ['foo=bar', 'bar=baz'],
|
178
|
+
'Set-Cookie2' => 'foo=bar;Version=1'
|
179
|
+
expect(cookies).to have(2).items
|
180
|
+
expect(jar.to_a).to have(2).items
|
183
181
|
# and has the version 1 cookie
|
184
|
-
cookies.find do |cookie|
|
182
|
+
expect(cookies.find do |cookie|
|
185
183
|
cookie.name == 'foo'
|
186
|
-
end.version.
|
184
|
+
end.version).to eq 1
|
187
185
|
end
|
188
|
-
it
|
186
|
+
it 'should silently drop invalid cookies' do
|
189
187
|
jar = Jar.new
|
190
|
-
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
191
|
-
|
192
|
-
cookies.
|
193
|
-
jar.to_a.
|
188
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
189
|
+
'Set-Cookie' => ['foo=bar', 'bar=baz;domain=.foo.com']
|
190
|
+
expect(cookies).to have(1).items
|
191
|
+
expect(jar.to_a).to have(1).items
|
194
192
|
end
|
195
193
|
end
|
196
194
|
begin
|
197
195
|
require 'json'
|
198
|
-
describe
|
199
|
-
it
|
200
|
-
|
196
|
+
describe '.to_json' do
|
197
|
+
it 'should serialize cookies to JSON' do
|
201
198
|
c = Cookie.from_set_cookie 'https://localhost/', 'foo=bar;secure;expires=Wed, 01-Nov-2028 12:00:00 GMT'
|
202
199
|
jar = Jar.new
|
203
200
|
jar.add_cookie c
|
204
201
|
json = jar.to_json
|
205
|
-
json.
|
202
|
+
expect(json).to be_a String
|
206
203
|
end
|
207
204
|
end
|
208
|
-
describe
|
209
|
-
it
|
210
|
-
json =
|
205
|
+
describe '.json_create' do
|
206
|
+
it 'should deserialize a JSON array to a jar' do
|
207
|
+
json = '[{"name":"foo","value":"bar","domain":"localhost.local","path":"\\/","created_at":"2009-09-11 12:51:03 -0600","expiry":"2028-11-01 12:00:00 GMT","secure":true}]'
|
211
208
|
array = JSON.parse json
|
212
|
-
|
209
|
+
|
213
210
|
jar = Jar.json_create array
|
214
|
-
jar.get_cookies('https://localhost/').
|
211
|
+
expect(jar.get_cookies('https://localhost/')).to have(1).items
|
215
212
|
end
|
216
|
-
it
|
217
|
-
json =
|
213
|
+
it 'should deserialize a JSON hash to a jar' do
|
214
|
+
json = '{"cookies":[{"name":"foo","value":"bar","domain":"localhost.local","path":"\\/","created_at":"2009-09-11 12:51:03 -0600","expiry":"2028-11-01 12:00:00 GMT","secure":true}]}'
|
218
215
|
hash = JSON.parse json
|
219
|
-
|
216
|
+
|
220
217
|
jar = Jar.json_create hash
|
221
|
-
jar.get_cookies('https://localhost/').
|
218
|
+
expect(jar.get_cookies('https://localhost/')).to have(1).items
|
222
219
|
end
|
223
|
-
|
224
|
-
it
|
225
|
-
json =
|
226
|
-
jar = JSON.parse json, :
|
227
|
-
jar.get_cookies('https://localhost/').
|
220
|
+
|
221
|
+
it 'should automatically deserialize to a jar' do
|
222
|
+
json = '{"json_class":"CookieJar::Jar","cookies":[{"name":"foo","value":"bar","domain":"localhost.local","path":"\\/","created_at":"2009-09-11 12:51:03 -0600","expiry":"2028-11-01 12:00:00 GMT","secure":true}]}'
|
223
|
+
jar = JSON.parse json, create_additions: true
|
224
|
+
expect(jar.get_cookies('https://localhost/')).to have(1).items
|
228
225
|
end
|
229
226
|
end
|
230
227
|
rescue LoadError
|
231
|
-
it
|
232
|
-
|
228
|
+
it 'does not appear the JSON library is installed' do
|
229
|
+
raise 'please install the JSON library'
|
233
230
|
end
|
234
231
|
end
|
235
232
|
end
|