http-cookie 1.0.3 → 1.1.6

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.
@@ -1,985 +0,0 @@
1
- require File.expand_path('helper', File.dirname(__FILE__))
2
- require 'tmpdir'
3
-
4
- module TestHTTPCookieJar
5
- class TestAutoloading < Test::Unit::TestCase
6
- def test_nonexistent_store
7
- assert_raises(NameError) {
8
- HTTP::CookieJar::NonexistentStore
9
- }
10
- end
11
-
12
- def test_erroneous_store
13
- Dir.mktmpdir { |dir|
14
- Dir.mkdir(File.join(dir, 'http'))
15
- Dir.mkdir(File.join(dir, 'http', 'cookie_jar'))
16
- rb = File.join(dir, 'http', 'cookie_jar', 'erroneous_store.rb')
17
- File.open(rb, 'w').close
18
- $LOAD_PATH.unshift(dir)
19
-
20
- assert_raises(NameError) {
21
- HTTP::CookieJar::ErroneousStore
22
- }
23
- if RUBY_VERSION >= "1.9"
24
- assert_includes $LOADED_FEATURES, rb
25
- else
26
- assert_includes $LOADED_FEATURES, rb[(dir.size + 1)..-1]
27
- end
28
- }
29
- end
30
-
31
- def test_nonexistent_saver
32
- assert_raises(NameError) {
33
- HTTP::CookieJar::NonexistentSaver
34
- }
35
- end
36
-
37
- def test_erroneous_saver
38
- Dir.mktmpdir { |dir|
39
- Dir.mkdir(File.join(dir, 'http'))
40
- Dir.mkdir(File.join(dir, 'http', 'cookie_jar'))
41
- rb = File.join(dir, 'http', 'cookie_jar', 'erroneous_saver.rb')
42
- File.open(rb, 'w').close
43
- $LOAD_PATH.unshift(dir)
44
-
45
- assert_raises(NameError) {
46
- HTTP::CookieJar::ErroneousSaver
47
- }
48
- if RUBY_VERSION >= "1.9"
49
- assert_includes $LOADED_FEATURES, rb
50
- else
51
- assert_includes $LOADED_FEATURES, rb[(dir.size + 1)..-1]
52
- end
53
- }
54
- end
55
- end
56
-
57
- module CommonTests
58
- def setup(options = nil, options2 = nil)
59
- default_options = {
60
- :store => :hash,
61
- :gc_threshold => 1500, # increased by 10 for shorter test time
62
- }
63
- new_options = default_options.merge(options || {})
64
- new_options2 = new_options.merge(options2 || {})
65
- @store_type = new_options[:store]
66
- @gc_threshold = new_options[:gc_threshold]
67
- @jar = HTTP::CookieJar.new(new_options)
68
- @jar2 = HTTP::CookieJar.new(new_options2)
69
- end
70
-
71
- #def hash_store?
72
- # @store_type == :hash
73
- #end
74
-
75
- def mozilla_store?
76
- @store_type == :mozilla
77
- end
78
-
79
- def cookie_values(options = {})
80
- {
81
- :name => 'Foo',
82
- :value => 'Bar',
83
- :path => '/',
84
- :expires => Time.at(Time.now.to_i + 10 * 86400), # to_i is important here
85
- :for_domain => true,
86
- :domain => 'rubyforge.org',
87
- :origin => 'http://rubyforge.org/'
88
- }.merge(options)
89
- end
90
-
91
- def test_empty?
92
- assert_equal true, @jar.empty?
93
- cookie = HTTP::Cookie.new(cookie_values)
94
- @jar.add(cookie)
95
- assert_equal false, @jar.empty?
96
- assert_equal false, @jar.empty?('http://rubyforge.org/')
97
- assert_equal true, @jar.empty?('http://example.local/')
98
- end
99
-
100
- def test_two_cookies_same_domain_and_name_different_paths
101
- url = URI 'http://rubyforge.org/'
102
-
103
- cookie = HTTP::Cookie.new(cookie_values)
104
- @jar.add(cookie)
105
- @jar.add(HTTP::Cookie.new(cookie_values(:path => '/onetwo')))
106
-
107
- assert_equal(1, @jar.cookies(url).length)
108
- assert_equal 2, @jar.cookies(URI('http://rubyforge.org/onetwo')).length
109
- end
110
-
111
- def test_domain_case
112
- url = URI 'http://rubyforge.org/'
113
-
114
- # Add one cookie with an expiration date in the future
115
- cookie = HTTP::Cookie.new(cookie_values)
116
- @jar.add(cookie)
117
- assert_equal(1, @jar.cookies(url).length)
118
-
119
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => 'RuByForge.Org', :name => 'aaron')))
120
-
121
- assert_equal(2, @jar.cookies(url).length)
122
-
123
- url2 = URI 'http://RuByFoRgE.oRg/'
124
- assert_equal(2, @jar.cookies(url2).length)
125
- end
126
-
127
- def test_host_only
128
- url = URI.parse('http://rubyforge.org/')
129
-
130
- @jar.add(HTTP::Cookie.new(
131
- cookie_values(:domain => 'rubyforge.org', :for_domain => false)))
132
-
133
- assert_equal(1, @jar.cookies(url).length)
134
-
135
- assert_equal(1, @jar.cookies(URI('http://RubyForge.org/')).length)
136
-
137
- assert_equal(1, @jar.cookies(URI('https://RubyForge.org/')).length)
138
-
139
- assert_equal(0, @jar.cookies(URI('http://www.rubyforge.org/')).length)
140
- end
141
-
142
- def test_empty_value
143
- url = URI 'http://rubyforge.org/'
144
- values = cookie_values(:value => "")
145
-
146
- # Add one cookie with an expiration date in the future
147
- cookie = HTTP::Cookie.new(values)
148
- @jar.add(cookie)
149
- assert_equal(1, @jar.cookies(url).length)
150
-
151
- @jar.add HTTP::Cookie.new(values.merge(:domain => 'RuByForge.Org',
152
- :name => 'aaron'))
153
-
154
- assert_equal(2, @jar.cookies(url).length)
155
-
156
- url2 = URI 'http://RuByFoRgE.oRg/'
157
- assert_equal(2, @jar.cookies(url2).length)
158
- end
159
-
160
- def test_add_future_cookies
161
- url = URI 'http://rubyforge.org/'
162
-
163
- # Add one cookie with an expiration date in the future
164
- cookie = HTTP::Cookie.new(cookie_values)
165
- @jar.add(cookie)
166
- assert_equal(1, @jar.cookies(url).length)
167
-
168
- # Add the same cookie, and we should still only have one
169
- @jar.add(HTTP::Cookie.new(cookie_values))
170
- assert_equal(1, @jar.cookies(url).length)
171
-
172
- # Make sure we can get the cookie from different paths
173
- assert_equal(1, @jar.cookies(URI('http://rubyforge.org/login')).length)
174
-
175
- # Make sure we can't get the cookie from different domains
176
- assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
177
- end
178
-
179
- def test_add_multiple_cookies
180
- url = URI 'http://rubyforge.org/'
181
-
182
- # Add one cookie with an expiration date in the future
183
- cookie = HTTP::Cookie.new(cookie_values)
184
- @jar.add(cookie)
185
- assert_equal(1, @jar.cookies(url).length)
186
-
187
- # Add the same cookie, and we should still only have one
188
- @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz')))
189
- assert_equal(2, @jar.cookies(url).length)
190
-
191
- # Make sure we can get the cookie from different paths
192
- assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
193
-
194
- # Make sure we can't get the cookie from different domains
195
- assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
196
- end
197
-
198
- def test_add_multiple_cookies_with_the_same_name
199
- now = Time.now
200
-
201
- cookies = [
202
- { :value => 'a', :path => '/', },
203
- { :value => 'b', :path => '/abc/def/', :created_at => now - 1 },
204
- { :value => 'c', :path => '/abc/def/', :domain => 'www.rubyforge.org', :origin => 'http://www.rubyforge.org/abc/def/', :created_at => now },
205
- { :value => 'd', :path => '/abc/' },
206
- ].map { |attrs|
207
- HTTP::Cookie.new(cookie_values(attrs))
208
- }
209
-
210
- url = URI 'http://www.rubyforge.org/abc/def/ghi'
211
-
212
- cookies.permutation(cookies.size) { |shuffled|
213
- @jar.clear
214
- shuffled.each { |cookie| @jar.add(cookie) }
215
- assert_equal %w[b c d a], @jar.cookies(url).map { |cookie| cookie.value }
216
- }
217
- end
218
-
219
- def test_fall_back_rules_for_local_domains
220
- url = URI 'http://www.example.local'
221
-
222
- sld_cookie = HTTP::Cookie.new(cookie_values(:domain => '.example.local', :origin => url))
223
- @jar.add(sld_cookie)
224
-
225
- assert_equal(1, @jar.cookies(url).length)
226
- end
227
-
228
- def test_add_makes_exception_for_localhost
229
- url = URI 'http://localhost'
230
-
231
- tld_cookie = HTTP::Cookie.new(cookie_values(:domain => 'localhost', :origin => url))
232
- @jar.add(tld_cookie)
233
-
234
- assert_equal(1, @jar.cookies(url).length)
235
- end
236
-
237
- def test_add_cookie_for_the_parent_domain
238
- url = URI 'http://x.foo.com'
239
-
240
- cookie = HTTP::Cookie.new(cookie_values(:domain => '.foo.com', :origin => url))
241
- @jar.add(cookie)
242
-
243
- assert_equal(1, @jar.cookies(url).length)
244
- end
245
-
246
- def test_add_rejects_cookies_with_unknown_domain_or_path
247
- cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :domain].include?(k) })
248
- assert_raises(ArgumentError) {
249
- @jar.add(cookie)
250
- }
251
-
252
- cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :path].include?(k) })
253
- assert_raises(ArgumentError) {
254
- @jar.add(cookie)
255
- }
256
- end
257
-
258
- def test_add_does_not_reject_cookies_from_a_nested_subdomain
259
- url = URI 'http://y.x.foo.com'
260
-
261
- cookie = HTTP::Cookie.new(cookie_values(:domain => '.foo.com', :origin => url))
262
- @jar.add(cookie)
263
-
264
- assert_equal(1, @jar.cookies(url).length)
265
- end
266
-
267
- def test_cookie_without_leading_dot_does_not_cause_substring_match
268
- url = URI 'http://arubyforge.org/'
269
-
270
- cookie = HTTP::Cookie.new(cookie_values(:domain => 'rubyforge.org'))
271
- @jar.add(cookie)
272
-
273
- assert_equal(0, @jar.cookies(url).length)
274
- end
275
-
276
- def test_cookie_without_leading_dot_matches_subdomains
277
- url = URI 'http://admin.rubyforge.org/'
278
-
279
- cookie = HTTP::Cookie.new(cookie_values(:domain => 'rubyforge.org', :origin => url))
280
- @jar.add(cookie)
281
-
282
- assert_equal(1, @jar.cookies(url).length)
283
- end
284
-
285
- def test_cookies_with_leading_dot_match_subdomains
286
- url = URI 'http://admin.rubyforge.org/'
287
-
288
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org', :origin => url)))
289
-
290
- assert_equal(1, @jar.cookies(url).length)
291
- end
292
-
293
- def test_cookies_with_leading_dot_match_parent_domains
294
- url = URI 'http://rubyforge.org/'
295
-
296
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org', :origin => url)))
297
-
298
- assert_equal(1, @jar.cookies(url).length)
299
- end
300
-
301
- def test_cookies_with_leading_dot_match_parent_domains_exactly
302
- url = URI 'http://arubyforge.org/'
303
-
304
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
305
-
306
- assert_equal(0, @jar.cookies(url).length)
307
- end
308
-
309
- def test_cookie_for_ipv4_address_matches_the_exact_ipaddress
310
- url = URI 'http://192.168.0.1/'
311
-
312
- cookie = HTTP::Cookie.new(cookie_values(:domain => '192.168.0.1', :origin => url))
313
- @jar.add(cookie)
314
-
315
- assert_equal(1, @jar.cookies(url).length)
316
- end
317
-
318
- def test_cookie_for_ipv6_address_matches_the_exact_ipaddress
319
- url = URI 'http://[fe80::0123:4567:89ab:cdef]/'
320
-
321
- cookie = HTTP::Cookie.new(cookie_values(:domain => '[fe80::0123:4567:89ab:cdef]', :origin => url))
322
- @jar.add(cookie)
323
-
324
- assert_equal(1, @jar.cookies(url).length)
325
- end
326
-
327
- def test_cookies_dot
328
- url = URI 'http://www.host.example/'
329
-
330
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => 'www.host.example', :origin => url)))
331
-
332
- url = URI 'http://wwwxhost.example/'
333
- assert_equal(0, @jar.cookies(url).length)
334
- end
335
-
336
- def test_cookies_no_host
337
- url = URI 'file:///path/'
338
-
339
- @jar.add(HTTP::Cookie.new(cookie_values(:origin => url)))
340
-
341
- assert_equal(0, @jar.cookies(url).length)
342
- end
343
-
344
- def test_clear
345
- url = URI 'http://rubyforge.org/'
346
-
347
- # Add one cookie with an expiration date in the future
348
- cookie = HTTP::Cookie.new(cookie_values(:origin => url))
349
- @jar.add(cookie)
350
- @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz', :origin => url)))
351
- assert_equal(2, @jar.cookies(url).length)
352
-
353
- @jar.clear
354
-
355
- assert_equal(0, @jar.cookies(url).length)
356
- end
357
-
358
- def test_save_cookies_yaml
359
- url = URI 'http://rubyforge.org/'
360
-
361
- # Add one cookie with an expiration date in the future
362
- cookie = HTTP::Cookie.new(cookie_values(:origin => url))
363
- s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar',
364
- :expires => nil,
365
- :origin => url))
366
-
367
- @jar.add(cookie)
368
- @jar.add(s_cookie)
369
- @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz', :for_domain => false, :origin => url)))
370
-
371
- assert_equal(3, @jar.cookies(url).length)
372
-
373
- Dir.mktmpdir do |dir|
374
- value = @jar.save(File.join(dir, "cookies.yml"))
375
- assert_same @jar, value
376
-
377
- @jar2.load(File.join(dir, "cookies.yml"))
378
- cookies = @jar2.cookies(url).sort_by { |cookie| cookie.name }
379
- assert_equal(2, cookies.length)
380
- assert_equal('Baz', cookies[0].name)
381
- assert_equal(false, cookies[0].for_domain)
382
- assert_equal('Foo', cookies[1].name)
383
- assert_equal(true, cookies[1].for_domain)
384
- end
385
-
386
- assert_equal(3, @jar.cookies(url).length)
387
- end
388
-
389
- def test_save_load_signature
390
- Dir.mktmpdir { |dir|
391
- filename = File.join(dir, "cookies.yml")
392
-
393
- @jar.save(filename, :format => :cookiestxt, :session => true)
394
- @jar.save(filename, :format => :cookiestxt, :session => true)
395
- @jar.save(filename, :format => :cookiestxt)
396
- @jar.save(filename, :cookiestxt, :session => true)
397
- @jar.save(filename, :cookiestxt)
398
- @jar.save(filename, HTTP::CookieJar::CookiestxtSaver)
399
- @jar.save(filename, HTTP::CookieJar::CookiestxtSaver.new)
400
- @jar.save(filename, :session => true)
401
- @jar.save(filename)
402
-
403
- assert_raises(ArgumentError) {
404
- @jar.save()
405
- }
406
- assert_raises(ArgumentError) {
407
- @jar.save(filename, :nonexistent)
408
- }
409
- assert_raises(TypeError) {
410
- @jar.save(filename, { :format => :cookiestxt }, { :session => true })
411
- }
412
- assert_raises(ArgumentError) {
413
- @jar.save(filename, :cookiestxt, { :session => true }, { :format => :cookiestxt })
414
- }
415
-
416
- @jar.load(filename, :format => :cookiestxt, :linefeed => "\n")
417
- @jar.load(filename, :format => :cookiestxt, :linefeed => "\n")
418
- @jar.load(filename, :format => :cookiestxt)
419
- @jar.load(filename, HTTP::CookieJar::CookiestxtSaver)
420
- @jar.load(filename, HTTP::CookieJar::CookiestxtSaver.new)
421
- @jar.load(filename, :cookiestxt, :linefeed => "\n")
422
- @jar.load(filename, :cookiestxt)
423
- @jar.load(filename, :linefeed => "\n")
424
- @jar.load(filename)
425
- assert_raises(ArgumentError) {
426
- @jar.load()
427
- }
428
- assert_raises(ArgumentError) {
429
- @jar.load(filename, :nonexistent)
430
- }
431
- assert_raises(TypeError) {
432
- @jar.load(filename, { :format => :cookiestxt }, { :linefeed => "\n" })
433
- }
434
- assert_raises(ArgumentError) {
435
- @jar.load(filename, :cookiestxt, { :linefeed => "\n" }, { :format => :cookiestxt })
436
- }
437
- }
438
- end
439
-
440
- def test_save_session_cookies_yaml
441
- url = URI 'http://rubyforge.org/'
442
-
443
- # Add one cookie with an expiration date in the future
444
- cookie = HTTP::Cookie.new(cookie_values)
445
- s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar',
446
- :expires => nil))
447
-
448
- @jar.add(cookie)
449
- @jar.add(s_cookie)
450
- @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz')))
451
-
452
- assert_equal(3, @jar.cookies(url).length)
453
-
454
- Dir.mktmpdir do |dir|
455
- @jar.save(File.join(dir, "cookies.yml"), :format => :yaml, :session => true)
456
-
457
- @jar2.load(File.join(dir, "cookies.yml"))
458
- assert_equal(3, @jar2.cookies(url).length)
459
- end
460
-
461
- assert_equal(3, @jar.cookies(url).length)
462
- end
463
-
464
- def test_save_and_read_cookiestxt
465
- url = URI 'http://rubyforge.org/foo/'
466
-
467
- # Add one cookie with an expiration date in the future
468
- cookie = HTTP::Cookie.new(cookie_values)
469
- expires = cookie.expires
470
- s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar',
471
- :expires => nil))
472
- cookie2 = HTTP::Cookie.new(cookie_values(:name => 'Baz',
473
- :value => 'Foo#Baz',
474
- :path => '/foo/',
475
- :for_domain => false))
476
- h_cookie = HTTP::Cookie.new(cookie_values(:name => 'Quux',
477
- :value => 'Foo#Quux',
478
- :httponly => true))
479
- ma_cookie = HTTP::Cookie.new(cookie_values(:name => 'Maxage',
480
- :value => 'Foo#Maxage',
481
- :max_age => 15000))
482
- @jar.add(cookie)
483
- @jar.add(s_cookie)
484
- @jar.add(cookie2)
485
- @jar.add(h_cookie)
486
- @jar.add(ma_cookie)
487
-
488
- assert_equal(5, @jar.cookies(url).length)
489
-
490
- Dir.mktmpdir do |dir|
491
- filename = File.join(dir, "cookies.txt")
492
- @jar.save(filename, :cookiestxt)
493
-
494
- content = File.read(filename)
495
-
496
- filename2 = File.join(dir, "cookies2.txt")
497
- open(filename2, 'w') { |w|
498
- w.puts '# HTTP Cookie File'
499
- @jar.save(w, :cookiestxt, :header => nil)
500
- }
501
- assert_equal content, File.read(filename2)
502
-
503
- assert_match(/^\.rubyforge\.org\t.*\tFoo\t/, content)
504
- assert_match(/^rubyforge\.org\t.*\tBaz\t/, content)
505
- assert_match(/^#HttpOnly_\.rubyforge\.org\t/, content)
506
-
507
- @jar2.load(filename, :cookiestxt) # HACK test the format
508
- cookies = @jar2.cookies(url)
509
- assert_equal(4, cookies.length)
510
- cookies.each { |cookie|
511
- case cookie.name
512
- when 'Foo'
513
- assert_equal 'Bar', cookie.value
514
- assert_equal expires, cookie.expires
515
- assert_equal 'rubyforge.org', cookie.domain
516
- assert_equal true, cookie.for_domain
517
- assert_equal '/', cookie.path
518
- assert_equal false, cookie.httponly?
519
- when 'Baz'
520
- assert_equal 'Foo#Baz', cookie.value
521
- assert_equal 'rubyforge.org', cookie.domain
522
- assert_equal false, cookie.for_domain
523
- assert_equal '/foo/', cookie.path
524
- assert_equal false, cookie.httponly?
525
- when 'Quux'
526
- assert_equal 'Foo#Quux', cookie.value
527
- assert_equal expires, cookie.expires
528
- assert_equal 'rubyforge.org', cookie.domain
529
- assert_equal true, cookie.for_domain
530
- assert_equal '/', cookie.path
531
- assert_equal true, cookie.httponly?
532
- when 'Maxage'
533
- assert_equal 'Foo#Maxage', cookie.value
534
- assert_equal nil, cookie.max_age
535
- assert_in_delta ma_cookie.expires, cookie.expires, 1
536
- else
537
- raise
538
- end
539
- }
540
- end
541
-
542
- assert_equal(5, @jar.cookies(url).length)
543
- end
544
-
545
- def test_load_yaml_mechanize
546
- @jar.load(test_file('mechanize.yml'), :yaml)
547
-
548
- assert_equal 4, @jar.to_a.size
549
-
550
- com_nid, com_pref = @jar.cookies('http://www.google.com/')
551
-
552
- assert_equal 'NID', com_nid.name
553
- assert_equal 'Sun, 23 Sep 2063 08:20:15 GMT', com_nid.expires.httpdate
554
- assert_equal 'google.com', com_nid.domain_name.hostname
555
-
556
- assert_equal 'PREF', com_pref.name
557
- assert_equal 'Tue, 24 Mar 2065 08:20:15 GMT', com_pref.expires.httpdate
558
- assert_equal 'google.com', com_pref.domain_name.hostname
559
-
560
- cojp_nid, cojp_pref = @jar.cookies('http://www.google.co.jp/')
561
-
562
- assert_equal 'NID', cojp_nid.name
563
- assert_equal 'Sun, 23 Sep 2063 08:20:16 GMT', cojp_nid.expires.httpdate
564
- assert_equal 'google.co.jp', cojp_nid.domain_name.hostname
565
-
566
- assert_equal 'PREF', cojp_pref.name
567
- assert_equal 'Tue, 24 Mar 2065 08:20:16 GMT', cojp_pref.expires.httpdate
568
- assert_equal 'google.co.jp', cojp_pref.domain_name.hostname
569
- end
570
-
571
- def test_expire_cookies
572
- url = URI 'http://rubyforge.org/'
573
-
574
- # Add one cookie with an expiration date in the future
575
- cookie = HTTP::Cookie.new(cookie_values)
576
- @jar.add(cookie)
577
- assert_equal(1, @jar.cookies(url).length)
578
-
579
- # Add a second cookie
580
- @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz')))
581
- assert_equal(2, @jar.cookies(url).length)
582
-
583
- # Make sure we can get the cookie from different paths
584
- assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
585
-
586
- # Expire the first cookie
587
- @jar.add(HTTP::Cookie.new(cookie_values(:expires => Time.now - (10 * 86400))))
588
- assert_equal(1, @jar.cookies(url).length)
589
-
590
- # Expire the second cookie
591
- @jar.add(HTTP::Cookie.new(cookie_values( :name => 'Baz', :expires => Time.now - (10 * 86400))))
592
- assert_equal(0, @jar.cookies(url).length)
593
- end
594
-
595
- def test_session_cookies
596
- values = cookie_values(:expires => nil)
597
- url = URI 'http://rubyforge.org/'
598
-
599
- # Add one cookie with an expiration date in the future
600
- cookie = HTTP::Cookie.new(values)
601
- @jar.add(cookie)
602
- assert_equal(1, @jar.cookies(url).length)
603
-
604
- # Add a second cookie
605
- @jar.add(HTTP::Cookie.new(values.merge(:name => 'Baz')))
606
- assert_equal(2, @jar.cookies(url).length)
607
-
608
- # Make sure we can get the cookie from different paths
609
- assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
610
-
611
- # Expire the first cookie
612
- @jar.add(HTTP::Cookie.new(values.merge(:expires => Time.now - (10 * 86400))))
613
- assert_equal(1, @jar.cookies(url).length)
614
-
615
- # Expire the second cookie
616
- @jar.add(HTTP::Cookie.new(values.merge(:name => 'Baz', :expires => Time.now - (10 * 86400))))
617
- assert_equal(0, @jar.cookies(url).length)
618
-
619
- # When given a URI with a blank path, CookieJar#cookies should return
620
- # cookies with the path '/':
621
- url = URI 'http://rubyforge.org'
622
- assert_equal '', url.path
623
- assert_equal(0, @jar.cookies(url).length)
624
- # Now add a cookie with the path set to '/':
625
- @jar.add(HTTP::Cookie.new(values.merge(:name => 'has_root_path', :path => '/')))
626
- assert_equal(1, @jar.cookies(url).length)
627
- end
628
-
629
- def test_paths
630
- url = URI 'http://rubyforge.org/login'
631
- values = cookie_values(:path => "/login", :expires => nil, :origin => url)
632
-
633
- # Add one cookie with an expiration date in the future
634
- cookie = HTTP::Cookie.new(values)
635
- @jar.add(cookie)
636
- assert_equal(1, @jar.cookies(url).length)
637
-
638
- # Add a second cookie
639
- @jar.add(HTTP::Cookie.new(values.merge( :name => 'Baz' )))
640
- assert_equal(2, @jar.cookies(url).length)
641
-
642
- # Make sure we don't get the cookie in a different path
643
- assert_equal(0, @jar.cookies(URI('http://rubyforge.org/hello')).length)
644
- assert_equal(0, @jar.cookies(URI('http://rubyforge.org/')).length)
645
-
646
- # Expire the first cookie
647
- @jar.add(HTTP::Cookie.new(values.merge( :expires => Time.now - (10 * 86400))))
648
- assert_equal(1, @jar.cookies(url).length)
649
-
650
- # Expire the second cookie
651
- @jar.add(HTTP::Cookie.new(values.merge( :name => 'Baz',
652
- :expires => Time.now - (10 * 86400))))
653
- assert_equal(0, @jar.cookies(url).length)
654
- end
655
-
656
- def test_ssl_cookies
657
- # thanks to michal "ocher" ochman for reporting the bug responsible for this test.
658
- values = cookie_values(:expires => nil)
659
- values_ssl = values.merge(:name => 'Baz', :domain => "#{values[:domain]}:443")
660
- url = URI 'https://rubyforge.org/login'
661
-
662
- cookie = HTTP::Cookie.new(values)
663
- @jar.add(cookie)
664
- assert_equal(1, @jar.cookies(url).length, "did not handle SSL cookie")
665
-
666
- cookie = HTTP::Cookie.new(values_ssl)
667
- @jar.add(cookie)
668
- assert_equal(2, @jar.cookies(url).length, "did not handle SSL cookie with :443")
669
- end
670
-
671
- def test_secure_cookie
672
- nurl = URI 'http://rubyforge.org/login'
673
- surl = URI 'https://rubyforge.org/login'
674
-
675
- nncookie = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => nurl))
676
- sncookie = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => surl))
677
- nscookie = HTTP::Cookie.new(cookie_values(:name => 'Foo2', :secure => true, :origin => nurl))
678
- sscookie = HTTP::Cookie.new(cookie_values(:name => 'Foo2', :secure => true, :origin => surl))
679
-
680
- @jar.add(nncookie)
681
- @jar.add(sncookie)
682
- @jar.add(nscookie)
683
- @jar.add(sscookie)
684
-
685
- assert_equal('Foo1', @jar.cookies(nurl).map { |c| c.name }.sort.join(' ') )
686
- assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
687
- end
688
-
689
- def test_delete
690
- cookie1 = HTTP::Cookie.new(cookie_values)
691
- cookie2 = HTTP::Cookie.new(:name => 'Foo', :value => '',
692
- :domain => 'rubyforge.org',
693
- :for_domain => false,
694
- :path => '/')
695
- cookie3 = HTTP::Cookie.new(:name => 'Foo', :value => '',
696
- :domain => 'rubyforge.org',
697
- :for_domain => true,
698
- :path => '/')
699
-
700
- @jar.add(cookie1)
701
- @jar.delete(cookie2)
702
-
703
- if mozilla_store?
704
- assert_equal(1, @jar.to_a.length)
705
- @jar.delete(cookie3)
706
- end
707
-
708
- assert_equal(0, @jar.to_a.length)
709
- end
710
-
711
- def test_accessed_at
712
- orig = HTTP::Cookie.new(cookie_values(:expires => nil))
713
- @jar.add(orig)
714
-
715
- time = orig.accessed_at
716
-
717
- assert_in_delta 1.0, time, Time.now, "accessed_at is initialized to the current time"
718
-
719
- cookie, = @jar.to_a
720
-
721
- assert_equal time, cookie.accessed_at, "accessed_at is not updated by each()"
722
-
723
- cookie, = @jar.cookies("http://rubyforge.org/")
724
-
725
- assert_send [cookie.accessed_at, :>, time], "accessed_at is not updated by each(url)"
726
- end
727
-
728
- def test_max_cookies
729
- slimit = HTTP::Cookie::MAX_COOKIES_TOTAL + @gc_threshold
730
-
731
- limit_per_domain = HTTP::Cookie::MAX_COOKIES_PER_DOMAIN
732
- uri = URI('http://www.example.org/')
733
- date = Time.at(Time.now.to_i + 86400)
734
- (1..(limit_per_domain + 1)).each { |i|
735
- @jar << HTTP::Cookie.new(cookie_values(
736
- :name => 'Foo%d' % i,
737
- :value => 'Bar%d' % i,
738
- :domain => uri.host,
739
- :for_domain => true,
740
- :path => '/dir%d/' % (i / 2),
741
- :origin => uri
742
- )).tap { |cookie|
743
- cookie.created_at = i == 42 ? date - i : date
744
- }
745
- }
746
- assert_equal limit_per_domain + 1, @jar.to_a.size
747
- @jar.cleanup
748
- count = @jar.to_a.size
749
- assert_equal limit_per_domain, count
750
- assert_equal [*1..(limit_per_domain + 1)] - [42], @jar.map { |cookie|
751
- cookie.name[/(\d+)$/].to_i
752
- }.sort
753
-
754
- hlimit = HTTP::Cookie::MAX_COOKIES_TOTAL
755
-
756
- n = hlimit / limit_per_domain * 2
757
-
758
- (1..n).each { |i|
759
- (1..(limit_per_domain + 1)).each { |j|
760
- uri = URI('http://www%d.example.jp/' % i)
761
- @jar << HTTP::Cookie.new(cookie_values(
762
- :name => 'Baz%d' % j,
763
- :value => 'www%d.example.jp' % j,
764
- :domain => uri.host,
765
- :for_domain => true,
766
- :path => '/dir%d/' % (i / 2),
767
- :origin => uri
768
- )).tap { |cookie|
769
- cookie.created_at = i == j ? date - i : date
770
- }
771
- count += 1
772
- }
773
- }
774
-
775
- assert_send [count, :>, slimit]
776
- assert_send [@jar.to_a.size, :<=, slimit]
777
- @jar.cleanup
778
- assert_equal hlimit, @jar.to_a.size
779
- assert_equal false, @jar.any? { |cookie|
780
- cookie.domain == cookie.value
781
- }
782
- end
783
-
784
- def test_parse
785
- set_cookie = [
786
- "name=Akinori; Domain=rubyforge.org; Expires=Sun, 08 Aug 2076 19:00:00 GMT; Path=/",
787
- "country=Japan; Domain=rubyforge.org; Expires=Sun, 08 Aug 2076 19:00:00 GMT; Path=/",
788
- "city=Tokyo; Domain=rubyforge.org; Expires=Sun, 08 Aug 2076 19:00:00 GMT; Path=/",
789
- ].join(', ')
790
-
791
- cookies = @jar.parse(set_cookie, 'http://rubyforge.org/')
792
- assert_equal %w[Akinori Japan Tokyo], cookies.map { |c| c.value }
793
- assert_equal %w[Tokyo Japan Akinori], @jar.to_a.sort_by { |c| c.name }.map { |c| c.value }
794
- end
795
-
796
- def test_parse_with_block
797
- set_cookie = [
798
- "name=Akinori; Domain=rubyforge.org; Expires=Sun, 08 Aug 2076 19:00:00 GMT; Path=/",
799
- "country=Japan; Domain=rubyforge.org; Expires=Sun, 08 Aug 2076 19:00:00 GMT; Path=/",
800
- "city=Tokyo; Domain=rubyforge.org; Expires=Sun, 08 Aug 2076 19:00:00 GMT; Path=/",
801
- ].join(', ')
802
-
803
- cookies = @jar.parse(set_cookie, 'http://rubyforge.org/') { |c| c.name != 'city' }
804
- assert_equal %w[Akinori Japan], cookies.map { |c| c.value }
805
- assert_equal %w[Japan Akinori], @jar.to_a.sort_by { |c| c.name }.map { |c| c.value }
806
- end
807
-
808
- def test_expire_by_each_and_cleanup
809
- uri = URI('http://www.example.org/')
810
-
811
- ts = Time.now.to_f
812
- if ts % 1 > 0.5
813
- sleep 0.5
814
- ts += 0.5
815
- end
816
- expires = Time.at(ts.floor)
817
- time = expires
818
-
819
- if mozilla_store?
820
- # MozillaStore only has the time precision of seconds.
821
- time = expires
822
- expires -= 1
823
- end
824
-
825
- 0.upto(2) { |i|
826
- c = HTTP::Cookie.new('Foo%d' % (3 - i), 'Bar', :expires => expires + i, :origin => uri)
827
- @jar << c
828
- @jar2 << c
829
- }
830
-
831
- assert_equal %w[Foo1 Foo2], @jar.cookies.map(&:name)
832
- assert_equal %w[Foo1 Foo2], @jar2.cookies(uri).map(&:name)
833
-
834
- sleep_until time + 1
835
-
836
- assert_equal %w[Foo1], @jar.cookies.map(&:name)
837
- assert_equal %w[Foo1], @jar2.cookies(uri).map(&:name)
838
-
839
- sleep_until time + 2
840
-
841
- @jar.cleanup
842
- @jar2.cleanup
843
-
844
- assert_send [@jar, :empty?]
845
- assert_send [@jar2, :empty?]
846
- end
847
- end
848
-
849
- class WithHashStore < Test::Unit::TestCase
850
- include CommonTests
851
-
852
- def test_new
853
- jar = HTTP::CookieJar.new(:store => :hash)
854
- assert_instance_of HTTP::CookieJar::HashStore, jar.store
855
-
856
- assert_raises(ArgumentError) {
857
- jar = HTTP::CookieJar.new(:store => :nonexistent)
858
- }
859
-
860
- jar = HTTP::CookieJar.new(:store => HTTP::CookieJar::HashStore.new)
861
- assert_instance_of HTTP::CookieJar::HashStore, jar.store
862
-
863
- jar = HTTP::CookieJar.new(:store => HTTP::CookieJar::HashStore)
864
- end
865
-
866
- def test_clone
867
- jar = @jar.clone
868
- assert_not_send [
869
- @jar.store,
870
- :equal?,
871
- jar.store
872
- ]
873
- assert_not_send [
874
- @jar.store.instance_variable_get(:@jar),
875
- :equal?,
876
- jar.store.instance_variable_get(:@jar)
877
- ]
878
- assert_equal @jar.cookies, jar.cookies
879
- end
880
- end
881
-
882
- class WithMozillaStore < Test::Unit::TestCase
883
- include CommonTests
884
-
885
- def setup
886
- super(
887
- { :store => :mozilla, :filename => ":memory:" },
888
- { :store => :mozilla, :filename => ":memory:" })
889
- end
890
-
891
- def add_and_delete(jar)
892
- jar.parse("name=Akinori; Domain=rubyforge.org; Expires=Sun, 08 Aug 2076 19:00:00 GMT; Path=/",
893
- 'http://rubyforge.org/')
894
- jar.parse("country=Japan; Domain=rubyforge.org; Expires=Sun, 08 Aug 2076 19:00:00 GMT; Path=/",
895
- 'http://rubyforge.org/')
896
- jar.delete(HTTP::Cookie.new("name", :domain => 'rubyforge.org'))
897
- end
898
-
899
- def test_clone
900
- assert_raises(TypeError) {
901
- @jar.clone
902
- }
903
- end
904
-
905
- def test_close
906
- add_and_delete(@jar)
907
-
908
- assert_not_send [@jar.store, :closed?]
909
- @jar.store.close
910
- assert_send [@jar.store, :closed?]
911
- @jar.store.close # should do nothing
912
- assert_send [@jar.store, :closed?]
913
- end
914
-
915
- def test_finalizer
916
- db = nil
917
- loop {
918
- jar = HTTP::CookieJar.new(:store => :mozilla, :filename => ':memory:')
919
- add_and_delete(jar)
920
- db = jar.store.instance_variable_get(:@db)
921
- class << db
922
- alias close_orig close
923
- def close
924
- STDERR.print "[finalizer is called]"
925
- STDERR.flush
926
- close_orig
927
- end
928
- end
929
- break
930
- }
931
- end
932
-
933
- def test_upgrade_mozillastore
934
- Dir.mktmpdir { |dir|
935
- filename = File.join(dir, 'cookies.sqlite')
936
-
937
- sqlite = SQLite3::Database.new(filename)
938
- sqlite.execute(<<-'SQL')
939
- CREATE TABLE moz_cookies (
940
- id INTEGER PRIMARY KEY,
941
- name TEXT,
942
- value TEXT,
943
- host TEXT,
944
- path TEXT,
945
- expiry INTEGER,
946
- isSecure INTEGER,
947
- isHttpOnly INTEGER)
948
- SQL
949
- sqlite.execute(<<-'SQL')
950
- PRAGMA user_version = 1
951
- SQL
952
-
953
- begin
954
- st_insert = sqlite.prepare(<<-'SQL')
955
- INSERT INTO moz_cookies (
956
- id, name, value, host, path, expiry, isSecure, isHttpOnly
957
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
958
- SQL
959
-
960
- st_insert.execute(1, 'name1', 'value1', '.example.co.jp', '/', 2312085765, 0, 0)
961
- st_insert.execute(2, 'name1', 'value2', '.example.co.jp', '/', 2312085765, 0, 0)
962
- st_insert.execute(3, 'name1', 'value3', 'www.example.co.jp', '/', 2312085765, 0, 0)
963
- ensure
964
- st_insert.close if st_insert
965
- end
966
-
967
- sqlite.close
968
- jar = HTTP::CookieJar.new(:store => :mozilla, :filename => filename)
969
-
970
- assert_equal 2, jar.to_a.size
971
- assert_equal 2, jar.cookies('http://www.example.co.jp/').size
972
-
973
- cookie, *rest = jar.cookies('http://host.example.co.jp/')
974
- assert_send [rest, :empty?]
975
- assert_equal 'value2', cookie.value
976
- }
977
- end
978
- end if begin
979
- require 'sqlite3'
980
- true
981
- rescue LoadError
982
- STDERR.puts 'sqlite3 missing?'
983
- false
984
- end
985
- end