http-cookie 1.0.0.pre6 → 1.0.0.pre7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 552b1f843786c58092655dff1e2587bafd65c995
4
- data.tar.gz: ead56fc0cdda0ee554a8826eaab35e4b665c7e8a
3
+ metadata.gz: 36d302ade786727a1d6a34617d6f5fd5a0849ad5
4
+ data.tar.gz: 9f0f607f3f78427ec0f1737ce770e58aeb831110
5
5
  SHA512:
6
- metadata.gz: ab0a84bce856ce0bde5b0200fe311c06875b9507a88c8829ff4e2a43db33a482ffc04ffbfb102caf30ca2af1528b2d1d42e6728ec2a4604aac7dc4107786da2d
7
- data.tar.gz: 4747df38af92f497d315840aa706742d5ac8b3514247f3b85955b7d9f8df696689349c4d7cf8bdbd4f81de1b16e4bf16f671c13df0860b3513c56b7a94268b61
6
+ metadata.gz: 2288ba0ccc9dda414610bb4b87765492c480c1aa886c2b7b716c3d57a4373baa2de020cffb4cb93cfbb00f5dd7283a814c98573a3bbcc752f6f213a0dd8f290c
7
+ data.tar.gz: b08eb434043d9601cf743ba30a4c279356da12b4da8c243d37f9f8974ebbfe8d8b55fbeb28d2020e9f346aebcd545f5abedd8b8bb9ecb0eb76dc890576491688
@@ -170,7 +170,7 @@ class HTTP::Cookie
170
170
  raise ArgumentError, "wrong number of arguments (#{args.size} for 1-3)"
171
171
  end
172
172
  for_domain = false
173
- max_age = origin = nil
173
+ domain = max_age = origin = nil
174
174
  attr_hash.each_pair { |key, val|
175
175
  skey = key.to_s.downcase
176
176
  if skey.sub!(/\?\z/, '')
@@ -179,6 +179,8 @@ class HTTP::Cookie
179
179
  case skey
180
180
  when 'for_domain'
181
181
  for_domain = !!val
182
+ when 'domain'
183
+ domain = val
182
184
  when 'origin'
183
185
  origin = val
184
186
  when 'max_age'
@@ -193,6 +195,7 @@ class HTTP::Cookie
193
195
  raise ArgumentError, "at least name and value must be specified"
194
196
  end
195
197
  @for_domain = for_domain
198
+ self.domain = domain if domain
196
199
  self.origin = origin if origin
197
200
  self.max_age = max_age if max_age
198
201
  end
@@ -283,8 +286,8 @@ class HTTP::Cookie
283
286
  begin
284
287
  case aname
285
288
  when 'domain'
286
- cookie.domain = avalue
287
289
  cookie.for_domain = true
290
+ cookie.domain = avalue # This may negate @for_domain
288
291
  when 'path'
289
292
  cookie.path = avalue
290
293
  when 'expires'
@@ -376,6 +379,10 @@ class HTTP::Cookie
376
379
  end
377
380
  @domain_name = DomainName.new(domain)
378
381
  end
382
+ # RFC 6265 5.3 5.
383
+ if @domain_name.domain.nil? # a public suffix or IP address
384
+ @for_domain = false
385
+ end
379
386
  @domain = @domain_name.hostname
380
387
  end
381
388
 
@@ -388,7 +395,7 @@ class HTTP::Cookie
388
395
  # Returns the domain attribute value as a DomainName object.
389
396
  attr_reader :domain_name
390
397
 
391
- # The domain flag.
398
+ # The domain flag. (the opposite of host-only-flag)
392
399
  #
393
400
  # If this flag is true, this cookie will be sent to any host in the
394
401
  # \#domain, including the host domain itself. If it is false, this
@@ -419,20 +426,20 @@ class HTTP::Cookie
419
426
  @origin = origin
420
427
  end
421
428
 
422
- # The secure flag.
429
+ # The secure flag. (secure-only-flag)
423
430
  #
424
431
  # A cookie with this flag on should only be sent via a secure
425
432
  # protocol like HTTPS.
426
433
  attr_accessor :secure
427
434
  alias secure? secure
428
435
 
429
- # The HttpOnly flag.
436
+ # The HttpOnly flag. (http-only-flag)
430
437
  #
431
438
  # A cookie with this flag on should be hidden from a client script.
432
439
  attr_accessor :httponly
433
440
  alias httponly? httponly
434
441
 
435
- # The session flag.
442
+ # The session flag. (the opposite of persistent-flag)
436
443
  #
437
444
  # A cookie with this flag on should be hidden from a client script.
438
445
  attr_reader :session
@@ -514,16 +521,12 @@ class HTTP::Cookie
514
521
  host = DomainName.new(uri.host)
515
522
 
516
523
  # RFC 6265 5.3
517
- # When the user agent "receives a cookie":
518
- return @domain.nil? || host.hostname == @domain unless @for_domain
524
+ return true if host.hostname == @domain
519
525
 
520
- if host.cookie_domain?(@domain_name)
521
- true
522
- elsif host.hostname == @domain
523
- @for_domain = false
524
- true
526
+ if @for_domain # !host-only-flag
527
+ host.cookie_domain?(@domain_name)
525
528
  else
526
- false
529
+ @domain.nil?
527
530
  end
528
531
  end
529
532
 
@@ -534,6 +537,7 @@ class HTTP::Cookie
534
537
  raise "cannot tell if this cookie is valid because the domain is unknown"
535
538
  end
536
539
  uri = URI(uri)
540
+ # RFC 6265 5.4
537
541
  return false if secure? && !(URI::HTTPS === uri)
538
542
  acceptable_from_uri?(uri) && HTTP::Cookie.path_match?(@path, uri.path)
539
543
  end
@@ -556,7 +560,7 @@ class HTTP::Cookie
556
560
  raise "origin must be specified to produce a value for Set-Cookie"
557
561
 
558
562
  string = "#{@name}=#{Scanner.quote(@value)}"
559
- if @for_domain || @domain != DomainName.new(origin.host).hostname
563
+ if @for_domain
560
564
  string << "; Domain=#{@domain}"
561
565
  end
562
566
  if (origin + './').path != @path
@@ -1,5 +1,5 @@
1
1
  module HTTP
2
2
  class Cookie
3
- VERSION = "1.0.0.pre6"
3
+ VERSION = "1.0.0.pre7"
4
4
  end
5
5
  end
@@ -1,4 +1,8 @@
1
+ require 'monitor'
2
+
1
3
  class HTTP::CookieJar::AbstractStore
4
+ include MonitorMixin
5
+
2
6
  class << self
3
7
  @@class_map = {}
4
8
 
@@ -31,6 +35,7 @@ class HTTP::CookieJar::AbstractStore
31
35
  private :default_options
32
36
 
33
37
  def initialize(options = nil)
38
+ super() # MonitorMixin
34
39
  options ||= {}
35
40
  @logger = options[:logger]
36
41
  # Initializes each instance variable of the same name as option
@@ -50,6 +55,11 @@ class HTTP::CookieJar::AbstractStore
50
55
  self
51
56
  end
52
57
 
58
+ def delete(cookie)
59
+ raise
60
+ self
61
+ end
62
+
53
63
  # Iterates over all cookies that are not expired.
54
64
  #
55
65
  # An optional argument +uri+ specifies a URI object indicating the
@@ -60,7 +70,13 @@ class HTTP::CookieJar::AbstractStore
60
70
  # If (and only if) the +uri+ option is given, last access time of
61
71
  # each cookie is updated to the current time.
62
72
  def each(uri = nil, &block)
63
- raise
73
+ if uri
74
+ raise
75
+ else
76
+ synchronize {
77
+ raise
78
+ }
79
+ end
64
80
  self
65
81
  end
66
82
  include Enumerable
@@ -80,7 +96,7 @@ class HTTP::CookieJar::AbstractStore
80
96
  else
81
97
  select(&:expired?)
82
98
  end.each { |cookie|
83
- add(cookie.expire)
99
+ delete(cookie)
84
100
  }
85
101
  # subclasses can optionally remove over-the-limit cookies.
86
102
  self
@@ -10,6 +10,15 @@ end
10
10
 
11
11
  class HTTP::CookieJar
12
12
  # A store class that uses a hash-based cookie store.
13
+ #
14
+ # In this store, cookies that share the same name, domain and path
15
+ # will overwrite each other regardless of the `for_domain` flag
16
+ # value. This store is built after the storage model described in
17
+ # RFC 6265 5.3 where there is no mention of how the host-only-flag
18
+ # affects in storing cookies. On the other hand, in MozillaStore
19
+ # two cookies with the same name, domain and path coexist as long as
20
+ # they differ in the `for_domain` flag value, which means they need
21
+ # to be expired individually.
13
22
  class HashStore < AbstractStore
14
23
  def default_options
15
24
  {
@@ -57,7 +66,6 @@ class HTTP::CookieJar
57
66
  path_cookies.delete(cookie.name)
58
67
  self
59
68
  end
60
- private :delete
61
69
 
62
70
  def each(uri = nil)
63
71
  now = Time.now
@@ -82,15 +90,17 @@ class HTTP::CookieJar
82
90
  }
83
91
  }
84
92
  else
85
- @jar.each { |domain, paths|
86
- paths.each { |path, hash|
87
- hash.delete_if { |name, cookie|
88
- if cookie.expired?(now)
89
- true
90
- else
91
- yield cookie
92
- false
93
- end
93
+ synchronize {
94
+ @jar.each { |domain, paths|
95
+ paths.each { |path, hash|
96
+ hash.delete_if { |name, cookie|
97
+ if cookie.expired?(now)
98
+ true
99
+ else
100
+ yield cookie
101
+ false
102
+ end
103
+ }
94
104
  }
95
105
  }
96
106
  }
@@ -110,45 +120,51 @@ class HTTP::CookieJar
110
120
  def cleanup(session = false)
111
121
  now = Time.now
112
122
  all_cookies = []
113
- @jar.each { |domain, paths|
114
- domain_cookies = []
115
-
116
- paths.each { |path, hash|
117
- hash.delete_if { |name, cookie|
118
- if cookie.expired?(now) || (session && cookie.session?)
119
- true
120
- else
121
- domain_cookies << cookie
122
- false
123
- end
123
+
124
+ synchronize {
125
+ break if @gc_index == 0
126
+
127
+ @jar.each { |domain, paths|
128
+ domain_cookies = []
129
+
130
+ paths.each { |path, hash|
131
+ hash.delete_if { |name, cookie|
132
+ if cookie.expired?(now) || (session && cookie.session?)
133
+ true
134
+ else
135
+ domain_cookies << cookie
136
+ false
137
+ end
138
+ }
124
139
  }
140
+
141
+ if (debt = domain_cookies.size - HTTP::Cookie::MAX_COOKIES_PER_DOMAIN) > 0
142
+ domain_cookies.sort_by!(&:created_at)
143
+ domain_cookies.slice!(0, debt).each { |cookie|
144
+ delete(cookie)
145
+ }
146
+ end
147
+
148
+ all_cookies.concat(domain_cookies)
125
149
  }
126
150
 
127
- if (debt = domain_cookies.size - HTTP::Cookie::MAX_COOKIES_PER_DOMAIN) > 0
128
- domain_cookies.sort_by!(&:created_at)
129
- domain_cookies.slice!(0, debt).each { |cookie|
151
+ if (debt = all_cookies.size - HTTP::Cookie::MAX_COOKIES_TOTAL) > 0
152
+ all_cookies.sort_by!(&:created_at)
153
+ all_cookies.slice!(0, debt).each { |cookie|
130
154
  delete(cookie)
131
155
  }
132
156
  end
133
157
 
134
- all_cookies.concat(domain_cookies)
135
- }
136
-
137
- if (debt = all_cookies.size - HTTP::Cookie::MAX_COOKIES_TOTAL) > 0
138
- all_cookies.sort_by!(&:created_at)
139
- all_cookies.slice!(0, debt).each { |cookie|
140
- delete(cookie)
158
+ @jar.delete_if { |domain, paths|
159
+ paths.delete_if { |path, hash|
160
+ hash.empty?
161
+ }
162
+ paths.empty?
141
163
  }
142
- end
143
164
 
144
- @jar.delete_if { |domain, paths|
145
- paths.delete_if { |path, hash|
146
- hash.empty?
147
- }
148
- paths.empty?
165
+ @gc_index = 0
149
166
  }
150
-
151
- @gc_index = 0
167
+ self
152
168
  end
153
169
  end
154
170
  end
@@ -5,6 +5,9 @@ require 'sqlite3'
5
5
  class HTTP::CookieJar
6
6
  # A store class that uses Mozilla compatible SQLite3 database as
7
7
  # backing store.
8
+ #
9
+ # Session cookies are stored separately on memory and will not be
10
+ # stored persistently in the SQLite3 database.
8
11
  class MozillaStore < AbstractStore
9
12
  SCHEMA_VERSION = 5
10
13
 
@@ -55,6 +58,7 @@ class HTTP::CookieJar
55
58
 
56
59
  @filename = options[:filename] or raise ArgumentError, ':filename option is missing'
57
60
 
61
+ @sjar = HashStore.new
58
62
  @db = SQLite3::Database.new(@filename)
59
63
  @db.results_as_hash = true
60
64
 
@@ -137,7 +141,8 @@ class HTTP::CookieJar
137
141
  st_update = @db.prepare("UPDATE moz_cookies SET baseDomain = :baseDomain WHERE id = :id")
138
142
 
139
143
  @db.execute("SELECT id, host FROM moz_cookies") { |row|
140
- domain = DomainName.new(row[:host]).domain
144
+ domain_name = DomainName.new(row[:host])
145
+ domain = domain_name.domain || domain_name.hostname
141
146
  st_update.execute(:baseDomain => domain, :id => row[:id])
142
147
  }
143
148
 
@@ -187,9 +192,7 @@ class HTTP::CookieJar
187
192
  end
188
193
  end
189
194
 
190
- public
191
-
192
- def add(cookie)
195
+ def db_add(cookie)
193
196
  @st_add ||=
194
197
  @db.prepare('INSERT OR REPLACE INTO moz_cookies (%s) VALUES (%s)' % [
195
198
  ALL_COLUMNS.join(', '),
@@ -197,7 +200,7 @@ class HTTP::CookieJar
197
200
  ])
198
201
 
199
202
  @st_add.execute({
200
- :baseDomain => cookie.domain_name.domain,
203
+ :baseDomain => cookie.domain_name.domain || cookie.domain,
201
204
  :appId => @app_id,
202
205
  :inBrowserElement => @in_browser_element ? 1 : 0,
203
206
  :name => cookie.name, :value => cookie.value,
@@ -214,6 +217,44 @@ class HTTP::CookieJar
214
217
  self
215
218
  end
216
219
 
220
+ def db_delete(cookie)
221
+ @st_delete ||=
222
+ @db.prepare(<<-'SQL')
223
+ DELETE FROM moz_cookies
224
+ WHERE appId = :appId AND
225
+ inBrowserElement = :inBrowserElement AND
226
+ name = :name AND
227
+ host = :host AND
228
+ path = :path
229
+ SQL
230
+
231
+ @st_delete.execute({
232
+ :appId => @app_id,
233
+ :inBrowserElement => @in_browser_element ? 1 : 0,
234
+ :name => cookie.name,
235
+ :host => cookie.dot_domain,
236
+ :path => cookie.path,
237
+ })
238
+ self
239
+ end
240
+
241
+ public
242
+
243
+ def add(cookie)
244
+ if cookie.session?
245
+ @sjar.add(cookie)
246
+ db_delete(cookie)
247
+ else
248
+ @sjar.delete(cookie)
249
+ db_add(cookie)
250
+ end
251
+ end
252
+
253
+ def delete(cookie)
254
+ @sjar.delete(cookie)
255
+ db_delete(cookie)
256
+ end
257
+
217
258
  def each(uri = nil)
218
259
  now = Time.now
219
260
  if uri
@@ -230,10 +271,10 @@ class HTTP::CookieJar
230
271
  @db.prepare("UPDATE moz_cookies SET lastAccessed = :lastAccessed where id = :id")
231
272
 
232
273
  thost = DomainName.new(uri.host)
233
- tpath = HTTP::Cookie.normalize_path(uri.path)
274
+ tpath = uri.path
234
275
 
235
276
  @st_cookies_for_domain.execute({
236
- :baseDomain => thost.domain_name.domain,
277
+ :baseDomain => thost.domain || thost.hostname,
237
278
  :appId => @app_id,
238
279
  :inBrowserElement => @in_browser_element ? 1 : 0,
239
280
  :expiry => now.to_i,
@@ -263,6 +304,7 @@ class HTTP::CookieJar
263
304
  yield cookie
264
305
  end
265
306
  }
307
+ @sjar.each(uri, &proc)
266
308
  else
267
309
  @st_all_cookies ||=
268
310
  @db.prepare(<<-'SQL')
@@ -291,12 +333,14 @@ class HTTP::CookieJar
291
333
 
292
334
  yield cookie
293
335
  }
336
+ @sjar.each(&proc)
294
337
  end
295
338
  self
296
339
  end
297
340
 
298
341
  def clear
299
342
  @db.execute("DELETE FROM moz_cookies")
343
+ @sjar.clear
300
344
  self
301
345
  end
302
346
 
@@ -309,13 +353,10 @@ class HTTP::CookieJar
309
353
  protected :count
310
354
 
311
355
  def empty?
312
- count == 0
356
+ @sjar.empty? && count == 0
313
357
  end
314
358
 
315
359
  def cleanup(session = false)
316
- now = Time.now
317
- all_cookies = []
318
-
319
360
  @st_delete_expired ||=
320
361
  @db.prepare("DELETE FROM moz_cookies WHERE expiry < :expiry")
321
362
 
@@ -342,26 +383,31 @@ class HTTP::CookieJar
342
383
  )
343
384
  SQL
344
385
 
345
- @st_delete_expired.execute({ 'expiry' => now.to_i })
386
+ synchronize {
387
+ break if @gc_index == 0
346
388
 
347
- @st_overusing_domains.execute({
348
- 'count' => HTTP::Cookie::MAX_COOKIES_PER_DOMAIN
349
- }).each { |row|
350
- domain, count = row['domain'], row['count']
389
+ @st_delete_expired.execute({ 'expiry' => Time.now.to_i })
351
390
 
352
- @st_delete_per_domain_overuse.execute({
353
- 'domain' => domain,
354
- 'limit' => count - HTTP::Cookie::MAX_COOKIES_PER_DOMAIN,
355
- })
356
- }
391
+ @st_overusing_domains.execute({
392
+ 'count' => HTTP::Cookie::MAX_COOKIES_PER_DOMAIN
393
+ }).each { |row|
394
+ domain, count = row['domain'], row['count']
357
395
 
358
- overrun = count - HTTP::Cookie::MAX_COOKIES_TOTAL
396
+ @st_delete_per_domain_overuse.execute({
397
+ 'domain' => domain,
398
+ 'limit' => count - HTTP::Cookie::MAX_COOKIES_PER_DOMAIN,
399
+ })
400
+ }
359
401
 
360
- if overrun > 0
361
- @st_delete_total_overuse.execute({ 'limit' => overrun })
362
- end
402
+ overrun = count - HTTP::Cookie::MAX_COOKIES_TOTAL
363
403
 
364
- @gc_index = 0
404
+ if overrun > 0
405
+ @st_delete_total_overuse.execute({ 'limit' => overrun })
406
+ end
407
+
408
+ @gc_index = 0
409
+ }
410
+ self
365
411
  end
366
412
  end
367
413
  end
@@ -177,6 +177,22 @@ class TestHTTPCookie < Test::Unit::TestCase
177
177
  assert_equal '.example.com', cookie.dot_domain
178
178
  end
179
179
 
180
+ def test_parse_public_suffix
181
+ cookie_str = 'a=b; domain=com'
182
+
183
+ cookie = HTTP::Cookie.parse(cookie_str).first
184
+ assert_equal('com', cookie.domain)
185
+ assert_equal(false, cookie.for_domain?)
186
+
187
+ cookie.origin = 'http://com/'
188
+ assert_equal('com', cookie.domain)
189
+ assert_equal(false, cookie.for_domain?)
190
+
191
+ assert_raises(ArgumentError) {
192
+ cookie.origin = 'http://example.com/'
193
+ }
194
+ end
195
+
180
196
  def test_parse_domain_none
181
197
  url = URI.parse('http://example.com/')
182
198
 
@@ -439,6 +455,19 @@ class TestHTTPCookie < Test::Unit::TestCase
439
455
  assert_equal 'key', cookie.name
440
456
  assert_equal 'value', cookie.value
441
457
  assert_equal expires, cookie.expires
458
+ assert_equal false, cookie.for_domain?
459
+
460
+ cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup, :domain => '.example.com')
461
+ assert_equal 'key', cookie.name
462
+ assert_equal 'value', cookie.value
463
+ assert_equal expires, cookie.expires
464
+ assert_equal true, cookie.for_domain?
465
+
466
+ cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup, :domain => 'example.com', :for_domain => false)
467
+ assert_equal 'key', cookie.name
468
+ assert_equal 'value', cookie.value
469
+ assert_equal expires, cookie.expires
470
+ assert_equal false, cookie.for_domain?
442
471
 
443
472
  cookie = HTTP::Cookie.new(:value => 'value', :name => 'key', :expires => expires.dup, :domain => 'example.org', :for_domain? => true)
444
473
  assert_equal 'key', cookie.name
@@ -664,6 +693,16 @@ class TestHTTPCookie < Test::Unit::TestCase
664
693
  }
665
694
  end
666
695
 
696
+ def test_acceptable_from_uri?
697
+ cookie = HTTP::Cookie.new(cookie_values(
698
+ :domain => 'uk',
699
+ :for_domain => true,
700
+ :origin => nil))
701
+ assert_equal false, cookie.for_domain?
702
+ assert_equal true, cookie.acceptable_from_uri?('http://uk/')
703
+ assert_equal false, cookie.acceptable_from_uri?('http://foo.uk/')
704
+ end
705
+
667
706
  def test_valid_for_uri?
668
707
  {
669
708
  HTTP::Cookie.parse('a1=b',
@@ -1,9 +1,28 @@
1
1
  require File.expand_path('helper', File.dirname(__FILE__))
2
2
  require 'tmpdir'
3
3
 
4
- class TestHTTPCookieJar < Test::Unit::TestCase
5
- def setup
6
- @jar = HTTP::CookieJar.new
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
19
+
20
+ def hash_store?
21
+ @store_type == :hash
22
+ end
23
+
24
+ def mozilla_store?
25
+ @store_type == :mozilla
7
26
  end
8
27
 
9
28
  def cookie_values(options = {})
@@ -307,9 +326,8 @@ class TestHTTPCookieJar < Test::Unit::TestCase
307
326
  value = @jar.save(File.join(dir, "cookies.yml"))
308
327
  assert_same @jar, value
309
328
 
310
- jar = HTTP::CookieJar.new
311
- jar.load(File.join(dir, "cookies.yml"))
312
- cookies = jar.cookies(url).sort_by { |cookie| cookie.name }
329
+ @jar2.load(File.join(dir, "cookies.yml"))
330
+ cookies = @jar2.cookies(url).sort_by { |cookie| cookie.name }
313
331
  assert_equal(2, cookies.length)
314
332
  assert_equal('Baz', cookies[0].name)
315
333
  assert_equal(false, cookies[0].for_domain)
@@ -338,9 +356,8 @@ class TestHTTPCookieJar < Test::Unit::TestCase
338
356
  Dir.mktmpdir do |dir|
339
357
  @jar.save(File.join(dir, "cookies.yml"), :format => :yaml, :session => true)
340
358
 
341
- jar = HTTP::CookieJar.new
342
- jar.load(File.join(dir, "cookies.yml"))
343
- assert_equal(3, jar.cookies(url).length)
359
+ @jar2.load(File.join(dir, "cookies.yml"))
360
+ assert_equal(3, @jar2.cookies(url).length)
344
361
  end
345
362
 
346
363
  assert_equal(3, @jar.cookies(url).length)
@@ -384,9 +401,8 @@ class TestHTTPCookieJar < Test::Unit::TestCase
384
401
  assert_match(/^rubyforge\.org\t.*\tBaz\t/, content)
385
402
  assert_match(/^#HttpOnly_\.rubyforge\.org\t/, content)
386
403
 
387
- jar = HTTP::CookieJar.new
388
- jar.load(filename, :cookiestxt) # HACK test the format
389
- cookies = jar.cookies(url)
404
+ @jar2.load(filename, :cookiestxt) # HACK test the format
405
+ cookies = @jar2.cookies(url)
390
406
  assert_equal(4, cookies.length)
391
407
  cookies.each { |cookie|
392
408
  case cookie.name
@@ -541,12 +557,27 @@ class TestHTTPCookieJar < Test::Unit::TestCase
541
557
  assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
542
558
  end
543
559
 
544
- def h_test_max_cookies(jar, slimit)
560
+ def _test_delete
561
+ nurl = URI 'http://rubyforge.org/login'
562
+ surl = URI 'https://rubyforge.org/login'
563
+
564
+ cookie1 = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => nurl))
565
+ cookie2 = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => surl))
566
+
567
+ @jar.add(nncookie)
568
+ @jar.add(sncookie)
569
+ @jar.add(nscookie)
570
+ @jar.add(sscookie)
571
+ end
572
+
573
+ def test_max_cookies
574
+ slimit = HTTP::Cookie::MAX_COOKIES_TOTAL + @gc_threshold
575
+
545
576
  limit_per_domain = HTTP::Cookie::MAX_COOKIES_PER_DOMAIN
546
577
  uri = URI('http://www.example.org/')
547
578
  date = Time.at(Time.now.to_i + 86400)
548
579
  (1..(limit_per_domain + 1)).each { |i|
549
- jar << HTTP::Cookie.new(cookie_values(
580
+ @jar << HTTP::Cookie.new(cookie_values(
550
581
  :name => 'Foo%d' % i,
551
582
  :value => 'Bar%d' % i,
552
583
  :domain => uri.host,
@@ -557,11 +588,11 @@ class TestHTTPCookieJar < Test::Unit::TestCase
557
588
  cookie.created_at = i == 42 ? date - i : date
558
589
  }
559
590
  }
560
- assert_equal limit_per_domain + 1, jar.to_a.size
561
- jar.cleanup
562
- count = jar.to_a.size
591
+ assert_equal limit_per_domain + 1, @jar.to_a.size
592
+ @jar.cleanup
593
+ count = @jar.to_a.size
563
594
  assert_equal limit_per_domain, count
564
- assert_equal [*1..41] + [*43..(limit_per_domain + 1)], jar.map { |cookie|
595
+ assert_equal [*1..41] + [*43..(limit_per_domain + 1)], @jar.map { |cookie|
565
596
  cookie.name[/(\d+)$/].to_i
566
597
  }.sort
567
598
 
@@ -572,7 +603,7 @@ class TestHTTPCookieJar < Test::Unit::TestCase
572
603
  (1..n).each { |i|
573
604
  (1..(limit_per_domain + 1)).each { |j|
574
605
  uri = URI('http://www%d.example.jp/' % i)
575
- jar << HTTP::Cookie.new(cookie_values(
606
+ @jar << HTTP::Cookie.new(cookie_values(
576
607
  :name => 'Baz%d' % j,
577
608
  :value => 'www%d.example.jp' % j,
578
609
  :domain => uri.host,
@@ -587,40 +618,33 @@ class TestHTTPCookieJar < Test::Unit::TestCase
587
618
  }
588
619
 
589
620
  assert_equal true, count > slimit
590
- assert_equal true, jar.to_a.size <= slimit
591
- jar.cleanup
592
- assert_equal hlimit, jar.to_a.size
593
- assert_equal false, jar.any? { |cookie|
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|
594
625
  cookie.domain == cookie.value
595
626
  }
596
627
  end
628
+ end
629
+
630
+ module TestHTTPCookieJar
631
+ class WithHashStore < Test::Unit::TestCase
632
+ include Tests
633
+ end
597
634
 
598
- def test_max_cookies_hashstore
599
- gc_threshold = 150
600
- h_test_max_cookies(
601
- HTTP::CookieJar.new(
602
- :store => :hash,
603
- :gc_threshold => gc_threshold),
604
- HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold)
605
- end
606
-
607
- def test_max_cookies_mozillastore
608
- gc_threshold = 150
609
- h_test_max_cookies(
610
- HTTP::CookieJar.new(
611
- :store => :mozilla,
612
- :gc_threshold => gc_threshold,
613
- :filename => ":memory:"),
614
- HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold)
615
- #Dir.mktmpdir { |dir|
616
- # h_test_max_cookies(
617
- # HTTP::CookieJar.new(
618
- # :store => :mozilla,
619
- # :gc_threshold => gc_threshold,
620
- # :filename => File.join(dir, "cookies.sqlite")),
621
- # HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold)
622
- #}
623
- rescue IndexError
635
+ class WithMozillaStore < Test::Unit::TestCase
636
+ include Tests
637
+
638
+ def setup
639
+ super(
640
+ { :store => :mozilla, :filename => ":memory:" },
641
+ { :store => :mozilla, :filename => ":memory:" })
642
+ end
643
+ end if begin
644
+ require 'sqlite3'
645
+ true
646
+ rescue LoadError
624
647
  STDERR.puts 'sqlite3 missing?'
648
+ false
625
649
  end
626
650
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http-cookie
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre6
4
+ version: 1.0.0.pre7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akinori MUSHA
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-03-27 00:00:00.000000000 Z
14
+ date: 2013-03-28 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: domain_name