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