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,1122 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require File.expand_path('helper', File.dirname(__FILE__))
3
-
4
- class TestHTTPCookie < Test::Unit::TestCase
5
- def setup
6
- httpdate = 'Sun, 27-Sep-2037 00:00:00 GMT'
7
-
8
- @cookie_params = {
9
- 'expires' => 'expires=%s' % httpdate,
10
- 'path' => 'path=/',
11
- 'domain' => 'domain=.rubyforge.org',
12
- 'httponly' => 'HttpOnly',
13
- }
14
-
15
- @expires = Time.parse(httpdate)
16
- end
17
-
18
- def test_parse_dates
19
- url = URI.parse('http://localhost/')
20
-
21
- yesterday = Time.now - 86400
22
-
23
- dates = [ "14 Apr 89 03:20:12",
24
- "14 Apr 89 03:20 GMT",
25
- "Fri, 17 Mar 89 4:01:33",
26
- "Fri, 17 Mar 89 4:01 GMT",
27
- "Mon Jan 16 16:12 PDT 1989",
28
- #"Mon Jan 16 16:12 +0130 1989",
29
- "6 May 1992 16:41-JST (Wednesday)",
30
- #"22-AUG-1993 10:59:12.82",
31
- "22-AUG-1993 10:59pm",
32
- "22-AUG-1993 12:59am",
33
- "22-AUG-1993 12:59 PM",
34
- #"Friday, August 04, 1995 3:54 PM",
35
- #"06/21/95 04:24:34 PM",
36
- #"20/06/95 21:07",
37
- #"95-06-08 19:32:48 EDT",
38
- ]
39
-
40
- dates.each do |date|
41
- cookie = "PREF=1; expires=#{date}"
42
- assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c|
43
- assert c.expires, "Tried parsing: #{date}"
44
- assert_send [c.expires, :<, yesterday]
45
- }.size
46
- end
47
-
48
- [
49
- ["PREF=1; expires=Wed, 01 Jan 100 12:34:56 GMT", nil],
50
- ["PREF=1; expires=Sat, 01 Jan 1600 12:34:56 GMT", nil],
51
- ["PREF=1; expires=Tue, 01 Jan 69 12:34:56 GMT", 2069],
52
- ["PREF=1; expires=Thu, 01 Jan 70 12:34:56 GMT", 1970],
53
- ["PREF=1; expires=Wed, 01 Jan 20 12:34:56 GMT", 2020],
54
- ["PREF=1; expires=Sat, 01 Jan 2020 12:34:60 GMT", nil],
55
- ["PREF=1; expires=Sat, 01 Jan 2020 12:60:56 GMT", nil],
56
- ["PREF=1; expires=Sat, 01 Jan 2020 24:00:00 GMT", nil],
57
- ["PREF=1; expires=Sat, 32 Jan 2020 12:34:56 GMT", nil],
58
- ].each { |set_cookie, year|
59
- cookie, = HTTP::Cookie.parse(set_cookie, url)
60
- if year
61
- assert_equal year, cookie.expires.year, "#{set_cookie}: expires in #{year}"
62
- else
63
- assert_equal nil, cookie.expires, "#{set_cookie}: invalid expiry date"
64
- end
65
- }
66
- end
67
-
68
- def test_parse_empty
69
- cookie_str = 'a=b; ; c=d'
70
-
71
- uri = URI.parse 'http://example'
72
-
73
- assert_equal 1, HTTP::Cookie.parse(cookie_str, uri) { |cookie|
74
- assert_equal 'a', cookie.name
75
- assert_equal 'b', cookie.value
76
- }.size
77
- end
78
-
79
- def test_parse_no_space
80
- cookie_str = "foo=bar;Expires=Sun, 06 Nov 2011 00:28:06 GMT;Path=/"
81
-
82
- uri = URI.parse 'http://example'
83
-
84
- assert_equal 1, HTTP::Cookie.parse(cookie_str, uri) { |cookie|
85
- assert_equal 'foo', cookie.name
86
- assert_equal 'bar', cookie.value
87
- assert_equal '/', cookie.path
88
- assert_equal Time.at(1320539286), cookie.expires
89
- }.size
90
- end
91
-
92
- def test_parse_too_long_cookie
93
- uri = URI.parse 'http://example'
94
-
95
- cookie_str = "foo=#{'Cookie' * 680}; path=/ab/"
96
- assert_equal(HTTP::Cookie::MAX_LENGTH - 1, cookie_str.bytesize)
97
-
98
- assert_equal 1, HTTP::Cookie.parse(cookie_str, uri).size
99
-
100
- assert_equal 1, HTTP::Cookie.parse(cookie_str.sub(';', 'x;'), uri).size
101
-
102
- assert_equal 0, HTTP::Cookie.parse(cookie_str.sub(';', 'xx;'), uri).size
103
- end
104
-
105
- def test_parse_quoted
106
- cookie_str =
107
- "quoted=\"value\"; Expires=Sun, 06 Nov 2011 00:11:18 GMT; Path=/; comment=\"comment is \\\"comment\\\"\""
108
-
109
- uri = URI.parse 'http://example'
110
-
111
- assert_equal 1, HTTP::Cookie.parse(cookie_str, uri) { |cookie|
112
- assert_equal 'quoted', cookie.name
113
- assert_equal 'value', cookie.value
114
- }.size
115
- end
116
-
117
- def test_parse_no_nothing
118
- cookie = '; "", ;'
119
- url = URI.parse('http://www.example.com/')
120
- assert_equal 0, HTTP::Cookie.parse(cookie, url).size
121
- end
122
-
123
- def test_parse_no_name
124
- cookie = '=no-name; path=/'
125
- url = URI.parse('http://www.example.com/')
126
- assert_equal 0, HTTP::Cookie.parse(cookie, url).size
127
- end
128
-
129
- def test_parse_bad_name
130
- cookie = "a\001b=c"
131
- url = URI.parse('http://www.example.com/')
132
- assert_nothing_raised {
133
- assert_equal 0, HTTP::Cookie.parse(cookie, url).size
134
- }
135
- end
136
-
137
- def test_parse_bad_value
138
- cookie = "a=b\001c"
139
- url = URI.parse('http://www.example.com/')
140
- assert_nothing_raised {
141
- assert_equal 0, HTTP::Cookie.parse(cookie, url).size
142
- }
143
- end
144
-
145
- def test_parse_weird_cookie
146
- cookie = 'n/a, ASPSESSIONIDCSRRQDQR=FBLDGHPBNDJCPCGNCPAENELB; path=/'
147
- url = URI.parse('http://www.searchinnovation.com/')
148
- assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c|
149
- assert_equal('ASPSESSIONIDCSRRQDQR', c.name)
150
- assert_equal('FBLDGHPBNDJCPCGNCPAENELB', c.value)
151
- }.size
152
- end
153
-
154
- def test_double_semicolon
155
- double_semi = 'WSIDC=WEST;; domain=.williams-sonoma.com; path=/'
156
- url = URI.parse('http://williams-sonoma.com/')
157
- assert_equal 1, HTTP::Cookie.parse(double_semi, url) { |cookie|
158
- assert_equal('WSIDC', cookie.name)
159
- assert_equal('WEST', cookie.value)
160
- }.size
161
- end
162
-
163
- def test_parse_bad_version
164
- bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Version=1.2; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
165
- url = URI.parse('http://192.168.6.196/')
166
- # The version attribute is obsolete and simply ignored
167
- cookies = HTTP::Cookie.parse(bad_cookie, url)
168
- assert_equal 1, cookies.size
169
- end
170
-
171
- def test_parse_bad_max_age
172
- bad_cookie = 'PRETANET=TGIAqbFXtt; Name=/PRETANET; Path=/; Max-Age=forever; Content-type=text/html; Domain=192.168.6.196; expires=Friday, 13-November-2026 23:01:46 GMT;'
173
- url = URI.parse('http://192.168.6.196/')
174
- # A bad max-age is simply ignored
175
- cookies = HTTP::Cookie.parse(bad_cookie, url)
176
- assert_equal 1, cookies.size
177
- assert_equal nil, cookies.first.max_age
178
- end
179
-
180
- def test_parse_date_fail
181
- url = URI.parse('http://localhost/')
182
-
183
- dates = [
184
- "20/06/95 21:07",
185
- ]
186
-
187
- dates.each { |date|
188
- cookie = "PREF=1; expires=#{date}"
189
- assert_equal 1, HTTP::Cookie.parse(cookie, url) { |c|
190
- assert_equal(true, c.expires.nil?)
191
- }.size
192
- }
193
- end
194
-
195
- def test_parse_domain_dot
196
- url = URI.parse('http://host.example.com/')
197
-
198
- cookie_str = 'a=b; domain=.example.com'
199
-
200
- cookie = HTTP::Cookie.parse(cookie_str, url).first
201
-
202
- assert_equal 'example.com', cookie.domain
203
- assert cookie.for_domain?
204
- assert_equal '.example.com', cookie.dot_domain
205
- end
206
-
207
- def test_parse_domain_no_dot
208
- url = URI.parse('http://host.example.com/')
209
-
210
- cookie_str = 'a=b; domain=example.com'
211
-
212
- cookie = HTTP::Cookie.parse(cookie_str, url).first
213
-
214
- assert_equal 'example.com', cookie.domain
215
- assert cookie.for_domain?
216
- assert_equal '.example.com', cookie.dot_domain
217
- end
218
-
219
- def test_parse_public_suffix
220
- cookie = HTTP::Cookie.new('a', 'b', :domain => 'com')
221
- assert_equal('com', cookie.domain)
222
- assert_equal(false, cookie.for_domain?)
223
-
224
- cookie.origin = 'http://com/'
225
- assert_equal('com', cookie.domain)
226
- assert_equal(false, cookie.for_domain?)
227
-
228
- assert_raises(ArgumentError) {
229
- cookie.origin = 'http://example.com/'
230
- }
231
- end
232
-
233
- def test_parse_domain_none
234
- url = URI.parse('http://example.com/')
235
-
236
- cookie_str = 'a=b;'
237
-
238
- cookie = HTTP::Cookie.parse(cookie_str, url).first
239
-
240
- assert_equal 'example.com', cookie.domain
241
- assert !cookie.for_domain?
242
- assert_equal 'example.com', cookie.dot_domain
243
- end
244
-
245
- def test_parse_max_age
246
- url = URI.parse('http://localhost/')
247
-
248
- epoch, date = 4485353164, 'Fri, 19 Feb 2112 19:26:04 GMT'
249
- base = Time.at(1363014000)
250
-
251
- cookie = HTTP::Cookie.parse("name=Akinori; expires=#{date}", url).first
252
- assert_equal Time.at(epoch), cookie.expires
253
-
254
- cookie = HTTP::Cookie.parse('name=Akinori; max-age=3600', url).first
255
- assert_in_delta Time.now + 3600, cookie.expires, 1
256
- cookie = HTTP::Cookie.parse('name=Akinori; max-age=3600', url, :created_at => base).first
257
- assert_equal base + 3600, cookie.expires
258
-
259
- # Max-Age has precedence over Expires
260
- cookie = HTTP::Cookie.parse("name=Akinori; max-age=3600; expires=#{date}", url).first
261
- assert_in_delta Time.now + 3600, cookie.expires, 1
262
- cookie = HTTP::Cookie.parse("name=Akinori; max-age=3600; expires=#{date}", url, :created_at => base).first
263
- assert_equal base + 3600, cookie.expires
264
-
265
- cookie = HTTP::Cookie.parse("name=Akinori; expires=#{date}; max-age=3600", url).first
266
- assert_in_delta Time.now + 3600, cookie.expires, 1
267
- cookie = HTTP::Cookie.parse("name=Akinori; expires=#{date}; max-age=3600", url, :created_at => base).first
268
- assert_equal base + 3600, cookie.expires
269
- end
270
-
271
- def test_parse_expires_session
272
- url = URI.parse('http://localhost/')
273
-
274
- [
275
- 'name=Akinori',
276
- 'name=Akinori; expires',
277
- 'name=Akinori; max-age',
278
- 'name=Akinori; expires=',
279
- 'name=Akinori; max-age=',
280
- ].each { |str|
281
- cookie = HTTP::Cookie.parse(str, url).first
282
- assert cookie.session?, str
283
- }
284
-
285
- [
286
- 'name=Akinori; expires=Mon, 19 Feb 2012 19:26:04 GMT',
287
- 'name=Akinori; max-age=3600',
288
- ].each { |str|
289
- cookie = HTTP::Cookie.parse(str, url).first
290
- assert !cookie.session?, str
291
- }
292
- end
293
-
294
- def test_parse_many
295
- url = URI 'http://localhost/'
296
- cookie_str =
297
- "abc, " \
298
- "name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
299
- "name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
300
- "name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/, " \
301
- "name=Aaron; Domain=localhost; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/; HttpOnly, " \
302
- "expired=doh; Expires=Fri, 04 Nov 2011 00:29:51 GMT; Path=/, " \
303
- "a_path=some_path; Expires=Sun, 06 Nov 2011 00:29:51 GMT; Path=/some_path, " \
304
- "no_path1=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT, no_expires=nope; Path=/, " \
305
- "no_path2=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path, " \
306
- "no_path3=no_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path=, " \
307
- "rel_path1=rel_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path=foo/bar, " \
308
- "rel_path1=rel_path; Expires=Sun, 06 Nov 2011 00:29:52 GMT; no_expires=nope; Path=foo, " \
309
- "no_domain1=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope, " \
310
- "no_domain2=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope; Domain, " \
311
- "no_domain3=no_domain; Expires=Sun, 06 Nov 2011 00:29:53 GMT; no_expires=nope; Domain="
312
-
313
- cookies = HTTP::Cookie.parse cookie_str, url
314
- assert_equal 15, cookies.length
315
-
316
- name = cookies.find { |c| c.name == 'name' }
317
- assert_equal "Aaron", name.value
318
- assert_equal "/", name.path
319
- assert_equal Time.at(1320539391), name.expires
320
-
321
- a_path = cookies.find { |c| c.name == 'a_path' }
322
- assert_equal "some_path", a_path.value
323
- assert_equal "/some_path", a_path.path
324
- assert_equal Time.at(1320539391), a_path.expires
325
-
326
- no_expires = cookies.find { |c| c.name == 'no_expires' }
327
- assert_equal "nope", no_expires.value
328
- assert_equal "/", no_expires.path
329
- assert_nil no_expires.expires
330
-
331
- no_path_cookies = cookies.select { |c| c.value == 'no_path' }
332
- assert_equal 3, no_path_cookies.size
333
- no_path_cookies.each { |c|
334
- assert_equal "/", c.path, c.name
335
- assert_equal Time.at(1320539392), c.expires, c.name
336
- }
337
-
338
- rel_path_cookies = cookies.select { |c| c.value == 'rel_path' }
339
- assert_equal 2, rel_path_cookies.size
340
- rel_path_cookies.each { |c|
341
- assert_equal "/", c.path, c.name
342
- assert_equal Time.at(1320539392), c.expires, c.name
343
- }
344
-
345
- no_domain_cookies = cookies.select { |c| c.value == 'no_domain' }
346
- assert_equal 3, no_domain_cookies.size
347
- no_domain_cookies.each { |c|
348
- assert !c.for_domain?, c.name
349
- assert_equal c.domain, url.host, c.name
350
- assert_equal Time.at(1320539393), c.expires, c.name
351
- }
352
-
353
- assert cookies.find { |c| c.name == 'expired' }
354
- end
355
-
356
- def test_parse_valid_cookie
357
- url = URI.parse('http://rubyforge.org/')
358
- cookie_params = @cookie_params
359
- cookie_value = '12345%7D=ASDFWEE345%3DASda'
360
-
361
- cookie_params.keys.combine.each do |keys|
362
- cookie_text = [cookie_value, *keys.map { |key| cookie_params[key] }].join('; ')
363
- cookie, = HTTP::Cookie.parse(cookie_text, url)
364
-
365
- assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
366
- assert_equal('/', cookie.path)
367
-
368
- assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
369
- assert_equal(keys.include?('httponly'), cookie.httponly?)
370
- end
371
- end
372
-
373
- def test_parse_valid_cookie_empty_value
374
- url = URI.parse('http://rubyforge.org/')
375
- cookie_params = @cookie_params
376
- cookie_value = '12345%7D='
377
-
378
- cookie_params.keys.combine.each do |keys|
379
- cookie_text = [cookie_value, *keys.map { |key| cookie_params[key] }].join('; ')
380
- cookie, = HTTP::Cookie.parse(cookie_text, url)
381
-
382
- assert_equal('12345%7D=', cookie.to_s)
383
- assert_equal('', cookie.value)
384
- assert_equal('/', cookie.path)
385
-
386
- assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
387
- assert_equal(keys.include?('httponly'), cookie.httponly?)
388
- end
389
- end
390
-
391
- # If no path was given, use the one from the URL
392
- def test_cookie_using_url_path
393
- url = URI.parse('http://rubyforge.org/login.php')
394
- cookie_params = @cookie_params
395
- cookie_value = '12345%7D=ASDFWEE345%3DASda'
396
-
397
- cookie_params.keys.combine.each do |keys|
398
- next if keys.include?('path')
399
- cookie_text = [cookie_value, *keys.map { |key| cookie_params[key] }].join('; ')
400
- cookie, = HTTP::Cookie.parse(cookie_text, url)
401
-
402
- assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
403
- assert_equal('/', cookie.path)
404
-
405
- assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
406
- assert_equal(keys.include?('httponly'), cookie.httponly?)
407
- end
408
- end
409
-
410
- # Test using secure cookies
411
- def test_cookie_with_secure
412
- url = URI.parse('http://rubyforge.org/')
413
- cookie_params = @cookie_params.merge('secure' => 'secure')
414
- cookie_value = '12345%7D=ASDFWEE345%3DASda'
415
-
416
- cookie_params.keys.combine.each do |keys|
417
- next unless keys.include?('secure')
418
- cookie_text = [cookie_value, *keys.map { |key| cookie_params[key] }].join('; ')
419
- cookie, = HTTP::Cookie.parse(cookie_text, url)
420
-
421
- assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
422
- assert_equal('/', cookie.path)
423
- assert_equal(true, cookie.secure)
424
-
425
- assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
426
- assert_equal(keys.include?('httponly'), cookie.httponly?)
427
- end
428
- end
429
-
430
- def test_cookie_value
431
- [
432
- ['foo="bar baz"', 'bar baz'],
433
- ['foo="bar\"; \"baz"', 'bar"; "baz'],
434
- ].each { |cookie_value, value|
435
- cookie = HTTP::Cookie.new('foo', value)
436
- assert_equal(cookie_value, cookie.cookie_value)
437
- }
438
-
439
- pairs = [
440
- ['Foo', 'value1'],
441
- ['Bar', 'value 2'],
442
- ['Baz', 'value3'],
443
- ['Bar', 'value"4'],
444
- ['Quux', 'x, value=5'],
445
- ]
446
-
447
- cookie_value = HTTP::Cookie.cookie_value(pairs.map { |name, value|
448
- HTTP::Cookie.new(:name => name, :value => value)
449
- })
450
-
451
- assert_equal 'Foo=value1; Bar="value 2"; Baz=value3; Bar="value\\"4"; Quux="x, value=5"', cookie_value
452
-
453
- hash = HTTP::Cookie.cookie_value_to_hash(cookie_value)
454
-
455
- assert_equal pairs.map(&:first).uniq.size, hash.size
456
-
457
- hash.each_pair { |name, value|
458
- _, pvalue = pairs.assoc(name)
459
- assert_equal pvalue, value
460
- }
461
-
462
- # Do not treat comma in a Cookie header value as separator; see CVE-2016-7401
463
- hash = HTTP::Cookie.cookie_value_to_hash('Quux=x, value=5; Foo=value1; Bar="value 2"; Baz=value3; Bar="value\\"4"')
464
-
465
- assert_equal pairs.map(&:first).uniq.size, hash.size
466
-
467
- hash.each_pair { |name, value|
468
- _, pvalue = pairs.assoc(name)
469
- assert_equal pvalue, value
470
- }
471
- end
472
-
473
- def test_set_cookie_value
474
- url = URI.parse('http://rubyforge.org/path/')
475
-
476
- [
477
- HTTP::Cookie.new('a', 'b', :domain => 'rubyforge.org', :path => '/path/'),
478
- HTTP::Cookie.new('a', 'b', :origin => url),
479
- ].each { |cookie|
480
- cookie.set_cookie_value
481
- }
482
-
483
- [
484
- HTTP::Cookie.new('a', 'b', :domain => 'rubyforge.org'),
485
- HTTP::Cookie.new('a', 'b', :for_domain => true, :path => '/path/'),
486
- ].each { |cookie|
487
- assert_raises(RuntimeError) {
488
- cookie.set_cookie_value
489
- }
490
- }
491
-
492
- ['foo=bar', 'foo="bar"', 'foo="ba\"r baz"'].each { |cookie_value|
493
- cookie_params = @cookie_params.merge('path' => '/path/', 'secure' => 'secure', 'max-age' => 'Max-Age=1000')
494
- date = Time.at(Time.now.to_i)
495
- cookie_params.keys.combine.each do |keys|
496
- cookie_text = [cookie_value, *keys.map { |key| cookie_params[key] }].join('; ')
497
- cookie, = HTTP::Cookie.parse(cookie_text, url, :created_at => date)
498
- cookie2, = HTTP::Cookie.parse(cookie.set_cookie_value, url, :created_at => date)
499
-
500
- assert_equal(cookie.name, cookie2.name)
501
- assert_equal(cookie.value, cookie2.value)
502
- assert_equal(cookie.domain, cookie2.domain)
503
- assert_equal(cookie.for_domain?, cookie2.for_domain?)
504
- assert_equal(cookie.path, cookie2.path)
505
- assert_equal(cookie.expires, cookie2.expires)
506
- if keys.include?('max-age')
507
- assert_equal(date + 1000, cookie2.expires)
508
- elsif keys.include?('expires')
509
- assert_equal(@expires, cookie2.expires)
510
- else
511
- assert_equal(nil, cookie2.expires)
512
- end
513
- assert_equal(cookie.secure?, cookie2.secure?)
514
- assert_equal(cookie.httponly?, cookie2.httponly?)
515
- end
516
- }
517
- end
518
-
519
- def test_parse_cookie_no_spaces
520
- url = URI.parse('http://rubyforge.org/')
521
- cookie_params = @cookie_params
522
- cookie_value = '12345%7D=ASDFWEE345%3DASda'
523
-
524
- cookie_params.keys.combine.each do |keys|
525
- cookie_text = [cookie_value, *keys.map { |key| cookie_params[key] }].join(';')
526
- cookie, = HTTP::Cookie.parse(cookie_text, url)
527
-
528
- assert_equal('12345%7D=ASDFWEE345%3DASda', cookie.to_s)
529
- assert_equal('/', cookie.path)
530
-
531
- assert_equal(keys.include?('expires') ? @expires : nil, cookie.expires)
532
- assert_equal(keys.include?('httponly'), cookie.httponly?)
533
- end
534
- end
535
-
536
- def test_new
537
- cookie = HTTP::Cookie.new('key', 'value')
538
- assert_equal 'key', cookie.name
539
- assert_equal 'value', cookie.value
540
- assert_equal nil, cookie.expires
541
- assert_raises(RuntimeError) {
542
- cookie.acceptable?
543
- }
544
-
545
- # Minimum unit for the expires attribute is second
546
- expires = Time.at((Time.now + 3600).to_i)
547
-
548
- cookie = HTTP::Cookie.new('key', 'value', :expires => expires.dup)
549
- assert_equal 'key', cookie.name
550
- assert_equal 'value', cookie.value
551
- assert_equal expires, cookie.expires
552
- assert_raises(RuntimeError) {
553
- cookie.acceptable?
554
- }
555
-
556
- # various keywords
557
- [
558
- ["Expires", /use downcased symbol/],
559
- ].each { |key, pattern|
560
- assert_warning(pattern, "warn of key: #{key.inspect}") {
561
- cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', key => expires.dup)
562
- assert_equal 'key', cookie.name
563
- assert_equal 'value', cookie.value
564
- assert_equal expires, cookie.expires, "key: #{key.inspect}"
565
- }
566
- }
567
- [
568
- [:Expires, /unknown attribute name/],
569
- [:expires?, /unknown attribute name/],
570
- [[:expires], /invalid keyword/],
571
- ].each { |key, pattern|
572
- assert_warning(pattern, "warn of key: #{key.inspect}") {
573
- cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', key => expires.dup)
574
- assert_equal 'key', cookie.name
575
- assert_equal 'value', cookie.value
576
- assert_equal nil, cookie.expires, "key: #{key.inspect}"
577
- }
578
- }
579
-
580
- cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup)
581
- assert_equal 'key', cookie.name
582
- assert_equal 'value', cookie.value
583
- assert_equal expires, cookie.expires
584
- assert_equal false, cookie.for_domain?
585
- assert_raises(RuntimeError) {
586
- # domain and path are missing
587
- cookie.acceptable?
588
- }
589
-
590
- cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup, :domain => '.example.com')
591
- assert_equal 'key', cookie.name
592
- assert_equal 'value', cookie.value
593
- assert_equal expires, cookie.expires
594
- assert_equal true, cookie.for_domain?
595
- assert_raises(RuntimeError) {
596
- # path is missing
597
- cookie.acceptable?
598
- }
599
-
600
- cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup, :domain => 'example.com', :for_domain => false)
601
- assert_equal 'key', cookie.name
602
- assert_equal 'value', cookie.value
603
- assert_equal expires, cookie.expires
604
- assert_equal false, cookie.for_domain?
605
- assert_raises(RuntimeError) {
606
- # path is missing
607
- cookie.acceptable?
608
- }
609
-
610
- cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup, :domain => 'example.org', :for_domain? => true)
611
- assert_equal 'key', cookie.name
612
- assert_equal 'value', cookie.value
613
- assert_equal expires, cookie.expires
614
- assert_equal 'example.org', cookie.domain
615
- assert_equal true, cookie.for_domain?
616
- assert_raises(RuntimeError) {
617
- # path is missing
618
- cookie.acceptable?
619
- }
620
-
621
- assert_raises(ArgumentError) { HTTP::Cookie.new() }
622
- assert_raises(ArgumentError) { HTTP::Cookie.new(:value => 'value') }
623
- assert_raises(ArgumentError) { HTTP::Cookie.new('', 'value') }
624
- assert_raises(ArgumentError) { HTTP::Cookie.new('key=key', 'value') }
625
- assert_raises(ArgumentError) { HTTP::Cookie.new("key\tkey", 'value') }
626
- assert_raises(ArgumentError) { HTTP::Cookie.new('key', 'value', 'something') }
627
- assert_raises(ArgumentError) { HTTP::Cookie.new('key', 'value', {}, 'something') }
628
-
629
- [
630
- HTTP::Cookie.new(:name => 'name'),
631
- HTTP::Cookie.new("key", nil, :for_domain => true),
632
- HTTP::Cookie.new("key", nil),
633
- HTTP::Cookie.new("key", :secure => true),
634
- HTTP::Cookie.new("key"),
635
- ].each { |cookie|
636
- assert_equal '', cookie.value
637
- assert_equal true, cookie.expired?
638
- }
639
-
640
- [
641
- HTTP::Cookie.new(:name => 'name', :max_age => 3600),
642
- HTTP::Cookie.new("key", nil, :expires => Time.now + 3600),
643
- HTTP::Cookie.new("key", :expires => Time.now + 3600),
644
- HTTP::Cookie.new("key", :expires => Time.now + 3600, :value => nil),
645
- ].each { |cookie|
646
- assert_equal '', cookie.value
647
- assert_equal false, cookie.expired?
648
- }
649
- end
650
-
651
- def cookie_values(options = {})
652
- {
653
- :name => 'Foo',
654
- :value => 'Bar',
655
- :path => '/',
656
- :expires => Time.now + (10 * 86400),
657
- :for_domain => true,
658
- :domain => 'rubyforge.org',
659
- :origin => 'http://rubyforge.org/'
660
- }.merge(options)
661
- end
662
-
663
- def test_bad_name
664
- [
665
- "a\tb", "a\vb", "a\rb", "a\nb", 'a b',
666
- "a\\b", 'a"b', # 'a:b', 'a/b', 'a[b]',
667
- 'a=b', 'a,b', 'a;b',
668
- ].each { |name|
669
- assert_raises(ArgumentError) {
670
- HTTP::Cookie.new(cookie_values(:name => name))
671
- }
672
- cookie = HTTP::Cookie.new(cookie_values)
673
- assert_raises(ArgumentError) {
674
- cookie.name = name
675
- }
676
- }
677
- end
678
-
679
- def test_bad_value
680
- [
681
- "a\tb", "a\vb", "a\rb", "a\nb",
682
- "a\\b", 'a"b', # 'a:b', 'a/b', 'a[b]',
683
- ].each { |name|
684
- assert_raises(ArgumentError) {
685
- HTTP::Cookie.new(cookie_values(:name => name))
686
- }
687
- cookie = HTTP::Cookie.new(cookie_values)
688
- assert_raises(ArgumentError) {
689
- cookie.name = name
690
- }
691
- }
692
- end
693
-
694
- def test_compare
695
- time = Time.now
696
- cookies = [
697
- { :created_at => time + 1 },
698
- { :created_at => time - 1 },
699
- { :created_at => time },
700
- { :created_at => time, :path => '/foo/bar/' },
701
- { :created_at => time, :path => '/foo/' },
702
- { :created_at => time, :path => '/foo' },
703
- ].map { |attrs| HTTP::Cookie.new(cookie_values(attrs)) }
704
-
705
- assert_equal([3, 4, 5, 1, 2, 0], cookies.sort.map { |i|
706
- cookies.find_index { |j| j.equal?(i) }
707
- })
708
- end
709
-
710
- def test_expiration
711
- cookie = HTTP::Cookie.new(cookie_values)
712
-
713
- assert_equal false, cookie.expired?
714
- assert_equal true, cookie.expired?(cookie.expires + 1)
715
- assert_equal false, cookie.expired?(cookie.expires - 1)
716
- cookie.expire!
717
- assert_equal true, cookie.expired?
718
- end
719
-
720
- def test_max_age=
721
- cookie = HTTP::Cookie.new(cookie_values)
722
- expires = cookie.expires
723
-
724
- assert_raises(ArgumentError) {
725
- cookie.max_age = "+1"
726
- }
727
- # make sure #expires is not destroyed
728
- assert_equal expires, cookie.expires
729
-
730
- assert_raises(ArgumentError) {
731
- cookie.max_age = "1.5"
732
- }
733
- # make sure #expires is not destroyed
734
- assert_equal expires, cookie.expires
735
-
736
- assert_raises(ArgumentError) {
737
- cookie.max_age = "1 day"
738
- }
739
- # make sure #expires is not destroyed
740
- assert_equal expires, cookie.expires
741
-
742
- assert_raises(TypeError) {
743
- cookie.max_age = [1]
744
- }
745
- # make sure #expires is not destroyed
746
- assert_equal expires, cookie.expires
747
-
748
- cookie.max_age = "12"
749
- assert_equal 12, cookie.max_age
750
-
751
- cookie.max_age = -3
752
- assert_equal -3, cookie.max_age
753
- end
754
-
755
- def test_session
756
- cookie = HTTP::Cookie.new(cookie_values)
757
-
758
- assert_equal false, cookie.session?
759
- assert_equal nil, cookie.max_age
760
-
761
- cookie.expires = nil
762
- assert_equal true, cookie.session?
763
- assert_equal nil, cookie.max_age
764
-
765
- cookie.expires = Time.now + 3600
766
- assert_equal false, cookie.session?
767
- assert_equal nil, cookie.max_age
768
-
769
- cookie.max_age = 3600
770
- assert_equal false, cookie.session?
771
- assert_equal cookie.created_at + 3600, cookie.expires
772
-
773
- cookie.max_age = nil
774
- assert_equal true, cookie.session?
775
- assert_equal nil, cookie.expires
776
- end
777
-
778
- def test_equal
779
- assert_not_equal(HTTP::Cookie.new(cookie_values),
780
- HTTP::Cookie.new(cookie_values(:value => 'bar')))
781
- end
782
-
783
- def test_new_tld_domain
784
- url = URI 'http://rubyforge.org/'
785
-
786
- tld_cookie1 = HTTP::Cookie.new(cookie_values(:domain => 'org', :origin => url))
787
- assert_equal false, tld_cookie1.for_domain?
788
- assert_equal 'org', tld_cookie1.domain
789
- assert_equal false, tld_cookie1.acceptable?
790
-
791
- tld_cookie2 = HTTP::Cookie.new(cookie_values(:domain => '.org', :origin => url))
792
- assert_equal false, tld_cookie1.for_domain?
793
- assert_equal 'org', tld_cookie2.domain
794
- assert_equal false, tld_cookie2.acceptable?
795
- end
796
-
797
- def test_new_tld_domain_from_tld
798
- url = URI 'http://org/'
799
-
800
- tld_cookie1 = HTTP::Cookie.new(cookie_values(:domain => 'org', :origin => url))
801
- assert_equal false, tld_cookie1.for_domain?
802
- assert_equal 'org', tld_cookie1.domain
803
- assert_equal true, tld_cookie1.acceptable?
804
-
805
- tld_cookie2 = HTTP::Cookie.new(cookie_values(:domain => '.org', :origin => url))
806
- assert_equal false, tld_cookie1.for_domain?
807
- assert_equal 'org', tld_cookie2.domain
808
- assert_equal true, tld_cookie2.acceptable?
809
- end
810
-
811
- def test_fall_back_rules_for_local_domains
812
- url = URI 'http://www.example.local'
813
-
814
- tld_cookie = HTTP::Cookie.new(cookie_values(:domain => '.local', :origin => url))
815
- assert_equal false, tld_cookie.acceptable?
816
-
817
- sld_cookie = HTTP::Cookie.new(cookie_values(:domain => '.example.local', :origin => url))
818
- assert_equal true, sld_cookie.acceptable?
819
- end
820
-
821
- def test_new_rejects_cookies_with_ipv4_address_subdomain
822
- url = URI 'http://192.168.0.1/'
823
-
824
- cookie = HTTP::Cookie.new(cookie_values(:domain => '.0.1', :origin => url))
825
- assert_equal false, cookie.acceptable?
826
- end
827
-
828
- def test_value
829
- cookie = HTTP::Cookie.new('name', 'value')
830
- assert_equal 'value', cookie.value
831
-
832
- cookie.value = 'new value'
833
- assert_equal 'new value', cookie.value
834
-
835
- assert_raises(ArgumentError) { cookie.value = "a\tb" }
836
- assert_raises(ArgumentError) { cookie.value = "a\nb" }
837
-
838
- assert_equal false, cookie.expired?
839
- cookie.value = nil
840
- assert_equal '', cookie.value
841
- assert_equal true, cookie.expired?
842
- end
843
-
844
- def test_path
845
- uri = URI.parse('http://example.com/foo/bar')
846
-
847
- assert_equal '/foo/bar', uri.path
848
-
849
- cookie_str = 'a=b'
850
- cookie = HTTP::Cookie.parse(cookie_str, uri).first
851
- assert '/foo/', cookie.path
852
-
853
- cookie_str = 'a=b; path=/foo'
854
- cookie = HTTP::Cookie.parse(cookie_str, uri).first
855
- assert '/foo', cookie.path
856
-
857
- uri = URI.parse('http://example.com')
858
-
859
- assert_equal '', uri.path
860
-
861
- cookie_str = 'a=b'
862
- cookie = HTTP::Cookie.parse(cookie_str, uri).first
863
- assert '/', cookie.path
864
-
865
- cookie_str = 'a=b; path=/foo'
866
- cookie = HTTP::Cookie.parse(cookie_str, uri).first
867
- assert '/foo', cookie.path
868
- end
869
-
870
- def test_domain_nil
871
- cookie = HTTP::Cookie.new('a', 'b')
872
- assert_raises(RuntimeError) {
873
- cookie.valid_for_uri?('http://example.com/')
874
- }
875
- end
876
-
877
- def test_domain=
878
- url = URI.parse('http://host.dom.example.com:8080/')
879
-
880
- cookie_str = 'a=b; domain=Example.Com'
881
- cookie = HTTP::Cookie.parse(cookie_str, url).first
882
- assert 'example.com', cookie.domain
883
-
884
- cookie.domain = DomainName(url.host)
885
- assert 'host.dom.example.com', cookie.domain
886
-
887
- cookie.domain = 'Dom.example.com'
888
- assert 'dom.example.com', cookie.domain
889
-
890
- cookie.domain = Object.new.tap { |o|
891
- def o.to_str
892
- 'Example.com'
893
- end
894
- }
895
- assert 'example.com', cookie.domain
896
-
897
- url = URI 'http://rubyforge.org/'
898
-
899
- [nil, '', '.'].each { |d|
900
- cookie = HTTP::Cookie.new('Foo', 'Bar', :path => '/')
901
- cookie.domain = d
902
- assert_equal nil, cookie.domain, "domain=#{d.inspect}"
903
- assert_equal nil, cookie.domain_name, "domain=#{d.inspect}"
904
- assert_raises(RuntimeError) {
905
- cookie.acceptable?
906
- }
907
-
908
- cookie = HTTP::Cookie.new('Foo', 'Bar', :path => '/')
909
- cookie.origin = url
910
- cookie.domain = d
911
- assert_equal url.host, cookie.domain, "domain=#{d.inspect}"
912
- assert_equal true, cookie.acceptable?, "domain=#{d.inspect}"
913
- }
914
- end
915
-
916
- def test_origin=
917
- url = URI.parse('http://example.com/path/')
918
-
919
- cookie = HTTP::Cookie.new('a', 'b')
920
- assert_raises(ArgumentError) {
921
- cookie.origin = 123
922
- }
923
- cookie.origin = url
924
- assert_equal '/path/', cookie.path
925
- assert_equal 'example.com', cookie.domain
926
- assert_equal false, cookie.for_domain
927
- assert_raises(ArgumentError) {
928
- # cannot change the origin once set
929
- cookie.origin = URI.parse('http://www.example.com/')
930
- }
931
-
932
- cookie = HTTP::Cookie.new('a', 'b', :domain => '.example.com', :path => '/')
933
- cookie.origin = url
934
- assert_equal '/', cookie.path
935
- assert_equal 'example.com', cookie.domain
936
- assert_equal true, cookie.for_domain
937
- assert_raises(ArgumentError) {
938
- # cannot change the origin once set
939
- cookie.origin = URI.parse('http://www.example.com/')
940
- }
941
-
942
- cookie = HTTP::Cookie.new('a', 'b', :domain => '.example.com')
943
- cookie.origin = URI.parse('http://example.org/')
944
- assert_equal false, cookie.acceptable?
945
-
946
- cookie = HTTP::Cookie.new('a', 'b', :domain => '.example.com')
947
- cookie.origin = 'file:///tmp/test.html'
948
- assert_equal nil, cookie.path
949
-
950
- cookie = HTTP::Cookie.new('a', 'b', :domain => '.example.com', :path => '/')
951
- cookie.origin = 'file:///tmp/test.html'
952
- assert_equal false, cookie.acceptable?
953
- end
954
-
955
- def test_acceptable_from_uri?
956
- cookie = HTTP::Cookie.new(cookie_values(
957
- :domain => 'uk',
958
- :for_domain => true,
959
- :origin => nil))
960
- assert_equal false, cookie.for_domain?
961
- assert_equal true, cookie.acceptable_from_uri?('http://uk/')
962
- assert_equal false, cookie.acceptable_from_uri?('http://foo.uk/')
963
- end
964
-
965
- def test_valid_for_uri?
966
- {
967
- HTTP::Cookie.parse('a1=b',
968
- 'http://example.com/dir/file.html').first => {
969
- true => [
970
- 'http://example.com/dir/',
971
- 'http://example.com/dir/test.html',
972
- 'https://example.com/dir/',
973
- 'https://example.com/dir/test.html',
974
- ],
975
- false => [
976
- 'file:///dir/test.html',
977
- 'http://example.com/dir',
978
- 'http://example.com/dir2/test.html',
979
- 'http://www.example.com/dir/test.html',
980
- 'http://www.example.com/dir2/test.html',
981
- 'https://example.com/dir',
982
- 'https://example.com/dir2/test.html',
983
- 'https://www.example.com/dir/test.html',
984
- 'https://www.example.com/dir2/test.html',
985
- ]
986
- },
987
- HTTP::Cookie.parse('a2=b; path=/dir2/',
988
- 'http://example.com/dir/file.html').first => {
989
- true => [
990
- 'http://example.com/dir2/',
991
- 'http://example.com/dir2/test.html',
992
- 'https://example.com/dir2/',
993
- 'https://example.com/dir2/test.html',
994
- ],
995
- false => [
996
- 'file:///dir/test.html',
997
- 'http://example.com/dir/test.html',
998
- 'http://www.example.com/dir/test.html',
999
- 'http://www.example.com/dir2',
1000
- 'http://www.example.com/dir2/test.html',
1001
- 'https://example.com/dir/test.html',
1002
- 'https://www.example.com/dir/test.html',
1003
- 'https://www.example.com/dir2',
1004
- 'https://www.example.com/dir2/test.html',
1005
- ]
1006
- },
1007
- HTTP::Cookie.parse('a4=b; domain=example.com; path=/dir2/',
1008
- URI('http://example.com/dir/file.html')).first => {
1009
- true => [
1010
- 'https://example.com/dir2/test.html',
1011
- 'http://example.com/dir2/test.html',
1012
- 'https://www.example.com/dir2/test.html',
1013
- 'http://www.example.com/dir2/test.html',
1014
- ],
1015
- false => [
1016
- 'https://example.com/dir/test.html',
1017
- 'http://example.com/dir/test.html',
1018
- 'https://www.example.com/dir/test.html',
1019
- 'http://www.example.com/dir/test.html',
1020
- 'file:///dir2/test.html',
1021
- ]
1022
- },
1023
- HTTP::Cookie.parse('a4=b; secure',
1024
- URI('https://example.com/dir/file.html')).first => {
1025
- true => [
1026
- 'https://example.com/dir/test.html',
1027
- ],
1028
- false => [
1029
- 'http://example.com/dir/test.html',
1030
- 'https://example.com/dir2/test.html',
1031
- 'http://example.com/dir2/test.html',
1032
- 'file:///dir2/test.html',
1033
- ]
1034
- },
1035
- HTTP::Cookie.parse('a5=b',
1036
- URI('https://example.com/')).first => {
1037
- true => [
1038
- 'https://example.com',
1039
- ],
1040
- false => [
1041
- 'file:///',
1042
- ]
1043
- },
1044
- HTTP::Cookie.parse('a6=b; path=/dir',
1045
- 'http://example.com/dir/file.html').first => {
1046
- true => [
1047
- 'http://example.com/dir',
1048
- 'http://example.com/dir/',
1049
- 'http://example.com/dir/test.html',
1050
- 'https://example.com/dir',
1051
- 'https://example.com/dir/',
1052
- 'https://example.com/dir/test.html',
1053
- ],
1054
- false => [
1055
- 'file:///dir/test.html',
1056
- 'http://example.com/dir2',
1057
- 'http://example.com/dir2/test.html',
1058
- 'http://www.example.com/dir/test.html',
1059
- 'http://www.example.com/dir2/test.html',
1060
- 'https://example.com/dir2',
1061
- 'https://example.com/dir2/test.html',
1062
- 'https://www.example.com/dir/test.html',
1063
- 'https://www.example.com/dir2/test.html',
1064
- ]
1065
- },
1066
- }.each { |cookie, hash|
1067
- hash.each { |expected, urls|
1068
- urls.each { |url|
1069
- assert_equal expected, cookie.valid_for_uri?(url), '%s: %s' % [cookie.name, url]
1070
- assert_equal expected, cookie.valid_for_uri?(URI(url)), "%s: URI(%s)" % [cookie.name, url]
1071
- }
1072
- }
1073
- }
1074
- end
1075
-
1076
- def test_yaml_expires
1077
- require 'yaml'
1078
- cookie = HTTP::Cookie.new(cookie_values)
1079
-
1080
- assert_equal false, cookie.session?
1081
- assert_equal nil, cookie.max_age
1082
-
1083
- ycookie = YAML.load(cookie.to_yaml)
1084
- assert_equal false, ycookie.session?
1085
- assert_equal nil, ycookie.max_age
1086
- assert_in_delta cookie.expires, ycookie.expires, 1
1087
-
1088
- cookie.expires = nil
1089
- ycookie = YAML.load(cookie.to_yaml)
1090
- assert_equal true, ycookie.session?
1091
- assert_equal nil, ycookie.max_age
1092
-
1093
- cookie.expires = Time.now + 3600
1094
- ycookie = YAML.load(cookie.to_yaml)
1095
- assert_equal false, ycookie.session?
1096
- assert_equal nil, ycookie.max_age
1097
- assert_in_delta cookie.expires, ycookie.expires, 1
1098
-
1099
- cookie.max_age = 3600
1100
- ycookie = YAML.load(cookie.to_yaml)
1101
- assert_equal false, ycookie.session?
1102
- assert_in_delta cookie.created_at + 3600, ycookie.expires, 1
1103
-
1104
- cookie.max_age = nil
1105
- ycookie = YAML.load(cookie.to_yaml)
1106
- assert_equal true, ycookie.session?
1107
- assert_equal nil, ycookie.expires
1108
- end
1109
-
1110
- def test_s_path_match?
1111
- assert_equal true, HTTP::Cookie.path_match?('/admin/', '/admin/index')
1112
- assert_equal false, HTTP::Cookie.path_match?('/admin/', '/Admin/index')
1113
- assert_equal true, HTTP::Cookie.path_match?('/admin/', '/admin/')
1114
- assert_equal false, HTTP::Cookie.path_match?('/admin/', '/admin')
1115
-
1116
- assert_equal true, HTTP::Cookie.path_match?('/admin', '/admin')
1117
- assert_equal false, HTTP::Cookie.path_match?('/admin', '/Admin')
1118
- assert_equal false, HTTP::Cookie.path_match?('/admin', '/admins')
1119
- assert_equal true, HTTP::Cookie.path_match?('/admin', '/admin/')
1120
- assert_equal true, HTTP::Cookie.path_match?('/admin', '/admin/index')
1121
- end
1122
- end