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.
@@ -1,633 +1,631 @@
1
1
  require File.expand_path('helper', File.dirname(__FILE__))
2
2
  require 'tmpdir'
3
3
 
4
- module TestHTTPCookieJar; end
5
-
6
- module TestHTTPCookieJar::Tests
7
- def setup(options = nil, options2 = nil)
8
- default_options = {
9
- :store => :hash,
10
- :gc_threshold => 150,
11
- }
12
- new_options = default_options.merge(options || {})
13
- new_options2 = new_options.merge(options2 || {})
14
- @store_type = new_options[:store]
15
- @gc_threshold = new_options[:gc_threshold]
16
- @jar = HTTP::CookieJar.new(new_options)
17
- @jar2 = HTTP::CookieJar.new(new_options)
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
- def hash_store?
21
- @store_type == :hash
22
- end
19
+ def hash_store?
20
+ @store_type == :hash
21
+ end
23
22
 
24
- def mozilla_store?
25
- @store_type == :mozilla
26
- end
23
+ def mozilla_store?
24
+ @store_type == :mozilla
25
+ end
27
26
 
28
- def cookie_values(options = {})
29
- {
30
- :name => 'Foo',
31
- :value => 'Bar',
32
- :path => '/',
33
- :expires => Time.at(Time.now.to_i + 10 * 86400), # to_i is important here
34
- :for_domain => true,
35
- :domain => 'rubyforge.org',
36
- :origin => 'http://rubyforge.org/'
37
- }.merge(options)
38
- end
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
- def test_empty?
41
- assert_equal true, @jar.empty?
42
- cookie = HTTP::Cookie.new(cookie_values)
43
- @jar.add(cookie)
44
- assert_equal false, @jar.empty?
45
- assert_equal false, @jar.empty?('http://rubyforge.org/')
46
- assert_equal true, @jar.empty?('http://example.local/')
47
- end
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
- def test_two_cookies_same_domain_and_name_different_paths
50
- url = URI 'http://rubyforge.org/'
48
+ def test_two_cookies_same_domain_and_name_different_paths
49
+ url = URI 'http://rubyforge.org/'
51
50
 
52
- cookie = HTTP::Cookie.new(cookie_values)
53
- @jar.add(cookie)
54
- @jar.add(HTTP::Cookie.new(cookie_values(:path => '/onetwo')))
51
+ cookie = HTTP::Cookie.new(cookie_values)
52
+ @jar.add(cookie)
53
+ @jar.add(HTTP::Cookie.new(cookie_values(:path => '/onetwo')))
55
54
 
56
- assert_equal(1, @jar.cookies(url).length)
57
- assert_equal 2, @jar.cookies(URI('http://rubyforge.org/onetwo')).length
58
- end
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
- def test_domain_case
61
- url = URI 'http://rubyforge.org/'
59
+ def test_domain_case
60
+ url = URI 'http://rubyforge.org/'
62
61
 
63
- # Add one cookie with an expiration date in the future
64
- cookie = HTTP::Cookie.new(cookie_values)
65
- @jar.add(cookie)
66
- assert_equal(1, @jar.cookies(url).length)
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
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => 'RuByForge.Org', :name => 'aaron')))
67
+ @jar.add(HTTP::Cookie.new(cookie_values(:domain => 'RuByForge.Org', :name => 'aaron')))
69
68
 
70
- assert_equal(2, @jar.cookies(url).length)
69
+ assert_equal(2, @jar.cookies(url).length)
71
70
 
72
- url2 = URI 'http://RuByFoRgE.oRg/'
73
- assert_equal(2, @jar.cookies(url2).length)
74
- end
71
+ url2 = URI 'http://RuByFoRgE.oRg/'
72
+ assert_equal(2, @jar.cookies(url2).length)
73
+ end
75
74
 
76
- def test_host_only
77
- url = URI.parse('http://rubyforge.org/')
75
+ def test_host_only
76
+ url = URI.parse('http://rubyforge.org/')
78
77
 
79
- @jar.add(HTTP::Cookie.new(
80
- cookie_values(:domain => 'rubyforge.org', :for_domain => false)))
78
+ @jar.add(HTTP::Cookie.new(
79
+ cookie_values(:domain => 'rubyforge.org', :for_domain => false)))
81
80
 
82
- assert_equal(1, @jar.cookies(url).length)
81
+ assert_equal(1, @jar.cookies(url).length)
83
82
 
84
- assert_equal(1, @jar.cookies(URI('http://RubyForge.org/')).length)
83
+ assert_equal(1, @jar.cookies(URI('http://RubyForge.org/')).length)
85
84
 
86
- assert_equal(1, @jar.cookies(URI('https://RubyForge.org/')).length)
85
+ assert_equal(1, @jar.cookies(URI('https://RubyForge.org/')).length)
87
86
 
88
- assert_equal(0, @jar.cookies(URI('http://www.rubyforge.org/')).length)
89
- end
87
+ assert_equal(0, @jar.cookies(URI('http://www.rubyforge.org/')).length)
88
+ end
90
89
 
91
- def test_empty_value
92
- url = URI 'http://rubyforge.org/'
93
- values = cookie_values(:value => "")
90
+ def test_empty_value
91
+ url = URI 'http://rubyforge.org/'
92
+ values = cookie_values(:value => "")
94
93
 
95
- # Add one cookie with an expiration date in the future
96
- cookie = HTTP::Cookie.new(values)
97
- @jar.add(cookie)
98
- assert_equal(1, @jar.cookies(url).length)
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
- @jar.add HTTP::Cookie.new(values.merge(:domain => 'RuByForge.Org',
101
- :name => 'aaron'))
99
+ @jar.add HTTP::Cookie.new(values.merge(:domain => 'RuByForge.Org',
100
+ :name => 'aaron'))
102
101
 
103
- assert_equal(2, @jar.cookies(url).length)
102
+ assert_equal(2, @jar.cookies(url).length)
104
103
 
105
- url2 = URI 'http://RuByFoRgE.oRg/'
106
- assert_equal(2, @jar.cookies(url2).length)
107
- end
104
+ url2 = URI 'http://RuByFoRgE.oRg/'
105
+ assert_equal(2, @jar.cookies(url2).length)
106
+ end
108
107
 
109
- def test_add_future_cookies
110
- url = URI 'http://rubyforge.org/'
108
+ def test_add_future_cookies
109
+ url = URI 'http://rubyforge.org/'
111
110
 
112
- # Add one cookie with an expiration date in the future
113
- cookie = HTTP::Cookie.new(cookie_values)
114
- @jar.add(cookie)
115
- assert_equal(1, @jar.cookies(url).length)
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
- # Add the same cookie, and we should still only have one
118
- @jar.add(HTTP::Cookie.new(cookie_values))
119
- assert_equal(1, @jar.cookies(url).length)
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
- # Make sure we can get the cookie from different paths
122
- assert_equal(1, @jar.cookies(URI('http://rubyforge.org/login')).length)
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
- # Make sure we can't get the cookie from different domains
125
- assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
126
- end
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
- def test_add_multiple_cookies
129
- url = URI 'http://rubyforge.org/'
127
+ def test_add_multiple_cookies
128
+ url = URI 'http://rubyforge.org/'
130
129
 
131
- # Add one cookie with an expiration date in the future
132
- cookie = HTTP::Cookie.new(cookie_values)
133
- @jar.add(cookie)
134
- assert_equal(1, @jar.cookies(url).length)
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
- # Add the same cookie, and we should still only have one
137
- @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz')))
138
- assert_equal(2, @jar.cookies(url).length)
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
- # Make sure we can get the cookie from different paths
141
- assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
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
- # Make sure we can't get the cookie from different domains
144
- assert_equal(0, @jar.cookies(URI('http://google.com/')).length)
145
- end
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
- def test_add_multiple_cookies_with_the_same_name
148
- now = Time.now
146
+ def test_add_multiple_cookies_with_the_same_name
147
+ now = Time.now
149
148
 
150
- cookies = [
151
- { :value => 'a', :path => '/', },
152
- { :value => 'b', :path => '/abc/def/', :created_at => now - 1 },
153
- { :value => 'c', :path => '/abc/def/', :domain => 'www.rubyforge.org', :origin => 'http://www.rubyforge.org/abc/def/', :created_at => now },
154
- { :value => 'd', :path => '/abc/' },
155
- ].map { |attrs|
156
- HTTP::Cookie.new(cookie_values(attrs))
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
- url = URI 'http://www.rubyforge.org/abc/def/ghi'
158
+ url = URI 'http://www.rubyforge.org/abc/def/ghi'
160
159
 
161
- cookies.permutation(cookies.size) { |shuffled|
162
- @jar.clear
163
- shuffled.each { |cookie| @jar.add(cookie) }
164
- assert_equal %w[b c d a], @jar.cookies(url).map { |cookie| cookie.value }
165
- }
166
- end
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
- def test_fall_back_rules_for_local_domains
169
- url = URI 'http://www.example.local'
167
+ def test_fall_back_rules_for_local_domains
168
+ url = URI 'http://www.example.local'
170
169
 
171
- sld_cookie = HTTP::Cookie.new(cookie_values(:domain => '.example.local', :origin => url))
172
- @jar.add(sld_cookie)
170
+ sld_cookie = HTTP::Cookie.new(cookie_values(:domain => '.example.local', :origin => url))
171
+ @jar.add(sld_cookie)
173
172
 
174
- assert_equal(1, @jar.cookies(url).length)
175
- end
173
+ assert_equal(1, @jar.cookies(url).length)
174
+ end
176
175
 
177
- def test_add_makes_exception_for_localhost
178
- url = URI 'http://localhost'
176
+ def test_add_makes_exception_for_localhost
177
+ url = URI 'http://localhost'
179
178
 
180
- tld_cookie = HTTP::Cookie.new(cookie_values(:domain => 'localhost', :origin => url))
181
- @jar.add(tld_cookie)
179
+ tld_cookie = HTTP::Cookie.new(cookie_values(:domain => 'localhost', :origin => url))
180
+ @jar.add(tld_cookie)
182
181
 
183
- assert_equal(1, @jar.cookies(url).length)
184
- end
182
+ assert_equal(1, @jar.cookies(url).length)
183
+ end
185
184
 
186
- def test_add_cookie_for_the_parent_domain
187
- url = URI 'http://x.foo.com'
185
+ def test_add_cookie_for_the_parent_domain
186
+ url = URI 'http://x.foo.com'
188
187
 
189
- cookie = HTTP::Cookie.new(cookie_values(:domain => '.foo.com', :origin => url))
190
- @jar.add(cookie)
188
+ cookie = HTTP::Cookie.new(cookie_values(:domain => '.foo.com', :origin => url))
189
+ @jar.add(cookie)
191
190
 
192
- assert_equal(1, @jar.cookies(url).length)
193
- end
191
+ assert_equal(1, @jar.cookies(url).length)
192
+ end
194
193
 
195
- def test_add_rejects_cookies_with_unknown_domain_or_path
196
- cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :domain].include?(k) })
197
- assert_raises(ArgumentError) {
198
- @jar.add(cookie)
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
- cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :path].include?(k) })
202
- assert_raises(ArgumentError) {
203
- @jar.add(cookie)
204
- }
205
- end
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
- def test_add_does_not_reject_cookies_from_a_nested_subdomain
208
- url = URI 'http://y.x.foo.com'
206
+ def test_add_does_not_reject_cookies_from_a_nested_subdomain
207
+ url = URI 'http://y.x.foo.com'
209
208
 
210
- cookie = HTTP::Cookie.new(cookie_values(:domain => '.foo.com', :origin => url))
211
- @jar.add(cookie)
209
+ cookie = HTTP::Cookie.new(cookie_values(:domain => '.foo.com', :origin => url))
210
+ @jar.add(cookie)
212
211
 
213
- assert_equal(1, @jar.cookies(url).length)
214
- end
212
+ assert_equal(1, @jar.cookies(url).length)
213
+ end
215
214
 
216
- def test_cookie_without_leading_dot_does_not_cause_substring_match
217
- url = URI 'http://arubyforge.org/'
215
+ def test_cookie_without_leading_dot_does_not_cause_substring_match
216
+ url = URI 'http://arubyforge.org/'
218
217
 
219
- cookie = HTTP::Cookie.new(cookie_values(:domain => 'rubyforge.org'))
220
- @jar.add(cookie)
218
+ cookie = HTTP::Cookie.new(cookie_values(:domain => 'rubyforge.org'))
219
+ @jar.add(cookie)
221
220
 
222
- assert_equal(0, @jar.cookies(url).length)
223
- end
221
+ assert_equal(0, @jar.cookies(url).length)
222
+ end
224
223
 
225
- def test_cookie_without_leading_dot_matches_subdomains
226
- url = URI 'http://admin.rubyforge.org/'
224
+ def test_cookie_without_leading_dot_matches_subdomains
225
+ url = URI 'http://admin.rubyforge.org/'
227
226
 
228
- cookie = HTTP::Cookie.new(cookie_values(:domain => 'rubyforge.org', :origin => url))
229
- @jar.add(cookie)
227
+ cookie = HTTP::Cookie.new(cookie_values(:domain => 'rubyforge.org', :origin => url))
228
+ @jar.add(cookie)
230
229
 
231
- assert_equal(1, @jar.cookies(url).length)
232
- end
230
+ assert_equal(1, @jar.cookies(url).length)
231
+ end
233
232
 
234
- def test_cookies_with_leading_dot_match_subdomains
235
- url = URI 'http://admin.rubyforge.org/'
233
+ def test_cookies_with_leading_dot_match_subdomains
234
+ url = URI 'http://admin.rubyforge.org/'
236
235
 
237
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org', :origin => url)))
236
+ @jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org', :origin => url)))
238
237
 
239
- assert_equal(1, @jar.cookies(url).length)
240
- end
238
+ assert_equal(1, @jar.cookies(url).length)
239
+ end
241
240
 
242
- def test_cookies_with_leading_dot_match_parent_domains
243
- url = URI 'http://rubyforge.org/'
241
+ def test_cookies_with_leading_dot_match_parent_domains
242
+ url = URI 'http://rubyforge.org/'
244
243
 
245
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org', :origin => url)))
244
+ @jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org', :origin => url)))
246
245
 
247
- assert_equal(1, @jar.cookies(url).length)
248
- end
246
+ assert_equal(1, @jar.cookies(url).length)
247
+ end
249
248
 
250
- def test_cookies_with_leading_dot_match_parent_domains_exactly
251
- url = URI 'http://arubyforge.org/'
249
+ def test_cookies_with_leading_dot_match_parent_domains_exactly
250
+ url = URI 'http://arubyforge.org/'
252
251
 
253
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
252
+ @jar.add(HTTP::Cookie.new(cookie_values(:domain => '.rubyforge.org')))
254
253
 
255
- assert_equal(0, @jar.cookies(url).length)
256
- end
254
+ assert_equal(0, @jar.cookies(url).length)
255
+ end
257
256
 
258
- def test_cookie_for_ipv4_address_matches_the_exact_ipaddress
259
- url = URI 'http://192.168.0.1/'
257
+ def test_cookie_for_ipv4_address_matches_the_exact_ipaddress
258
+ url = URI 'http://192.168.0.1/'
260
259
 
261
- cookie = HTTP::Cookie.new(cookie_values(:domain => '192.168.0.1', :origin => url))
262
- @jar.add(cookie)
260
+ cookie = HTTP::Cookie.new(cookie_values(:domain => '192.168.0.1', :origin => url))
261
+ @jar.add(cookie)
263
262
 
264
- assert_equal(1, @jar.cookies(url).length)
265
- end
263
+ assert_equal(1, @jar.cookies(url).length)
264
+ end
266
265
 
267
- def test_cookie_for_ipv6_address_matches_the_exact_ipaddress
268
- url = URI 'http://[fe80::0123:4567:89ab:cdef]/'
266
+ def test_cookie_for_ipv6_address_matches_the_exact_ipaddress
267
+ url = URI 'http://[fe80::0123:4567:89ab:cdef]/'
269
268
 
270
- cookie = HTTP::Cookie.new(cookie_values(:domain => '[fe80::0123:4567:89ab:cdef]', :origin => url))
271
- @jar.add(cookie)
269
+ cookie = HTTP::Cookie.new(cookie_values(:domain => '[fe80::0123:4567:89ab:cdef]', :origin => url))
270
+ @jar.add(cookie)
272
271
 
273
- assert_equal(1, @jar.cookies(url).length)
274
- end
272
+ assert_equal(1, @jar.cookies(url).length)
273
+ end
275
274
 
276
- def test_cookies_dot
277
- url = URI 'http://www.host.example/'
275
+ def test_cookies_dot
276
+ url = URI 'http://www.host.example/'
278
277
 
279
- @jar.add(HTTP::Cookie.new(cookie_values(:domain => 'www.host.example', :origin => url)))
278
+ @jar.add(HTTP::Cookie.new(cookie_values(:domain => 'www.host.example', :origin => url)))
280
279
 
281
- url = URI 'http://wwwxhost.example/'
282
- assert_equal(0, @jar.cookies(url).length)
283
- end
280
+ url = URI 'http://wwwxhost.example/'
281
+ assert_equal(0, @jar.cookies(url).length)
282
+ end
284
283
 
285
- def test_cookies_no_host
286
- url = URI 'file:///path/'
284
+ def test_cookies_no_host
285
+ url = URI 'file:///path/'
287
286
 
288
- assert_raises(ArgumentError) {
289
- @jar.add(HTTP::Cookie.new(cookie_values(:origin => url)))
290
- }
287
+ assert_raises(ArgumentError) {
288
+ @jar.add(HTTP::Cookie.new(cookie_values(:origin => url)))
289
+ }
291
290
 
292
- assert_equal(0, @jar.cookies(url).length)
293
- end
291
+ assert_equal(0, @jar.cookies(url).length)
292
+ end
294
293
 
295
- def test_clear
296
- url = URI 'http://rubyforge.org/'
294
+ def test_clear
295
+ url = URI 'http://rubyforge.org/'
297
296
 
298
- # Add one cookie with an expiration date in the future
299
- cookie = HTTP::Cookie.new(cookie_values(:origin => url))
300
- @jar.add(cookie)
301
- @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz', :origin => url)))
302
- assert_equal(2, @jar.cookies(url).length)
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
- @jar.clear
303
+ @jar.clear
305
304
 
306
- assert_equal(0, @jar.cookies(url).length)
307
- end
305
+ assert_equal(0, @jar.cookies(url).length)
306
+ end
308
307
 
309
- def test_save_cookies_yaml
310
- url = URI 'http://rubyforge.org/'
308
+ def test_save_cookies_yaml
309
+ url = URI 'http://rubyforge.org/'
311
310
 
312
- # Add one cookie with an expiration date in the future
313
- cookie = HTTP::Cookie.new(cookie_values(:origin => url))
314
- s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar',
315
- :expires => nil,
316
- :session => true,
317
- :origin => url))
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
- @jar.add(cookie)
320
- @jar.add(s_cookie)
321
- @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz', :for_domain => false, :origin => url)))
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
- assert_equal(3, @jar.cookies(url).length)
324
+ Dir.mktmpdir do |dir|
325
+ value = @jar.save(File.join(dir, "cookies.yml"))
326
+ assert_same @jar, value
324
327
 
325
- Dir.mktmpdir do |dir|
326
- value = @jar.save(File.join(dir, "cookies.yml"))
327
- assert_same @jar, value
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
- @jar2.load(File.join(dir, "cookies.yml"))
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
- assert_equal(3, @jar.cookies(url).length)
339
- end
340
+ def test_save_session_cookies_yaml
341
+ url = URI 'http://rubyforge.org/'
340
342
 
341
- def test_save_session_cookies_yaml
342
- url = URI 'http://rubyforge.org/'
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
- # Add one cookie with an expiration date in the future
345
- cookie = HTTP::Cookie.new(cookie_values)
346
- s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar',
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
- @jar.add(cookie)
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
- assert_equal(3, @jar.cookies(url).length)
355
+ Dir.mktmpdir do |dir|
356
+ @jar.save(File.join(dir, "cookies.yml"), :format => :yaml, :session => true)
355
357
 
356
- Dir.mktmpdir do |dir|
357
- @jar.save(File.join(dir, "cookies.yml"), :format => :yaml, :session => true)
358
+ @jar2.load(File.join(dir, "cookies.yml"))
359
+ assert_equal(3, @jar2.cookies(url).length)
360
+ end
358
361
 
359
- @jar2.load(File.join(dir, "cookies.yml"))
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
- def test_save_and_read_cookiestxt
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
- assert_equal(5, @jar.cookies(url).length)
440
- end
441
+ def test_expire_cookies
442
+ url = URI 'http://rubyforge.org/'
441
443
 
442
- def test_expire_cookies
443
- url = URI 'http://rubyforge.org/'
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
- # Add one cookie with an expiration date in the future
446
- cookie = HTTP::Cookie.new(cookie_values)
447
- @jar.add(cookie)
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
- # Add a second cookie
451
- @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz')))
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
- # Make sure we can get the cookie from different paths
455
- assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
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
- # Expire the first cookie
458
- @jar.add(HTTP::Cookie.new(cookie_values(:expires => Time.now - (10 * 86400))))
459
- assert_equal(1, @jar.cookies(url).length)
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
- # Expire the second cookie
462
- @jar.add(HTTP::Cookie.new(cookie_values( :name => 'Baz', :expires => Time.now - (10 * 86400))))
463
- assert_equal(0, @jar.cookies(url).length)
464
- end
465
+ def test_session_cookies
466
+ values = cookie_values(:expires => nil)
467
+ url = URI 'http://rubyforge.org/'
465
468
 
466
- def test_session_cookies
467
- values = cookie_values(:expires => nil)
468
- url = URI 'http://rubyforge.org/'
469
-
470
- # Add one cookie with an expiration date in the future
471
- cookie = HTTP::Cookie.new(values)
472
- @jar.add(cookie)
473
- assert_equal(1, @jar.cookies(url).length)
474
-
475
- # Add a second cookie
476
- @jar.add(HTTP::Cookie.new(values.merge(:name => 'Baz')))
477
- assert_equal(2, @jar.cookies(url).length)
478
-
479
- # Make sure we can get the cookie from different paths
480
- assert_equal(2, @jar.cookies(URI('http://rubyforge.org/login')).length)
481
-
482
- # Expire the first cookie
483
- @jar.add(HTTP::Cookie.new(values.merge(:expires => Time.now - (10 * 86400))))
484
- assert_equal(1, @jar.cookies(url).length)
485
-
486
- # Expire the second cookie
487
- @jar.add(HTTP::Cookie.new(values.merge(:name => 'Baz', :expires => Time.now - (10 * 86400))))
488
- assert_equal(0, @jar.cookies(url).length)
489
-
490
- # When given a URI with a blank path, CookieJar#cookies should return
491
- # cookies with the path '/':
492
- url = URI 'http://rubyforge.org'
493
- assert_equal '', url.path
494
- assert_equal(0, @jar.cookies(url).length)
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
- def test_paths
501
- url = URI 'http://rubyforge.org/login'
502
- values = cookie_values(:path => "/login", :expires => nil, :origin => url)
499
+ def test_paths
500
+ url = URI 'http://rubyforge.org/login'
501
+ values = cookie_values(:path => "/login", :expires => nil, :origin => url)
503
502
 
504
- # Add one cookie with an expiration date in the future
505
- cookie = HTTP::Cookie.new(values)
506
- @jar.add(cookie)
507
- assert_equal(1, @jar.cookies(url).length)
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
- # Add a second cookie
510
- @jar.add(HTTP::Cookie.new(values.merge( :name => 'Baz' )))
511
- assert_equal(2, @jar.cookies(url).length)
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
- # Make sure we don't get the cookie in a different path
514
- assert_equal(0, @jar.cookies(URI('http://rubyforge.org/hello')).length)
515
- assert_equal(0, @jar.cookies(URI('http://rubyforge.org/')).length)
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
- # Expire the first cookie
518
- @jar.add(HTTP::Cookie.new(values.merge( :expires => Time.now - (10 * 86400))))
519
- assert_equal(1, @jar.cookies(url).length)
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
- # Expire the second cookie
522
- @jar.add(HTTP::Cookie.new(values.merge( :name => 'Baz',
523
- :expires => Time.now - (10 * 86400))))
524
- assert_equal(0, @jar.cookies(url).length)
525
- end
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
- def test_ssl_cookies
528
- # thanks to michal "ocher" ochman for reporting the bug responsible for this test.
529
- values = cookie_values(:expires => nil)
530
- values_ssl = values.merge(:name => 'Baz', :domain => "#{values[:domain]}:443")
531
- url = URI 'https://rubyforge.org/login'
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
- cookie = HTTP::Cookie.new(values)
534
- @jar.add(cookie)
535
- assert_equal(1, @jar.cookies(url).length, "did not handle SSL cookie")
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
- cookie = HTTP::Cookie.new(values_ssl)
538
- @jar.add(cookie)
539
- assert_equal(2, @jar.cookies(url).length, "did not handle SSL cookie with :443")
540
- end
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
- def test_secure_cookie
543
- nurl = URI 'http://rubyforge.org/login'
544
- surl = URI 'https://rubyforge.org/login'
541
+ def test_secure_cookie
542
+ nurl = URI 'http://rubyforge.org/login'
543
+ surl = URI 'https://rubyforge.org/login'
545
544
 
546
- nncookie = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => nurl))
547
- sncookie = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => surl))
548
- nscookie = HTTP::Cookie.new(cookie_values(:name => 'Foo2', :secure => true, :origin => nurl))
549
- sscookie = HTTP::Cookie.new(cookie_values(:name => 'Foo2', :secure => true, :origin => surl))
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
- @jar.add(nncookie)
552
- @jar.add(sncookie)
553
- @jar.add(nscookie)
554
- @jar.add(sscookie)
550
+ @jar.add(nncookie)
551
+ @jar.add(sncookie)
552
+ @jar.add(nscookie)
553
+ @jar.add(sscookie)
555
554
 
556
- assert_equal('Foo1', @jar.cookies(nurl).map { |c| c.name }.sort.join(' ') )
557
- assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
558
- end
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
- def _test_delete
561
- nurl = URI 'http://rubyforge.org/login'
562
- surl = URI 'https://rubyforge.org/login'
559
+ def _test_delete
560
+ nurl = URI 'http://rubyforge.org/login'
561
+ surl = URI 'https://rubyforge.org/login'
563
562
 
564
- cookie1 = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => nurl))
565
- cookie2 = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => surl))
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
- @jar.add(nncookie)
568
- @jar.add(sncookie)
569
- @jar.add(nscookie)
570
- @jar.add(sscookie)
571
- end
566
+ @jar.add(nncookie)
567
+ @jar.add(sncookie)
568
+ @jar.add(nscookie)
569
+ @jar.add(sscookie)
570
+ end
572
571
 
573
- def test_max_cookies
574
- slimit = HTTP::Cookie::MAX_COOKIES_TOTAL + @gc_threshold
575
-
576
- limit_per_domain = HTTP::Cookie::MAX_COOKIES_PER_DOMAIN
577
- uri = URI('http://www.example.org/')
578
- date = Time.at(Time.now.to_i + 86400)
579
- (1..(limit_per_domain + 1)).each { |i|
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 => 'Baz%d' % j,
608
- :value => 'www%d.example.jp' % j,
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 == j ? date - i : date
587
+ cookie.created_at = i == 42 ? date - i : date
615
588
  }
616
- count += 1
617
589
  }
618
- }
619
-
620
- assert_equal true, count > slimit
621
- assert_equal true, @jar.to_a.size <= slimit
622
- @jar.cleanup
623
- assert_equal hlimit, @jar.to_a.size
624
- assert_equal false, @jar.any? { |cookie|
625
- cookie.domain == cookie.value
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