Add more tests.

This commit is contained in:
Akinori MUSHA 2013-04-14 13:40:49 +09:00
parent 9a6b18463b
commit 1a05bb4dd0
2 changed files with 72 additions and 1 deletions

View file

@ -54,6 +54,25 @@ class TestHTTPCookie < Test::Unit::TestCase
}.size
end
end
[
["PREF=1; expires=Wed, 01 Jan 100 12:34:56 GMT", nil],
["PREF=1; expires=Sat, 01 Jan 1600 12:34:56 GMT", nil],
["PREF=1; expires=Tue, 01 Jan 69 12:34:56 GMT", 2069],
["PREF=1; expires=Thu, 01 Jan 70 12:34:56 GMT", 1970],
["PREF=1; expires=Wed, 01 Jan 20 12:34:56 GMT", 2020],
["PREF=1; expires=Sat, 01 Jan 2020 12:34:60 GMT", nil],
["PREF=1; expires=Sat, 01 Jan 2020 12:60:56 GMT", nil],
["PREF=1; expires=Sat, 01 Jan 2020 24:00:00 GMT", nil],
["PREF=1; expires=Sat, 32 Jan 2020 12:34:56 GMT", nil],
].each { |set_cookie, year|
cookie, = HTTP::Cookie.parse(set_cookie, url)
if year
assert_equal year, cookie.expires.year, "#{set_cookie}: expires in #{year}"
else
assert_equal nil, cookie.expires, "#{set_cookie}: invalid expiry date"
end
}
end
def test_parse_empty
@ -105,6 +124,12 @@ class TestHTTPCookie < Test::Unit::TestCase
}.size
end
def test_parse_no_nothing
cookie = '; "", ;'
url = URI.parse('http://www.example.com/')
assert_equal 0, HTTP::Cookie.parse(cookie, url).size
end
def test_parse_no_name
cookie = '=no-name; path=/'
url = URI.parse('http://www.example.com/')

View file

@ -360,6 +360,46 @@ module TestHTTPCookieJar
assert_equal(3, @jar.cookies(url).length)
end
def test_save_load_signature
Dir.mktmpdir { |dir|
filename = File.join(dir, "cookies.yml")
@jar.save(filename, :format => :cookiestxt, :session => true)
@jar.save(filename, :format => :cookiestxt, :session => true)
@jar.save(filename, :format => :cookiestxt)
@jar.save(filename, :cookiestxt, :session => true)
@jar.save(filename, :cookiestxt)
@jar.save(filename, :session => true)
@jar.save(filename)
assert_raises(ArgumentError) {
@jar.save()
}
assert_raises(ArgumentError) {
@jar.save(filename, { :format => :cookiestxt }, { :session => true })
}
assert_raises(ArgumentError) {
@jar.save(filename, :cookiestxt, { :session => true }, { :format => :cookiestxt })
}
@jar.load(filename, :format => :cookiestxt, :linefeed => "\n")
@jar.load(filename, :format => :cookiestxt, :linefeed => "\n")
@jar.load(filename, :format => :cookiestxt)
@jar.load(filename, :cookiestxt, :linefeed => "\n")
@jar.load(filename, :cookiestxt)
@jar.load(filename, :linefeed => "\n")
@jar.load(filename)
assert_raises(ArgumentError) {
@jar.load()
}
assert_raises(ArgumentError) {
@jar.load(filename, { :format => :cookiestxt }, { :linefeed => "\n" })
}
assert_raises(ArgumentError) {
@jar.load(filename, :cookiestxt, { :linefeed => "\n" }, { :format => :cookiestxt })
}
}
end
def test_save_session_cookies_yaml
url = URI 'http://rubyforge.org/'
@ -384,7 +424,6 @@ module TestHTTPCookieJar
assert_equal(3, @jar.cookies(url).length)
end
def test_save_and_read_cookiestxt
url = URI 'http://rubyforge.org/foo/'
@ -417,6 +456,13 @@ module TestHTTPCookieJar
content = File.read(filename)
filename2 = File.join(dir, "cookies2.txt")
open(filename2, 'w') { |w|
w.puts '# HTTP Cookie File'
@jar.save(w, :cookiestxt, :header => nil)
}
assert_equal content, File.read(filename2)
assert_match(/^\.rubyforge\.org\t.*\tFoo\t/, content)
assert_match(/^rubyforge\.org\t.*\tBaz\t/, content)
assert_match(/^#HttpOnly_\.rubyforge\.org\t/, content)