secobarbital-cookiejar 0.2.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cookiejar/cookie.rb +252 -0
- data/lib/cookiejar/cookie_validation.rb +400 -0
- data/lib/cookiejar/jar.rb +308 -0
- data/lib/cookiejar.rb +2 -0
- data/test/cookie_test.rb +176 -0
- data/test/cookie_validation_test.rb +251 -0
- data/test/jar_test.rb +226 -0
- metadata +73 -0
@@ -0,0 +1,251 @@
|
|
1
|
+
require 'cookiejar'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
include CookieJar
|
5
|
+
describe CookieValidation do
|
6
|
+
describe "#validate_cookie" do
|
7
|
+
localaddr = 'http://localhost/foo/bar/'
|
8
|
+
it "should fail if version unset" do
|
9
|
+
lambda do
|
10
|
+
unversioned = Cookie.from_set_cookie localaddr, 'foo=bar'
|
11
|
+
unversioned.instance_variable_set :@version, nil
|
12
|
+
CookieValidation.validate_cookie localaddr, unversioned
|
13
|
+
end.should raise_error InvalidCookieError
|
14
|
+
end
|
15
|
+
it "should fail if the path is more specific" do
|
16
|
+
lambda do
|
17
|
+
subdirred = Cookie.from_set_cookie localaddr, 'foo=bar;path=/foo/bar/baz'
|
18
|
+
# validate_cookie localaddr, subdirred
|
19
|
+
end.should raise_error InvalidCookieError
|
20
|
+
end
|
21
|
+
it "should fail if the path is different than the request" do
|
22
|
+
lambda do
|
23
|
+
difdirred = Cookie.from_set_cookie localaddr, 'foo=bar;path=/baz/'
|
24
|
+
# validate_cookie localaddr, difdirred
|
25
|
+
end.should raise_error InvalidCookieError
|
26
|
+
end
|
27
|
+
it "should fail if the domain has no dots" do
|
28
|
+
lambda do
|
29
|
+
nodot = Cookie.from_set_cookie 'http://zero/', 'foo=bar;domain=zero'
|
30
|
+
# validate_cookie 'http://zero/', nodot
|
31
|
+
end.should raise_error InvalidCookieError
|
32
|
+
end
|
33
|
+
it "should fail for explicit localhost" do
|
34
|
+
lambda do
|
35
|
+
localhost = Cookie.from_set_cookie localaddr, 'foo=bar;domain=localhost'
|
36
|
+
# validate_cookie localaddr, localhost
|
37
|
+
end.should raise_error InvalidCookieError
|
38
|
+
end
|
39
|
+
it "should fail for mismatched domains" do
|
40
|
+
lambda do
|
41
|
+
foobar = Cookie.from_set_cookie 'http://www.foo.com/', 'foo=bar;domain=bar.com'
|
42
|
+
# validate_cookie 'http://www.foo.com/', foobar
|
43
|
+
end.should raise_error InvalidCookieError
|
44
|
+
end
|
45
|
+
it "should fail for domains more than one level up" do
|
46
|
+
lambda do
|
47
|
+
xyz = Cookie.from_set_cookie 'http://x.y.z.com/', 'foo=bar;domain=z.com'
|
48
|
+
# validate_cookie 'http://x.y.z.com/', xyz
|
49
|
+
end.should raise_error InvalidCookieError
|
50
|
+
end
|
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
|
58
|
+
normal = Cookie.from_set_cookie 'http://foo.com/', 'foo=bar'
|
59
|
+
CookieValidation.validate_cookie('http://foo.com/', normal).should be_true
|
60
|
+
end
|
61
|
+
it "should handle a normal implicit localhost cookie" do
|
62
|
+
localhost = Cookie.from_set_cookie 'http://localhost/', 'foo=bar'
|
63
|
+
CookieValidation.validate_cookie('http://localhost/', localhost).should be_true
|
64
|
+
end
|
65
|
+
it "should handle an implicit IP address cookie" do
|
66
|
+
ipaddr = Cookie.from_set_cookie 'http://127.0.0.1/', 'foo=bar'
|
67
|
+
CookieValidation.validate_cookie('http://127.0.0.1/', ipaddr).should be_true
|
68
|
+
end
|
69
|
+
it "should handle an explicit domain on an internet site" do
|
70
|
+
explicit = Cookie.from_set_cookie 'http://foo.com/', 'foo=bar;domain=.foo.com'
|
71
|
+
CookieValidation.validate_cookie('http://foo.com/', explicit).should be_true
|
72
|
+
end
|
73
|
+
it "should handle setting a cookie explicitly on a superdomain" do
|
74
|
+
superdomain = Cookie.from_set_cookie 'http://auth.foo.com/', 'foo=bar;domain=.foo.com'
|
75
|
+
CookieValidation.validate_cookie('http://foo.com/', superdomain).should be_true
|
76
|
+
end
|
77
|
+
it "should handle explicitly setting a cookie" do
|
78
|
+
explicit = Cookie.from_set_cookie 'http://foo.com/bar/', 'foo=bar;path=/bar/'
|
79
|
+
CookieValidation.validate_cookie('http://foo.com/bar/', explicit)
|
80
|
+
end
|
81
|
+
it "should handle setting a cookie on a higher path" do
|
82
|
+
higher = Cookie.from_set_cookie 'http://foo.com/bar/baz/', 'foo=bar;path=/bar/'
|
83
|
+
CookieValidation.validate_cookie('http://foo.com/bar/baz/', higher)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
describe '#cookie_base_path' do
|
87
|
+
it "should leave '/' alone" do
|
88
|
+
CookieValidation.cookie_base_path('/').should == '/'
|
89
|
+
end
|
90
|
+
it "should strip off everything after the last '/'" do
|
91
|
+
CookieValidation.cookie_base_path('/foo/bar/baz').should == '/foo/bar/'
|
92
|
+
end
|
93
|
+
it "should handle query parameters and fragments with slashes" do
|
94
|
+
CookieValidation.cookie_base_path('/foo/bar?query=a/b/c#fragment/b/c').should == '/foo/'
|
95
|
+
end
|
96
|
+
it "should handle URI objects" do
|
97
|
+
CookieValidation.cookie_base_path(URI.parse('http://www.foo.com/bar/')).should == '/bar/'
|
98
|
+
end
|
99
|
+
it "should preserve case" do
|
100
|
+
CookieValidation.cookie_base_path("/BaR/").should == '/BaR/'
|
101
|
+
end
|
102
|
+
end
|
103
|
+
describe '#determine_cookie_path' do
|
104
|
+
it "should use the requested path when none is specified for the cookie" do
|
105
|
+
CookieValidation.determine_cookie_path('http://foo.com/', nil).should == '/'
|
106
|
+
CookieValidation.determine_cookie_path('http://foo.com/bar/baz', '').should == '/bar/'
|
107
|
+
end
|
108
|
+
it "should handle URI objects" do
|
109
|
+
CookieValidation.determine_cookie_path(URI.parse('http://foo.com/bar/'), '').should == '/bar/'
|
110
|
+
end
|
111
|
+
it "should handle Cookie objects" do
|
112
|
+
cookie = Cookie.from_set_cookie('http://foo.com/', "name=value;path=/")
|
113
|
+
CookieValidation.determine_cookie_path('http://foo.com/', cookie).should == '/'
|
114
|
+
end
|
115
|
+
it "should ignore the request when a path is specified" do
|
116
|
+
CookieValidation.determine_cookie_path('http://foo.com/ignorable/path', '/path/').should == '/path/'
|
117
|
+
end
|
118
|
+
end
|
119
|
+
describe '#compute_search_domains' do
|
120
|
+
it "should handle subdomains" do
|
121
|
+
CookieValidation.compute_search_domains('http://www.auth.foo.com/').should ==
|
122
|
+
['www.auth.foo.com', '.www.auth.foo.com', '.auth.foo.com']
|
123
|
+
end
|
124
|
+
it "should handle root domains" do
|
125
|
+
CookieValidation.compute_search_domains('http://foo.com/').should ==
|
126
|
+
['foo.com', '.foo.com']
|
127
|
+
end
|
128
|
+
it "should handle hexadecimal TLDs" do
|
129
|
+
CookieValidation.compute_search_domains('http://tiny.cc/').should ==
|
130
|
+
['tiny.cc', '.tiny.cc']
|
131
|
+
end
|
132
|
+
it "should handle IP addresses" do
|
133
|
+
CookieValidation.compute_search_domains('http://127.0.0.1/').should ==
|
134
|
+
['127.0.0.1']
|
135
|
+
end
|
136
|
+
it "should handle local addresses" do
|
137
|
+
CookieValidation.compute_search_domains('http://zero/').should ==
|
138
|
+
['zero.local', '.zero.local', '.local']
|
139
|
+
end
|
140
|
+
end
|
141
|
+
describe '#determine_cookie_domain' do
|
142
|
+
it "should add a dot to the front of domains" do
|
143
|
+
CookieValidation.determine_cookie_domain('http://foo.com/', 'foo.com').should == '.foo.com'
|
144
|
+
end
|
145
|
+
it "should not add a second dot if one present" do
|
146
|
+
CookieValidation.determine_cookie_domain('http://foo.com/', '.foo.com').should == '.foo.com'
|
147
|
+
end
|
148
|
+
it "should handle Cookie objects" do
|
149
|
+
c = Cookie.from_set_cookie('http://foo.com/', "foo=bar;domain=foo.com")
|
150
|
+
CookieValidation.determine_cookie_domain('http://foo.com/', c).should == '.foo.com'
|
151
|
+
end
|
152
|
+
it "should handle URI objects" do
|
153
|
+
CookieValidation.determine_cookie_domain(URI.parse('http://foo.com/'), '.foo.com').should == '.foo.com'
|
154
|
+
end
|
155
|
+
it "should use an exact hostname when no domain specified" do
|
156
|
+
CookieValidation.determine_cookie_domain('http://foo.com/', '').should == 'foo.com'
|
157
|
+
end
|
158
|
+
it "should leave IPv4 addresses alone" do
|
159
|
+
CookieValidation.determine_cookie_domain('http://127.0.0.1/', '127.0.0.1').should == '127.0.0.1'
|
160
|
+
end
|
161
|
+
it "should leave IPv6 addresses alone" do
|
162
|
+
['2001:db8:85a3::8a2e:370:7334', '::ffff:192.0.2.128'].each do |value|
|
163
|
+
CookieValidation.determine_cookie_domain("http://[#{value}]/", value).should == value
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
describe "#effective_host" do
|
168
|
+
it "should leave proper domains the same" do
|
169
|
+
['google.com', 'www.google.com', 'google.com.'].each do |value|
|
170
|
+
CookieValidation.effective_host(value).should == value
|
171
|
+
end
|
172
|
+
end
|
173
|
+
it "should leave blank domains blank" do
|
174
|
+
['', nil].each do |value|
|
175
|
+
CookieValidation.effective_host(value).should == ''
|
176
|
+
end
|
177
|
+
end
|
178
|
+
it "should handle a URI object" do
|
179
|
+
CookieValidation.effective_host(URI.parse('http://example.com/')).should == 'example.com'
|
180
|
+
end
|
181
|
+
it "should add a local suffix on unqualified hosts" do
|
182
|
+
CookieValidation.effective_host('localhost').should == 'localhost.local'
|
183
|
+
end
|
184
|
+
it "should leave IPv4 addresses alone" do
|
185
|
+
CookieValidation.effective_host('127.0.0.1').should == '127.0.0.1'
|
186
|
+
end
|
187
|
+
it "should leave IPv6 addresses alone" do
|
188
|
+
['2001:db8:85a3::8a2e:370:7334', ':ffff:192.0.2.128'].each do |value|
|
189
|
+
CookieValidation.effective_host(value).should == value
|
190
|
+
end
|
191
|
+
end
|
192
|
+
it "should lowercase addresses" do
|
193
|
+
CookieValidation.effective_host('FOO.COM').should == 'foo.com'
|
194
|
+
end
|
195
|
+
end
|
196
|
+
describe '#match_domains' do
|
197
|
+
it "should handle exact matches" do
|
198
|
+
CookieValidation.domains_match('localhost.local', 'localhost.local').should == 'localhost.local'
|
199
|
+
CookieValidation.domains_match('foo.com', 'foo.com').should == 'foo.com'
|
200
|
+
CookieValidation.domains_match('127.0.0.1', '127.0.0.1').should == '127.0.0.1'
|
201
|
+
CookieValidation.domains_match('::ffff:192.0.2.128', '::ffff:192.0.2.128').should == '::ffff:192.0.2.128'
|
202
|
+
end
|
203
|
+
it "should handle matching a superdomain" do
|
204
|
+
CookieValidation.domains_match('.foo.com', 'auth.foo.com').should == '.foo.com'
|
205
|
+
CookieValidation.domains_match('.y.z.foo.com', 'x.y.z.foo.com').should == '.y.z.foo.com'
|
206
|
+
end
|
207
|
+
it "should not match superdomains, or illegal domains" do
|
208
|
+
CookieValidation.domains_match('.z.foo.com', 'x.y.z.foo.com').should be_nil
|
209
|
+
CookieValidation.domains_match('foo.com', 'com').should be_nil
|
210
|
+
end
|
211
|
+
it "should not match blank domains" do
|
212
|
+
CookieValidation.domains_match('.foo.com', '').should be_nil
|
213
|
+
CookieValidation.domains_match('.foo.com', nil).should be_nil
|
214
|
+
CookieValidation.domains_match('', 'foo.com').should be_nil
|
215
|
+
CookieValidation.domains_match('', '').should be_nil
|
216
|
+
CookieValidation.domains_match('', nil).should be_nil
|
217
|
+
end
|
218
|
+
it "should not match domains with and without a dot suffix together" do
|
219
|
+
CookieValidation.domains_match('foo.com.', 'foo.com').should be_nil
|
220
|
+
end
|
221
|
+
end
|
222
|
+
describe '#hostname_reach' do
|
223
|
+
it "should find the next highest subdomain" do
|
224
|
+
{'www.google.com' => 'google.com', 'auth.corp.companyx.com' => 'corp.companyx.com'}.each do |entry|
|
225
|
+
CookieValidation.hostname_reach(entry[0]).should == entry[1]
|
226
|
+
end
|
227
|
+
end
|
228
|
+
it "should handle domains with suffixed dots" do
|
229
|
+
CookieValidation.hostname_reach('www.google.com.').should == 'google.com.'
|
230
|
+
end
|
231
|
+
it "should return nil for a root domain" do
|
232
|
+
CookieValidation.hostname_reach('github.com').should be_nil
|
233
|
+
end
|
234
|
+
it "should return 'local' for a local domain" do
|
235
|
+
['foo.local', 'foo.local.'].each do |hostname|
|
236
|
+
CookieValidation.hostname_reach(hostname).should == 'local'
|
237
|
+
end
|
238
|
+
end
|
239
|
+
it "should handle mixed-case '.local'" do
|
240
|
+
CookieValidation.hostname_reach('foo.LOCAL').should == 'local'
|
241
|
+
end
|
242
|
+
it "should return nil for an IPv4 address" do
|
243
|
+
CookieValidation.hostname_reach('127.0.0.1').should be_nil
|
244
|
+
end
|
245
|
+
it "should return nil for IPv6 addresses" do
|
246
|
+
['2001:db8:85a3::8a2e:370:7334', '::ffff:192.0.2.128'].each do |value|
|
247
|
+
CookieValidation.hostname_reach(value).should be_nil
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
data/test/jar_test.rb
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
require 'cookiejar'
|
2
|
+
require 'yaml'
|
3
|
+
require 'rubygems'
|
4
|
+
|
5
|
+
include CookieJar
|
6
|
+
|
7
|
+
describe Jar do
|
8
|
+
describe '.setCookie' do
|
9
|
+
it "should allow me to set a cookie" do
|
10
|
+
jar = Jar.new
|
11
|
+
jar.set_cookie 'http://foo.com/', 'foo=bar'
|
12
|
+
end
|
13
|
+
it "should allow me to set multiple cookies" do
|
14
|
+
jar = Jar.new
|
15
|
+
jar.set_cookie 'http://foo.com/', 'foo=bar'
|
16
|
+
jar.set_cookie 'http://foo.com/', 'bar=baz'
|
17
|
+
jar.set_cookie 'http://auth.foo.com/', 'foo=bar'
|
18
|
+
jar.set_cookie 'http://auth.foo.com/', 'auth=135121...;domain=foo.com'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
describe '.get_cookies' do
|
22
|
+
it "should let me read back cookies which are set" do
|
23
|
+
jar = Jar.new
|
24
|
+
jar.set_cookie 'http://foo.com/', 'foo=bar'
|
25
|
+
jar.set_cookie 'http://foo.com/', 'bar=baz'
|
26
|
+
jar.set_cookie 'http://auth.foo.com/', 'foo=bar'
|
27
|
+
jar.set_cookie 'http://auth.foo.com/', 'auth=135121...;domain=foo.com'
|
28
|
+
jar.get_cookies('http://foo.com/').should have(3).items
|
29
|
+
end
|
30
|
+
it "should return cookies longest path first" do
|
31
|
+
jar = Jar.new
|
32
|
+
uri = 'http://foo.com/a/b/c/d'
|
33
|
+
jar.set_cookie uri, 'a=bar'
|
34
|
+
jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
|
35
|
+
jar.set_cookie uri, 'c=bar;path=/a/b'
|
36
|
+
jar.set_cookie uri, 'd=bar;path=/a/'
|
37
|
+
cookies = jar.get_cookies(uri)
|
38
|
+
cookies.should have(4).items
|
39
|
+
cookies[0].name.should == 'b'
|
40
|
+
cookies[1].name.should == 'a'
|
41
|
+
cookies[2].name.should == 'c'
|
42
|
+
cookies[3].name.should == 'd'
|
43
|
+
end
|
44
|
+
it "should not return expired cookies" do
|
45
|
+
jar = Jar.new
|
46
|
+
uri = 'http://localhost/'
|
47
|
+
jar.set_cookie uri, 'foo=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
|
48
|
+
cookies = jar.get_cookies(uri)
|
49
|
+
cookies.should have(0).items
|
50
|
+
end
|
51
|
+
end
|
52
|
+
describe '.get_cookie_headers' do
|
53
|
+
it "should return cookie headers" do
|
54
|
+
jar = Jar.new
|
55
|
+
uri = 'http://foo.com/a/b/c/d'
|
56
|
+
jar.set_cookie uri, 'a=bar'
|
57
|
+
jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
|
58
|
+
cookie_headers = jar.get_cookie_header uri
|
59
|
+
cookie_headers.should == "b=baz;a=bar"
|
60
|
+
end
|
61
|
+
it "should handle a version 1 cookie" do
|
62
|
+
jar = Jar.new
|
63
|
+
uri = 'http://foo.com/a/b/c/d'
|
64
|
+
jar.set_cookie uri, 'a=bar'
|
65
|
+
jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
|
66
|
+
jar.set_cookie2 uri, 'c=baz;Version=1;path="/"'
|
67
|
+
cookie_headers = jar.get_cookie_header uri
|
68
|
+
cookie_headers.should == '$Version=0;b=baz;$Path="/a/b/c/d";a=bar;$Path="/a/b/c/",$Version=1;c=baz;$Path="/"'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
describe '.add_cookie' do
|
72
|
+
it "should let me add a pre-existing cookie" do
|
73
|
+
jar = Jar.new
|
74
|
+
cookie = Cookie.from_set_cookie 'http://localhost/', 'foo=bar'
|
75
|
+
jar.add_cookie cookie
|
76
|
+
end
|
77
|
+
end
|
78
|
+
describe '.to_a' do
|
79
|
+
it "should return me an array of all cookie objects" do
|
80
|
+
uri = 'http://foo.com/a/b/c/d'
|
81
|
+
jar = Jar.new
|
82
|
+
jar.set_cookie uri, 'a=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
|
83
|
+
jar.set_cookie uri, 'b=baz;path=/a/b/c/d'
|
84
|
+
jar.set_cookie uri, 'c=bar;path=/a/b'
|
85
|
+
jar.set_cookie uri, 'd=bar;path=/a/'
|
86
|
+
jar.set_cookie 'http://localhost/', 'foo=bar'
|
87
|
+
jar.to_a.should have(5).items
|
88
|
+
end
|
89
|
+
end
|
90
|
+
describe '.expire_cookies' do
|
91
|
+
it "should expire cookies which are no longer valid" do
|
92
|
+
uri = 'http://foo.com/a/b/c/d'
|
93
|
+
jar = Jar.new
|
94
|
+
jar.set_cookie uri, 'a=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
|
95
|
+
jar.set_cookie uri, 'b=baz;path=/a/b/c/d;expires=Wednesday, 01-Nov-2028 12:00:00 GMT'
|
96
|
+
jar.set_cookie uri, 'c=bar;path=/a/b'
|
97
|
+
jar.set_cookie uri, 'd=bar;path=/a/'
|
98
|
+
jar.set_cookie 'http://localhost/', 'foo=bar'
|
99
|
+
jar.to_a.should have(5).items
|
100
|
+
jar.expire_cookies
|
101
|
+
jar.to_a.should have(4).items
|
102
|
+
end
|
103
|
+
it "should let me expire all session cookies" do
|
104
|
+
uri = 'http://foo.com/a/b/c/d'
|
105
|
+
jar = Jar.new
|
106
|
+
jar.set_cookie uri, 'a=bar;expires=Wednesday, 09-Nov-99 23:12:40 GMT'
|
107
|
+
jar.set_cookie uri, 'b=baz;path=/a/b/c/d;expires=Wednesday, 01-Nov-2028 12:00:00 GMT'
|
108
|
+
jar.set_cookie uri, 'c=bar;path=/a/b'
|
109
|
+
jar.set_cookie uri, 'd=bar;path=/a/'
|
110
|
+
jar.set_cookie 'http://localhost/', 'foo=bar'
|
111
|
+
jar.to_a.should have(5).items
|
112
|
+
jar.expire_cookies true
|
113
|
+
jar.to_a.should have(1).items
|
114
|
+
end
|
115
|
+
end
|
116
|
+
describe '#set_cookies_from_headers' do
|
117
|
+
it "should handle a Set-Cookie header" do
|
118
|
+
jar = Jar.new
|
119
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
120
|
+
{ 'Set-Cookie' => 'foo=bar' }
|
121
|
+
cookies.should have(1).items
|
122
|
+
jar.to_a.should have(1).items
|
123
|
+
end
|
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
|
+
cookies.should have(1).items
|
129
|
+
jar.to_a.should have(1).items
|
130
|
+
end
|
131
|
+
it "should handle multiple Set-Cookie headers" do
|
132
|
+
jar = Jar.new
|
133
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
134
|
+
{ 'Set-Cookie' => ['foo=bar','bar=baz'] }
|
135
|
+
cookies.should have(2).items
|
136
|
+
jar.to_a.should have(2).items
|
137
|
+
end
|
138
|
+
it "should handle a Set-Cookie2 header" do
|
139
|
+
jar = Jar.new
|
140
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
141
|
+
{ 'Set-Cookie2' => 'foo=bar;Version=1' }
|
142
|
+
cookies.should have(1).items
|
143
|
+
jar.to_a.should have(1).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
|
+
cookies.should have(1).items
|
150
|
+
jar.to_a.should have(1).items
|
151
|
+
end
|
152
|
+
it "should handle multiple Set-Cookie2 headers" do
|
153
|
+
jar = Jar.new
|
154
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
155
|
+
{ 'Set-Cookie2' => ['foo=bar;Version=1','bar=baz;Version=1'] }
|
156
|
+
cookies.should have(2).items
|
157
|
+
jar.to_a.should have(2).items
|
158
|
+
end
|
159
|
+
it "should handle mixed distinct Set-Cookie and Set-Cookie2 headers" do
|
160
|
+
jar = Jar.new
|
161
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
162
|
+
{ 'Set-Cookie' => 'foo=bar',
|
163
|
+
'Set-Cookie2' => 'bar=baz;Version=1' }
|
164
|
+
cookies.should have(2).items
|
165
|
+
jar.to_a.should have(2).items
|
166
|
+
end
|
167
|
+
it "should handle overlapping Set-Cookie and Set-Cookie2 headers" do
|
168
|
+
jar = Jar.new
|
169
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
170
|
+
{ 'Set-Cookie' => ['foo=bar','bar=baz'],
|
171
|
+
'Set-Cookie2' => 'foo=bar;Version=1' }
|
172
|
+
cookies.should have(2).items
|
173
|
+
jar.to_a.should have(2).items
|
174
|
+
# and has the version 1 cookie
|
175
|
+
cookies.find do |cookie|
|
176
|
+
cookie.name == 'foo'
|
177
|
+
end.version.should == 1
|
178
|
+
end
|
179
|
+
it "should silently drop invalid cookies" do
|
180
|
+
jar = Jar.new
|
181
|
+
cookies = jar.set_cookies_from_headers 'http://localhost/',
|
182
|
+
{ 'Set-Cookie' => ['foo=bar','bar=baz;domain=.foo.com'] }
|
183
|
+
cookies.should have(1).items
|
184
|
+
jar.to_a.should have(1).items
|
185
|
+
end
|
186
|
+
end
|
187
|
+
begin
|
188
|
+
require 'json'
|
189
|
+
describe ".to_json" do
|
190
|
+
it "should serialize cookies to JSON" do
|
191
|
+
|
192
|
+
c = Cookie.from_set_cookie 'https://localhost/', 'foo=bar;secure;expires=Wed, 01-Nov-2028 12:00:00 GMT'
|
193
|
+
jar = Jar.new
|
194
|
+
jar.add_cookie c
|
195
|
+
json = jar.to_json
|
196
|
+
json.should be_a String
|
197
|
+
end
|
198
|
+
end
|
199
|
+
describe ".json_create" do
|
200
|
+
it "should deserialize a JSON array to a jar" do
|
201
|
+
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}]"
|
202
|
+
array = JSON.parse json
|
203
|
+
|
204
|
+
jar = Jar.json_create array
|
205
|
+
jar.get_cookies('https://localhost/').should have(1).items
|
206
|
+
end
|
207
|
+
it "should deserialize a JSON hash to a jar" do
|
208
|
+
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}]}"
|
209
|
+
hash = JSON.parse json
|
210
|
+
|
211
|
+
jar = Jar.json_create hash
|
212
|
+
jar.get_cookies('https://localhost/').should have(1).items
|
213
|
+
end
|
214
|
+
|
215
|
+
it "should automatically deserialize to a jar" do
|
216
|
+
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}]}"
|
217
|
+
jar = JSON.parse json
|
218
|
+
jar.get_cookies('https://localhost/').should have(1).items
|
219
|
+
end
|
220
|
+
end
|
221
|
+
rescue LoadError
|
222
|
+
it "does not appear the JSON library is installed" do
|
223
|
+
raise 'please install the JSON lirbary'
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: secobarbital-cookiejar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 9
|
9
|
+
- 1
|
10
|
+
version: 0.2.9.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- David Waite
|
14
|
+
- Seggy Umboh
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-06-04 00:00:00 -07:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: Allows for parsing and returning cookies in Ruby HTTP client code
|
24
|
+
email:
|
25
|
+
- david@alkaline-solutions.com
|
26
|
+
- sumboh@thingbuzz.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/cookiejar/cookie.rb
|
35
|
+
- lib/cookiejar/cookie_validation.rb
|
36
|
+
- lib/cookiejar/jar.rb
|
37
|
+
- lib/cookiejar.rb
|
38
|
+
- test/cookie_test.rb
|
39
|
+
- test/cookie_validation_test.rb
|
40
|
+
- test/jar_test.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://alkaline-solutions.com
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --title
|
48
|
+
- CookieJar -- Client-side HTTP Cookies
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.6
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Client-side HTTP Cookie library
|
72
|
+
test_files: []
|
73
|
+
|