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