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