http-cookie 0.1.0
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +24 -0
- data/README.md +139 -0
- data/Rakefile +26 -0
- data/http-cookie.gemspec +29 -0
- data/lib/http-cookie.rb +1 -0
- data/lib/http/cookie.rb +447 -0
- data/lib/http/cookie/version.rb +5 -0
- data/lib/http/cookie_jar.rb +233 -0
- data/lib/http/cookie_jar/abstract_saver.rb +53 -0
- data/lib/http/cookie_jar/abstract_store.rb +90 -0
- data/lib/http/cookie_jar/cookiestxt_saver.rb +74 -0
- data/lib/http/cookie_jar/hash_store.rb +141 -0
- data/lib/http/cookie_jar/yaml_saver.rb +37 -0
- data/test/helper.rb +33 -0
- data/test/simplecov_start.rb +2 -0
- data/test/test_http_cookie.rb +624 -0
- data/test/test_http_cookie_jar.rb +585 -0
- metadata +138 -0
@@ -0,0 +1,585 @@
|
|
1
|
+
require File.expand_path('helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class TestHTTPCookieJar < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@jar = HTTP::CookieJar.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def in_tmpdir
|
9
|
+
Dir.mktmpdir do |dir|
|
10
|
+
Dir.chdir dir do
|
11
|
+
yield
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def cookie_values(options = {})
|
17
|
+
{
|
18
|
+
:name => 'Foo',
|
19
|
+
:value => 'Bar',
|
20
|
+
:path => '/',
|
21
|
+
:expires => Time.at(Time.now.to_i + 10 * 86400), # to_i is important here
|
22
|
+
:for_domain => true,
|
23
|
+
:domain => 'rubyforge.org',
|
24
|
+
:origin => 'http://rubyforge.org/'
|
25
|
+
}.merge(options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_empty?
|
29
|
+
assert_equal true, @jar.empty?
|
30
|
+
cookie = HTTP::Cookie.new(cookie_values)
|
31
|
+
@jar.add(cookie)
|
32
|
+
assert_equal false, @jar.empty?
|
33
|
+
assert_equal false, @jar.empty?('http://rubyforge.org/')
|
34
|
+
assert_equal true, @jar.empty?('http://example.local/')
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_two_cookies_same_domain_and_name_different_paths
|
38
|
+
url = URI 'http://rubyforge.org/'
|
39
|
+
|
40
|
+
cookie = HTTP::Cookie.new(cookie_values)
|
41
|
+
@jar.add(cookie)
|
42
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:path => '/onetwo')))
|
43
|
+
|
44
|
+
assert_equal(1, @jar.cookies(url).length)
|
45
|
+
assert_equal 2, @jar.cookies(URI('http://rubyforge.org/onetwo')).length
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_domain_case
|
49
|
+
url = URI 'http://rubyforge.org/'
|
50
|
+
|
51
|
+
# Add one cookie with an expiration date in the future
|
52
|
+
cookie = HTTP::Cookie.new(cookie_values)
|
53
|
+
@jar.add(cookie)
|
54
|
+
assert_equal(1, @jar.cookies(url).length)
|
55
|
+
|
56
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:domain => 'RuByForge.Org', :name => 'aaron')))
|
57
|
+
|
58
|
+
assert_equal(2, @jar.cookies(url).length)
|
59
|
+
|
60
|
+
url2 = URI 'http://RuByFoRgE.oRg/'
|
61
|
+
assert_equal(2, @jar.cookies(url2).length)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_host_only
|
65
|
+
url = URI.parse('http://rubyforge.org/')
|
66
|
+
|
67
|
+
@jar.add(HTTP::Cookie.new(
|
68
|
+
cookie_values(:domain => 'rubyforge.org', :for_domain => false)))
|
69
|
+
|
70
|
+
assert_equal(1, @jar.cookies(url).length)
|
71
|
+
|
72
|
+
assert_equal(1, @jar.cookies(URI('http://RubyForge.org/')).length)
|
73
|
+
|
74
|
+
assert_equal(1, @jar.cookies(URI('https://RubyForge.org/')).length)
|
75
|
+
|
76
|
+
assert_equal(0, @jar.cookies(URI('http://www.rubyforge.org/')).length)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_empty_value
|
80
|
+
url = URI 'http://rubyforge.org/'
|
81
|
+
values = cookie_values(:value => "")
|
82
|
+
|
83
|
+
# Add one cookie with an expiration date in the future
|
84
|
+
cookie = HTTP::Cookie.new(values)
|
85
|
+
@jar.add(cookie)
|
86
|
+
assert_equal(1, @jar.cookies(url).length)
|
87
|
+
|
88
|
+
@jar.add HTTP::Cookie.new(values.merge(:domain => 'RuByForge.Org',
|
89
|
+
:name => 'aaron'))
|
90
|
+
|
91
|
+
assert_equal(2, @jar.cookies(url).length)
|
92
|
+
|
93
|
+
url2 = URI 'http://RuByFoRgE.oRg/'
|
94
|
+
assert_equal(2, @jar.cookies(url2).length)
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_add_future_cookies
|
98
|
+
url = URI 'http://rubyforge.org/'
|
99
|
+
|
100
|
+
# Add one cookie with an expiration date in the future
|
101
|
+
cookie = HTTP::Cookie.new(cookie_values)
|
102
|
+
@jar.add(cookie)
|
103
|
+
assert_equal(1, @jar.cookies(url).length)
|
104
|
+
|
105
|
+
# Add the same cookie, and we should still only have one
|
106
|
+
@jar.add(HTTP::Cookie.new(cookie_values))
|
107
|
+
assert_equal(1, @jar.cookies(url).length)
|
108
|
+
|
109
|
+
# Make sure we can get the cookie from different paths
|
110
|
+
assert_equal(1, @jar.cookies(URI('http://rubyforge.org/login')).length)
|
111
|
+
|
112
|
+
# Make sure we can't get the cookie from different domains
|
113
|
+
assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_add_multiple_cookies
|
117
|
+
url = URI 'http://rubyforge.org/'
|
118
|
+
|
119
|
+
# Add one cookie with an expiration date in the future
|
120
|
+
cookie = HTTP::Cookie.new(cookie_values)
|
121
|
+
@jar.add(cookie)
|
122
|
+
assert_equal(1, @jar.cookies(url).length)
|
123
|
+
|
124
|
+
# Add the same cookie, and we should still only have one
|
125
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz')))
|
126
|
+
assert_equal(2, @jar.cookies(url).length)
|
127
|
+
|
128
|
+
# Make sure we can get the cookie from different paths
|
129
|
+
assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
|
130
|
+
|
131
|
+
# Make sure we can't get the cookie from different domains
|
132
|
+
assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_add_multiple_cookies_with_the_same_name
|
136
|
+
now = Time.now
|
137
|
+
|
138
|
+
cookies = [
|
139
|
+
{ :value => 'a', :path => '/', },
|
140
|
+
{ :value => 'b', :path => '/abc/def/', :created_at => now - 1 },
|
141
|
+
{ :value => 'c', :path => '/abc/def/', :domain => 'www.rubyforge.org', :origin => 'http://www.rubyforge.org/abc/def/', :created_at => now },
|
142
|
+
{ :value => 'd', :path => '/abc/' },
|
143
|
+
].map { |attrs|
|
144
|
+
HTTP::Cookie.new(cookie_values(attrs))
|
145
|
+
}
|
146
|
+
|
147
|
+
url = URI 'http://www.rubyforge.org/abc/def/ghi'
|
148
|
+
|
149
|
+
cookies.permutation(cookies.size) { |shuffled|
|
150
|
+
@jar.clear
|
151
|
+
shuffled.each { |cookie| @jar.add(cookie) }
|
152
|
+
assert_equal %w[b c d a], @jar.cookies(url).map { |cookie| cookie.value }
|
153
|
+
}
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_fall_back_rules_for_local_domains
|
157
|
+
url = URI 'http://www.example.local'
|
158
|
+
|
159
|
+
sld_cookie = HTTP::Cookie.new(cookie_values(:domain => '.example.local', :origin => url))
|
160
|
+
@jar.add(sld_cookie)
|
161
|
+
|
162
|
+
assert_equal(1, @jar.cookies(url).length)
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_add_makes_exception_for_localhost
|
166
|
+
url = URI 'http://localhost'
|
167
|
+
|
168
|
+
tld_cookie = HTTP::Cookie.new(cookie_values(:domain => 'localhost', :origin => url))
|
169
|
+
@jar.add(tld_cookie)
|
170
|
+
|
171
|
+
assert_equal(1, @jar.cookies(url).length)
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_add_cookie_for_the_parent_domain
|
175
|
+
url = URI 'http://x.foo.com'
|
176
|
+
|
177
|
+
cookie = HTTP::Cookie.new(cookie_values(:domain => '.foo.com', :origin => url))
|
178
|
+
@jar.add(cookie)
|
179
|
+
|
180
|
+
assert_equal(1, @jar.cookies(url).length)
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_add_rejects_cookies_with_unknown_domain_or_path
|
184
|
+
cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :domain].include?(k) })
|
185
|
+
assert_raises(ArgumentError) {
|
186
|
+
@jar.add(cookie)
|
187
|
+
}
|
188
|
+
|
189
|
+
cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :path].include?(k) })
|
190
|
+
assert_raises(ArgumentError) {
|
191
|
+
@jar.add(cookie)
|
192
|
+
}
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_add_does_not_reject_cookies_from_a_nested_subdomain
|
196
|
+
url = URI 'http://y.x.foo.com'
|
197
|
+
|
198
|
+
cookie = HTTP::Cookie.new(cookie_values(:domain => '.foo.com', :origin => url))
|
199
|
+
@jar.add(cookie)
|
200
|
+
|
201
|
+
assert_equal(1, @jar.cookies(url).length)
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_cookie_without_leading_dot_does_not_cause_substring_match
|
205
|
+
url = URI 'http://arubyforge.org/'
|
206
|
+
|
207
|
+
cookie = HTTP::Cookie.new(cookie_values(:domain => 'rubyforge.org'))
|
208
|
+
@jar.add(cookie)
|
209
|
+
|
210
|
+
assert_equal(0, @jar.cookies(url).length)
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_cookie_without_leading_dot_matches_subdomains
|
214
|
+
url = URI 'http://admin.rubyforge.org/'
|
215
|
+
|
216
|
+
cookie = HTTP::Cookie.new(cookie_values(:domain => 'rubyforge.org', :origin => url))
|
217
|
+
@jar.add(cookie)
|
218
|
+
|
219
|
+
assert_equal(1, @jar.cookies(url).length)
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_cookies_with_leading_dot_match_subdomains
|
223
|
+
url = URI 'http://admin.rubyforge.org/'
|
224
|
+
|
225
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org', :origin => url)))
|
226
|
+
|
227
|
+
assert_equal(1, @jar.cookies(url).length)
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_cookies_with_leading_dot_match_parent_domains
|
231
|
+
url = URI 'http://rubyforge.org/'
|
232
|
+
|
233
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org', :origin => url)))
|
234
|
+
|
235
|
+
assert_equal(1, @jar.cookies(url).length)
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_cookies_with_leading_dot_match_parent_domains_exactly
|
239
|
+
url = URI 'http://arubyforge.org/'
|
240
|
+
|
241
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
|
242
|
+
|
243
|
+
assert_equal(0, @jar.cookies(url).length)
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_cookie_for_ipv4_address_matches_the_exact_ipaddress
|
247
|
+
url = URI 'http://192.168.0.1/'
|
248
|
+
|
249
|
+
cookie = HTTP::Cookie.new(cookie_values(:domain => '192.168.0.1', :origin => url))
|
250
|
+
@jar.add(cookie)
|
251
|
+
|
252
|
+
assert_equal(1, @jar.cookies(url).length)
|
253
|
+
end
|
254
|
+
|
255
|
+
def test_cookie_for_ipv6_address_matches_the_exact_ipaddress
|
256
|
+
url = URI 'http://[fe80::0123:4567:89ab:cdef]/'
|
257
|
+
|
258
|
+
cookie = HTTP::Cookie.new(cookie_values(:domain => '[fe80::0123:4567:89ab:cdef]', :origin => url))
|
259
|
+
@jar.add(cookie)
|
260
|
+
|
261
|
+
assert_equal(1, @jar.cookies(url).length)
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_cookies_dot
|
265
|
+
url = URI 'http://www.host.example/'
|
266
|
+
|
267
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:domain => 'www.host.example', :origin => url)))
|
268
|
+
|
269
|
+
url = URI 'http://wwwxhost.example/'
|
270
|
+
assert_equal(0, @jar.cookies(url).length)
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_clear
|
274
|
+
url = URI 'http://rubyforge.org/'
|
275
|
+
|
276
|
+
# Add one cookie with an expiration date in the future
|
277
|
+
cookie = HTTP::Cookie.new(cookie_values(:origin => url))
|
278
|
+
@jar.add(cookie)
|
279
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz', :origin => url)))
|
280
|
+
assert_equal(2, @jar.cookies(url).length)
|
281
|
+
|
282
|
+
@jar.clear
|
283
|
+
|
284
|
+
assert_equal(0, @jar.cookies(url).length)
|
285
|
+
end
|
286
|
+
|
287
|
+
def test_save_cookies_yaml
|
288
|
+
url = URI 'http://rubyforge.org/'
|
289
|
+
|
290
|
+
# Add one cookie with an expiration date in the future
|
291
|
+
cookie = HTTP::Cookie.new(cookie_values(:origin => url))
|
292
|
+
s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar',
|
293
|
+
:expires => nil,
|
294
|
+
:session => true,
|
295
|
+
:origin => url))
|
296
|
+
|
297
|
+
@jar.add(cookie)
|
298
|
+
@jar.add(s_cookie)
|
299
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz', :for_domain => false, :origin => url)))
|
300
|
+
|
301
|
+
assert_equal(3, @jar.cookies(url).length)
|
302
|
+
|
303
|
+
in_tmpdir do
|
304
|
+
value = @jar.save("cookies.yml")
|
305
|
+
assert_same @jar, value
|
306
|
+
|
307
|
+
jar = HTTP::CookieJar.new
|
308
|
+
jar.load("cookies.yml")
|
309
|
+
cookies = jar.cookies(url).sort_by { |cookie| cookie.name }
|
310
|
+
assert_equal(2, cookies.length)
|
311
|
+
assert_equal('Baz', cookies[0].name)
|
312
|
+
assert_equal(false, cookies[0].for_domain)
|
313
|
+
assert_equal('Foo', cookies[1].name)
|
314
|
+
assert_equal(true, cookies[1].for_domain)
|
315
|
+
end
|
316
|
+
|
317
|
+
assert_equal(3, @jar.cookies(url).length)
|
318
|
+
end
|
319
|
+
|
320
|
+
def test_save_session_cookies_yaml
|
321
|
+
url = URI 'http://rubyforge.org/'
|
322
|
+
|
323
|
+
# Add one cookie with an expiration date in the future
|
324
|
+
cookie = HTTP::Cookie.new(cookie_values)
|
325
|
+
s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar',
|
326
|
+
:expires => nil,
|
327
|
+
:session => true))
|
328
|
+
|
329
|
+
@jar.add(cookie)
|
330
|
+
@jar.add(s_cookie)
|
331
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz')))
|
332
|
+
|
333
|
+
assert_equal(3, @jar.cookies(url).length)
|
334
|
+
|
335
|
+
in_tmpdir do
|
336
|
+
@jar.save("cookies.yml", :format => :yaml, :session => true)
|
337
|
+
|
338
|
+
jar = HTTP::CookieJar.new
|
339
|
+
jar.load("cookies.yml")
|
340
|
+
assert_equal(3, jar.cookies(url).length)
|
341
|
+
end
|
342
|
+
|
343
|
+
assert_equal(3, @jar.cookies(url).length)
|
344
|
+
end
|
345
|
+
|
346
|
+
|
347
|
+
def test_save_and_read_cookiestxt
|
348
|
+
url = URI 'http://rubyforge.org/foo/'
|
349
|
+
|
350
|
+
# Add one cookie with an expiration date in the future
|
351
|
+
cookie = HTTP::Cookie.new(cookie_values)
|
352
|
+
expires = cookie.expires
|
353
|
+
s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar',
|
354
|
+
:expires => nil,
|
355
|
+
:session => true))
|
356
|
+
cookie2 = HTTP::Cookie.new(cookie_values(:name => 'Baz',
|
357
|
+
:value => 'Foo#Baz',
|
358
|
+
:path => '/foo/',
|
359
|
+
:for_domain => false))
|
360
|
+
|
361
|
+
@jar.add(cookie)
|
362
|
+
@jar.add(s_cookie)
|
363
|
+
@jar.add(cookie2)
|
364
|
+
|
365
|
+
assert_equal(3, @jar.cookies(url).length)
|
366
|
+
|
367
|
+
in_tmpdir do
|
368
|
+
@jar.save("cookies.txt", :cookiestxt)
|
369
|
+
|
370
|
+
jar = HTTP::CookieJar.new
|
371
|
+
jar.load("cookies.txt", :cookiestxt) # HACK test the format
|
372
|
+
cookies = jar.cookies(url)
|
373
|
+
assert_equal(2, cookies.length)
|
374
|
+
cookies.each { |cookie|
|
375
|
+
case cookie.name
|
376
|
+
when 'Foo'
|
377
|
+
assert_equal 'Bar', cookie.value
|
378
|
+
assert_equal expires, cookie.expires
|
379
|
+
assert_equal 'rubyforge.org', cookie.domain
|
380
|
+
assert_equal true, cookie.for_domain
|
381
|
+
assert_equal '/', cookie.path
|
382
|
+
when 'Baz'
|
383
|
+
assert_equal 'Foo#Baz', cookie.value
|
384
|
+
assert_equal 'rubyforge.org', cookie.domain
|
385
|
+
assert_equal false, cookie.for_domain
|
386
|
+
assert_equal '/foo/', cookie.path
|
387
|
+
else
|
388
|
+
raise
|
389
|
+
end
|
390
|
+
}
|
391
|
+
end
|
392
|
+
|
393
|
+
assert_equal(3, @jar.cookies(url).length)
|
394
|
+
end
|
395
|
+
|
396
|
+
def test_expire_cookies
|
397
|
+
url = URI 'http://rubyforge.org/'
|
398
|
+
|
399
|
+
# Add one cookie with an expiration date in the future
|
400
|
+
cookie = HTTP::Cookie.new(cookie_values)
|
401
|
+
@jar.add(cookie)
|
402
|
+
assert_equal(1, @jar.cookies(url).length)
|
403
|
+
|
404
|
+
# Add a second cookie
|
405
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz')))
|
406
|
+
assert_equal(2, @jar.cookies(url).length)
|
407
|
+
|
408
|
+
# Make sure we can get the cookie from different paths
|
409
|
+
assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
|
410
|
+
|
411
|
+
# Expire the first cookie
|
412
|
+
@jar.add(HTTP::Cookie.new(cookie_values(:expires => Time.now - (10 * 86400))))
|
413
|
+
assert_equal(1, @jar.cookies(url).length)
|
414
|
+
|
415
|
+
# Expire the second cookie
|
416
|
+
@jar.add(HTTP::Cookie.new(cookie_values( :name => 'Baz', :expires => Time.now - (10 * 86400))))
|
417
|
+
assert_equal(0, @jar.cookies(url).length)
|
418
|
+
end
|
419
|
+
|
420
|
+
def test_session_cookies
|
421
|
+
values = cookie_values(:expires => nil)
|
422
|
+
url = URI 'http://rubyforge.org/'
|
423
|
+
|
424
|
+
# Add one cookie with an expiration date in the future
|
425
|
+
cookie = HTTP::Cookie.new(values)
|
426
|
+
@jar.add(cookie)
|
427
|
+
assert_equal(1, @jar.cookies(url).length)
|
428
|
+
|
429
|
+
# Add a second cookie
|
430
|
+
@jar.add(HTTP::Cookie.new(values.merge(:name => 'Baz')))
|
431
|
+
assert_equal(2, @jar.cookies(url).length)
|
432
|
+
|
433
|
+
# Make sure we can get the cookie from different paths
|
434
|
+
assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
|
435
|
+
|
436
|
+
# Expire the first cookie
|
437
|
+
@jar.add(HTTP::Cookie.new(values.merge(:expires => Time.now - (10 * 86400))))
|
438
|
+
assert_equal(1, @jar.cookies(url).length)
|
439
|
+
|
440
|
+
# Expire the second cookie
|
441
|
+
@jar.add(HTTP::Cookie.new(values.merge(:name => 'Baz', :expires => Time.now - (10 * 86400))))
|
442
|
+
assert_equal(0, @jar.cookies(url).length)
|
443
|
+
|
444
|
+
# When given a URI with a blank path, CookieJar#cookies should return
|
445
|
+
# cookies with the path '/':
|
446
|
+
url = URI 'http://rubyforge.org'
|
447
|
+
assert_equal '', url.path
|
448
|
+
assert_equal(0, @jar.cookies(url).length)
|
449
|
+
# Now add a cookie with the path set to '/':
|
450
|
+
@jar.add(HTTP::Cookie.new(values.merge(:name => 'has_root_path', :path => '/')))
|
451
|
+
assert_equal(1, @jar.cookies(url).length)
|
452
|
+
end
|
453
|
+
|
454
|
+
def test_paths
|
455
|
+
url = URI 'http://rubyforge.org/login'
|
456
|
+
values = cookie_values(:path => "/login", :expires => nil, :origin => url)
|
457
|
+
|
458
|
+
# Add one cookie with an expiration date in the future
|
459
|
+
cookie = HTTP::Cookie.new(values)
|
460
|
+
@jar.add(cookie)
|
461
|
+
assert_equal(1, @jar.cookies(url).length)
|
462
|
+
|
463
|
+
# Add a second cookie
|
464
|
+
@jar.add(HTTP::Cookie.new(values.merge( :name => 'Baz' )))
|
465
|
+
assert_equal(2, @jar.cookies(url).length)
|
466
|
+
|
467
|
+
# Make sure we don't get the cookie in a different path
|
468
|
+
assert_equal(0, @jar.cookies(URI('http://rubyforge.org/hello')).length)
|
469
|
+
assert_equal(0, @jar.cookies(URI('http://rubyforge.org/')).length)
|
470
|
+
|
471
|
+
# Expire the first cookie
|
472
|
+
@jar.add(HTTP::Cookie.new(values.merge( :expires => Time.now - (10 * 86400))))
|
473
|
+
assert_equal(1, @jar.cookies(url).length)
|
474
|
+
|
475
|
+
# Expire the second cookie
|
476
|
+
@jar.add(HTTP::Cookie.new(values.merge( :name => 'Baz',
|
477
|
+
:expires => Time.now - (10 * 86400))))
|
478
|
+
assert_equal(0, @jar.cookies(url).length)
|
479
|
+
end
|
480
|
+
|
481
|
+
def test_ssl_cookies
|
482
|
+
# thanks to michal "ocher" ochman for reporting the bug responsible for this test.
|
483
|
+
values = cookie_values(:expires => nil)
|
484
|
+
values_ssl = values.merge(:name => 'Baz', :domain => "#{values[:domain]}:443")
|
485
|
+
url = URI 'https://rubyforge.org/login'
|
486
|
+
|
487
|
+
cookie = HTTP::Cookie.new(values)
|
488
|
+
@jar.add(cookie)
|
489
|
+
assert_equal(1, @jar.cookies(url).length, "did not handle SSL cookie")
|
490
|
+
|
491
|
+
cookie = HTTP::Cookie.new(values_ssl)
|
492
|
+
@jar.add(cookie)
|
493
|
+
assert_equal(2, @jar.cookies(url).length, "did not handle SSL cookie with :443")
|
494
|
+
end
|
495
|
+
|
496
|
+
def test_secure_cookie
|
497
|
+
nurl = URI 'http://rubyforge.org/login'
|
498
|
+
surl = URI 'https://rubyforge.org/login'
|
499
|
+
|
500
|
+
nncookie = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => nurl))
|
501
|
+
sncookie = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => surl))
|
502
|
+
nscookie = HTTP::Cookie.new(cookie_values(:name => 'Foo2', :secure => true, :origin => nurl))
|
503
|
+
sscookie = HTTP::Cookie.new(cookie_values(:name => 'Foo2', :secure => true, :origin => surl))
|
504
|
+
|
505
|
+
@jar.add(nncookie)
|
506
|
+
@jar.add(sncookie)
|
507
|
+
@jar.add(nscookie)
|
508
|
+
@jar.add(sscookie)
|
509
|
+
|
510
|
+
assert_equal('Foo1', @jar.cookies(nurl).map { |c| c.name }.sort.join(' ') )
|
511
|
+
assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
|
512
|
+
end
|
513
|
+
|
514
|
+
def test_max_cookies
|
515
|
+
jar = HTTP::CookieJar.new
|
516
|
+
limit_per_domain = HTTP::Cookie::MAX_COOKIES_PER_DOMAIN
|
517
|
+
uri = URI('http://www.example.org/')
|
518
|
+
date = Time.at(Time.now.to_i + 86400)
|
519
|
+
(1..(limit_per_domain + 1)).each { |i|
|
520
|
+
jar << HTTP::Cookie.new(cookie_values(
|
521
|
+
:name => 'Foo%d' % i,
|
522
|
+
:value => 'Bar%d' % i,
|
523
|
+
:domain => uri.host,
|
524
|
+
:for_domain => true,
|
525
|
+
:path => '/dir%d/' % (i / 2),
|
526
|
+
:origin => uri,
|
527
|
+
)).tap { |cookie|
|
528
|
+
cookie.created_at = i == 42 ? date - i : date
|
529
|
+
}
|
530
|
+
}
|
531
|
+
assert_equal limit_per_domain + 1, jar.to_a.size
|
532
|
+
jar.cleanup
|
533
|
+
count = jar.to_a.size
|
534
|
+
assert_equal limit_per_domain, count
|
535
|
+
assert_equal [*1..41] + [*43..(limit_per_domain + 1)], jar.map { |cookie|
|
536
|
+
cookie.name[/(?<=^Foo)(\d+)$/].to_i
|
537
|
+
}
|
538
|
+
|
539
|
+
hlimit = HTTP::Cookie::MAX_COOKIES_TOTAL
|
540
|
+
slimit = hlimit + HTTP::CookieJar::HashStore::GC_THRESHOLD
|
541
|
+
|
542
|
+
n = hlimit / limit_per_domain * 2
|
543
|
+
|
544
|
+
(1..n).each { |i|
|
545
|
+
(1..(limit_per_domain + 1)).each { |j|
|
546
|
+
uri = URI('http://www%d.example.jp/' % i)
|
547
|
+
jar << HTTP::Cookie.new(cookie_values(
|
548
|
+
:name => 'Baz%d' % j,
|
549
|
+
:value => 'www%d.example.jp' % j,
|
550
|
+
:domain => uri.host,
|
551
|
+
:for_domain => true,
|
552
|
+
:path => '/dir%d/' % (i / 2),
|
553
|
+
:origin => uri,
|
554
|
+
)).tap { |cookie|
|
555
|
+
cookie.created_at = i == j ? date - i : date
|
556
|
+
}
|
557
|
+
count += 1
|
558
|
+
}
|
559
|
+
}
|
560
|
+
|
561
|
+
assert_equal true, count > slimit
|
562
|
+
assert_equal true, jar.to_a.size <= slimit
|
563
|
+
jar.cleanup
|
564
|
+
assert_equal hlimit, jar.to_a.size
|
565
|
+
assert_equal false, jar.any? { |cookie|
|
566
|
+
cookie.domain == cookie.value
|
567
|
+
}
|
568
|
+
end
|
569
|
+
|
570
|
+
def test_migration
|
571
|
+
cookie = HTTP::Cookie.new(cookie_values)
|
572
|
+
assert_raises_with_message(ArgumentError, /equivalent/) {
|
573
|
+
@jar.add('http://example.com/', cookie)
|
574
|
+
}
|
575
|
+
assert_raises_with_message(NoMethodError, /equivalent/) {
|
576
|
+
@jar.add!(cookie)
|
577
|
+
}
|
578
|
+
assert_raises_with_message(NoMethodError, /equivalent/) {
|
579
|
+
@jar.clear!()
|
580
|
+
}
|
581
|
+
assert_raises_with_message(NoMethodError, /equivalent/) {
|
582
|
+
@jar.save_as('/dev/null')
|
583
|
+
}
|
584
|
+
end
|
585
|
+
end
|