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