mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-04-24 14:27:39 +00:00
Fully test MozillaStore as well as HashStore.
This commit is contained in:
parent
4f526857aa
commit
8d7da32cc3
1 changed files with 71 additions and 47 deletions
|
|
@ -1,9 +1,28 @@
|
||||||
require File.expand_path('helper', File.dirname(__FILE__))
|
require File.expand_path('helper', File.dirname(__FILE__))
|
||||||
require 'tmpdir'
|
require 'tmpdir'
|
||||||
|
|
||||||
class TestHTTPCookieJar < Test::Unit::TestCase
|
module TestHTTPCookieJar; end
|
||||||
def setup
|
|
||||||
@jar = HTTP::CookieJar.new
|
module TestHTTPCookieJar::Tests
|
||||||
|
def setup(options = nil, options2 = nil)
|
||||||
|
default_options = {
|
||||||
|
:store => :hash,
|
||||||
|
:gc_threshold => 150,
|
||||||
|
}
|
||||||
|
new_options = default_options.merge(options || {})
|
||||||
|
new_options2 = new_options.merge(options2 || {})
|
||||||
|
@store_type = new_options[:store]
|
||||||
|
@gc_threshold = new_options[:gc_threshold]
|
||||||
|
@jar = HTTP::CookieJar.new(new_options)
|
||||||
|
@jar2 = HTTP::CookieJar.new(new_options)
|
||||||
|
end
|
||||||
|
|
||||||
|
def hash_store?
|
||||||
|
@store_type == :hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def mozilla_store?
|
||||||
|
@store_type == :mozilla
|
||||||
end
|
end
|
||||||
|
|
||||||
def cookie_values(options = {})
|
def cookie_values(options = {})
|
||||||
|
|
@ -307,9 +326,8 @@ class TestHTTPCookieJar < Test::Unit::TestCase
|
||||||
value = @jar.save(File.join(dir, "cookies.yml"))
|
value = @jar.save(File.join(dir, "cookies.yml"))
|
||||||
assert_same @jar, value
|
assert_same @jar, value
|
||||||
|
|
||||||
jar = HTTP::CookieJar.new
|
@jar2.load(File.join(dir, "cookies.yml"))
|
||||||
jar.load(File.join(dir, "cookies.yml"))
|
cookies = @jar2.cookies(url).sort_by { |cookie| cookie.name }
|
||||||
cookies = jar.cookies(url).sort_by { |cookie| cookie.name }
|
|
||||||
assert_equal(2, cookies.length)
|
assert_equal(2, cookies.length)
|
||||||
assert_equal('Baz', cookies[0].name)
|
assert_equal('Baz', cookies[0].name)
|
||||||
assert_equal(false, cookies[0].for_domain)
|
assert_equal(false, cookies[0].for_domain)
|
||||||
|
|
@ -338,9 +356,8 @@ class TestHTTPCookieJar < Test::Unit::TestCase
|
||||||
Dir.mktmpdir do |dir|
|
Dir.mktmpdir do |dir|
|
||||||
@jar.save(File.join(dir, "cookies.yml"), :format => :yaml, :session => true)
|
@jar.save(File.join(dir, "cookies.yml"), :format => :yaml, :session => true)
|
||||||
|
|
||||||
jar = HTTP::CookieJar.new
|
@jar2.load(File.join(dir, "cookies.yml"))
|
||||||
jar.load(File.join(dir, "cookies.yml"))
|
assert_equal(3, @jar2.cookies(url).length)
|
||||||
assert_equal(3, jar.cookies(url).length)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal(3, @jar.cookies(url).length)
|
assert_equal(3, @jar.cookies(url).length)
|
||||||
|
|
@ -384,9 +401,8 @@ class TestHTTPCookieJar < Test::Unit::TestCase
|
||||||
assert_match(/^rubyforge\.org\t.*\tBaz\t/, content)
|
assert_match(/^rubyforge\.org\t.*\tBaz\t/, content)
|
||||||
assert_match(/^#HttpOnly_\.rubyforge\.org\t/, content)
|
assert_match(/^#HttpOnly_\.rubyforge\.org\t/, content)
|
||||||
|
|
||||||
jar = HTTP::CookieJar.new
|
@jar2.load(filename, :cookiestxt) # HACK test the format
|
||||||
jar.load(filename, :cookiestxt) # HACK test the format
|
cookies = @jar2.cookies(url)
|
||||||
cookies = jar.cookies(url)
|
|
||||||
assert_equal(4, cookies.length)
|
assert_equal(4, cookies.length)
|
||||||
cookies.each { |cookie|
|
cookies.each { |cookie|
|
||||||
case cookie.name
|
case cookie.name
|
||||||
|
|
@ -541,12 +557,27 @@ class TestHTTPCookieJar < Test::Unit::TestCase
|
||||||
assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
|
assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') )
|
||||||
end
|
end
|
||||||
|
|
||||||
def h_test_max_cookies(jar, slimit)
|
def _test_delete
|
||||||
|
nurl = URI 'http://rubyforge.org/login'
|
||||||
|
surl = URI 'https://rubyforge.org/login'
|
||||||
|
|
||||||
|
cookie1 = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => nurl))
|
||||||
|
cookie2 = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => surl))
|
||||||
|
|
||||||
|
@jar.add(nncookie)
|
||||||
|
@jar.add(sncookie)
|
||||||
|
@jar.add(nscookie)
|
||||||
|
@jar.add(sscookie)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_max_cookies
|
||||||
|
slimit = HTTP::Cookie::MAX_COOKIES_TOTAL + @gc_threshold
|
||||||
|
|
||||||
limit_per_domain = HTTP::Cookie::MAX_COOKIES_PER_DOMAIN
|
limit_per_domain = HTTP::Cookie::MAX_COOKIES_PER_DOMAIN
|
||||||
uri = URI('http://www.example.org/')
|
uri = URI('http://www.example.org/')
|
||||||
date = Time.at(Time.now.to_i + 86400)
|
date = Time.at(Time.now.to_i + 86400)
|
||||||
(1..(limit_per_domain + 1)).each { |i|
|
(1..(limit_per_domain + 1)).each { |i|
|
||||||
jar << HTTP::Cookie.new(cookie_values(
|
@jar << HTTP::Cookie.new(cookie_values(
|
||||||
:name => 'Foo%d' % i,
|
:name => 'Foo%d' % i,
|
||||||
:value => 'Bar%d' % i,
|
:value => 'Bar%d' % i,
|
||||||
:domain => uri.host,
|
:domain => uri.host,
|
||||||
|
|
@ -557,11 +588,11 @@ class TestHTTPCookieJar < Test::Unit::TestCase
|
||||||
cookie.created_at = i == 42 ? date - i : date
|
cookie.created_at = i == 42 ? date - i : date
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_equal limit_per_domain + 1, jar.to_a.size
|
assert_equal limit_per_domain + 1, @jar.to_a.size
|
||||||
jar.cleanup
|
@jar.cleanup
|
||||||
count = jar.to_a.size
|
count = @jar.to_a.size
|
||||||
assert_equal limit_per_domain, count
|
assert_equal limit_per_domain, count
|
||||||
assert_equal [*1..41] + [*43..(limit_per_domain + 1)], jar.map { |cookie|
|
assert_equal [*1..41] + [*43..(limit_per_domain + 1)], @jar.map { |cookie|
|
||||||
cookie.name[/(\d+)$/].to_i
|
cookie.name[/(\d+)$/].to_i
|
||||||
}.sort
|
}.sort
|
||||||
|
|
||||||
|
|
@ -572,7 +603,7 @@ class TestHTTPCookieJar < Test::Unit::TestCase
|
||||||
(1..n).each { |i|
|
(1..n).each { |i|
|
||||||
(1..(limit_per_domain + 1)).each { |j|
|
(1..(limit_per_domain + 1)).each { |j|
|
||||||
uri = URI('http://www%d.example.jp/' % i)
|
uri = URI('http://www%d.example.jp/' % i)
|
||||||
jar << HTTP::Cookie.new(cookie_values(
|
@jar << HTTP::Cookie.new(cookie_values(
|
||||||
:name => 'Baz%d' % j,
|
:name => 'Baz%d' % j,
|
||||||
:value => 'www%d.example.jp' % j,
|
:value => 'www%d.example.jp' % j,
|
||||||
:domain => uri.host,
|
:domain => uri.host,
|
||||||
|
|
@ -587,40 +618,33 @@ class TestHTTPCookieJar < Test::Unit::TestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_equal true, count > slimit
|
assert_equal true, count > slimit
|
||||||
assert_equal true, jar.to_a.size <= slimit
|
assert_equal true, @jar.to_a.size <= slimit
|
||||||
jar.cleanup
|
@jar.cleanup
|
||||||
assert_equal hlimit, jar.to_a.size
|
assert_equal hlimit, @jar.to_a.size
|
||||||
assert_equal false, jar.any? { |cookie|
|
assert_equal false, @jar.any? { |cookie|
|
||||||
cookie.domain == cookie.value
|
cookie.domain == cookie.value
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_max_cookies_hashstore
|
module TestHTTPCookieJar
|
||||||
gc_threshold = 150
|
class WithHashStore < Test::Unit::TestCase
|
||||||
h_test_max_cookies(
|
include Tests
|
||||||
HTTP::CookieJar.new(
|
|
||||||
:store => :hash,
|
|
||||||
:gc_threshold => gc_threshold),
|
|
||||||
HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_max_cookies_mozillastore
|
class WithMozillaStore < Test::Unit::TestCase
|
||||||
gc_threshold = 150
|
include Tests
|
||||||
h_test_max_cookies(
|
|
||||||
HTTP::CookieJar.new(
|
def setup
|
||||||
:store => :mozilla,
|
super(
|
||||||
:gc_threshold => gc_threshold,
|
{ :store => :mozilla, :filename => ":memory:" },
|
||||||
:filename => ":memory:"),
|
{ :store => :mozilla, :filename => ":memory:" })
|
||||||
HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold)
|
end
|
||||||
#Dir.mktmpdir { |dir|
|
end if begin
|
||||||
# h_test_max_cookies(
|
require 'sqlite3'
|
||||||
# HTTP::CookieJar.new(
|
true
|
||||||
# :store => :mozilla,
|
rescue LoadError
|
||||||
# :gc_threshold => gc_threshold,
|
|
||||||
# :filename => File.join(dir, "cookies.sqlite")),
|
|
||||||
# HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold)
|
|
||||||
#}
|
|
||||||
rescue IndexError
|
|
||||||
STDERR.puts 'sqlite3 missing?'
|
STDERR.puts 'sqlite3 missing?'
|
||||||
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue