cookiejar2 0.3.4
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 +7 -0
- data/.circleci/config.yml +15 -0
- data/.gitignore +8 -0
- data/.rspec +2 -0
- data/.travis.yml +17 -0
- data/Gemfile +2 -0
- data/LICENSE +10 -0
- data/README.markdown +37 -0
- data/Rakefile +30 -0
- data/_config.yml +1 -0
- data/contributors.json +14 -0
- data/cookiejar.gemspec +28 -0
- data/lib/cookiejar/cookie.rb +257 -0
- data/lib/cookiejar/cookie_validation.rb +410 -0
- data/lib/cookiejar/jar.rb +314 -0
- data/lib/cookiejar/version.rb +4 -0
- data/lib/cookiejar.rb +3 -0
- data/spec/cookie_spec.rb +176 -0
- data/spec/cookie_validation_spec.rb +236 -0
- data/spec/jar_spec.rb +232 -0
- data/spec/spec_helper.rb +9 -0
- metadata +139 -0
@@ -0,0 +1,236 @@
|
|
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
ADDED
@@ -0,0 +1,232 @@
|
|
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
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cookiejar2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Waite
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-collection_matchers
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Allows for parsing and returning cookies in Ruby HTTP client code
|
84
|
+
email:
|
85
|
+
- david@alkaline-solutions.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".circleci/config.yml"
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE
|
96
|
+
- README.markdown
|
97
|
+
- Rakefile
|
98
|
+
- _config.yml
|
99
|
+
- contributors.json
|
100
|
+
- cookiejar.gemspec
|
101
|
+
- lib/cookiejar.rb
|
102
|
+
- lib/cookiejar/cookie.rb
|
103
|
+
- lib/cookiejar/cookie_validation.rb
|
104
|
+
- lib/cookiejar/jar.rb
|
105
|
+
- lib/cookiejar/version.rb
|
106
|
+
- spec/cookie_spec.rb
|
107
|
+
- spec/cookie_validation_spec.rb
|
108
|
+
- spec/jar_spec.rb
|
109
|
+
- spec/spec_helper.rb
|
110
|
+
homepage: http://alkaline-solutions.com
|
111
|
+
licenses:
|
112
|
+
- BSD-2-Clause
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options:
|
116
|
+
- "--title"
|
117
|
+
- CookieJar -- Client-side HTTP Cookies
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubygems_version: 3.3.13
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Client-side HTTP Cookie library
|
135
|
+
test_files:
|
136
|
+
- spec/cookie_spec.rb
|
137
|
+
- spec/cookie_validation_spec.rb
|
138
|
+
- spec/jar_spec.rb
|
139
|
+
- spec/spec_helper.rb
|